SDL2 main game loop

27.9k views Asked by At

My question popped out while reading the tutorials on SDL2, on lazyfoo.net and the code is being copied from this page

int main( int argc, char* args[] )
{

//Start up SDL and create window

if( !init() )
{
    printf( "Failed to initialize!\n" );
}
else
{
    //Load media
    if( !loadMedia() )
    {
        printf( "Failed to load media!\n" );
    }
    else
    {
        //Main loop flag
        bool quit = false;

        //Event handler
        SDL_Event e;

        //While application is running
        while( !quit )
        {
            //Handle events on queue
            while( SDL_PollEvent( &e ) != 0 )
            {
                //User requests quit
                if( e.type == SDL_QUIT )
                {
                    quit = true;
                }
            }

            //Clear screen
            SDL_RenderClear( gRenderer );

            //Render texture to screen
            SDL_RenderCopy( gRenderer, gTexture, NULL, NULL );

            //Update screen
            SDL_RenderPresent( gRenderer );
        }
    }
}

//Free resources and close SDL
close();

return 0;
}

Here why are we rendering the effects inside the main loop and make it run again and again rather than like:

int main( int argc, char* args[] )
{
//Start up SDL and create window
if( !init() )
{
    printf( "Failed to initialize!\n" );
}
else
{
    //Load media
    if( !loadMedia() )
    {
        printf( "Failed to load media!\n" );
    }
    else
    {
        //Main loop flag
        bool quit = false;

        //Event handler
        SDL_Event e;

       //Clear screen
       SDL_RenderClear( gRenderer );

        //Render texture to screen
        SDL_RenderCopy( gRenderer, gTexture, NULL, NULL );

        //Update screen
        SDL_RenderPresent( gRenderer );

        //While application is running
        while( !quit )
        {
            //Handle events on queue
            while( SDL_PollEvent( &e ) != 0 )
            {
                //User requests quit
                if( e.type == SDL_QUIT )
                {
                    quit = true;
                }
            }
        }
    }
}

//Free resources and close SDL
close();

return 0;
}

I suppose there is a reason as this is done in many tutorials. But i am unable to get the reason.

2

There are 2 answers

3
Joseph Hutchinson On BEST ANSWER

You render the screen again and again because typically things represented on the screen are changing. To represent those changes you need to update the display. For example, if a ball is moving across the screen and you only render the screen once, the ball will not appear to move. However, if you continue to "run again and again" then you will be able to see the ball move across the screen.

1
aslg On

Lazyfoo's tutorials are pretty good, I'd say stick to them.

For some reason the lessons on update time and frame rates are 24 and 25, which I'd say is a bit late but they're there.

Like I said in the comment, it's Event Driven Programming. The main loop should read events, such as key presses and mouse clicks, update your application using how much time has passed since the last update and then render it. This will happen X times per second, which is the frame rate.

That code is made in a way in which it could later acommodate this time checking but the lesson on that comes later.

This is an example for a very simple main loop.

while ( appIsRunning ) {
    handleEvents();

    update( deltaTime );

    render();

    // If frames were 30, wait 33 ms before running the loop again
    delay( 1000/frames ); 
}