summaryrefslogtreecommitdiff
path: root/src/resources
diff options
context:
space:
mode:
Diffstat (limited to 'src/resources')
-rw-r--r--src/resources/image.cpp42
-rw-r--r--src/resources/image.h10
2 files changed, 49 insertions, 3 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)
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,12 +111,18 @@ 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.
*/
virtual void setAlpha(float alpha);
@@ -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;