简体   繁体   English

SDL2主游戏循环

[英]SDL2 main game loop

My question popped out while reading the tutorials on SDL2, on lazyfoo.net and the code is being copied from this page 在阅读lazyfoo.net上有关SDL2的教程时,我的问题突然出现了,并且代码正在从此页面复制

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. 但是我无法找到原因。

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. 但是,如果继续“反复运行”,则可以看到球在屏幕上移动。

Lazyfoo's tutorials are pretty good, I'd say stick to them. Lazyfoo的教程非常不错,我会坚持下去。

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. 出于某种原因,关于更新时间和帧速率的课程分别为24和25,我想说这有点晚了,但是它们在那里。

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. 这将每秒发生X次,即帧速率。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM