restore trunk
[supertux.git] / src / video / sdl_texture.hpp
1 //  $Id: sdl_texture.hpp 4063 2006-07-21 21:05:23Z anmaster $
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #ifndef __SDL_TEXTURE_HPP__
21 #define __SDL_TEXTURE_HPP__
22
23 #include <config.h>
24
25 #include <SDL.h>
26
27 #include "texture.hpp"
28 #include "color.hpp"
29
30 namespace SDL
31 {
32   class Texture : public ::Texture
33   {
34   protected:
35     SDL_Surface *texture;
36     //unsigned int width;
37     //unsigned int height;
38
39     struct ColorCache
40     {
41       static const int HASHED_BITS = 3;
42       static const int CACHE_SIZE = 1 << (HASHED_BITS * 3);
43
44       static void ref(SDL_Surface *surface)
45       {
46         if(surface)
47         {
48           surface->refcount++;
49         }
50       }
51
52       static int hash(const Color &color)
53       {
54         return
55       ((int) (color.red * ((1 << HASHED_BITS) - 1)) << (HASHED_BITS - 1) * 2) |
56       ((int) (color.green * ((1 << HASHED_BITS) - 1)) << (HASHED_BITS - 1)) |
57       ((int) (color.blue * ((1 << HASHED_BITS) - 1)) << 0);
58       }
59
60       SDL_Surface *data[CACHE_SIZE];
61
62       ColorCache()
63       {
64         memset(data, 0, CACHE_SIZE * sizeof(SDL_Surface *));
65       }
66
67       ~ColorCache()
68       {
69         std::for_each(data, data + CACHE_SIZE, SDL_FreeSurface);
70       }
71
72       void operator = (const ColorCache &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
79       SDL_Surface *&operator [] (const Color &color)
80       {
81         return data[hash(color)];
82       }
83     };
84     //typedef std::map<Color, SDL_Surface *> ColorCache;
85     ColorCache cache[NUM_EFFECTS];
86
87   public:
88     Texture(SDL_Surface* sdlsurface);
89     virtual ~Texture();
90
91     SDL_Surface *get_transform(const Color &color, DrawingEffect effect);
92
93     SDL_Surface *get_texture() const
94     {
95       return texture;
96     }
97
98     unsigned int get_texture_width() const
99     {
100       return texture->w;
101     }
102
103     unsigned int get_texture_height() const
104     {
105       return texture->h;
106     }
107
108     unsigned int get_image_width() const
109     {
110       return texture->w;
111     }
112
113     unsigned int get_image_height() const
114     {
115       return texture->h;
116     }
117
118     /*unsigned int get_texture_width() const
119     {
120       return width;
121     }
122
123     unsigned int get_texture_height() const
124     {
125       return height;
126     }
127
128     unsigned int get_image_width() const
129     {
130       return width;
131     }
132
133     unsigned int get_image_height() const
134     {
135       return height;
136     }*/
137   };
138 }
139
140 #endif