From: Holger Schemel Date: Wed, 6 Mar 2024 08:57:30 +0000 (+0100) Subject: added function to create bitmap from SDL surface X-Git-Tag: 4.4.0.0-test-1~248 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=0196637b0c226707f177cf52c7c1182bf08e7956;p=rocksndiamonds.git added function to create bitmap from SDL surface --- diff --git a/src/libgame/sdl.c b/src/libgame/sdl.c index 045b7057..76c70cc4 100644 --- a/src/libgame/sdl.c +++ b/src/libgame/sdl.c @@ -502,6 +502,31 @@ SDL_Surface *SDLCreateNativeSurface(int width, int height, int depth) return surface; } +Bitmap *SDLGetBitmapFromSurface(SDL_Surface *surface) +{ + int width = surface->w; + int height = surface->h; + int depth = video.default_depth; + Bitmap *bitmap = CreateBitmap(width, height, depth); + + // free default surface (not needed anymore) + SDL_FreeSurface(bitmap->surface); + + // get native, non-transparent surface from original surface + bitmap->surface = SDLGetOpaqueSurface(surface); + + // get native, potentially transparent surface from original surface + bitmap->surface_masked = SDLGetNativeSurface(surface); + + // set black pixel to transparent if no alpha channel / transparent color + if (!SDLHasAlpha(bitmap->surface_masked) && + !SDLHasColorKey(bitmap->surface_masked)) + SDL_SetColorKey(bitmap->surface_masked, SET_TRANSPARENT_PIXEL, + SDL_MapRGB(bitmap->surface_masked->format, 0x00, 0x00, 0x00)); + + return bitmap; +} + static SDL_Texture *SDLCreateTextureFromSurface(SDL_Surface *surface) { if (program.headless) diff --git a/src/libgame/sdl.h b/src/libgame/sdl.h index ee0759d3..2caf6af0 100644 --- a/src/libgame/sdl.h +++ b/src/libgame/sdl.h @@ -400,6 +400,7 @@ const char *SDLGetRendererName(void); boolean SDLSetNativeSurface(SDL_Surface **); SDL_Surface *SDLGetNativeSurface(SDL_Surface *); SDL_Surface *SDLCreateNativeSurface(int, int, int); +Bitmap *SDLGetBitmapFromSurface(SDL_Surface *); void SDLCreateBitmapTextures(Bitmap *); void SDLFreeBitmapTextures(Bitmap *);