我决定做我的第一个游戏,它会很简单,但我想使用 c++,我选择了 SDL 来学习。所以我的问题是关于编写代码时如何处理“缓冲区”。我将在底部发布我的相关代码。
好的,所以基本上我理解的方式是 SDL 负责实际将哪个缓冲区绘制到屏幕上。当我写入缓冲区时,它始终是我正在写入的后备缓冲区,或者当前未在屏幕上绘制的缓冲区。因此,当我调用 SDL_Flip(screen) 时,它会将我的屏幕表面“blit”到后备缓冲区上,然后将指向哪个缓冲区的指针移动到那个曾经是后备缓冲区的缓冲区,我一直在处理的那个缓冲区,以及现在显示的旧缓冲区成为后缓冲区。此时如果我调用 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;
}
}