• Advertisement
Sign in to follow this  

Structuring Program Flow

This topic is 3382 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiya, I've written a basic 2D engine using SDL which can create animations and draw them to screen, play sounds, and take input. Currently, I "register" functions to be run in response to certain input, then I run a function which loops over and over, taking input and running the correct functions then re-blitting everything to the screen. At this point I'm not really sure how I'd do something like different parts of a game. I could make a basic game at this point, but to run the menu separately I'd have to have a switch or something in each of my event-response functions so that the right stuff was drawn to the screen. I've seen some tutorials in the past that talk about some sort of state machine using function pointers -- I'm wondering whether or not this is a good way to structure the flow of my program, so I'd do something like -- state_register ("state_name", KEYPRESS_FUNCTION, keypress_function_ptr); state_register ("state_name", MOUSEMOVE_FUNCTION, mousemove_function_ptr); Then, I could call a function such as switch_state ("state_name"); And the state would be switched to use the new functions. My engine loop would then look like: while (1) { keypress = get_keypress_input (); current_state_keypress_function_ptr (keypress); mousemove = get_mousemove_input (); current_state_mousemove_function_ptr (mousemove); handle_graphics (); } This way, if I'm in the keypress function for a certain state, I can run the switch_state function to switch into a different game state, and the function pointers will all be changed according to what was registered to them earlier. This is all in a hypothetical, so if anyone has a good tutorial or a better way to handle different game states, I would appreciate your posting it here. Thanks in advance! --Russ

Share this post


Link to post
Share on other sites
Advertisement
remember that sometimes you want to free up memory when going from one gamestate to another..
(ie, gameover => mainmenu propably wants to clean up whatever game objects it had)
Cant really tell if you thought about this in your current design.

Anyway, having a statemachine to handle gamestates is a really good idea, I've seen way to many games having bool variables for gameover, inmenu, etc. It becomes a bigg mess to maintain this!

You can achieve this using a object hierarchy (my preference) rather than function pointers, and I personally like that solution better because adding a new state is just writing a new class, and you dont have to manually maintain the switch/case that can get pretty huge.
ie.

class Game
{
void Setup()
{
cGameStateMachine.AddState(e_GameOver, new GameOverState() );
cGameStateMachine.AddState(e_GameRunning, new GameRunningState() );
cGameStateMachine.AddState(e_GameMenu, new GameMenuState() );
cGameStateMachine.AddState(e_GameDebug, new GameDegbugState() );
// etc..
}
void RunFrame()
{
HandleInput();
cGameStateMachine.Run();
DoRendering();
}
}




Edit: meh. just saw that you actually register the states the same way but using function pointers instead of objects.. so the cleanness part is moot ;) Well, I still think its better practice to use object than functions pointers, but then again; many might disagree!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement