SDL_Surface duplication

Hi,

In the SDL WiKi, at the page: http://www.libsdl.org/cgi/docwiki.cgi/SDL_Surface
it is shown the proper way to duplicate an SDL_Surface.
But, OMG, the code is not duplicating anything, it is only creating a “shared” surface,
for which the refcount equals 2, preventing the memory to be freed at the
first call of SDL_FreeSurface.

By the way, what is the proper way to really make a deep copy of a surface ?
I wonder why there’s no “SDL_CopySurface” or something like this… the only trick
I’ve found is to use SDL_DisplayFormat() which is not really optimal.

Thanks
Julien CLEMENT

I’ve written my own surface duplication routine (SDL 1.2 only), which works only for
non-palette surfaces (BytesPerPixel > 1) :

SDL_Surface*
duplicateSurface (SDL_Surface* surf) {
SDL_Surface* cpy;
cpy = (SDL_Surface *)malloc(sizeof(SDL_Surface));
memcpy((SDL_Surface *)cpy, (SDL_Surface *)surf, sizeof(SDL_Surface));
cpy->format = (SDL_PixelFormat *)malloc(sizeof(SDL_PixelFormat));
memcpy((SDL_PixelFormat *)cpy->format, (SDL_PixelFormat *)surf->format,
sizeof(SDL_PixelFormat));
size_t size = surf->pitch * surf->h;
cpy->pixels = malloc(size);
memcpy((Uint8 *)cpy->pixels, (Uint8 *)surf->pixels, size * sizeof(Uint8));
return cpy;
}

It seems to be slightly faster than the SDL_DisplayFormat() trick.
If it can help some of you …

Julien CLEMENT________________________________
De : julien CLEMENT <@Julien_Clement1>
? : SDL
Envoy? le : Mercredi, 5 Ao?t 2009, 10h57mn 14s
Objet : [SDL] SDL_Surface duplication

Hi,

In the SDL WiKi, at the page: http://www.libsdl.org/cgi/docwiki.cgi/SDL_Surface
it is shown the proper way to duplicate an SDL_Surface.
But, OMG, the code is not duplicating anything, it is only creating a “shared” surface,
for which the refcount equals 2, preventing the memory to be freed at the
first call of SDL_FreeSurface.

By the way, what is the proper way to really make a deep copy of a surface ?
I wonder why there’s no “SDL_CopySurface” or something like this… the only trick
I’ve found is to use SDL_DisplayFormat() which is not really optimal.

Thanks
Julien CLEMENT

I just use SDL_ConvertSurface(src, src->format, SDL_SWSURFACE).

Jonny DOn Wed, Aug 5, 2009 at 6:05 AM, julien CLEMENT wrote:

I’ve written my own surface duplication routine (SDL 1.2 only), which works
only for
non-palette surfaces (BytesPerPixel > 1) :

SDL_Surface*
duplicateSurface (SDL_Surface* surf) {
? SDL_Surface* cpy;
? cpy = (SDL_Surface *)malloc(sizeof(SDL_Surface));
? memcpy((SDL_Surface *)cpy, (SDL_Surface *)surf, sizeof(SDL_Surface));
? cpy->format = (SDL_PixelFormat *)malloc(sizeof(SDL_PixelFormat));
? memcpy((SDL_PixelFormat *)cpy->format, (SDL_PixelFormat *)surf->format,
??? sizeof(SDL_PixelFormat));
? size_t size = surf->pitch * surf->h;
? cpy->pixels = malloc(size);
? memcpy((Uint8 *)cpy->pixels, (Uint8 *)surf->pixels, size * sizeof(Uint8));
? return cpy;
}

It seems to be slightly faster than the SDL_DisplayFormat() trick.
If it can help some of you …

Julien CLEMENT


De : julien CLEMENT
? : SDL
Envoy? le : Mercredi, 5 Ao?t 2009, 10h57mn 14s
Objet?: [SDL] SDL_Surface duplication

Hi,
In the SDL WiKi, at the
page:?http://www.libsdl.org/cgi/docwiki.cgi/SDL_Surface
it is shown the proper way to duplicate an SDL_Surface.
But, OMG, the code is not duplicating anything, it is only creating a
"shared" surface,
for which the refcount equals 2, preventing the memory to be freed at the
first call of SDL_FreeSurface.
By the way, what is the proper way to really make a deep copy of a surface ?
I wonder why there’s no “SDL_CopySurface” or something like this… the only
trick
I’ve found is to use SDL_DisplayFormat() which is not really optimal.
Thanks

Julien CLEMENT


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org