2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef HEADER_SUPERTUX_VIDEO_SDL_TEXTURE_HPP
18 #define HEADER_SUPERTUX_VIDEO_SDL_TEXTURE_HPP
25 #include "video/color.hpp"
26 #include "video/texture.hpp"
28 class SDLTexture : public Texture
33 //unsigned int height;
37 static const int HASHED_BITS = 3;
38 static const int CACHE_SIZE = 1 << (HASHED_BITS * 3);
40 static void ref(SDL_Surface *surface)
48 static int hash(const Color &color)
51 ((int) (color.red * ((1 << HASHED_BITS) - 1)) << (HASHED_BITS - 1) * 2) |
52 ((int) (color.green * ((1 << HASHED_BITS) - 1)) << (HASHED_BITS - 1)) |
53 ((int) (color.blue * ((1 << HASHED_BITS) - 1)) << 0);
56 SDL_Surface *data[CACHE_SIZE];
60 memset(data, 0, CACHE_SIZE * sizeof(SDL_Surface *));
65 std::for_each(data, data + CACHE_SIZE, SDL_FreeSurface);
68 void operator = (const ColorCache &other)
70 std::for_each(other.data, other.data + CACHE_SIZE, ref);
71 std::for_each(data, data + CACHE_SIZE, SDL_FreeSurface);
72 memcpy(data, other.data, CACHE_SIZE * sizeof(SDL_Surface *));
75 SDL_Surface *&operator [] (const Color &color)
77 return data[hash(color)];
80 //typedef std::map<Color, SDL_Surface *> ColorCache;
81 ColorCache cache[NUM_EFFECTS];
84 SDLTexture(SDL_Surface* sdlsurface);
85 virtual ~SDLTexture();
87 SDL_Surface *get_transform(const Color &color, DrawingEffect effect);
89 SDL_Surface *get_texture() const
94 unsigned int get_texture_width() const
99 unsigned int get_texture_height() const
104 unsigned int get_image_width() const
109 unsigned int get_image_height() const
114 /*unsigned int get_texture_width() const
119 unsigned int get_texture_height() const
124 unsigned int get_image_width() const
129 unsigned int get_image_height() const
135 SDLTexture(const SDLTexture&);
136 SDLTexture& operator=(const SDLTexture&);