Sign in to follow this  
julian boolean

gamestates and the gameloop

Recommended Posts

julian boolean    122
heres what i got so far.. doesnt work (no surprise):
#include <allegro.h>

int main()
{
	allegro_init();
	install_timer();
	install_keyboard();
	install_mouse();
	
	set_color_depth(16);	
	set_gfx_mode(GFX_AUTODETECT, 640,480,0,0);


//	load graphics
//	and do some other stuff
	
	enum gamestate { LOGINSCREEN = 0, MAINSCREEN = 1, CREATESCREEN = 2, SHUTDOWN = 3 };
	
	gamestate GAMESTATE;
	
	void loginscreen()
	{
		// load background
		// do stuff for buttons
    	}
    
	void mainscreen()
	{
		// load background
		// do stuff for buttons
    	}
    
    
	void createscreen()
	{
		// load background
		// do stuff for buttons
    	}
	
	int DONE = false;
	
	do
	{

	switch (GAMESTATE)
	{

	case LOGINSCREEN:
	{
	loginscreen();

//	turn on login screen buttons
                
	if(the exit button is clicked())
	{
		GAMESTATE = SHUTDOWN;
	}
            
	if(the login button is clicked())
	{             
		// turn off login screen buttons
		GAMESTATE = MAINSCREEN;
	}
            
                return(1);
            }break;
        
//>>
// the main screen case
//>>

//>>
// the create screen case
//>>

	case SHUTDOWN:
	{      
		DONE = true;
 
	return(0);                
	}break;
            
	return(1);
	}
        
	show_mouse(buffer);
	set_mouse_sprite(the_pointer);
		
	blit(buffer, screen, 0,0,0,0,640,480);
	clear(buffer);
        
	}while(!DONE);

	return(0);
}

END_OF_MAIN();


Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
thanks for the example, but I think I'll stick to my current gameloop design...

Share this post


Link to post
Share on other sites
rip-off    10976
I assume by "doesnt work" you mean that it doesnt compile.

Its easier to get good answers if you include the details of how it doesnt work, in this case the compilers errors would be helpful.

Anyway, c++ does not allow nesting of functions. you should move the functions inside main to be outside it, like so:



void loginscreen()
{
// load background
// do stuff for buttons
}

void mainscreen()
{
// load background
// do stuff for buttons
}


void createscreen()
{
// load background
// do stuff for buttons
}

int main()
{
// allegro setup stuff

enum gamestate { LOGINSCREEN = 0, MAINSCREEN = 1, CREATESCREEN = 2, SHUTDOWN = 3 };


gamestate GAMESTATE;



int DONE = false;

do
{

switch (GAMESTATE)
{

case LOGINSCREEN:
{
loginscreen();

// turn on login screen buttons

if(the exit button is clicked())
{
GAMESTATE = SHUTDOWN;
}

if(the login button is clicked())
{
// turn off login screen buttons
GAMESTATE = MAINSCREEN;
}

return(1);
}break;



case SHUTDOWN:
{
DONE = true;

return(0);
}break;

return(1);
}

// more allegro stuff

}while(!DONE);

return(0);
}



Also note that it is idiomatic c++ to use uppercase for constants, like your enum value. However its more usual to have lowercase or mixed case for variable names. This is of course up to you but it seems inconsistent to have both constants and variable names in all caps, it would be easier to read if you differentiated between them.

Share this post


Link to post
Share on other sites
julian boolean    122
sorry lol whenever i try to compile it, it will just open and close right away

thanks! ill try that out

(edit)

doing that seems to give me a lot of "undeclared" errors, im guessing because it cant find the graphics/buffer

Share this post


Link to post
Share on other sites
rip-off    10976
Quote:
Original post by julian boolean
sorry lol whenever i try to compile it, it will just open and close right away

thanks! ill try that out


What compiler are you using? There must be some way to capture the output, or you are going to have great fun guessing why it rejects your code [smile]

Share this post


Link to post
Share on other sites
julian boolean    122
im using dev c++

(edit)

wooooo got it fixed!.. had to rewrite the entire thing but got it fixed! lol.. the only problem is the mouse again, it doesnt look like the double buffer is going through it even though it is

[Edited by - julian boolean on November 6, 2006 6:51:26 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this