added abstractions for alpha blending and color key handling for SDL/SDL2
authorHolger Schemel <info@artsoft.org>
Mon, 27 Jun 2016 23:27:08 +0000 (01:27 +0200)
committerHolger Schemel <info@artsoft.org>
Mon, 27 Jun 2016 23:27:08 +0000 (01:27 +0200)
src/libgame/sdl.c

index 4b44daa1c53d6ce82c6c59f62cc134800b671d61..893f765351bf8c45a9a58e0cf3ddde11d61de68e 100644 (file)
@@ -226,6 +226,39 @@ static boolean equalSDLPixelFormat(SDL_PixelFormat *format1,
          format1->Bmask         == format2->Bmask);
 }
 
+static Pixel SDLGetColorKey(SDL_Surface *surface)
+{
+  Pixel color_key;
+
+  if (SDL_GetColorKey(surface, &color_key) != 0)
+    return -1;
+
+  return color_key;
+}
+
+static boolean SDLHasColorKey(SDL_Surface *surface)
+{
+  return (SDLGetColorKey(surface) != -1);
+}
+
+static boolean SDLHasAlpha(SDL_Surface *surface)
+{
+  SDL_BlendMode blend_mode;
+
+  if (SDL_GetSurfaceBlendMode(surface, &blend_mode) != 0)
+    return FALSE;
+
+  return (blend_mode == SDL_BLENDMODE_BLEND);
+}
+
+static void SDLSetAlpha(SDL_Surface *surface, boolean set, int alpha)
+{
+  SDL_BlendMode blend_mode = (set ? SDL_BLENDMODE_BLEND : SDL_BLENDMODE_NONE);
+
+  SDL_SetSurfaceBlendMode(surface, blend_mode);
+  SDL_SetSurfaceAlphaMod(surface, alpha);
+}
+
 SDL_Surface *SDLGetNativeSurface(SDL_Surface *surface)
 {
   SDL_PixelFormat format;
@@ -277,6 +310,29 @@ boolean SDLSetNativeSurface(SDL_Surface **surface)
 
 #else
 
+static Pixel SDLGetColorKey(SDL_Surface *surface)
+{
+  if ((surface->flags & SDL_SRCCOLORKEY) == 0)
+    return -1;
+
+  return surface->format->colorkey;
+}
+
+static boolean SDLHasColorKey(SDL_Surface *surface)
+{
+  return (SDLGetColorKey(surface) != -1);
+}
+
+static boolean SDLHasAlpha(SDL_Surface *surface)
+{
+  return ((surface->flags & SDL_SRCALPHA) != 0);
+}
+
+static void SDLSetAlpha(SDL_Surface *surface, boolean set, int alpha)
+{
+  SDL_SetAlpha(surface, (set ? SDL_SRCALPHA : 0), alpha);
+}
+
 SDL_Surface *SDLGetNativeSurface(SDL_Surface *surface)
 {
   SDL_Surface *new_surface;