New grow and skid sounds from remaxim
[supertux.git] / src / video / sdl_texture.hpp
index 0a8045f..be4f2fc 100644 (file)
 #define __SDL_TEXTURE_HPP__
 
 #include <config.h>
+#include <algorithm>
 
 #include <SDL.h>
 
 #include "texture.hpp"
-
-class Color;
+#include "color.hpp"
 
 namespace SDL
 {
@@ -34,11 +34,14 @@ namespace SDL
   {
   protected:
     SDL_Surface *texture;
-    int numerator;
-    int denominator;
+    //unsigned int width;
+    //unsigned int height;
 
-    struct Cache
+    struct ColorCache
     {
+      static const int HASHED_BITS = 3;
+      static const int CACHE_SIZE = 1 << (HASHED_BITS * 3);
+
       static void ref(SDL_Surface *surface)
       {
         if(surface)
@@ -47,31 +50,40 @@ namespace SDL
         }
       }
 
-      SDL_Surface *data[NUM_EFFECTS];
+      static int hash(const Color &color)
+      {
+        return
+      ((int) (color.red * ((1 << HASHED_BITS) - 1)) << (HASHED_BITS - 1) * 2) |
+      ((int) (color.green * ((1 << HASHED_BITS) - 1)) << (HASHED_BITS - 1)) |
+      ((int) (color.blue * ((1 << HASHED_BITS) - 1)) << 0);
+      }
+
+      SDL_Surface *data[CACHE_SIZE];
 
-      Cache()
+      ColorCache()
       {
-        memset(data, 0, NUM_EFFECTS * sizeof(SDL_Surface *));
+        memset(data, 0, CACHE_SIZE * sizeof(SDL_Surface *));
       }
 
-      ~Cache()
+      ~ColorCache()
       {
-        std::for_each(data, data + NUM_EFFECTS, SDL_FreeSurface);
+        std::for_each(data, data + CACHE_SIZE, SDL_FreeSurface);
       }
 
-      void operator = (const Cache &other)
+      void operator = (const ColorCache &other)
       {
-        std::for_each(other.data, other.data + NUM_EFFECTS, ref);
-        std::for_each(data, data + NUM_EFFECTS, SDL_FreeSurface);
-        memcpy(data, other.data, sizeof(Cache));
+        std::for_each(other.data, other.data + CACHE_SIZE, ref);
+        std::for_each(data, data + CACHE_SIZE, SDL_FreeSurface);
+        memcpy(data, other.data, CACHE_SIZE * sizeof(SDL_Surface *));
       }
 
-      SDL_Surface *&operator [] (DrawingEffect effect)
+      SDL_Surface *&operator [] (const Color &color)
       {
-        return data[effect];
+        return data[hash(color)];
       }
     };
-    mutable std::map<Color, Cache> cache; /**< Cache for processed surfaces */
+    //typedef std::map<Color, SDL_Surface *> ColorCache;
+    ColorCache cache[NUM_EFFECTS];
 
   public:
     Texture(SDL_Surface* sdlsurface);
@@ -103,6 +115,26 @@ namespace SDL
     {
       return texture->h;
     }
+
+    /*unsigned int get_texture_width() const
+    {
+      return width;
+    }
+
+    unsigned int get_texture_height() const
+    {
+      return height;
+    }
+
+    unsigned int get_image_width() const
+    {
+      return width;
+    }
+
+    unsigned int get_image_height() const
+    {
+      return height;
+    }*/
   };
 }