More fixes, down to 4 errors
[supertux.git] / src / video / sdl / sdl_texture.hpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #ifndef HEADER_SUPERTUX_VIDEO_SDL_TEXTURE_HPP
18 #define HEADER_SUPERTUX_VIDEO_SDL_TEXTURE_HPP
19
20 #include <algorithm>
21 #include <config.h>
22
23 #include <SDL.h>
24
25 #include "video/color.hpp"
26 #include "video/texture.hpp"
27
28 class SDLTexture : public Texture
29 {
30 protected:
31   SDL_Surface *texture;
32   //unsigned int width;
33   //unsigned int height;
34
35   struct ColorCache
36   {
37     static const int HASHED_BITS = 3;
38     static const int CACHE_SIZE = 1 << (HASHED_BITS * 3);
39
40     static void ref(SDL_Surface *surface)
41     {
42       if(surface)
43       {
44         surface->refcount++;
45       }
46     }
47
48     static int hash(const Color &color)
49     {
50       return
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);
54     }
55
56     SDL_Surface *data[CACHE_SIZE];
57
58     ColorCache()
59     {
60       memset(data, 0, CACHE_SIZE * sizeof(SDL_Surface *));
61     }
62
63     ColorCache(const ColorCache&);
64
65     ~ColorCache()
66     {
67       std::for_each(data, data + CACHE_SIZE, SDL_FreeSurface);
68     }
69
70     ColorCache& operator=(const ColorCache &other)
71     {
72       if (this != &other)
73       {
74         std::for_each(other.data, other.data + CACHE_SIZE, ref);
75         std::for_each(data, data + CACHE_SIZE, SDL_FreeSurface);
76         memcpy(data, other.data, CACHE_SIZE * sizeof(SDL_Surface *));
77       }
78       return *this;
79     }
80
81     SDL_Surface *&operator [] (const Color &color)
82     {
83       return data[hash(color)];
84     }
85   };
86   //typedef std::map<Color, SDL_Surface *> ColorCache;
87   ColorCache cache[NUM_EFFECTS];
88
89 public:
90   SDLTexture(SDL_Surface* sdlsurface);
91   virtual ~SDLTexture();
92
93   SDL_Surface *get_transform(const Color &color, DrawingEffect effect);
94
95   SDL_Surface *get_texture() const
96   {
97     return texture;
98   }
99
100   unsigned int get_texture_width() const
101   {
102     return texture->w;
103   }
104
105   unsigned int get_texture_height() const
106   {
107     return texture->h;
108   }
109
110   unsigned int get_image_width() const
111   {
112     return texture->w;
113   }
114
115   unsigned int get_image_height() const
116   {
117     return texture->h;
118   }
119
120   /*unsigned int get_texture_width() const
121     {
122     return width;
123     }
124
125     unsigned int get_texture_height() const
126     {
127     return height;
128     }
129
130     unsigned int get_image_width() const
131     {
132     return width;
133     }
134
135     unsigned int get_image_height() const
136     {
137     return height;
138     }*/
139
140 private:
141   SDLTexture(const SDLTexture&);
142   SDLTexture& operator=(const SDLTexture&);
143 };
144
145 #endif
146
147 /* EOF */