All pastes #1843569 Raw Edit

Unnamed

public text v1 · immutable
#1843569 ·published 2010-03-17 14:19 UTC
rendered paste body
diff --git a/src/resources/image.cpp b/src/resources/image.cpp
index 0e7dc89..c8987d9 100644
--- a/src/resources/image.cpp
+++ b/src/resources/image.cpp
@@ -165,6 +165,19 @@ void Image::unload()
 {
     mLoaded = false;
 
+    // Clear the cache
+    SurfaceCache::iterator it = mSDLSurfaceCache.begin();
+    while (it != mSDLSurfaceCache.end())
+    {
+        // We delete mSDLSurface later
+        if (it->second != mSDLSurface && it->second)
+        {
+            SDL_FreeSurface(it->second);
+            it->second = NULL;
+        }
+        it++;
+    }
+
     if (mSDLSurface)
     {
         // Free the image surface.
@@ -214,10 +227,26 @@ void Image::setAlpha(float alpha)
     if (alpha < 0.0f || alpha > 1.0f)
         return;
 
+    float oldAlpha = mAlpha;
     mAlpha = alpha;
 
     if (mSDLSurface)
     {
+        // See if we need to cache oldAlpha (currently in mSDLSurface)
+        SurfaceCache::iterator cache = mSDLSurfaceCache.find(oldAlpha);
+        if (cache == mSDLSurfaceCache.end())
+        {
+            mSDLSurfaceCache[oldAlpha] = SDL_ConvertSurface(
+                                            mSDLSurface, mSDLSurface->format, SDL_SWSURFACE);
+        }
+        // See if the new alpha is already in the cache
+        cache = mSDLSurfaceCache.find(mAlpha);
+        if (cache != mSDLSurfaceCache.end())
+        {
+            SDL_FreeSurface(mSDLSurface);
+            mSDLSurface = cache->second;
+            return;
+        }
         if (!hasAlphaChannel())
         {
             // Set the alpha value this image is drawn at
diff --git a/src/resources/image.h b/src/resources/image.h
index 3e8ad55..83808cf 100644
--- a/src/resources/image.h
+++ b/src/resources/image.h
@@ -24,6 +24,8 @@
 
 #include "main.h"
 
+#include <map>
+
 #include "resources/resource.h"
 
 #include <SDL.h>
@@ -42,6 +44,7 @@
 class Dye;
 class Position;
 
+typedef std::map<float, SDL_Surface* > SurfaceCache;
 /**
  * Defines a class for loading and storing images.
  */
@@ -210,6 +213,9 @@ class Image : public Resource
 
         SDL_Surface *mSDLSurface;
 
+        /** Used to cache different alphas */
+        SurfaceCache mSDLSurfaceCache;
+
         /** Alpha Channel pointer used for 32bit based SDL surfaces */
         Uint8 *mAlphaChannel;