ec15d307ba595f875b7ba9231bd0102fa1916510
[supertux.git] / src / video / sdl / sdl_renderer.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //      Updated by GiBy 2013 for SDL2 <giby_the_kid@yahoo.fr>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "video/sdl/sdl_renderer.hpp"
19
20 #include "util/log.hpp"
21 #include "video/drawing_request.hpp"
22 #include "video/sdl/sdl_surface_data.hpp"
23 #include "video/sdl/sdl_texture.hpp"
24
25 #include <iomanip>
26 #include <iostream>
27 #include <physfs.h>
28 #include <sstream>
29 #include <stdexcept>
30 #include "SDL2/SDL_video.h"
31
32 SDLRenderer::SDLRenderer() :
33   window(),
34   renderer()
35 {
36   Renderer::instance_ = this;
37
38   log_info << "creating SDLRenderer" << std::endl;
39   int width  = g_config->window_size.width;
40   int height = g_config->window_size.height;
41
42   int flags = 0;
43   if(g_config->use_fullscreen)
44   {
45     flags |= SDL_WINDOW_FULLSCREEN;
46     width  = g_config->fullscreen_size.width;
47     height = g_config->fullscreen_size.height;
48   }
49
50   int ret = SDL_CreateWindowAndRenderer(width, height, flags,
51                                         &window, &renderer);
52
53   if(ret != 0) {
54     std::stringstream msg;
55     msg << "Couldn't set video mode (" << width << "x" << height
56         << "): " << SDL_GetError();
57     throw std::runtime_error(msg.str());
58   }
59
60   SDL_RendererInfo info;
61   if (SDL_GetRendererInfo(renderer, &info) != 0)
62   {
63     log_warning << "Couldn't get RendererInfo: " << SDL_GetError() << std::endl;
64   }
65   else
66   {
67     log_info << "SDL_Renderer: " << info.name << std::endl;
68     log_info << "SDL_RendererFlags: " << std::endl;
69     if (info.flags & SDL_RENDERER_SOFTWARE) log_info << "  SDL_RENDERER_SOFTWARE" << std::endl;
70     if (info.flags & SDL_RENDERER_ACCELERATED) log_info << "  SDL_RENDERER_ACCELERATED" << std::endl;
71     if (info.flags & SDL_RENDERER_PRESENTVSYNC) log_info << "  SDL_RENDERER_PRESENTVSYNC" << std::endl;
72     if (info.flags & SDL_RENDERER_TARGETTEXTURE) log_info << "  SDL_RENDERER_TARGETTEXTURE" << std::endl;
73     log_info << "Texture Formats: " << std::endl;
74     for(size_t i = 0; i < info.num_texture_formats; ++i)
75     {
76       log_info << "  " << SDL_GetPixelFormatName(info.texture_formats[i]) << std::endl;
77     }
78     log_info << "Max Texture Width: " << info.max_texture_width << std::endl;
79     log_info << "Max Texture Height: " << info.max_texture_height << std::endl;
80   }
81
82   if(texture_manager == 0)
83     texture_manager = new TextureManager();
84 }
85
86 SDLRenderer::~SDLRenderer()
87 {
88   SDL_DestroyRenderer(renderer);
89   SDL_DestroyWindow(window);
90 }
91
92 void
93 SDLRenderer::draw_surface(const DrawingRequest& request)
94 {
95   //FIXME: support parameters request.angle, request.blend
96   const Surface* surface = (const Surface*) request.request_data;
97   boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->get_texture());
98
99   SDL_Rect dst_rect;
100   dst_rect.x = request.pos.x;
101   dst_rect.y = request.pos.y;
102   dst_rect.w = sdltexture->get_image_width();
103   dst_rect.h = sdltexture->get_image_height();
104
105   Uint8 r = static_cast<Uint8>(request.color.red * 255);
106   Uint8 g = static_cast<Uint8>(request.color.green * 255);
107   Uint8 b = static_cast<Uint8>(request.color.blue * 255);
108   Uint8 a = static_cast<Uint8>(request.color.alpha * request.alpha * 255);
109   SDL_SetTextureColorMod(sdltexture->get_texture(), r, g, b);
110   SDL_SetTextureAlphaMod(sdltexture->get_texture(), a);
111
112   if (surface->get_flipx())
113   {
114     SDL_RenderCopyEx(renderer, sdltexture->get_texture(), NULL, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
115   }
116   else
117   {
118     switch(request.drawing_effect)
119     {
120       case VERTICAL_FLIP:
121         SDL_RenderCopyEx(renderer, sdltexture->get_texture(), NULL, &dst_rect, 0, NULL, SDL_FLIP_VERTICAL);
122         break;
123
124       case HORIZONTAL_FLIP:
125         SDL_RenderCopyEx(renderer, sdltexture->get_texture(), NULL, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
126         break;
127
128       default:
129       case NO_EFFECT:
130         SDL_RenderCopy(renderer, sdltexture->get_texture(), NULL, &dst_rect);
131         break;
132     }
133   }
134 }
135
136 void
137 SDLRenderer::draw_surface_part(const DrawingRequest& request)
138 {
139   //FIXME: support parameters request.angle, request.blend
140   const SurfacePartRequest* surface = (const SurfacePartRequest*) request.request_data;
141   const SurfacePartRequest* surfacepartrequest = (SurfacePartRequest*) request.request_data;
142
143   boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->surface->get_texture());
144
145   SDL_Rect src_rect;
146   src_rect.x = surfacepartrequest->source.x;
147   src_rect.y = surfacepartrequest->source.y;
148   src_rect.w = surfacepartrequest->size.x;
149   src_rect.h = surfacepartrequest->size.y;
150
151   SDL_Rect dst_rect;
152   dst_rect.x = request.pos.x;
153   dst_rect.y = request.pos.y;
154   dst_rect.w = surfacepartrequest->size.x;
155   dst_rect.h = surfacepartrequest->size.y;
156
157   Uint8 r = static_cast<Uint8>(request.color.red * 255);
158   Uint8 g = static_cast<Uint8>(request.color.green * 255);
159   Uint8 b = static_cast<Uint8>(request.color.blue * 255);
160   Uint8 a = static_cast<Uint8>(request.color.alpha * request.alpha * 255);
161   SDL_SetTextureColorMod(sdltexture->get_texture(), r, g, b);
162   SDL_SetTextureAlphaMod(sdltexture->get_texture(), a);
163
164   if (surface->surface->get_flipx())
165   {
166     SDL_RenderCopyEx(renderer, sdltexture->get_texture(), &src_rect, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
167   }
168   else
169   {
170     switch(request.drawing_effect)
171     {
172       case VERTICAL_FLIP:
173         SDL_RenderCopyEx(renderer, sdltexture->get_texture(), &src_rect, &dst_rect, 0, NULL, SDL_FLIP_VERTICAL);
174         break;
175
176       case HORIZONTAL_FLIP:
177         SDL_RenderCopyEx(renderer, sdltexture->get_texture(), &src_rect, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
178         break;
179
180       default:
181       case NO_EFFECT:
182         SDL_RenderCopy(renderer, sdltexture->get_texture(), &src_rect, &dst_rect);
183         break;
184     }
185   }
186 }
187
188 void
189 SDLRenderer::draw_gradient(const DrawingRequest& request)
190 {
191   const GradientRequest* gradientrequest 
192     = (GradientRequest*) request.request_data;
193   const Color& top = gradientrequest->top;
194   const Color& bottom = gradientrequest->bottom;
195
196   int w;
197   int h;
198   SDL_GetWindowSize(window, &w, &h);
199
200   // calculate the maximum number of steps needed for the gradient
201   int n = static_cast<int>(std::max(std::max(fabsf(top.red - bottom.red),
202                                              fabsf(top.green - bottom.green)),
203                                     std::max(fabsf(top.blue - bottom.blue),
204                                              fabsf(top.alpha - bottom.alpha))) * 255);
205   for(int i = 0; i < n; ++i)
206   {
207     SDL_Rect rect;
208     rect.x = 0;
209     rect.y = h * i / n;
210     rect.w = w;
211     rect.h = (h * (i+1) / n) - rect.y;
212
213     float p = static_cast<float>(i+1) / static_cast<float>(n);
214     Uint8 r = static_cast<Uint8>(((1.0f - p) * top.red + p * bottom.red)  * 255);
215     Uint8 g = static_cast<Uint8>(((1.0f - p) * top.green + p * bottom.green) * 255);
216     Uint8 b = static_cast<Uint8>(((1.0f - p) * top.blue + p * bottom.blue) * 255);
217     Uint8 a = static_cast<Uint8>(((1.0f - p) * top.alpha + p * bottom.alpha) * 255);
218
219     SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
220     SDL_SetRenderDrawColor(renderer, r, g, b, a);
221     SDL_RenderFillRect(renderer, &rect);
222   }
223 }
224
225 void
226 SDLRenderer::draw_filled_rect(const DrawingRequest& request)
227 {
228   const FillRectRequest* fillrectrequest
229     = (FillRectRequest*) request.request_data;
230
231   SDL_Rect rect;
232   rect.x = request.pos.x;
233   rect.y = request.pos.y;
234   rect.w = fillrectrequest->size.x;
235   rect.h = fillrectrequest->size.y;
236
237   Uint8 r = static_cast<Uint8>(fillrectrequest->color.red * 255);
238   Uint8 g = static_cast<Uint8>(fillrectrequest->color.green * 255);
239   Uint8 b = static_cast<Uint8>(fillrectrequest->color.blue * 255);
240   Uint8 a = static_cast<Uint8>(fillrectrequest->color.alpha * 255);
241
242   int radius = std::min(std::min(rect.h / 2, rect.w / 2),
243                         static_cast<int>(fillrectrequest->radius));
244
245   if (radius)
246   {
247     int slices = radius;
248
249     // rounded top and bottom parts
250     std::vector<SDL_Rect> rects;
251     rects.reserve(2*slices + 1);
252     for(int i = 0; i < slices; ++i)
253     {
254       float p = (static_cast<float>(i) + 0.5f) / static_cast<float>(slices);
255       int xoff = radius - static_cast<int>(sqrtf(1.0f - p*p) * radius);
256
257       SDL_Rect tmp;
258       tmp.x = rect.x + xoff;
259       tmp.y = rect.y + (radius - i);
260       tmp.w = rect.w - 2*(xoff);
261       tmp.h = 1;
262       rects.push_back(tmp);
263
264       SDL_Rect tmp2;
265       tmp2.x = rect.x + xoff;
266       tmp2.y = rect.y + rect.h - radius + i;
267       tmp2.w = rect.w - 2*xoff;
268       tmp2.h = 1;
269
270       if (tmp2.y != tmp.y)
271       {
272         rects.push_back(tmp2);
273       }
274     }
275
276     if (2*radius < rect.h)
277     {
278       // center rectangle
279       SDL_Rect tmp;
280       tmp.x = rect.x;
281       tmp.y = rect.y + radius + 1;
282       tmp.w = rect.w;
283       tmp.h = rect.h - 2*radius - 1;
284       rects.push_back(tmp);
285     }
286
287     SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
288     SDL_SetRenderDrawColor(renderer, r, g, b, a);
289     SDL_RenderFillRects(renderer, &*rects.begin(), rects.size());
290   }
291   else
292   {
293     if((rect.w != 0) && (rect.h != 0))
294     {
295       SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
296       SDL_SetRenderDrawColor(renderer, r, g, b, a);
297       SDL_RenderFillRect(renderer, &rect);
298     }
299   }
300 }
301
302 void
303 SDLRenderer::draw_inverse_ellipse(const DrawingRequest& request)
304 {
305   const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
306
307   int window_w;
308   int window_h;
309   SDL_GetWindowSize(window, &window_w, &window_h);
310
311   float x = request.pos.x;
312   float w = ellipse->size.x;
313   float h = ellipse->size.y;
314
315   int top = request.pos.y - (h / 2);
316
317   const int max_slices = 256;
318   SDL_Rect rects[2*max_slices+2];
319   int slices = std::min(static_cast<int>(ellipse->size.y), max_slices);
320   for(int i = 0; i < slices; ++i)
321   {
322     float p = ((static_cast<float>(i) + 0.5f) / static_cast<float>(slices)) * 2.0f - 1.0f; 
323     int xoff = static_cast<int>(sqrtf(1.0f - p*p) * w / 2);
324
325     SDL_Rect& left  = rects[2*i+0];
326     SDL_Rect& right = rects[2*i+1];
327
328     left.x = 0;
329     left.y = top + (i * h / slices);
330     left.w = x - xoff;
331     left.h = (top + ((i+1) * h / slices)) - left.y;
332
333     right.x = x + xoff;
334     right.y = left.y;
335     right.w = window_w - right.x;
336     right.h = left.h;
337   }
338
339   SDL_Rect& top_rect = rects[2*slices+0];
340   SDL_Rect& bottom_rect = rects[2*slices+1];
341
342   top_rect.x = 0;
343   top_rect.y = 0;
344   top_rect.w = window_w;
345   top_rect.h = top;
346
347   bottom_rect.x = 0;
348   bottom_rect.y = top + h;
349   bottom_rect.w = window_w;
350   bottom_rect.h = window_h - bottom_rect.y;
351
352   Uint8 r = static_cast<Uint8>(ellipse->color.red * 255);
353   Uint8 g = static_cast<Uint8>(ellipse->color.green * 255);
354   Uint8 b = static_cast<Uint8>(ellipse->color.blue * 255);
355   Uint8 a = static_cast<Uint8>(ellipse->color.alpha * 255);
356
357   SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
358   SDL_SetRenderDrawColor(renderer, r, g, b, a);
359   SDL_RenderFillRects(renderer, rects, 2*slices+2);
360 }
361
362 void 
363 SDLRenderer::do_take_screenshot()
364 {
365   // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
366   int width;
367   int height;
368   if (SDL_GetRendererOutputSize(renderer, &width, &height) != 0)
369   {
370     log_warning << "SDL_GetRenderOutputSize failed: " << SDL_GetError() << std::endl;
371   }
372   else
373   {
374 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
375     Uint32 rmask = 0xff000000;
376     Uint32 gmask = 0x00ff0000;
377     Uint32 bmask = 0x0000ff00;
378     Uint32 amask = 0x000000ff;
379 #else
380     Uint32 rmask = 0x000000ff;
381     Uint32 gmask = 0x0000ff00;
382     Uint32 bmask = 0x00ff0000;
383     Uint32 amask = 0xff000000;
384 #endif
385     SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, 32,
386                                                 rmask, gmask, bmask, amask);
387     if (!surface)
388     {
389       log_warning << "SDL_CreateRGBSurface failed: " << SDL_GetError() << std::endl;
390     }
391     else
392     {
393       int ret = SDL_RenderReadPixels(renderer, NULL,
394                                      SDL_PIXELFORMAT_ABGR8888,
395                                      surface->pixels,
396                                      surface->pitch);
397       if (ret != 0)
398       {
399         log_warning << "SDL_RenderReadPixels failed: " << SDL_GetError() << std::endl;
400       }
401       else
402       {
403         // save screenshot
404         static const std::string writeDir = PHYSFS_getWriteDir();
405         static const std::string dirSep = PHYSFS_getDirSeparator();
406         static const std::string baseName = "screenshot";
407         static const std::string fileExt = ".bmp";
408         std::string fullFilename;
409         for (int num = 0; num < 1000; num++) {
410           std::ostringstream oss;
411           oss << baseName;
412           oss << std::setw(3) << std::setfill('0') << num;
413           oss << fileExt;
414           std::string fileName = oss.str();
415           fullFilename = writeDir + dirSep + fileName;
416           if (!PHYSFS_exists(fileName.c_str())) {
417             SDL_SaveBMP(surface, fullFilename.c_str());
418             log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
419             return;
420           }
421         }
422         log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
423       }
424     }
425   }
426 }
427
428 void
429 SDLRenderer::flip()
430 {
431   SDL_RenderPresent(renderer);
432 }
433
434 void
435 SDLRenderer::resize(int, int)
436 {
437     
438 }
439
440 void
441 SDLRenderer::set_gamma(float gamma)
442 {
443   Uint16 ramp[256];
444   SDL_CalculateGammaRamp(gamma, ramp);
445   SDL_SetWindowGammaRamp(window, ramp, ramp, ramp);
446 }
447
448 /* EOF */