SFML community forums

Help => General => Topic started by: janszy on August 09, 2012, 06:21:56 pm

Title: Structure of Game loop
Post by: janszy on August 09, 2012, 06:21:56 pm
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/ (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!
Title: Re: Structure of Game loop
Post by: eXpl0it3r on August 09, 2012, 06:37:09 pm
Game::GameLoop(){

 
I kind of have the feeling that something is missing there. ::)

There are various ways to handle states and some might be better than others. Most of the state implementations give the state a Update and a Draw function. In the update function you can handle events and inputs and update the positions etc., whereas in the draw function you just draw all the objects.

An example of a state machine can be found in my GitHub repository (https://github.com/eXpl0it3r/SmallGameEngine). Feel free to use it. :)
Title: Re: Structure of Game loop
Post by: janszy on August 09, 2012, 06:50:37 pm
Yes, I accidently posted it while I was writing it. I edited my post, and added the missing section.
Thank you for your answer! I will check the gitHub  ;)