使用 SDL 的硬件缓冲,有关其工作原理的问题

发布于 2024-07-18 19:09:41 字数 1746 浏览 3 评论 0原文

我决定做我的第一个游戏,它会很简单,但我想使用 c++,所以我选择 SDL 来学习。 所以我的问题是关于编写代码时如何处理“缓冲区”。 我将在底部发布我的相关代码。

好的,基本上我的理解是 SDL 负责实际将哪个缓冲区绘制到屏幕上。 当我写入缓冲区时,它始终是我正在写入的后备缓冲区,或者当前未在屏幕上绘制的缓冲区。 因此,当我调用 SDL_Flip(screen) 时,它会将我的屏幕表面“位块”到后备缓冲区上,然后将正在绘制的缓冲区的指针移动到曾经是后备缓冲区的缓冲区(我一直在处理的缓冲区),以及现在显示的旧缓冲区将成为后备缓冲区。 此时,如果我调用 SDL_FillRect(arguments) ,它将在现在的后台缓冲区上执行?

我将发布我的学习游戏的整个“心跳”,因为它可能有助于澄清我的问题:

//While the user hasn't quit
while( quit == false )
{
    //If there's an event to handle
    if( SDL_PollEvent( &event ) )
    {
        //If a key was pressed
        if( event.type == SDL_KEYDOWN )
        {
            //Set the proper message surface
            switch( event.key.keysym.sym )
            {
                case SDLK_UP: message = upMessage; break;
                case SDLK_DOWN: message = downMessage; break;
                case SDLK_LEFT: message = leftMessage; break;
                case SDLK_RIGHT: message = rightMessage; break;
            }
        }

        else if( event.type == SDL_QUIT ) //if the user clicks the little X in the upper right corner.
        {
            quit = true;
        }
    }

    //If a message needs to be displayed
 if( message != NULL )
 {
      // Clear the back buffer.
      SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );

      //Draw the backgroudn to the back buffer.
      apply_surface( 0, 0, background, screen );

      // Draw the "message" to the back buffer.
      apply_surface( ( SCREEN_WIDTH - message->w ) / 2, ( SCREEN_HEIGHT - message->h ) / 2, message, screen );

      //Null the surface pointer
      message = NULL;
    }

    //Swap the current and back buffer.
    if( SDL_Flip( screen ) == -1 )
    {
        return 1;
    }
}

I'm deciding to do my first game, its going to be simple but I want to use c++ and I chose SDL for my learning. So my question is about how the "buffers" are handled when writing code. I'll post my related code at the bottom.

Ok, so basically the way I understand it is that SDL takes care of which buffer is actually being drawn to the screen. When I am writing to a buffer it is always the backbuffer I am writing to, or the buffer currently not being drawn on the screen. So, when I call SDL_Flip(screen) it "blits" my screen surface onto the backbuffer, then moves the pointer to which buffer is being drawn to that buffer which used to be the backbuffer, the one I had been working on, and the old buffer that was showing now becomes the backbuffer. At this point if I call SDL_FillRect(arguments) it will be performed on the now back buffer?

I'm going to post my entire "heartbeat" of my learning game as it may help clarify my question:

//While the user hasn't quit
while( quit == false )
{
    //If there's an event to handle
    if( SDL_PollEvent( &event ) )
    {
        //If a key was pressed
        if( event.type == SDL_KEYDOWN )
        {
            //Set the proper message surface
            switch( event.key.keysym.sym )
            {
                case SDLK_UP: message = upMessage; break;
                case SDLK_DOWN: message = downMessage; break;
                case SDLK_LEFT: message = leftMessage; break;
                case SDLK_RIGHT: message = rightMessage; break;
            }
        }

        else if( event.type == SDL_QUIT ) //if the user clicks the little X in the upper right corner.
        {
            quit = true;
        }
    }

    //If a message needs to be displayed
 if( message != NULL )
 {
      // Clear the back buffer.
      SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );

      //Draw the backgroudn to the back buffer.
      apply_surface( 0, 0, background, screen );

      // Draw the "message" to the back buffer.
      apply_surface( ( SCREEN_WIDTH - message->w ) / 2, ( SCREEN_HEIGHT - message->h ) / 2, message, screen );

      //Null the surface pointer
      message = NULL;
    }

    //Swap the current and back buffer.
    if( SDL_Flip( screen ) == -1 )
    {
        return 1;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风夜微凉 2024-07-25 19:09:41

它很大程度上取决于您的系统(即 X11、Linux 帧缓冲区、Windows)以及用于与之交互的后端 SDL。 还将其标志传递给 SDL_SetVideoMode。 基本上,软件表面位于程序的内存区域中,而硬件表面则位于图形卡的内存中。 在我看来,您所描述的似乎是一个双缓冲区,如果您通过 SDL_HWSURFACE | 则会启用该缓冲区。 SDL_DOUBLEBUF 到 SDL。 请记住,并非所有平台和配置都支持此功能,您可能会得到不同的东西。

it highly depends on the your system (ie. X11, Linux frame buffer, Windows), and the backend SDL uses to interact with it. Also which flags you passs to SDL_SetVideoMode. There are basically software surfaces which sit in a region of memory in you program and hardware surfaces which are placed in graphical card's memory. What you describe seems to me to be a double buffer, which is enabled if you pass SDL_HWSURFACE | SDL_DOUBLEBUF to SDL. Just remember this is not supported on all platforms and configurations and you may get something different instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文