From 32e63222acc750fce68048211df3434256802ec4 Mon Sep 17 00:00:00 2001 From: Bertram Date: Fri, 14 Aug 2009 00:19:00 +0200 Subject: Added a new function to know if an image is using an alpha channel. This all will be useful for my next patch: Repair windows opacity break in SDL mode. --- src/resources/image.cpp | 42 +++++++++++++++++++++++++++++++++++++++++- src/resources/image.h | 10 ++++++++-- 2 files changed, 49 insertions(+), 3 deletions(-) (limited to 'src/resources') diff --git a/src/resources/image.cpp b/src/resources/image.cpp index b3f7140d..0540d543 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -48,6 +48,8 @@ Image::Image(SDL_Surface *image): { mBounds.w = mSDLSurface->w; mBounds.h = mSDLSurface->h; + + mAlphaChannel = hasAlphaChannel(); mLoaded = true; } else @@ -64,7 +66,8 @@ Image::Image(GLuint glimage, int width, int height, int texWidth, int texHeight) mTexWidth(texWidth), mTexHeight(texHeight), mSDLSurface(0), - mAlpha(1.0) + mAlpha(1.0), + mAlphaChannel(true) { mBounds.x = 0; mBounds.y = 0; @@ -189,6 +192,43 @@ bool Image::isAnOpenGLOne() const #endif } +bool Image::hasAlphaChannel() +{ + if (mLoaded) + return mAlphaChannel; + +#ifdef USE_OPENGL + if (mUseOpenGL) + return true; +#endif + + if (!mSDLSurface) + return false; + + bool hasAlpha = false; + + if (mSDLSurface->format->BitsPerPixel == 32) + { + // Figure out whether the image uses its alpha layer + for (int i = 0; i < mSDLSurface->w * mSDLSurface->h; ++i) + { + Uint8 r, g, b, a; + SDL_GetRGBA( + ((Uint32*) mSDLSurface->pixels)[i], + mSDLSurface->format, + &r, &g, &b, &a); + + if (a != 255) + { + hasAlpha = true; + break; + } + } + } + + return hasAlpha; +} + void Image::setAlpha(float a) { if (mAlpha == a) diff --git a/src/resources/image.h b/src/resources/image.h index c97523a9..535a3536 100644 --- a/src/resources/image.h +++ b/src/resources/image.h @@ -111,11 +111,17 @@ class Image : public Resource { return mBounds.h; } /** - * Tells if the image was loade using OpenGL or SDL + * Tells if the image was loaded using OpenGL or SDL * @return true if OpenGL, false if SDL. */ bool isAnOpenGLOne() const; + /** + * Tells if the image has got an alpha channel + * @return true if OpenGL, false if SDL. + */ + bool hasAlphaChannel(); + /** * Sets the alpha value of this image. */ @@ -181,6 +187,7 @@ class Image : public Resource SDL_Rect mBounds; bool mLoaded; float mAlpha; + bool mAlphaChannel; // ----------------------- // SDL protected members @@ -191,7 +198,6 @@ class Image : public Resource static Image *_SDLload(SDL_Surface *tmpImage); - SDL_Surface *mSDLSurface; -- cgit v1.2.3-60-g2f50