From: Florian Forster Date: Thu, 14 Jan 2010 17:45:21 +0000 (+0100) Subject: sdl/sdl_{renderer,texture}.cpp: Do no access (SDL_Surface *)->format->alpha directly. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=9fb702e68a4b3bd96645dc715f565e117ae953d5;p=supertux.git sdl/sdl_{renderer,texture}.cpp: Do no access (SDL_Surface *)->format->alpha directly. SDL_GetSurfaceAlphaMod is used instead. --- diff --git a/src/video/sdl/sdl_renderer.cpp b/src/video/sdl/sdl_renderer.cpp index 39e8860f8..10817f976 100644 --- a/src/video/sdl/sdl_renderer.cpp +++ b/src/video/sdl/sdl_renderer.cpp @@ -193,6 +193,12 @@ SDLRenderer::draw_surface(const DrawingRequest& request) Uint8 alpha = 0; if(request.alpha != 1.0) { +#if SDL_VERSION_ATLEAST(1,3,0) + Uint8 tmp; + SDL_GetSurfaceAlphaMod (transform, &alpha); + tmp = (Uint8) (((double) alpha) * request.alpha + .5); + SDL_SetSurfaceAlphaMod (transform, tmp); +#else if(!transform->format->Amask) { if(transform->flags & SDL_SRCALPHA) @@ -209,6 +215,7 @@ SDLRenderer::draw_surface(const DrawingRequest& request) { transform = apply_alpha(transform, request.alpha); }*/ +#endif } SDL_BlitSurface(transform, src_rect, screen, &dst_rect); @@ -288,7 +295,11 @@ SDLRenderer::draw_surface_part(const DrawingRequest& request) { if(transform->flags & SDL_SRCALPHA) { +#if SDL_VERSION_ATLEAST(1,3,0) + SDL_GetSurfaceAlphaMod (transform, &alpha); +#else alpha = transform->format->alpha; +#endif } else { diff --git a/src/video/sdl/sdl_texture.cpp b/src/video/sdl/sdl_texture.cpp index 2a93c6d37..6e32e60b4 100644 --- a/src/video/sdl/sdl_texture.cpp +++ b/src/video/sdl/sdl_texture.cpp @@ -88,6 +88,37 @@ SDL_Surface *scale(SDL_Surface *src, int numerator, int denominator) } // namespace #endif +static void surface_copy_alpha (SDL_Surface *dst, SDL_Surface *src) +{ +#if SDL_VERSION_ATLEAST(1,3,0) + Uint8 alpha; + int status; + + alpha = 255; + status = SDL_GetSurfaceAlphaMod (src, &alpha); + if (status == 0) + SDL_SetSurfaceAlphaMod (dst, alpha); +#else + SDL_SetAlpha(dst, SDL_SRCALPHA | SDL_RLEACCEL, src->format->alpha); +#endif +} + +static void surface_copy_colorkey (SDL_Surface *dst, SDL_Surface *src) +{ +#if SDL_VERSION_ATLEAST(1,3,0) + Uint32 colorkey; + int status; + + colorkey = 0; + /* Returns non-zero if color key is disabled. */ + status = SDL_GetColorKey (src, &colorkey); + if (status == 0) + SDL_SetColorKey (dst, /* enabled = */ 1, colorkey); +#else + SDL_SetColorKey(dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, src->format->colorkey); +#endif +} + #ifdef BILINEAR void getpixel(SDL_Surface *src, int srcx, int srcy, Uint8 color[4]) { @@ -207,17 +238,17 @@ SDL_Surface *scale(SDL_Surface *src, int numerator, int denominator) { if(src->flags & SDL_SRCALPHA) { - SDL_SetAlpha(dst, SDL_SRCALPHA | SDL_RLEACCEL, src->format->alpha); + surface_copy_alpha (dst, src); } if(src->flags & SDL_SRCCOLORKEY) { - SDL_SetColorKey(dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, src->format->colorkey); + surface_copy_colorkey (dst, src); } } return dst; } } -#endif +#endif /* BILINEAR */ SDL_Surface *horz_flip(SDL_Surface *src) { @@ -259,11 +290,11 @@ SDL_Surface *horz_flip(SDL_Surface *src) { if(src->flags & SDL_SRCALPHA) { - SDL_SetAlpha(dst, SDL_SRCALPHA | SDL_RLEACCEL, src->format->alpha); + surface_copy_alpha (dst, src); } if(src->flags & SDL_SRCCOLORKEY) { - SDL_SetColorKey(dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, src->format->colorkey); + surface_copy_colorkey (dst, src); } } return dst; @@ -309,11 +340,11 @@ SDL_Surface *vert_flip(SDL_Surface *src) { if(src->flags & SDL_SRCALPHA) { - SDL_SetAlpha(dst, SDL_SRCALPHA | SDL_RLEACCEL, src->format->alpha); + surface_copy_alpha (dst, src); } if(src->flags & SDL_SRCCOLORKEY) { - SDL_SetColorKey(dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, src->format->colorkey); + surface_copy_colorkey (dst, src); } } return dst; @@ -321,12 +352,23 @@ SDL_Surface *vert_flip(SDL_Surface *src) SDL_Surface *colorize(SDL_Surface *src, const Color &color) { + SDL_Surface *dst = SDL_CreateRGBSurface(src->flags, src->w, src->h, src->format->BitsPerPixel, src->format->Rmask, src->format->Gmask, src->format->Bmask, src->format->Amask); +#if SDL_VERSION_ATLEAST(1,3,0) + assert ((color.red >= 0) && (color.red <= 1.0)); + assert ((color.green >= 0) && (color.green <= 1.0)); + assert ((color.blue >= 0) && (color.blue <= 1.0)); + assert(color.red != 1.0 || color.green != 1.0 || color.blue != 1.0); + Uint8 red = (Uint8) (color.red * 255.0); + Uint8 green = (Uint8) (color.green * 255.0); + Uint8 blue = (Uint8) (color.blue * 255.0); + + SDL_SetSurfaceColorMod (dst, red, green, blue); +#else // FIXME: This is really slow assert(color.red != 1.0 || color.green != 1.0 || color.blue != 1.0); int red = (int) (color.red * 256); int green = (int) (color.green * 256); int blue = (int) (color.blue * 256); - SDL_Surface *dst = SDL_CreateRGBSurface(src->flags, src->w, src->h, src->format->BitsPerPixel, src->format->Rmask, src->format->Gmask, src->format->Bmask, src->format->Amask); int bpp = dst->format->BytesPerPixel; if(SDL_MUSTLOCK(src)) { @@ -393,6 +435,7 @@ SDL_Surface *colorize(SDL_Surface *src, const Color &color) } } } +#endif /* SDL version < 1.3.0 */ if(SDL_MUSTLOCK(dst)) { SDL_UnlockSurface(dst); @@ -405,11 +448,11 @@ SDL_Surface *colorize(SDL_Surface *src, const Color &color) { if(src->flags & SDL_SRCALPHA) { - SDL_SetAlpha(dst, SDL_SRCALPHA | SDL_RLEACCEL, src->format->alpha); + surface_copy_alpha (dst, src); } if(src->flags & SDL_SRCCOLORKEY) { - SDL_SetColorKey(dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, src->format->colorkey); + surface_copy_colorkey (dst, src); } } return dst;