X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fvideo%2Fsdl_texture.hpp;h=34479dbca2478e27d410450b16a6303bf136ad3a;hb=fea3446f05e1e7673607b835c269d3e8d1929ab3;hp=0a8045fca0ac04c50d9954bea66e7ea8371bb360;hpb=20efd7620620892d92b1c7df124c3a0c8df22a82;p=supertux.git diff --git a/src/video/sdl_texture.hpp b/src/video/sdl_texture.hpp index 0a8045fca..34479dbca 100644 --- a/src/video/sdl_texture.hpp +++ b/src/video/sdl_texture.hpp @@ -25,8 +25,7 @@ #include #include "texture.hpp" - -class Color; +#include "color.hpp" namespace SDL { @@ -34,11 +33,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 +49,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 cache; /**< Cache for processed surfaces */ + //typedef std::map ColorCache; + ColorCache cache[NUM_EFFECTS]; public: Texture(SDL_Surface* sdlsurface); @@ -103,6 +114,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; + }*/ }; }