f75137c8e3d074efa8ff7b136ff7085addab9293
[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   // Cannot currently find a way to do this with SDL2
39   //const SDL_VideoInfo *info = SDL_GetVideoInfo();
40   //log_info << "Hardware surfaces are " << (info->hw_available ? "" : "not ") << "available." << std::endl;
41   //log_info << "Hardware to hardware blits are " << (info->blit_hw ? "" : "not ") << "accelerated." << std::endl;
42   //log_info << "Hardware to hardware blits with colorkey are " << (info->blit_hw_CC ? "" : "not ") << "accelerated." << std::endl;
43   //log_info << "Hardware to hardware blits with alpha are " << (info->blit_hw_A ? "" : "not ") << "accelerated." << std::endl;
44   //log_info << "Software to hardware blits are " << (info->blit_sw ? "" : "not ") << "accelerated." << std::endl;
45   //log_info << "Software to hardware blits with colorkey are " << (info->blit_sw_CC ? "" : "not ") << "accelerated." << std::endl;
46   //log_info << "Software to hardware blits with alpha are " << (info->blit_sw_A ? "" : "not ") << "accelerated." << std::endl;
47   //log_info << "Color fills are " << (info->blit_fill ? "" : "not ") << "accelerated." << std::endl;
48
49  // int flags = SDL_SWSURFACE | SDL_ANYFORMAT;
50  // if(g_config->use_fullscreen)
51  //   flags |= SDL_FULLSCREEN;
52     
53   log_info << "creating SDLRenderer" << std::endl;
54   int width  = 800; //FIXME: config->screenwidth;
55   int height = 600; //FIXME: config->screenheight;
56   int flags = 0;
57   int ret = SDL_CreateWindowAndRenderer(width, height, flags,
58                                         &window, &renderer);
59
60   if(ret != 0) {
61     std::stringstream msg;
62     msg << "Couldn't set video mode (" << width << "x" << height
63         << "): " << SDL_GetError();
64     throw std::runtime_error(msg.str());
65   }
66   SDL_SetWindowTitle(window, "SuperTux");
67   if(texture_manager == 0)
68     texture_manager = new TextureManager();
69 }
70
71 SDLRenderer::~SDLRenderer()
72 {
73   SDL_DestroyRenderer(renderer);
74   SDL_DestroyWindow(window);
75 }
76
77 void
78 SDLRenderer::draw_surface(const DrawingRequest& request)
79 {
80   //FIXME: support parameters request.alpha, request.angle, request.blend
81   const Surface* surface = (const Surface*) request.request_data;
82   boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->get_texture());
83
84   SDL_Rect dst_rect;
85   dst_rect.x = request.pos.x;
86   dst_rect.y = request.pos.y;
87   dst_rect.w = sdltexture->get_image_width();
88   dst_rect.h = sdltexture->get_image_height();
89
90   if (surface->get_flipx())
91   {
92     SDL_RenderCopyEx(renderer, sdltexture->get_texture(), NULL, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
93   }
94   else
95   {
96     switch(request.drawing_effect)
97     {
98       case VERTICAL_FLIP:
99         SDL_RenderCopyEx(renderer, sdltexture->get_texture(), NULL, &dst_rect, 0, NULL, SDL_FLIP_VERTICAL);
100         break;
101
102       case HORIZONTAL_FLIP:
103         SDL_RenderCopyEx(renderer, sdltexture->get_texture(), NULL, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
104         break;
105
106       default:
107       case NO_EFFECT:
108         SDL_RenderCopy(renderer, sdltexture->get_texture(), NULL, &dst_rect);
109         break;
110     }
111   }
112 }
113
114 void
115 SDLRenderer::draw_surface_part(const DrawingRequest& request)
116 {
117   //FIXME: support parameters request.alpha, request.angle, request.blend
118   const SurfacePartRequest* surface = (const SurfacePartRequest*) request.request_data;
119   const SurfacePartRequest* surfacepartrequest = (SurfacePartRequest*) request.request_data;
120
121   boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->surface->get_texture());
122
123   SDL_Rect src_rect;
124   src_rect.x = surfacepartrequest->source.x;
125   src_rect.y = surfacepartrequest->source.y;
126   src_rect.w = surfacepartrequest->size.x;
127   src_rect.h = surfacepartrequest->size.y;
128
129   SDL_Rect dst_rect;
130   dst_rect.x = request.pos.x;
131   dst_rect.y = request.pos.y;
132   dst_rect.w = surfacepartrequest->size.x;
133   dst_rect.h = surfacepartrequest->size.y;
134
135   if (surface->surface->get_flipx())
136   {
137     SDL_RenderCopyEx(renderer, sdltexture->get_texture(), &src_rect, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
138   }
139   else
140   {
141     switch(request.drawing_effect)
142     {
143       case VERTICAL_FLIP:
144         SDL_RenderCopyEx(renderer, sdltexture->get_texture(), &src_rect, &dst_rect, 0, NULL, SDL_FLIP_VERTICAL);
145         break;
146
147       case HORIZONTAL_FLIP:
148         SDL_RenderCopyEx(renderer, sdltexture->get_texture(), &src_rect, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
149         break;
150
151       default:
152       case NO_EFFECT:
153         SDL_RenderCopy(renderer, sdltexture->get_texture(), &src_rect, &dst_rect);
154         break;
155     }
156   }
157 }
158
159 void
160 SDLRenderer::draw_gradient(const DrawingRequest& request)
161 {
162   const GradientRequest* gradientrequest 
163     = (GradientRequest*) request.request_data;
164   const Color& top = gradientrequest->top;
165   const Color& bottom = gradientrequest->bottom;
166
167   int w;
168   int h;
169   SDL_GetWindowSize(window, &w, &h);
170
171   // calculate the maximum number of steps needed for the gradient
172   int n = static_cast<int>(std::max(std::max(fabsf(top.red - bottom.red),
173                                              fabsf(top.green - bottom.green)),
174                                     std::max(fabsf(top.blue - bottom.blue),
175                                              fabsf(top.alpha - bottom.alpha))) * 255);
176   for(int i = 0; i < n; ++i)
177   {
178     SDL_Rect rect;
179     rect.x = 0;
180     rect.y = h * i / n;
181     rect.w = w;
182     rect.h = (h * (i+1) / n) - rect.y;
183
184     float p = static_cast<float>(i+1) / static_cast<float>(n);
185     Uint8 r = static_cast<Uint8>(((1.0f - p) * top.red + p * bottom.red)  * 255);
186     Uint8 g = static_cast<Uint8>(((1.0f - p) * top.green + p * bottom.green) * 255);
187     Uint8 b = static_cast<Uint8>(((1.0f - p) * top.blue + p * bottom.blue) * 255);
188     Uint8 a = static_cast<Uint8>(((1.0f - p) * top.alpha + p * bottom.alpha) * 255);
189
190     SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
191     SDL_SetRenderDrawColor(renderer, r, g, b, a);
192     SDL_RenderFillRect(renderer, &rect);
193   }
194 }
195
196 void
197 SDLRenderer::draw_filled_rect(const DrawingRequest& request)
198 {
199   const FillRectRequest* fillrectrequest
200     = (FillRectRequest*) request.request_data;
201
202   SDL_Rect rect;
203   rect.x = request.pos.x;
204   rect.y = request.pos.y;
205   rect.w = fillrectrequest->size.x;
206   rect.h = fillrectrequest->size.y;
207
208   Uint8 r = static_cast<Uint8>(fillrectrequest->color.red * 255);
209   Uint8 g = static_cast<Uint8>(fillrectrequest->color.green * 255);
210   Uint8 b = static_cast<Uint8>(fillrectrequest->color.blue * 255);
211   Uint8 a = static_cast<Uint8>(fillrectrequest->color.alpha * 255);
212
213   int radius = std::min(std::min(rect.h / 2, rect.w / 2),
214                         static_cast<int>(fillrectrequest->radius));
215
216   if (radius)
217   {
218     int slices = radius;
219
220     // rounded top and bottom parts
221     std::vector<SDL_Rect> rects;
222     rects.reserve(2*slices + 1);
223     for(int i = 0; i < slices; ++i)
224     {
225       float p = (static_cast<float>(i) + 0.5f) / static_cast<float>(slices);
226       int xoff = radius - static_cast<int>(sqrtf(1.0f - p*p) * radius);
227
228       SDL_Rect tmp;
229       tmp.x = rect.x + xoff;
230       tmp.y = rect.y + (radius - i);
231       tmp.w = rect.w - 2*(xoff);
232       tmp.h = 1;
233       rects.push_back(tmp);
234
235       SDL_Rect tmp2;
236       tmp2.x = rect.x + xoff;
237       tmp2.y = rect.y + rect.h - radius + i;
238       tmp2.w = rect.w - 2*xoff;
239       tmp2.h = 1;
240
241       if (tmp2.y != tmp.y)
242       {
243         rects.push_back(tmp2);
244       }
245     }
246
247     if (2*radius < rect.h)
248     {
249       // center rectangle
250       SDL_Rect tmp;
251       tmp.x = rect.x;
252       tmp.y = rect.y + radius + 1;
253       tmp.w = rect.w;
254       tmp.h = rect.h - 2*radius - 1;
255       rects.push_back(tmp);
256     }
257
258     SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
259     SDL_SetRenderDrawColor(renderer, r, g, b, a);
260     SDL_RenderFillRects(renderer, &*rects.begin(), rects.size());
261   }
262   else
263   {
264     if((rect.w != 0) && (rect.h != 0))
265     {
266       SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
267       SDL_SetRenderDrawColor(renderer, r, g, b, a);
268       SDL_RenderFillRect(renderer, &rect);
269     }
270   }
271 }
272
273 void
274 SDLRenderer::draw_inverse_ellipse(const DrawingRequest& request)
275 {
276   const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
277
278   int window_w;
279   int window_h;
280   SDL_GetWindowSize(window, &window_w, &window_h);
281
282   float x = request.pos.x;
283   float w = ellipse->size.x;
284   float h = ellipse->size.y;
285
286   int top = request.pos.y - (h / 2);
287
288   const int max_slices = 256;
289   SDL_Rect rects[2*max_slices+2];
290   int slices = std::min(static_cast<int>(ellipse->size.y), max_slices);
291   for(int i = 0; i < slices; ++i)
292   {
293     float p = ((static_cast<float>(i) + 0.5f) / static_cast<float>(slices)) * 2.0f - 1.0f; 
294     int xoff = static_cast<int>(sqrtf(1.0f - p*p) * w / 2);
295
296     SDL_Rect& left  = rects[2*i+0];
297     SDL_Rect& right = rects[2*i+1];
298
299     left.x = 0;
300     left.y = top + (i * h / slices);
301     left.w = x - xoff;
302     left.h = (top + ((i+1) * h / slices)) - left.y;
303
304     right.x = x + xoff;
305     right.y = left.y;
306     right.w = window_w - right.x;
307     right.h = left.h;
308   }
309
310   SDL_Rect& top_rect = rects[2*slices+0];
311   SDL_Rect& bottom_rect = rects[2*slices+1];
312
313   top_rect.x = 0;
314   top_rect.y = 0;
315   top_rect.w = window_w;
316   top_rect.h = top;
317
318   bottom_rect.x = 0;
319   bottom_rect.y = top + h;
320   bottom_rect.w = window_w;
321   bottom_rect.h = window_h - bottom_rect.y;
322
323   Uint8 r = static_cast<Uint8>(ellipse->color.red * 255);
324   Uint8 g = static_cast<Uint8>(ellipse->color.green * 255);
325   Uint8 b = static_cast<Uint8>(ellipse->color.blue * 255);
326   Uint8 a = static_cast<Uint8>(ellipse->color.alpha * 255);
327
328   SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
329   SDL_SetRenderDrawColor(renderer, r, g, b, a);
330   SDL_RenderFillRects(renderer, rects, 2*slices+2);
331 }
332
333 void 
334 SDLRenderer::do_take_screenshot()
335 {
336   // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
337   int width;
338   int height;
339   if (SDL_GetRendererOutputSize(renderer, &width, &height) != 0)
340   {
341     log_warning << "SDL_GetRenderOutputSize failed: " << SDL_GetError() << std::endl;
342   }
343   else
344   {
345 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
346     Uint32 rmask = 0xff000000;
347     Uint32 gmask = 0x00ff0000;
348     Uint32 bmask = 0x0000ff00;
349     Uint32 amask = 0x000000ff;
350 #else
351     Uint32 rmask = 0x000000ff;
352     Uint32 gmask = 0x0000ff00;
353     Uint32 bmask = 0x00ff0000;
354     Uint32 amask = 0xff000000;
355 #endif
356     SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, 32,
357                                                 rmask, gmask, bmask, amask);
358     if (!surface)
359     {
360       log_warning << "SDL_CreateRGBSurface failed: " << SDL_GetError() << std::endl;
361     }
362     else
363     {
364       int ret = SDL_RenderReadPixels(renderer, NULL,
365                                      SDL_PIXELFORMAT_ABGR8888,
366                                      surface->pixels,
367                                      surface->pitch);
368       if (ret != 0)
369       {
370         log_warning << "SDL_RenderReadPixels failed: " << SDL_GetError() << std::endl;
371       }
372       else
373       {
374         // save screenshot
375         static const std::string writeDir = PHYSFS_getWriteDir();
376         static const std::string dirSep = PHYSFS_getDirSeparator();
377         static const std::string baseName = "screenshot";
378         static const std::string fileExt = ".bmp";
379         std::string fullFilename;
380         for (int num = 0; num < 1000; num++) {
381           std::ostringstream oss;
382           oss << baseName;
383           oss << std::setw(3) << std::setfill('0') << num;
384           oss << fileExt;
385           std::string fileName = oss.str();
386           fullFilename = writeDir + dirSep + fileName;
387           if (!PHYSFS_exists(fileName.c_str())) {
388             SDL_SaveBMP(surface, fullFilename.c_str());
389             log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
390             return;
391           }
392         }
393         log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
394       }
395     }
396   }
397 }
398
399 void
400 SDLRenderer::flip()
401 {
402   SDL_RenderPresent(renderer);
403 }
404
405 void
406 SDLRenderer::resize(int, int)
407 {
408     
409 }
410
411 /* EOF */