00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef HEADER_SUPERTUX_VIDEO_SDL_TEXTURE_HPP
00018 #define HEADER_SUPERTUX_VIDEO_SDL_TEXTURE_HPP
00019
00020 #include <algorithm>
00021 #include <config.h>
00022
00023 #include <SDL.h>
00024
00025 #include "video/color.hpp"
00026 #include "video/texture.hpp"
00027
00028 class SDLTexture : public Texture
00029 {
00030 protected:
00031 SDL_Surface *texture;
00032
00033
00034
00035 struct ColorCache
00036 {
00037 static const int HASHED_BITS = 3;
00038 static const int CACHE_SIZE = 1 << (HASHED_BITS * 3);
00039
00040 static void ref(SDL_Surface *surface)
00041 {
00042 if(surface)
00043 {
00044 surface->refcount++;
00045 }
00046 }
00047
00048 static int hash(const Color &color)
00049 {
00050 return
00051 ((int) (color.red * ((1 << HASHED_BITS) - 1)) << (HASHED_BITS - 1) * 2) |
00052 ((int) (color.green * ((1 << HASHED_BITS) - 1)) << (HASHED_BITS - 1)) |
00053 ((int) (color.blue * ((1 << HASHED_BITS) - 1)) << 0);
00054 }
00055
00056 SDL_Surface *data[CACHE_SIZE];
00057
00058 ColorCache()
00059 {
00060 memset(data, 0, CACHE_SIZE * sizeof(SDL_Surface *));
00061 }
00062
00063 ColorCache(const ColorCache&);
00064
00065 ~ColorCache()
00066 {
00067 std::for_each(data, data + CACHE_SIZE, SDL_FreeSurface);
00068 }
00069
00070 ColorCache& operator=(const ColorCache &other)
00071 {
00072 if (this != &other)
00073 {
00074 std::for_each(other.data, other.data + CACHE_SIZE, ref);
00075 std::for_each(data, data + CACHE_SIZE, SDL_FreeSurface);
00076 memcpy(data, other.data, CACHE_SIZE * sizeof(SDL_Surface *));
00077 }
00078 return *this;
00079 }
00080
00081 SDL_Surface *&operator [] (const Color &color)
00082 {
00083 return data[hash(color)];
00084 }
00085 };
00086
00087 ColorCache cache[NUM_EFFECTS];
00088
00089 public:
00090 SDLTexture(SDL_Surface* sdlsurface);
00091 virtual ~SDLTexture();
00092
00093 SDL_Surface *get_transform(const Color &color, DrawingEffect effect);
00094
00095 SDL_Surface *get_texture() const
00096 {
00097 return texture;
00098 }
00099
00100 unsigned int get_texture_width() const
00101 {
00102 return texture->w;
00103 }
00104
00105 unsigned int get_texture_height() const
00106 {
00107 return texture->h;
00108 }
00109
00110 unsigned int get_image_width() const
00111 {
00112 return texture->w;
00113 }
00114
00115 unsigned int get_image_height() const
00116 {
00117 return texture->h;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 private:
00141 SDLTexture(const SDLTexture&);
00142 SDLTexture& operator=(const SDLTexture&);
00143 };
00144
00145 #endif
00146
00147