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