Use SCREEN_WIDTH/HEIGHT instead of SDL_RendererGetViewport(), as logical coordinates...
[supertux.git] / src / video / sdl / sdl_painter.cpp
1 //  SuperTux
2 //  Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
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 #include "video/sdl/sdl_painter.hpp"
18
19 #include "SDL.h"
20
21 #include "util/log.hpp"
22 #include "video/drawing_request.hpp"
23 #include "video/sdl/sdl_texture.hpp"
24
25 namespace {
26
27 SDL_BlendMode blend2sdl(const Blend& blend)
28 {
29   if (blend.sfactor == GL_ONE &&
30       blend.dfactor == GL_ZERO)
31   {
32     return SDL_BLENDMODE_NONE;
33   }
34   else if (blend.sfactor == GL_SRC_ALPHA &&
35            blend.dfactor == GL_ONE_MINUS_SRC_ALPHA)
36   {
37     return SDL_BLENDMODE_BLEND;
38   }
39   else if (blend.sfactor == GL_SRC_ALPHA &&
40            blend.dfactor == GL_ONE)
41   {
42     return SDL_BLENDMODE_ADD;
43   }
44   else if (blend.sfactor == GL_DST_COLOR &&
45            blend.dfactor == GL_ZERO)
46   {
47     return SDL_BLENDMODE_MOD;
48   }
49   else
50   {
51     log_warning << "unknown blend mode combinations: sfactor=" << blend.sfactor << " dfactor=" << blend.dfactor << std::endl;
52     return SDL_BLENDMODE_BLEND;
53   }
54 }
55
56 } // namespace
57
58 void
59 SDLPainter::draw_surface(SDL_Renderer* renderer, const DrawingRequest& request)
60 {
61   const Surface* surface = (const Surface*) request.request_data;
62   boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->get_texture());
63
64   SDL_Rect dst_rect;
65   dst_rect.x = request.pos.x;
66   dst_rect.y = request.pos.y;
67   dst_rect.w = sdltexture->get_image_width();
68   dst_rect.h = sdltexture->get_image_height();
69
70   Uint8 r = static_cast<Uint8>(request.color.red * 255);
71   Uint8 g = static_cast<Uint8>(request.color.green * 255);
72   Uint8 b = static_cast<Uint8>(request.color.blue * 255);
73   Uint8 a = static_cast<Uint8>(request.color.alpha * request.alpha * 255);
74   SDL_SetTextureColorMod(sdltexture->get_texture(), r, g, b);
75   SDL_SetTextureAlphaMod(sdltexture->get_texture(), a);
76   SDL_SetTextureBlendMode(sdltexture->get_texture(), blend2sdl(request.blend));
77
78   SDL_RendererFlip flip = SDL_FLIP_NONE;
79   if (surface->get_flipx() || request.drawing_effect == HORIZONTAL_FLIP)
80   {
81     flip = SDL_FLIP_HORIZONTAL;
82   }
83   else if (request.drawing_effect == VERTICAL_FLIP)
84   {
85     flip = SDL_FLIP_VERTICAL;
86   }
87
88   SDL_RenderCopyEx(renderer, sdltexture->get_texture(), NULL, &dst_rect, request.angle, NULL, flip);
89 }
90
91 void
92 SDLPainter::draw_surface_part(SDL_Renderer* renderer, const DrawingRequest& request)
93 {
94   //FIXME: support parameters request.blend
95   const SurfacePartRequest* surface = (const SurfacePartRequest*) request.request_data;
96   const SurfacePartRequest* surfacepartrequest = (SurfacePartRequest*) request.request_data;
97
98   boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->surface->get_texture());
99
100   SDL_Rect src_rect;
101   src_rect.x = surfacepartrequest->source.x;
102   src_rect.y = surfacepartrequest->source.y;
103   src_rect.w = surfacepartrequest->size.x;
104   src_rect.h = surfacepartrequest->size.y;
105
106   SDL_Rect dst_rect;
107   dst_rect.x = request.pos.x;
108   dst_rect.y = request.pos.y;
109   dst_rect.w = surfacepartrequest->size.x;
110   dst_rect.h = surfacepartrequest->size.y;
111
112   Uint8 r = static_cast<Uint8>(request.color.red * 255);
113   Uint8 g = static_cast<Uint8>(request.color.green * 255);
114   Uint8 b = static_cast<Uint8>(request.color.blue * 255);
115   Uint8 a = static_cast<Uint8>(request.color.alpha * request.alpha * 255);
116   SDL_SetTextureColorMod(sdltexture->get_texture(), r, g, b);
117   SDL_SetTextureAlphaMod(sdltexture->get_texture(), a);
118   SDL_SetTextureBlendMode(sdltexture->get_texture(), blend2sdl(request.blend));
119
120   SDL_RendererFlip flip = SDL_FLIP_NONE;
121   if (surface->surface->get_flipx() || request.drawing_effect == HORIZONTAL_FLIP)
122   {
123     flip = SDL_FLIP_HORIZONTAL;
124   }
125   else if (request.drawing_effect == VERTICAL_FLIP)
126   {
127     flip = SDL_FLIP_VERTICAL;
128   }
129
130   SDL_RenderCopyEx(renderer, sdltexture->get_texture(), &src_rect, &dst_rect, request.angle, NULL, flip);
131 }
132
133 void
134 SDLPainter::draw_gradient(SDL_Renderer* renderer, const DrawingRequest& request)
135 {
136   const GradientRequest* gradientrequest 
137     = (GradientRequest*) request.request_data;
138   const Color& top = gradientrequest->top;
139   const Color& bottom = gradientrequest->bottom;
140
141   // calculate the maximum number of steps needed for the gradient
142   int n = static_cast<int>(std::max(std::max(fabsf(top.red - bottom.red),
143                                              fabsf(top.green - bottom.green)),
144                                     std::max(fabsf(top.blue - bottom.blue),
145                                              fabsf(top.alpha - bottom.alpha))) * 255);
146   for(int i = 0; i < n; ++i)
147   {
148     SDL_Rect rect;
149     rect.x = 0;
150     rect.y = SCREEN_HEIGHT * i / n;
151     rect.w = SCREEN_WIDTH;
152     rect.h = (SCREEN_HEIGHT * (i+1) / n) - rect.y;
153
154     float p = static_cast<float>(i+1) / static_cast<float>(n);
155     Uint8 r = static_cast<Uint8>(((1.0f - p) * top.red + p * bottom.red)  * 255);
156     Uint8 g = static_cast<Uint8>(((1.0f - p) * top.green + p * bottom.green) * 255);
157     Uint8 b = static_cast<Uint8>(((1.0f - p) * top.blue + p * bottom.blue) * 255);
158     Uint8 a = static_cast<Uint8>(((1.0f - p) * top.alpha + p * bottom.alpha) * 255);
159
160     SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
161     SDL_SetRenderDrawColor(renderer, r, g, b, a);
162     SDL_RenderFillRect(renderer, &rect);
163   }
164 }
165
166 void
167 SDLPainter::draw_filled_rect(SDL_Renderer* renderer, const DrawingRequest& request)
168 {
169   const FillRectRequest* fillrectrequest
170     = (FillRectRequest*) request.request_data;
171
172   SDL_Rect rect;
173   rect.x = request.pos.x;
174   rect.y = request.pos.y;
175   rect.w = fillrectrequest->size.x;
176   rect.h = fillrectrequest->size.y;
177
178   Uint8 r = static_cast<Uint8>(fillrectrequest->color.red * 255);
179   Uint8 g = static_cast<Uint8>(fillrectrequest->color.green * 255);
180   Uint8 b = static_cast<Uint8>(fillrectrequest->color.blue * 255);
181   Uint8 a = static_cast<Uint8>(fillrectrequest->color.alpha * 255);
182
183   int radius = std::min(std::min(rect.h / 2, rect.w / 2),
184                         static_cast<int>(fillrectrequest->radius));
185
186   if (radius)
187   {
188     int slices = radius;
189
190     // rounded top and bottom parts
191     std::vector<SDL_Rect> rects;
192     rects.reserve(2*slices + 1);
193     for(int i = 0; i < slices; ++i)
194     {
195       float p = (static_cast<float>(i) + 0.5f) / static_cast<float>(slices);
196       int xoff = radius - static_cast<int>(sqrtf(1.0f - p*p) * radius);
197
198       SDL_Rect tmp;
199       tmp.x = rect.x + xoff;
200       tmp.y = rect.y + (radius - i);
201       tmp.w = rect.w - 2*(xoff);
202       tmp.h = 1;
203       rects.push_back(tmp);
204
205       SDL_Rect tmp2;
206       tmp2.x = rect.x + xoff;
207       tmp2.y = rect.y + rect.h - radius + i;
208       tmp2.w = rect.w - 2*xoff;
209       tmp2.h = 1;
210
211       if (tmp2.y != tmp.y)
212       {
213         rects.push_back(tmp2);
214       }
215     }
216
217     if (2*radius < rect.h)
218     {
219       // center rectangle
220       SDL_Rect tmp;
221       tmp.x = rect.x;
222       tmp.y = rect.y + radius + 1;
223       tmp.w = rect.w;
224       tmp.h = rect.h - 2*radius - 1;
225       rects.push_back(tmp);
226     }
227
228     SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
229     SDL_SetRenderDrawColor(renderer, r, g, b, a);
230     SDL_RenderFillRects(renderer, &*rects.begin(), rects.size());
231   }
232   else
233   {
234     if((rect.w != 0) && (rect.h != 0))
235     {
236       SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
237       SDL_SetRenderDrawColor(renderer, r, g, b, a);
238       SDL_RenderFillRect(renderer, &rect);
239     }
240   }
241 }
242
243 void
244 SDLPainter::draw_inverse_ellipse(SDL_Renderer* renderer, const DrawingRequest& request)
245 {
246   const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
247   
248   float x = request.pos.x;
249   float w = ellipse->size.x;
250   float h = ellipse->size.y;
251
252   int top = request.pos.y - (h / 2);
253
254   const int max_slices = 256;
255   SDL_Rect rects[2*max_slices+2];
256   int slices = std::min(static_cast<int>(ellipse->size.y), max_slices);
257   for(int i = 0; i < slices; ++i)
258   {
259     float p = ((static_cast<float>(i) + 0.5f) / static_cast<float>(slices)) * 2.0f - 1.0f; 
260     int xoff = static_cast<int>(sqrtf(1.0f - p*p) * w / 2);
261
262     SDL_Rect& left  = rects[2*i+0];
263     SDL_Rect& right = rects[2*i+1];
264
265     left.x = 0;
266     left.y = top + (i * h / slices);
267     left.w = x - xoff;
268     left.h = (top + ((i+1) * h / slices)) - left.y;
269
270     right.x = x + xoff;
271     right.y = left.y;
272     right.w = SCREEN_WIDTH - right.x;
273     right.h = left.h;
274   }
275
276   SDL_Rect& top_rect = rects[2*slices+0];
277   SDL_Rect& bottom_rect = rects[2*slices+1];
278
279   top_rect.x = 0;
280   top_rect.y = 0;
281   top_rect.w = SCREEN_WIDTH;
282   top_rect.h = top;
283
284   bottom_rect.x = 0;
285   bottom_rect.y = top + h;
286   bottom_rect.w = SCREEN_WIDTH;
287   bottom_rect.h = SCREEN_HEIGHT - bottom_rect.y;
288
289   Uint8 r = static_cast<Uint8>(ellipse->color.red * 255);
290   Uint8 g = static_cast<Uint8>(ellipse->color.green * 255);
291   Uint8 b = static_cast<Uint8>(ellipse->color.blue * 255);
292   Uint8 a = static_cast<Uint8>(ellipse->color.alpha * 255);
293
294   SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
295   SDL_SetRenderDrawColor(renderer, r, g, b, a);
296   SDL_RenderFillRects(renderer, rects, 2*slices+2);
297 }
298
299 /* EOF */