diff options
author | Bertram <bertram@cegetel.net> | 2009-08-14 01:05:49 +0200 |
---|---|---|
committer | Bertram <bertram@cegetel.net> | 2009-08-14 01:05:49 +0200 |
commit | 0c9ee8a74e62fe65acb551370db07ca568e3a0b3 (patch) | |
tree | 8245533eb473c2012e042cfb6fcba7f0e53f2948 /src/resources | |
parent | ed8025fe0dfcb1ca54c62ffc2905c1cb48772cde (diff) | |
download | mana-0c9ee8a74e62fe65acb551370db07ca568e3a0b3.tar.gz mana-0c9ee8a74e62fe65acb551370db07ca568e3a0b3.tar.bz2 mana-0c9ee8a74e62fe65acb551370db07ca568e3a0b3.tar.xz mana-0c9ee8a74e62fe65acb551370db07ca568e3a0b3.zip |
Added a Alpha Channel copier for 32 bit SDL based images.
This will later be used to keep the original alpha value...
Diffstat (limited to 'src/resources')
-rw-r--r-- | src/resources/image.cpp | 40 | ||||
-rw-r--r-- | src/resources/image.h | 11 |
2 files changed, 45 insertions, 6 deletions
diff --git a/src/resources/image.cpp b/src/resources/image.cpp index 172ff0f0..1689aa1d 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -38,8 +38,9 @@ Image::Image(SDL_Surface *image): #ifdef USE_OPENGL mGLImage(0), #endif - mSDLSurface(image), - mAlpha(1.0f) + mAlpha(1.0f), + mAlphaChannel(0), + mSDLSurface(image) { mBounds.x = 0; mBounds.y = 0; @@ -51,7 +52,7 @@ Image::Image(SDL_Surface *image): mBounds.w = mSDLSurface->w; mBounds.h = mSDLSurface->h; - mAlphaChannel = hasAlphaChannel(); + mHasAlphaChannel = hasAlphaChannel(); mLoaded = true; } else @@ -64,9 +65,10 @@ Image::Image(GLuint glimage, int width, int height, int texWidth, int texHeight) mGLImage(glimage), mTexWidth(texWidth), mTexHeight(texHeight), - mSDLSurface(0), mAlpha(1.0), - mAlphaChannel(true) + mHasAlphaChannel(true), + mAlphaChannel(0), + mSDLSurface(0) { mBounds.x = 0; mBounds.y = 0; @@ -388,6 +390,34 @@ Image *Image::_SDLload(SDL_Surface *tmpImage) return new Image(image); } +Uint8 *Image::_SDLgetAlphaChannel() +{ + if (!mSDLSurface) + return NULL; + + // If an old channel was stored, we free it. + free(mAlphaChannel); + mAlphaChannel = NULL; + + // We allocate the place to put our data + Uint8* mAlphaChannel = (Uint8*)malloc(mSDLSurface->w * mSDLSurface->h * sizeof(Uint8)); + + 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); + + mAlphaChannel[i] = a; + } + } +} + #ifdef USE_OPENGL Image *Image::_GLload(SDL_Surface *tmpImage) { diff --git a/src/resources/image.h b/src/resources/image.h index 535a3536..34ec2887 100644 --- a/src/resources/image.h +++ b/src/resources/image.h @@ -187,7 +187,7 @@ class Image : public Resource SDL_Rect mBounds; bool mLoaded; float mAlpha; - bool mAlphaChannel; + bool mHasAlphaChannel; // ----------------------- // SDL protected members @@ -196,8 +196,17 @@ class Image : public Resource /** SDL Constructor */ Image(SDL_Surface *image); + /** SDL_Surface to SDL_Surface Image loader */ static Image *_SDLload(SDL_Surface *tmpImage); + /** + * Make a converted copy of the alpha channel + * used for 32 bits SDLbased images + * in order to support changing the opacity. + */ + Uint8 *_SDLgetAlphaChannel(); + Uint8* mAlphaChannel; + SDL_Surface *mSDLSurface; |