summaryrefslogtreecommitdiff
path: root/src/resources
diff options
context:
space:
mode:
authorBertram <bertram@cegetel.net>2009-09-17 00:04:21 +0200
committerBertram <bertram@cegetel.net>2009-09-17 00:04:21 +0200
commit78134dc88b0b39b0d051392fa937418bcd384404 (patch)
tree6e2491a0fba0f75c8fce07364cba5785f0853d22 /src/resources
parent96ca40bdc307be0a527d1c98edffdb1bc4c85f8f (diff)
downloadmana-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')
-rw-r--r--src/resources/image.cpp39
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);