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;
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;
51 log_info << "creating SDLRenderer" << std::endl;
52 int width = g_config->window_size.width;
53 int height = g_config->window_size.height;
56 if(g_config->use_fullscreen)
58 flags |= SDL_WINDOW_FULLSCREEN;
59 width = g_config->fullscreen_size.width;
60 height = g_config->fullscreen_size.height;
63 int ret = SDL_CreateWindowAndRenderer(width, height, flags,
67 std::stringstream msg;
68 msg << "Couldn't set video mode (" << width << "x" << height
69 << "): " << SDL_GetError();
70 throw std::runtime_error(msg.str());
72 SDL_SetWindowTitle(window, "SuperTux");
73 if(texture_manager == 0)
74 texture_manager = new TextureManager();
77 SDLRenderer::~SDLRenderer()
79 SDL_DestroyRenderer(renderer);
80 SDL_DestroyWindow(window);
84 SDLRenderer::draw_surface(const DrawingRequest& request)
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());
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();
96 if (surface->get_flipx())
98 SDL_RenderCopyEx(renderer, sdltexture->get_texture(), NULL, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
102 switch(request.drawing_effect)
105 SDL_RenderCopyEx(renderer, sdltexture->get_texture(), NULL, &dst_rect, 0, NULL, SDL_FLIP_VERTICAL);
108 case HORIZONTAL_FLIP:
109 SDL_RenderCopyEx(renderer, sdltexture->get_texture(), NULL, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
114 SDL_RenderCopy(renderer, sdltexture->get_texture(), NULL, &dst_rect);
121 SDLRenderer::draw_surface_part(const DrawingRequest& request)
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;
127 boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->surface->get_texture());
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;
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;
141 if (surface->surface->get_flipx())
143 SDL_RenderCopyEx(renderer, sdltexture->get_texture(), &src_rect, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
147 switch(request.drawing_effect)
150 SDL_RenderCopyEx(renderer, sdltexture->get_texture(), &src_rect, &dst_rect, 0, NULL, SDL_FLIP_VERTICAL);
153 case HORIZONTAL_FLIP:
154 SDL_RenderCopyEx(renderer, sdltexture->get_texture(), &src_rect, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
159 SDL_RenderCopy(renderer, sdltexture->get_texture(), &src_rect, &dst_rect);
166 SDLRenderer::draw_gradient(const DrawingRequest& request)
168 const GradientRequest* gradientrequest
169 = (GradientRequest*) request.request_data;
170 const Color& top = gradientrequest->top;
171 const Color& bottom = gradientrequest->bottom;
175 SDL_GetWindowSize(window, &w, &h);
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)
188 rect.h = (h * (i+1) / n) - rect.y;
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);
196 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
197 SDL_SetRenderDrawColor(renderer, r, g, b, a);
198 SDL_RenderFillRect(renderer, &rect);
203 SDLRenderer::draw_filled_rect(const DrawingRequest& request)
205 const FillRectRequest* fillrectrequest
206 = (FillRectRequest*) request.request_data;
209 rect.x = request.pos.x;
210 rect.y = request.pos.y;
211 rect.w = fillrectrequest->size.x;
212 rect.h = fillrectrequest->size.y;
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);
219 int radius = std::min(std::min(rect.h / 2, rect.w / 2),
220 static_cast<int>(fillrectrequest->radius));
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)
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);
235 tmp.x = rect.x + xoff;
236 tmp.y = rect.y + (radius - i);
237 tmp.w = rect.w - 2*(xoff);
239 rects.push_back(tmp);
242 tmp2.x = rect.x + xoff;
243 tmp2.y = rect.y + rect.h - radius + i;
244 tmp2.w = rect.w - 2*xoff;
249 rects.push_back(tmp2);
253 if (2*radius < rect.h)
258 tmp.y = rect.y + radius + 1;
260 tmp.h = rect.h - 2*radius - 1;
261 rects.push_back(tmp);
264 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
265 SDL_SetRenderDrawColor(renderer, r, g, b, a);
266 SDL_RenderFillRects(renderer, &*rects.begin(), rects.size());
270 if((rect.w != 0) && (rect.h != 0))
272 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
273 SDL_SetRenderDrawColor(renderer, r, g, b, a);
274 SDL_RenderFillRect(renderer, &rect);
280 SDLRenderer::draw_inverse_ellipse(const DrawingRequest& request)
282 const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
286 SDL_GetWindowSize(window, &window_w, &window_h);
288 float x = request.pos.x;
289 float w = ellipse->size.x;
290 float h = ellipse->size.y;
292 int top = request.pos.y - (h / 2);
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)
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);
302 SDL_Rect& left = rects[2*i+0];
303 SDL_Rect& right = rects[2*i+1];
306 left.y = top + (i * h / slices);
308 left.h = (top + ((i+1) * h / slices)) - left.y;
312 right.w = window_w - right.x;
316 SDL_Rect& top_rect = rects[2*slices+0];
317 SDL_Rect& bottom_rect = rects[2*slices+1];
321 top_rect.w = window_w;
325 bottom_rect.y = top + h;
326 bottom_rect.w = window_w;
327 bottom_rect.h = window_h - bottom_rect.y;
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);
334 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
335 SDL_SetRenderDrawColor(renderer, r, g, b, a);
336 SDL_RenderFillRects(renderer, rects, 2*slices+2);
340 SDLRenderer::do_take_screenshot()
342 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
345 if (SDL_GetRendererOutputSize(renderer, &width, &height) != 0)
347 log_warning << "SDL_GetRenderOutputSize failed: " << SDL_GetError() << std::endl;
351 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
352 Uint32 rmask = 0xff000000;
353 Uint32 gmask = 0x00ff0000;
354 Uint32 bmask = 0x0000ff00;
355 Uint32 amask = 0x000000ff;
357 Uint32 rmask = 0x000000ff;
358 Uint32 gmask = 0x0000ff00;
359 Uint32 bmask = 0x00ff0000;
360 Uint32 amask = 0xff000000;
362 SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, 32,
363 rmask, gmask, bmask, amask);
366 log_warning << "SDL_CreateRGBSurface failed: " << SDL_GetError() << std::endl;
370 int ret = SDL_RenderReadPixels(renderer, NULL,
371 SDL_PIXELFORMAT_ABGR8888,
376 log_warning << "SDL_RenderReadPixels failed: " << SDL_GetError() << std::endl;
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;
389 oss << std::setw(3) << std::setfill('0') << num;
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;
399 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
408 SDL_RenderPresent(renderer);
412 SDLRenderer::resize(int, int)