Double buffering code

I’m trying to load several bitmaps, and blit them to one 640x480 bitmap,
then blit that to the screen, instead of just doing each one
individually. I’m stumped on what I’m doing wrong. Sorry if it’s
something silly, here’s my blitting code:

void hover::anim(void)
{

cerr << “in anim Loop” << endl;

/* Convert the image to the video format (maps colors) */
cerr << “Die here? 1” << endl;
surface[sectorDisplay + 10] =
SDL_DisplayFormat(surface[sectorDisplay]);
cerr << “Die here? 2” << endl;
obstacles[sectorDisplay + 10] =
SDL_DisplayFormat(obstacles[sectorDisplay]);
cerr << “Die here? 3” << endl;
barrier[sectorDisplay + 10] =
SDL_DisplayFormat(barrier[sectorDisplay]);
cerr << “Die here? 4” << endl;
playerCar[1] = SDL_DisplayFormat(playerCar[0]);
cerr << “Die here? 5” << endl;
AICar[1] = SDL_DisplayFormat(AICar[0]);
cerr << “alive” << endl;
// Code to blit the background to the screen

// Blits Surface of terrain
surfrect.x = 0;
surfrect.y = 0;
surfrect.w = surface[sectorDisplay + 10]->w;
surfrect.h = surface[sectorDisplay + 10]->h;
cerr << “dead 0” << endl;
if ( SDL_BlitSurface(surface[sectorDisplay + 10], NULL, screen,
&surfrect) < 0 )
{
freeVidMem();
ComplainAndExit();
}
cerr << “dead .5” << endl;
SDL_FreeSurface(surface[sectorDisplay + 10]);
// SDL_UpdateRects(screen, 1, &surfrect);
cerr << “dead one” << endl;
// Blits the barrier(s)
barrect.x = barrierX[sectorDisplay];
barrect.y = barrierY[sectorDisplay];
barrect.w = barrier[sectorDisplay + 10]->w;
barrect.h = barrier[sectorDisplay + 10]->h;
if ( SDL_BlitSurface(barrier[sectorDisplay + 10], NULL, screen,
&barrect) < 0 )
{
freeVidMem();
ComplainAndExit();
}

SDL_FreeSurface(barrier[sectorDisplay + 10]);
// SDL_UpdateRects(screen, 1, &barrect);

// Blits obstacle(s) to the screen
obsrect.x = obstacleX[sectorDisplay];
obsrect.y = obstacleY[sectorDisplay];
obsrect.w = obstacles[sectorDisplay + 10]->w;
obsrect.h = obstacles[sectorDisplay + 10]->h;
if ( SDL_BlitSurface(obstacles[sectorDisplay + 10], NULL, screen,
&obsrect) < 0 )
{
freeVidMem();
ComplainAndExit();
}
SDL_FreeSurface(obstacles[sectorDisplay + 10]);
// SDL_UpdateRects(screen, 1, &obsrect);

// Code to blit the Player Car(s) to the screen

plyrect.x = playerX;
plyrect.y = playerY;
plyrect.w = playerCar[1]->w;
plyrect.h = playerCar[1]->h;
if ( SDL_BlitSurface(playerCar[1], NULL, screen, &plyrect) < 0 )
{
ComplainAndExit();
}

// Update the screen 

SDL_FreeSurface(playerCar[1]);
// SDL_UpdateRects(screen, 1, &plyrect);

// Code to blit the AI Car(s) to the screen.

AIrect.x = AIX;
AIrect.y = AIY;
AIrect.w = AICar[1]->w;
AIrect.h = AICar[1]->h;
if ( SDL_BlitSurface(AICar[1], NULL, screen, &AIrect) < 0 )
{
ComplainAndExit();
}

// Update the screen 

SDL_FreeSurface(AICar[1]);
// SDL_UpdateRects(screen, 1, &AIrect);

cerr << “at image display” << endl;
img.x = 0;
img.y = 0;
img.w = image->w;
img.h = image->h;
/* if ( SDL_BlitSurface(screen, NULL, images, &img) < 0 )
{
ComplainAndExit();
}*/
cerr << “not dead” << endl;
// SDL_FreeSurface(screen);
// SDL_UpdateRects(images,1,&img);
}

void hover::freeVidMem(void)
{
SDL_FreeSurface(background);
if ( background == NULL )
{
ComplainAndExit();
}
SDL_FreeSurface(playerCar[0]);
if ( playerCar[0] == NULL )
{
ComplainAndExit();
}
SDL_FreeSurface(bkgrnd);
SDL_FreeSurface(playerCar[1]);
SDL_FreeSurface(AICar[1]);
for (int x = 0; x <= 20; x++)
{
SDL_FreeSurface(obstacles[x]);
SDL_FreeSurface(surface[x]);
SDL_FreeSurface(barrier[x]);
}
SDL_FreeSurface(surface[sectorDisplay]);
SDL_FreeSurface(barrier[sectorDisplay]);
SDL_FreeSurface(obstacles[sectorDisplay]);
SDL_FreeSurface(playerCar[0]);
SDL_FreeSurface(AICar[0]);

}

Any help is greatly appreciated. I realize I’m probably doing something
pretty stupid… :slight_smile:

Mike.

I’m trying to load several bitmaps, and blit them to one 640x480 bitmap,
then blit that to the screen, instead of just doing each one
individually. I’m stumped on what I’m doing wrong.

You forgot to mention what happens. :slight_smile:

See ya!
-Sam Lantinga (slouken at devolution.com)

Lead Programmer, Loki Entertainment Software–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

Sam Lantinga wrote:

I’m trying to load several bitmaps, and blit them to one 640x480 bitmap,
then blit that to the screen, instead of just doing each one
individually. I’m stumped on what I’m doing wrong.

You forgot to mention what happens. :slight_smile:

See ya!
-Sam Lantinga (slouken at devolution.com)

Lead Programmer, Loki Entertainment Software

Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

Oopsies! :slight_smile:

Well here’s what happens, I also had some stuff comented out that I
shouldn’t have, so here’s the correct source.

Problem: SDL_UpperBlit: Passed mismatched rectangles
SDL Warning: 15 SDL surfaces extant

here’s the code:

void hover::anim(void)
{

cerr << “in anim Loop” << endl;

/* Convert the image to the video format (maps colors) */
cerr << “Die here? 1” << endl;
surface[sectorDisplay + 10] =
SDL_DisplayFormat(surface[sectorDisplay]);
cerr << “Die here? 2” << endl;
obstacles[sectorDisplay + 10] =
SDL_DisplayFormat(obstacles[sectorDisplay]);
cerr << “Die here? 3” << endl;
barrier[sectorDisplay + 10] =
SDL_DisplayFormat(barrier[sectorDisplay]);
cerr << “Die here? 4” << endl;
playerCar[1] = SDL_DisplayFormat(playerCar[0]);
cerr << “Die here? 5” << endl;
AICar[1] = SDL_DisplayFormat(AICar[0]);
cerr << “alive” << endl;
// Code to blit the background to the screen

// Blits Surface of terrain
surfrect.x = 0;
surfrect.y = 0;
surfrect.w = surface[sectorDisplay + 10]->w;
surfrect.h = surface[sectorDisplay + 10]->h;
cerr << “dead 0” << endl;
if ( SDL_BlitSurface(surface[sectorDisplay + 10], NULL, screen,
&surfrect) < 0 )
{
freeVidMem();
ComplainAndExit();
}
cerr << “dead .5” << endl;
SDL_FreeSurface(surface[sectorDisplay + 10]);
SDL_UpdateRects(screen, 1, &surfrect);
cerr << “dead one” << endl;
// Blits the barrier(s)
barrect.x = barrierX[sectorDisplay];
barrect.y = barrierY[sectorDisplay];
barrect.w = barrier[sectorDisplay + 10]->w;
barrect.h = barrier[sectorDisplay + 10]->h;
if ( SDL_BlitSurface(barrier[sectorDisplay + 10], NULL, screen,
&barrect) < 0 )
{
freeVidMem();
ComplainAndExit();
}

SDL_FreeSurface(barrier[sectorDisplay + 10]);
SDL_UpdateRects(screen, 1, &barrect);

// Blits obstacle(s) to the screen
obsrect.x = obstacleX[sectorDisplay];
obsrect.y = obstacleY[sectorDisplay];
obsrect.w = obstacles[sectorDisplay + 10]->w;
obsrect.h = obstacles[sectorDisplay + 10]->h;
if ( SDL_BlitSurface(obstacles[sectorDisplay + 10], NULL, screen,
&obsrect) < 0 )
{
freeVidMem();
ComplainAndExit();
}
SDL_FreeSurface(obstacles[sectorDisplay + 10]);
SDL_UpdateRects(screen, 1, &obsrect);

// Code to blit the Player Car(s) to the screen

plyrect.x = playerX;
plyrect.y = playerY;
plyrect.w = playerCar[1]->w;
plyrect.h = playerCar[1]->h;
if ( SDL_BlitSurface(playerCar[1], NULL, screen, &plyrect) < 0 )
{
ComplainAndExit();
}

// Update the screen 

SDL_FreeSurface(playerCar[1]);
SDL_UpdateRects(screen, 1, &plyrect);

// Code to blit the AI Car(s) to the screen.

AIrect.x = AIX;
AIrect.y = AIY;
AIrect.w = AICar[1]->w;
AIrect.h = AICar[1]->h;
if ( SDL_BlitSurface(AICar[1], NULL, screen, &AIrect) < 0 )
{
ComplainAndExit();
}

// Update the screen 

SDL_FreeSurface(AICar[1]);
SDL_UpdateRects(screen, 1, &AIrect);

cerr << “at image display” << endl;
img.x = 0;
img.y = 0;
img.w = image->w;
img.h = image->h;
if ( SDL_BlitSurface(screen, NULL, images, &img) < 0 )
{
ComplainAndExit();
}
cerr << “not dead” << endl;
SDL_FreeSurface(screen);
SDL_UpdateRects(images,1,&img);
}

Mike.

Sam Lantinga wrote:

Well here’s what happens, I also had some stuff comented out that I
shouldn’t have, so here’s the correct source.

Problem: SDL_UpperBlit: Passed mismatched rectangles
SDL Warning: 15 SDL surfaces extant

Which blit is complaining? Are you double checking the rectangles to
make sure they aren’t clipping the edge of the screen?

See ya!
-Sam Lantinga (slouken at devolution.com)

Lead Programmer, Loki Entertainment Software

Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

It’s dying in this function:

if ( SDL_BlitSurface(screen, NULL, images, &img) < 0 )
{
ComplainAndExit();
}

screen is what I’m trying to blit to images, and I’m trying to get
images to draw to the screen.

I’ve got screen and images defined as follows:

    screen = SDL_SetVideoMode(640, 480, 0,

SDL_HWSURFACE|SDL_FULLSCREEN);

    images = SDL_SetVideoMode(640, 480, 0,

SDL_HWSURFACE|SDL_FULLSCREEN);

I honestly don’t know how to check to see if the rectangles clip the
screen. I’m sorry, I just haven’t had much experience with game
programming and sdl in general yet.