diff options
author | Bertram <bertram@cegetel.net> | 2009-08-14 00:19:00 +0200 |
---|---|---|
committer | Bertram <bertram@cegetel.net> | 2009-08-14 00:19:00 +0200 |
commit | 32e63222acc750fce68048211df3434256802ec4 (patch) | |
tree | 608bdf91eb83019dfe0a8a96b721d3eee1bb6d6f /src/resources/image.cpp | |
parent | 1f2a31c0a97ee2ff858fe142e778c49011b93f71 (diff) | |
download | mana-32e63222acc750fce68048211df3434256802ec4.tar.gz mana-32e63222acc750fce68048211df3434256802ec4.tar.bz2 mana-32e63222acc750fce68048211df3434256802ec4.tar.xz mana-32e63222acc750fce68048211df3434256802ec4.zip |
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.
Diffstat (limited to 'src/resources/image.cpp')
-rw-r--r-- | src/resources/image.cpp | 42 |
1 files changed, 41 insertions, 1 deletions
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) |