summaryrefslogtreecommitdiff
path: root/src/resources/image.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/resources/image.cpp')
-rw-r--r--src/resources/image.cpp42
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)