Surface->surface blit

I’m having a problem with blitting different parts of a surface onto other
surfaces. What I’m trying to do is take an image file and then split it up
into 3 different surfaces. My code is bellow. SDL gives the error:
SDL_UpperBlit: passed a NULL surface
I’ve tried many different things (different rects, images, etc) but none of
them seem to work.
The offending code:

SDL_Surface **tiles;

tiles = new SDL_Surface *[numtiles];
SDL_Surface *temp = SDL_LoadBMP(filename);
if(temp == NULL)
fprintf(stderr, “Load Error: %s\n”, SDL_GetError());

int tilesdone = 0;
SDL_Rect copyrect;
copyrect.w = 31;
copyrect.h = 25;
copyrect.y = 0;

while (tilesdone < numtiles)
{
copyrect.x = tilesdone * width;
if(SDL_BlitSurface(temp, &copyrect,tiles[tilesdone],NULL) < 0)
fprintf(stderr, “BlitSurface error: %s\n”, SDL_GetError());
tilesdone++;
}
SDL_FreeSurface(temp);

I see one serious problem, you never allocate a surface to blit to. You
have allocated a vector of pointers to surfaces, but you haven’t
actually created the surfaces you want. Look at the line:

tiles = new SDL_Surface *[numtiles];

it allocates pointers to surfaces, not surfaces. I believe you need to
use SDL_CreateRGBSurface() to create the surfaces to blit to. You need
to call it in a loop to populate the vector of pointers you have already
created.

Bob P.

Carlin Theroux-Jones wrote:>

I’m having a problem with blitting different parts of a surface onto other
surfaces. What I’m trying to do is take an image file and then split it up
into 3 different surfaces. My code is bellow. SDL gives the error:
SDL_UpperBlit: passed a NULL surface
I’ve tried many different things (different rects, images, etc) but none of
them seem to work.
The offending code:

SDL_Surface **tiles;

tiles = new SDL_Surface *[numtiles];
SDL_Surface *temp = SDL_LoadBMP(filename);
if(temp == NULL)
fprintf(stderr, “Load Error: %s\n”, SDL_GetError());

int tilesdone = 0;
SDL_Rect copyrect;
copyrect.w = 31;
copyrect.h = 25;
copyrect.y = 0;

while (tilesdone < numtiles)
{
copyrect.x = tilesdone * width;
if(SDL_BlitSurface(temp, &copyrect,tiles[tilesdone],NULL) < 0)
fprintf(stderr, “BlitSurface error: %s\n”, SDL_GetError());
tilesdone++;
}
SDL_FreeSurface(temp);


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


±-----------------------------------+

  • Bob Pendleton is seeking contract +
  • and consulting work. Find out more +
  • at http://www.jump.net/~bobp +
    ±-----------------------------------+