gpt4 book ai didi

c++ - 使用 SDL 的硬件缓冲,关于它如何工作的问题

转载 作者:太空宇宙 更新时间:2023-11-04 14:28:40 25 4
gpt4 key购买 nike

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

好吧,基本上我的理解是 SDL 负责实际将哪个缓冲区绘制到屏幕上。当我写入缓冲区时,它始终是我正在写入的后备缓冲区,或者当前未在屏幕上绘制的缓冲区。因此,当我调用 SDL_Flip(screen) 时,它将我的屏幕表面“blits”到后台缓冲区,然后将正在绘制缓冲区的指针移动到曾经是后台缓冲区的缓冲区,我一直在处理的缓冲区,以及现在显示的旧缓冲区成为后备缓冲区。此时如果我调用 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;
}
}

最佳答案

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

关于c++ - 使用 SDL 的硬件缓冲,关于它如何工作的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/814326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com