Refactored video/ subsystem to make adding other methods of rendering (in particular...
[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
29 class Color;
30
31 namespace SDL
32 {
33   class Texture : public ::Texture
34   {
35   protected:
36     SDL_Surface *texture;
37     int numerator;
38     int denominator;
39
40     struct Cache
41     {
42       static void ref(SDL_Surface *surface)
43       {
44         if(surface)
45         {
46           surface->refcount++;
47         }
48       }
49
50       SDL_Surface *data[NUM_EFFECTS];
51
52       Cache()
53       {
54         memset(data, 0, NUM_EFFECTS * sizeof(SDL_Surface *));
55       }
56
57       ~Cache()
58       {
59         std::for_each(data, data + NUM_EFFECTS, SDL_FreeSurface);
60       }
61
62       void operator = (const Cache &other)
63       {
64         std::for_each(other.data, other.data + NUM_EFFECTS, ref);
65         std::for_each(data, data + NUM_EFFECTS, SDL_FreeSurface);
66         memcpy(data, other.data, sizeof(Cache));
67       }
68
69       SDL_Surface *&operator [] (DrawingEffect effect)
70       {
71         return data[effect];
72       }
73     };
74     mutable std::map<Color, Cache> cache; /**< Cache for processed surfaces */
75
76   public:
77     Texture(SDL_Surface* sdlsurface);
78     virtual ~Texture();
79
80     SDL_Surface *get_transform(const Color &color, DrawingEffect effect);
81
82     SDL_Surface *get_texture() const
83     {
84       return texture;
85     }
86
87     unsigned int get_texture_width() const
88     {
89       return texture->w;
90     }
91
92     unsigned int get_texture_height() const
93     {
94       return texture->h;
95     }
96
97     unsigned int get_image_width() const
98     {
99       return texture->w;
100     }
101
102     unsigned int get_image_height() const
103     {
104       return texture->h;
105     }
106   };
107 }
108
109 #endif