diff options
author | Bertram <bertram@cegetel.net> | 2009-09-17 00:04:21 +0200 |
---|---|---|
committer | Bertram <bertram@cegetel.net> | 2009-09-17 00:04:21 +0200 |
commit | 78134dc88b0b39b0d051392fa937418bcd384404 (patch) | |
tree | 6e2491a0fba0f75c8fce07364cba5785f0853d22 /src/resources/image.cpp | |
parent | 96ca40bdc307be0a527d1c98edffdb1bc4c85f8f (diff) | |
download | mana-78134dc88b0b39b0d051392fa937418bcd384404.tar.gz mana-78134dc88b0b39b0d051392fa937418bcd384404.tar.bz2 mana-78134dc88b0b39b0d051392fa937418bcd384404.tar.xz mana-78134dc88b0b39b0d051392fa937418bcd384404.zip |
Optimized the Image::setAlpha() function for SDL.
Diffstat (limited to 'src/resources/image.cpp')
-rw-r--r-- | src/resources/image.cpp | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/src/resources/image.cpp b/src/resources/image.cpp index d2a1c82e..15ad3b54 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -229,20 +229,31 @@ void Image::setAlpha(float alpha) if (SDL_MUSTLOCK(mSDLSurface)) SDL_LockSurface(mSDLSurface); - 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); - - a = (Uint8) (mAlphaChannel[i] * mAlpha); - - // Here is the pixel we want to set - ((Uint32 *)(mSDLSurface->pixels))[i] = - SDL_MapRGBA(mSDLSurface->format, r, g, b, a); - } + // Precompute as much as possible + int maxHeight = std::min((mBounds.y + mBounds.h), mSDLSurface->h); + int maxWidth = std::min((mBounds.x + mBounds.w), mSDLSurface->w); + int i = 0; + + for (int y = mBounds.y; y < maxHeight; y++) + for (int x = mBounds.x; x < maxWidth; x++) + { + i = y * mSDLSurface->w + x; + // Only change the pixel if it was visible at load time... + Uint8 sourceAlpha = mAlphaChannel[i]; + if (sourceAlpha > 0) + { + Uint8 r, g, b, a; + SDL_GetRGBA(((Uint32*) mSDLSurface->pixels)[i], + mSDLSurface->format, + &r, &g, &b, &a); + + a = (Uint8) (sourceAlpha * mAlpha); + + // Here is the pixel we want to set + ((Uint32 *)(mSDLSurface->pixels))[i] = + SDL_MapRGBA(mSDLSurface->format, r, g, b, a); + } + } if (SDL_MUSTLOCK(mSDLSurface)) SDL_UnlockSurface(mSDLSurface); |