Duplicate Image ... Contiued

“Mattias Engdeg?rd” wrote

i’m trying to create an exact duplicate of some surface data.
for some reason this is harder than it seems. what is making it
tricky is that i want it to work with all surfaces types
(alphas, colorkeys, etc)

newsurface = SDL_ConvertSurface(surface, surface->format, surface->flags)

great solution. now when i use this in the whole of my puzzle,
it isn’t working very nicely.

i’m writing a routine to duplicate a section of another image.
the meat of it looks like this. i create the ‘subimage’ with
CreateRGBSurfaceFrom, then use ConvertSurface to make the
copy.

/* blah, blah, code to error check positions and sizes /
/
i have srcsurf as the given source, then use tempsurf /
/
and dupesurf to hold results. /
/
also srcformat is srcsurf->format */

firstpixel = ((char*)srcsurf->pixels) +
(srcsurf->pitch * height) +
(srcformat->BytesPerPixel * width);

tempsurf = SDL_CreateRGBSurfaceFrom(firstpixel, x, y,
srcformat->BytesPerPixel, srcsurf->pitch,
srcformat->Rmask, srcformat->Gmask,
srcformat->Bmask, srcformat->Amask);

printf(“tempsurf = %d x %d\n”, tempsurf->w, tempsurf->h);

dupesurf = SDL_ConvertSurface(tempsurf, srcformat,
tempsurf->flags);

printf(“pixels: orig=%d, temp=%d, dupe=%d\n”,
(int)firstpixel, (int)tempsurf->pixels,
(int)dupesurf->pixels);

SDL_FreeSurface(tempsurf);

you’ll notice i have some sanity-checking print values in
the code. i’m testing with 32bit images, so for my testing
this works well. the printout when calling this function…

tempsurf = 200 x 200
pixels: orig=1111, temp=1111, dupe=0

the results i get from this are an all black image. all the
formats and flags seem correct, there’s just no pixel data.
(ps, in this test there is no alpha or colorkey, nothing fancy,
although now that i think about it, srcsurf is the screen surface,
(but that is being created as SWSURFACE, so i should be safe…?)

on another note, if i change the Convert line to use tempsurf->format
instead of srcsurf->format, i get a nasty crash. strange, but i haven’t
dug into it yet.

“Mattias Engdeg?rd” wrote

newsurface = SDL_ConvertSurface(surface, surface->format, surface->flags)

great solution. now when i use this in the whole of my puzzle,
it isn’t working very nicely.

/* blah, blah, code to error check positions and sizes /
/
i have srcsurf as the given source, then use tempsurf /
/
and dupesurf to hold results. /
/
also srcformat is srcsurf->format */

firstpixel = ((char*)srcsurf->pixels) +
(srcsurf->pitch * height) +
(srcformat->BytesPerPixel * width);

tempsurf = SDL_CreateRGBSurfaceFrom(firstpixel, x, y,
srcformat->BytesPerPixel, srcsurf->pitch,
srcformat->Rmask, srcformat->Gmask,
srcformat->Bmask, srcformat->Amask);

printf(“tempsurf = %d x %d\n”, tempsurf->w, tempsurf->h);

dupesurf = SDL_ConvertSurface(tempsurf, srcformat,
tempsurf->flags);

printf(“pixels: orig=%d, temp=%d, dupe=%d\n”,
(int)firstpixel, (int)tempsurf->pixels,
(int)dupesurf->pixels);

SDL_FreeSurface(tempsurf);

I think that Mattias ment to do it like this:

// begin surface copy code

dupesurf = SDL_ConvertSurface(srcsurf, srcformat,
srcsurf->flags);

// end surface copy code

You shouldn’t need tempsurf. ConvertSurface does the copy for
you. Hope this is what you need.

-ArthurFrom: pete@visionart.com (Pete J Shinners)
Subject: [SDL] Re: Duplicate Image … Contiued
Date: Tue, 19 Sep 2000 15:20:04 -0700