fabf01e79247735b71f7aab0a8dc40268b8a94d7
[supertux.git] / src / unison / src / video / sdl / Texture.hpp
1 //          Copyright Timothy Goya 2007.
2 // Distributed under the Boost Software License, Version 1.0.
3 //    (See accompanying file LICENSE_1_0.txt or copy at
4 //          http://www.boost.org/LICENSE_1_0.txt)
5
6 #ifndef UNISON_VIDEO_SDL_TEXTURE_HPP
7 #define UNISON_VIDEO_SDL_TEXTURE_HPP
8
9 #include <unison/video/backend/Texture.hpp>
10 #include <unison/video/Surface.hpp>
11
12 #include <string>
13 #include <map>
14
15 #include "SDL.h"
16
17 namespace Unison
18 {
19    namespace Video
20    {
21       namespace SDL
22       {
23          class Texture : public Backend::Texture
24          {
25             public:
26                Texture(const Surface &surface);
27                //Texture(const Surface &surface, const std::string &name);
28                //Texture(Backend::Texture *texture);
29                ~Texture();
30
31                const Surface get_surface();
32                void save();
33                void blit(const Surface &src, const Point &dst_pos, const Rect &src_rect, const RenderOptions &options);
34                void blit(const Video::Texture &src, const Point &dst_pos, const Rect &src_rect, const RenderOptions &options);
35                void fill(const Color &color, const Rect &rect);
36                void fill_blend(const Color &color, const Rect &rect);
37
38                SDL_Surface *get_transform(const RenderOptions &options, unsigned int numerator, unsigned int denominator);
39             private:
40                Surface scaled;
41                struct AlphaMap
42                {
43                   static void ref(SDL_Surface *surface)
44                   {
45                     if(surface)
46                     {
47                       surface->refcount++;
48                     }
49                   }
50
51                   SDL_Surface *data[256];
52
53                   AlphaMap()
54                   {
55                      memset(data, 0, 256 * sizeof(SDL_Surface *));
56                   }
57
58                   ~AlphaMap()
59                   {
60                      std::for_each(data, data + 256, SDL_FreeSurface);
61                   }
62
63                   void operator = (const AlphaMap &other)
64                   {
65                      std::for_each(other.data, other.data + 256, ref);
66                      std::for_each(data, data + 256, SDL_FreeSurface);
67                      memcpy(data, other.data, 256 * sizeof(SDL_Surface *));
68                   }
69
70                   SDL_Surface *&operator [] (Uint8 alpha)
71                   {
72                      return data[alpha];
73                   }
74                };
75                //std::map<Color, AlphaMap> cache;
76                std::map<Color, AlphaMap> n_cache;
77                std::map<Color, AlphaMap> h_cache;
78                std::map<Color, AlphaMap> v_cache;
79                std::map<Color, AlphaMap> d_cache;
80          };
81       }
82    }
83 }
84
85 #endif