Implemented basic rendering operations, game is now playable again, rounded rectangle...
[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_Texture* texture;
32   int width;
33   int height;
34
35 #ifdef OLD_SDL1
36   struct ColorCache
37   {
38     static const int HASHED_BITS = 3;
39     static const int CACHE_SIZE = 1 << (HASHED_BITS * 3);
40
41     static void ref(SDL_Surface *surface)
42     {
43       if(surface)
44       {
45         surface->refcount++;
46       }
47     }
48
49     static int hash(const Color &color)
50     {
51       return
52         ((int) (color.red * ((1 << HASHED_BITS) - 1)) << (HASHED_BITS - 1) * 2) |
53         ((int) (color.green * ((1 << HASHED_BITS) - 1)) << (HASHED_BITS - 1)) |
54         ((int) (color.blue * ((1 << HASHED_BITS) - 1)) << 0);
55     }
56
57     SDL_Surface *data[CACHE_SIZE];
58
59     ColorCache()
60     {
61       memset(data, 0, CACHE_SIZE * sizeof(SDL_Surface *));
62     }
63
64     ColorCache(const ColorCache&);
65
66     ~ColorCache()
67     {
68       std::for_each(data, data + CACHE_SIZE, SDL_FreeSurface);
69     }
70
71     ColorCache& operator=(const ColorCache &other)
72     {
73       if (this != &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       return *this;
80     }
81
82     SDL_Surface *&operator [] (const Color &color)
83     {
84       return data[hash(color)];
85     }
86   };
87   //typedef std::map<Color, SDL_Surface *> ColorCache;
88   ColorCache cache[NUM_EFFECTS];
89 #endif
90
91 public:
92   SDLTexture(SDL_Surface* sdlsurface);
93   virtual ~SDLTexture();
94
95 #ifdef OLD_SDL1
96   SDL_Surface *get_transform(const Color &color, DrawingEffect effect);
97 #endif
98
99   SDL_Texture *get_texture() const
100   {
101     return texture;
102   }
103
104   unsigned int get_texture_width() const
105   {
106     return width;
107   }
108
109   unsigned int get_texture_height() const
110   {
111     return height;
112   }
113
114   unsigned int get_image_width() const
115   {
116     return width;
117   }
118
119   unsigned int get_image_height() const
120   {
121     return height;
122   }
123
124 private:
125   SDLTexture(const SDLTexture&);
126   SDLTexture& operator=(const SDLTexture&);
127 };
128
129 #endif
130
131 /* EOF */