Hello Everybody!
I'm new in game programming, and I need a little help to set up my game loop. My game has several states, like 'Initialize', 'MainMenu', 'GamePlay', etc. I'm not sure what is the correct order of event handling and checking gamestate. My current solution is that I check the state first, then I handle events, if I need it. My code structure is like this:
Game::GameLoop(){
while( GameState != Closed ){
sf::Event evt; //I can't declare variable in the switch statement, because then I got compile error
switch( GameState ){
case Initialize:
InitializeGame(); //I don't need to handle events here
break;
case MainMenu:
while( window.pollEvent( evt ){
if( evt.type == sf::Event::Closed ){
GameState=Exit;
}
if( evt.type == sf::Event::KeyPressed ){ //and other input events
HandleInput( evt );
}
}
//Other game specific stuff goes here, like update and render menus, etc.
break;
case GamePlay:
//It's almost the same, as above
break;
//Check all the states...
} //end of switch
} //end of while
} //end of the function
I do it like this way because not every state needs event handling (for example Initialize, Exit). Would be the other way better, when I always handle events, and I check the states in the EventHandler function?
Such like this:
Game::GameLoop(){
while( GameState != Closed ){
sf::Event evt;
while( window.pollEvent( evt ) ){
EventHandler( evt );
}
//Do game specific stuff here
}
}
I'm a little bit confused, which is the better solution. What do you suggest? Does this change game to game?
I would like to ask one more question. I read an article (
http://www.koonsolo.com/news/dewitters-gameloop/ ) about implementations of the game loop. I'm using the second implementation (Game Speed dependent on Variable FPS) at the moment, but the article says it is the worst. I read this implementation in two different game development books, that's why I use it. Should I change this approach with something else? Or is this ok for PC games?
Thank you for your answers!