c71854415944dc22080ad21cabb74598de867c32
[supertux.git] / src / unison / include / unison / video / sdl / Blitters.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_BLITTERS_HPP
7 #define UNISON_VIDEO_SDL_BLITTERS_HPP
8
9 #include <unison/video/Blitters.hpp>
10 #include <unison/video/Coord.hpp>
11 #include <unison/video/Surface.hpp>
12
13 #include "SDL.h"
14
15 namespace Unison
16 {
17    namespace Video
18    {
19       namespace SDL
20       {
21          struct Blitters
22          {
23             static SDL_Surface *create_sdl_surface_from(Surface &src)
24             {
25 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
26                SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(src.get_pixels(), src.get_size().x, src.get_size().y, 32, src.get_size().x * 4, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
27 #else
28                SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(src.get_pixels().get_pixels(), src.get_size().x, src.get_size().y, 32, src.get_size().x * 4, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
29 #endif
30                return surface;
31             }
32
33             static SDL_Surface *create_sdl_surface_from(const Surface &src)
34             {
35 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
36                SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(const_cast<Color *>(src.get_pixels()), src.get_size().x, src.get_size().y, 32, src.get_size().x * 4, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
37 #else
38                SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(const_cast<Color *>(src.get_pixels()).get_pixels(), src.get_size().x, src.get_size().y, 32, src.get_size().x * 4, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
39 #endif
40                return surface;
41             }
42
43             static SDL_Surface *optimize(const Surface &src);
44
45             static void blit_upper(SDL_Surface *src, Rect src_rect, SDL_Surface *dst, Point dst_pos, void (*blit_lower)(SDL_Surface *, const Rect &, SDL_Surface *, const Point &));
46
47             static void blit_lower_none(SDL_Surface *src, const Rect &src_rect, SDL_Surface *dst, const Point &dst_pos);
48
49             static void blit_lower_mask(SDL_Surface *src, const Rect &src_rect, SDL_Surface *dst, const Point &dst_pos);
50
51             static void blit_lower_alpha(SDL_Surface *src, const Rect &src_rect, SDL_Surface *dst, const Point &dst_pos);
52
53             static void blit_lower_add(SDL_Surface *src, const Rect &src_rect, SDL_Surface *dst, const Point &dst_pos);
54
55             static void blit_lower_mod(SDL_Surface *src, const Rect &src_rect, SDL_Surface *dst, const Point &dst_pos);
56
57             static void blit_blend_none(SDL_Surface *src, const Rect &src_rect, SDL_Surface *dst, const Point &dst_pos)
58             {
59                blit_upper(src, src_rect, dst, dst_pos, blit_lower_none);
60             }
61
62             static void blit_blend_mask(SDL_Surface *src, const Rect &src_rect, SDL_Surface *dst, const Point &dst_pos)
63             {
64                blit_upper(src, src_rect, dst, dst_pos, blit_lower_mask);
65             }
66
67             static void blit_blend_alpha(SDL_Surface *src, const Rect &src_rect, SDL_Surface *dst, const Point &dst_pos)
68             {
69                blit_upper(src, src_rect, dst, dst_pos, blit_lower_alpha);
70             }
71
72             static void blit_blend_add(SDL_Surface *src, const Rect &src_rect, SDL_Surface *dst, const Point &dst_pos)
73             {
74                blit_upper(src, src_rect, dst, dst_pos, blit_lower_add);
75             }
76
77             static void blit_blend_mod(SDL_Surface *src, const Rect &src_rect, SDL_Surface *dst, const Point &dst_pos)
78             {
79                blit_upper(src, src_rect, dst, dst_pos, blit_lower_mod);
80             }
81
82             static void blit_blend(SDL_Surface *src, const Rect &src_rect, SDL_Surface *dst, const Point &dst_pos, BlendMode blend)
83             {
84                switch(blend)
85                {
86                   case BLEND_NONE:
87                      blit_blend_none(src, src_rect, dst, dst_pos);
88                      break;
89                   case BLEND_MASK:
90                      blit_blend_mask(src, src_rect, dst, dst_pos);
91                      break;
92                   case BLEND_ALPHA:
93                      blit_blend_alpha(src, src_rect, dst, dst_pos);
94                      break;
95                   case BLEND_ADD:
96                      blit_blend_add(src, src_rect, dst, dst_pos);
97                      break;
98                   case BLEND_MOD:
99                      blit_blend_mod(src, src_rect, dst, dst_pos);
100                      break;
101                }
102             }
103          };
104       }
105    }
106 }
107
108 #endif