Initial integration, lots of broken stuff
[supertux.git] / src / unison / src / video / sdl / Texture.cpp
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 #include "Texture.hpp"
7 #include <unison/video/Surface.hpp>
8 #include <unison/video/Window.hpp>
9 #include <unison/video/Renderers.hpp>
10 #include <unison/video/sdl/Blitters.hpp>
11 #include <unison/video/backend/Renderer.hpp>
12
13 #include <assert.h>
14
15 namespace Unison
16 {
17    namespace Video
18    {
19       namespace SDL
20       {
21          Texture::Texture(const Surface &surface) :
22             Backend::Texture(surface),
23             scaled(),
24             n_cache(),
25             h_cache(),
26             v_cache(),
27             d_cache()
28          {
29          }
30          /*Texture::Texture(const Surface &surface, const std::string &name) :
31             Backend::Texture(surface, name),
32             scaled(),
33             n_cache(),
34             h_cache(),
35             v_cache(),
36             d_cache()
37          {
38          }
39
40          Texture::Texture(Backend::Texture *texture) :
41             Backend::Texture(texture),
42             scaled(),
43             n_cache(),
44             h_cache(),
45             v_cache(),
46             d_cache()
47          {
48          }*/
49
50          Texture::~Texture()
51          {
52          }
53
54          const Surface Texture::get_surface()
55          {
56             return surface;
57          }
58
59          void Texture::save()
60          {
61             scaled = Surface();
62             n_cache.clear();
63             h_cache.clear();
64             v_cache.clear();
65             d_cache.clear();
66          }
67
68          void Texture::blit(const Surface &src, const Point &dst_pos, const Rect &src_rect, const RenderOptions &options)
69          {
70             save();
71             Renderers::get().get_renderer().blit(src, src_rect, surface, dst_pos, options);
72          }
73
74          void Texture::blit(const Video::Texture &src, const Point &dst_pos, const Rect &src_rect, const RenderOptions &options)
75          {
76             save();
77             Texture *texture = dynamic_cast<Texture *>(Backend::Texture::get_texture(src.get_id()));
78             Renderers::get().get_renderer().blit(texture, src_rect, surface, dst_pos, options);
79          }
80
81          void Texture::fill(const Color &color, const Rect &rect)
82          {
83             save();
84             Renderers::get().get_renderer().fill(surface, color, rect);
85          }
86
87          void Texture::fill_blend(const Color &color, const Rect &rect)
88          {
89             save();
90             Renderers::get().get_renderer().fill_blend(surface, color, rect);
91          }
92
93          SDL_Surface *Texture::get_transform(const RenderOptions &options, unsigned int numerator, unsigned int denominator)
94          {
95             if(scaled.get_size() == Area())
96             {
97                scaled = surface.scale(numerator, denominator);
98             }
99             assert(scaled.get_size() == surface.get_size() * numerator / denominator);
100             std::map<Color, AlphaMap> &cache = options.h_flip ? (options.v_flip ? d_cache : h_cache) : (options.v_flip ? v_cache : n_cache);
101             if(!cache[options.color][options.alpha])
102             {
103                Surface transformed = scaled.modulate(options.color).modulate(options.alpha);
104                if(options.h_flip)
105                {
106                   transformed = transformed.h_flip();
107                }
108                if(options.v_flip)
109                {
110                   transformed = transformed.v_flip();
111                }
112                cache[options.color][options.alpha] = Blitters::optimize(transformed);
113             }
114             return cache[options.color][options.alpha];
115          }
116       }
117    }
118 }