New grow and skid sounds from remaxim
[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 #include <algorithm>
25
26 #include <SDL.h>
27
28 #include "texture.hpp"
29 #include "color.hpp"
30
31 namespace SDL
32 {
33   class Texture : public ::Texture
34   {
35   protected:
36     SDL_Surface *texture;
37     //unsigned int width;
38     //unsigned int height;
39
40     struct ColorCache
41     {
42       static const int HASHED_BITS = 3;
43       static const int CACHE_SIZE = 1 << (HASHED_BITS * 3);
44
45       static void ref(SDL_Surface *surface)
46       {
47         if(surface)
48         {
49           surface->refcount++;
50         }
51       }
52
53       static int hash(const Color &color)
54       {
55         return
56       ((int) (color.red * ((1 << HASHED_BITS) - 1)) << (HASHED_BITS - 1) * 2) |
57       ((int) (color.green * ((1 << HASHED_BITS) - 1)) << (HASHED_BITS - 1)) |
58       ((int) (color.blue * ((1 << HASHED_BITS) - 1)) << 0);
59       }
60
61       SDL_Surface *data[CACHE_SIZE];
62
63       ColorCache()
64       {
65         memset(data, 0, CACHE_SIZE * sizeof(SDL_Surface *));
66       }
67
68       ~ColorCache()
69       {
70         std::for_each(data, data + CACHE_SIZE, SDL_FreeSurface);
71       }
72
73       void operator = (const ColorCache &other)
74       {
75         std::for_each(other.data, other.data + CACHE_SIZE, ref);
76         std::for_each(data, data + CACHE_SIZE, SDL_FreeSurface);
77         memcpy(data, other.data, CACHE_SIZE * sizeof(SDL_Surface *));
78       }
79
80       SDL_Surface *&operator [] (const Color &color)
81       {
82         return data[hash(color)];
83       }
84     };
85     //typedef std::map<Color, SDL_Surface *> ColorCache;
86     ColorCache cache[NUM_EFFECTS];
87
88   public:
89     Texture(SDL_Surface* sdlsurface);
90     virtual ~Texture();
91
92     SDL_Surface *get_transform(const Color &color, DrawingEffect effect);
93
94     SDL_Surface *get_texture() const
95     {
96       return texture;
97     }
98
99     unsigned int get_texture_width() const
100     {
101       return texture->w;
102     }
103
104     unsigned int get_texture_height() const
105     {
106       return texture->h;
107     }
108
109     unsigned int get_image_width() const
110     {
111       return texture->w;
112     }
113
114     unsigned int get_image_height() const
115     {
116       return texture->h;
117     }
118
119     /*unsigned int get_texture_width() const
120     {
121       return width;
122     }
123
124     unsigned int get_texture_height() const
125     {
126       return height;
127     }
128
129     unsigned int get_image_width() const
130     {
131       return width;
132     }
133
134     unsigned int get_image_height() const
135     {
136       return height;
137     }*/
138   };
139 }
140
141 #endif