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