views:

168

answers:

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;
    }
}
A: 

Hello, 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.

kyku