SDL2 主游戏循环 [英] SDL2 main game loop

查看:107
本文介绍了SDL2 主游戏循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在lazyfoo.net 上阅读有关SDL2 的教程时出现了我的问题,并且正在从此页面

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.

推荐答案

您一次又一次地渲染屏幕,因为通常屏幕上显示的事物正在发生变化.要表示这些更改,您需要更新显示.例如,如果一个球在屏幕上移动,而您只渲染屏幕一次,则球看起来不会移动.但是,如果您继续一次又一次地奔跑",那么您将能够看到球在屏幕上移动.

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.

这篇关于SDL2 主游戏循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆