2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 // Updated by GiBy 2013 for SDL2 <giby_the_kid@yahoo.fr>
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.
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.
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/>.
18 #include "video/sdl/sdl_renderer.hpp"
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"
30 #include "SDL2/SDL_video.h"
32 SDLRenderer::SDLRenderer() :
36 Renderer::instance_ = this;
38 log_info << "creating SDLRenderer" << std::endl;
39 int width = g_config->window_size.width;
40 int height = g_config->window_size.height;
43 if(g_config->use_fullscreen)
45 flags |= SDL_WINDOW_FULLSCREEN;
46 width = g_config->fullscreen_size.width;
47 height = g_config->fullscreen_size.height;
50 int ret = SDL_CreateWindowAndRenderer(width, height, flags,
54 std::stringstream msg;
55 msg << "Couldn't set video mode (" << width << "x" << height
56 << "): " << SDL_GetError();
57 throw std::runtime_error(msg.str());
60 SDL_RendererInfo info;
61 if (SDL_GetRendererInfo(renderer, &info) != 0)
63 log_warning << "Couldn't get RendererInfo: " << SDL_GetError() << std::endl;
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)
76 log_info << " " << SDL_GetPixelFormatName(info.texture_formats[i]) << std::endl;
78 log_info << "Max Texture Width: " << info.max_texture_width << std::endl;
79 log_info << "Max Texture Height: " << info.max_texture_height << std::endl;
82 SDL_SetWindowTitle(window, "SuperTux");
83 if(texture_manager == 0)
84 texture_manager = new TextureManager();
87 SDLRenderer::~SDLRenderer()
89 SDL_DestroyRenderer(renderer);
90 SDL_DestroyWindow(window);
94 SDLRenderer::draw_surface(const DrawingRequest& request)
96 //FIXME: support parameters request.alpha, 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());
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();
106 if (surface->get_flipx())
108 SDL_RenderCopyEx(renderer, sdltexture->get_texture(), NULL, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
112 switch(request.drawing_effect)
115 SDL_RenderCopyEx(renderer, sdltexture->get_texture(), NULL, &dst_rect, 0, NULL, SDL_FLIP_VERTICAL);
118 case HORIZONTAL_FLIP:
119 SDL_RenderCopyEx(renderer, sdltexture->get_texture(), NULL, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
124 SDL_RenderCopy(renderer, sdltexture->get_texture(), NULL, &dst_rect);
131 SDLRenderer::draw_surface_part(const DrawingRequest& request)
133 //FIXME: support parameters request.alpha, request.angle, request.blend
134 const SurfacePartRequest* surface = (const SurfacePartRequest*) request.request_data;
135 const SurfacePartRequest* surfacepartrequest = (SurfacePartRequest*) request.request_data;
137 boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->surface->get_texture());
140 src_rect.x = surfacepartrequest->source.x;
141 src_rect.y = surfacepartrequest->source.y;
142 src_rect.w = surfacepartrequest->size.x;
143 src_rect.h = surfacepartrequest->size.y;
146 dst_rect.x = request.pos.x;
147 dst_rect.y = request.pos.y;
148 dst_rect.w = surfacepartrequest->size.x;
149 dst_rect.h = surfacepartrequest->size.y;
151 if (surface->surface->get_flipx())
153 SDL_RenderCopyEx(renderer, sdltexture->get_texture(), &src_rect, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
157 switch(request.drawing_effect)
160 SDL_RenderCopyEx(renderer, sdltexture->get_texture(), &src_rect, &dst_rect, 0, NULL, SDL_FLIP_VERTICAL);
163 case HORIZONTAL_FLIP:
164 SDL_RenderCopyEx(renderer, sdltexture->get_texture(), &src_rect, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
169 SDL_RenderCopy(renderer, sdltexture->get_texture(), &src_rect, &dst_rect);
176 SDLRenderer::draw_gradient(const DrawingRequest& request)
178 const GradientRequest* gradientrequest
179 = (GradientRequest*) request.request_data;
180 const Color& top = gradientrequest->top;
181 const Color& bottom = gradientrequest->bottom;
185 SDL_GetWindowSize(window, &w, &h);
187 // calculate the maximum number of steps needed for the gradient
188 int n = static_cast<int>(std::max(std::max(fabsf(top.red - bottom.red),
189 fabsf(top.green - bottom.green)),
190 std::max(fabsf(top.blue - bottom.blue),
191 fabsf(top.alpha - bottom.alpha))) * 255);
192 for(int i = 0; i < n; ++i)
198 rect.h = (h * (i+1) / n) - rect.y;
200 float p = static_cast<float>(i+1) / static_cast<float>(n);
201 Uint8 r = static_cast<Uint8>(((1.0f - p) * top.red + p * bottom.red) * 255);
202 Uint8 g = static_cast<Uint8>(((1.0f - p) * top.green + p * bottom.green) * 255);
203 Uint8 b = static_cast<Uint8>(((1.0f - p) * top.blue + p * bottom.blue) * 255);
204 Uint8 a = static_cast<Uint8>(((1.0f - p) * top.alpha + p * bottom.alpha) * 255);
206 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
207 SDL_SetRenderDrawColor(renderer, r, g, b, a);
208 SDL_RenderFillRect(renderer, &rect);
213 SDLRenderer::draw_filled_rect(const DrawingRequest& request)
215 const FillRectRequest* fillrectrequest
216 = (FillRectRequest*) request.request_data;
219 rect.x = request.pos.x;
220 rect.y = request.pos.y;
221 rect.w = fillrectrequest->size.x;
222 rect.h = fillrectrequest->size.y;
224 Uint8 r = static_cast<Uint8>(fillrectrequest->color.red * 255);
225 Uint8 g = static_cast<Uint8>(fillrectrequest->color.green * 255);
226 Uint8 b = static_cast<Uint8>(fillrectrequest->color.blue * 255);
227 Uint8 a = static_cast<Uint8>(fillrectrequest->color.alpha * 255);
229 int radius = std::min(std::min(rect.h / 2, rect.w / 2),
230 static_cast<int>(fillrectrequest->radius));
236 // rounded top and bottom parts
237 std::vector<SDL_Rect> rects;
238 rects.reserve(2*slices + 1);
239 for(int i = 0; i < slices; ++i)
241 float p = (static_cast<float>(i) + 0.5f) / static_cast<float>(slices);
242 int xoff = radius - static_cast<int>(sqrtf(1.0f - p*p) * radius);
245 tmp.x = rect.x + xoff;
246 tmp.y = rect.y + (radius - i);
247 tmp.w = rect.w - 2*(xoff);
249 rects.push_back(tmp);
252 tmp2.x = rect.x + xoff;
253 tmp2.y = rect.y + rect.h - radius + i;
254 tmp2.w = rect.w - 2*xoff;
259 rects.push_back(tmp2);
263 if (2*radius < rect.h)
268 tmp.y = rect.y + radius + 1;
270 tmp.h = rect.h - 2*radius - 1;
271 rects.push_back(tmp);
274 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
275 SDL_SetRenderDrawColor(renderer, r, g, b, a);
276 SDL_RenderFillRects(renderer, &*rects.begin(), rects.size());
280 if((rect.w != 0) && (rect.h != 0))
282 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
283 SDL_SetRenderDrawColor(renderer, r, g, b, a);
284 SDL_RenderFillRect(renderer, &rect);
290 SDLRenderer::draw_inverse_ellipse(const DrawingRequest& request)
292 const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
296 SDL_GetWindowSize(window, &window_w, &window_h);
298 float x = request.pos.x;
299 float w = ellipse->size.x;
300 float h = ellipse->size.y;
302 int top = request.pos.y - (h / 2);
304 const int max_slices = 256;
305 SDL_Rect rects[2*max_slices+2];
306 int slices = std::min(static_cast<int>(ellipse->size.y), max_slices);
307 for(int i = 0; i < slices; ++i)
309 float p = ((static_cast<float>(i) + 0.5f) / static_cast<float>(slices)) * 2.0f - 1.0f;
310 int xoff = static_cast<int>(sqrtf(1.0f - p*p) * w / 2);
312 SDL_Rect& left = rects[2*i+0];
313 SDL_Rect& right = rects[2*i+1];
316 left.y = top + (i * h / slices);
318 left.h = (top + ((i+1) * h / slices)) - left.y;
322 right.w = window_w - right.x;
326 SDL_Rect& top_rect = rects[2*slices+0];
327 SDL_Rect& bottom_rect = rects[2*slices+1];
331 top_rect.w = window_w;
335 bottom_rect.y = top + h;
336 bottom_rect.w = window_w;
337 bottom_rect.h = window_h - bottom_rect.y;
339 Uint8 r = static_cast<Uint8>(ellipse->color.red * 255);
340 Uint8 g = static_cast<Uint8>(ellipse->color.green * 255);
341 Uint8 b = static_cast<Uint8>(ellipse->color.blue * 255);
342 Uint8 a = static_cast<Uint8>(ellipse->color.alpha * 255);
344 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
345 SDL_SetRenderDrawColor(renderer, r, g, b, a);
346 SDL_RenderFillRects(renderer, rects, 2*slices+2);
350 SDLRenderer::do_take_screenshot()
352 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
355 if (SDL_GetRendererOutputSize(renderer, &width, &height) != 0)
357 log_warning << "SDL_GetRenderOutputSize failed: " << SDL_GetError() << std::endl;
361 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
362 Uint32 rmask = 0xff000000;
363 Uint32 gmask = 0x00ff0000;
364 Uint32 bmask = 0x0000ff00;
365 Uint32 amask = 0x000000ff;
367 Uint32 rmask = 0x000000ff;
368 Uint32 gmask = 0x0000ff00;
369 Uint32 bmask = 0x00ff0000;
370 Uint32 amask = 0xff000000;
372 SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, 32,
373 rmask, gmask, bmask, amask);
376 log_warning << "SDL_CreateRGBSurface failed: " << SDL_GetError() << std::endl;
380 int ret = SDL_RenderReadPixels(renderer, NULL,
381 SDL_PIXELFORMAT_ABGR8888,
386 log_warning << "SDL_RenderReadPixels failed: " << SDL_GetError() << std::endl;
391 static const std::string writeDir = PHYSFS_getWriteDir();
392 static const std::string dirSep = PHYSFS_getDirSeparator();
393 static const std::string baseName = "screenshot";
394 static const std::string fileExt = ".bmp";
395 std::string fullFilename;
396 for (int num = 0; num < 1000; num++) {
397 std::ostringstream oss;
399 oss << std::setw(3) << std::setfill('0') << num;
401 std::string fileName = oss.str();
402 fullFilename = writeDir + dirSep + fileName;
403 if (!PHYSFS_exists(fileName.c_str())) {
404 SDL_SaveBMP(surface, fullFilename.c_str());
405 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
409 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
418 SDL_RenderPresent(renderer);
422 SDLRenderer::resize(int, int)
428 SDLRenderer::set_gamma(float gamma)
431 SDL_CalculateGammaRamp(gamma, ramp);
432 SDL_SetWindowGammaRamp(window, ramp, ramp, ramp);