SDL2 main game loop

丶灬走出姿态 提交于 2021-01-27 07:08:37

问题


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.


回答1:


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.




回答2:


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 ); 
}


来源:https://stackoverflow.com/questions/30738868/sdl2-main-game-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!