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"
34 SDL_Surface *apply_alpha(SDL_Surface *src, float alpha_factor)
36 // FIXME: This is really slow
37 assert(src->format->Amask);
38 int alpha = (int) (alpha_factor * 256);
39 SDL_Surface *dst = SDL_CreateRGBSurface(src->flags, src->w, src->h, src->format->BitsPerPixel, src->format->Rmask, src->format->Gmask, src->format->Bmask, src->format->Amask);
40 int bpp = dst->format->BytesPerPixel;
49 for(int y = 0;y < dst->h;y++) {
50 for(int x = 0;x < dst->w;x++) {
51 Uint8 *srcpixel = (Uint8 *) src->pixels + y * src->pitch + x * bpp;
52 Uint8 *dstpixel = (Uint8 *) dst->pixels + y * dst->pitch + x * bpp;
59 mapped = *(Uint16 *)srcpixel;
62 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
63 mapped |= srcpixel[0] << 16;
64 mapped |= srcpixel[1] << 8;
65 mapped |= srcpixel[2] << 0;
67 mapped |= srcpixel[0] << 0;
68 mapped |= srcpixel[1] << 8;
69 mapped |= srcpixel[2] << 16;
73 mapped = *(Uint32 *)srcpixel;
77 SDL_GetRGBA(mapped, src->format, &r, &g, &b, &a);
78 mapped = SDL_MapRGBA(dst->format, r, g, b, (a * alpha) >> 8);
84 *(Uint16 *)dstpixel = mapped;
87 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
88 dstpixel[0] = (mapped >> 16) & 0xff;
89 dstpixel[1] = (mapped >> 8) & 0xff;
90 dstpixel[2] = (mapped >> 0) & 0xff;
92 dstpixel[0] = (mapped >> 0) & 0xff;
93 dstpixel[1] = (mapped >> 8) & 0xff;
94 dstpixel[2] = (mapped >> 16) & 0xff;
98 *(Uint32 *)dstpixel = mapped;
103 if(SDL_MUSTLOCK(dst))
105 SDL_UnlockSurface(dst);
107 if(SDL_MUSTLOCK(src))
109 SDL_UnlockSurface(src);
116 SDLRenderer::SDLRenderer() :
122 Renderer::instance_ = this;
124 // Cannot currently find a way to do this with SDL2
125 //const SDL_VideoInfo *info = SDL_GetVideoInfo();
126 //log_info << "Hardware surfaces are " << (info->hw_available ? "" : "not ") << "available." << std::endl;
127 //log_info << "Hardware to hardware blits are " << (info->blit_hw ? "" : "not ") << "accelerated." << std::endl;
128 //log_info << "Hardware to hardware blits with colorkey are " << (info->blit_hw_CC ? "" : "not ") << "accelerated." << std::endl;
129 //log_info << "Hardware to hardware blits with alpha are " << (info->blit_hw_A ? "" : "not ") << "accelerated." << std::endl;
130 //log_info << "Software to hardware blits are " << (info->blit_sw ? "" : "not ") << "accelerated." << std::endl;
131 //log_info << "Software to hardware blits with colorkey are " << (info->blit_sw_CC ? "" : "not ") << "accelerated." << std::endl;
132 //log_info << "Software to hardware blits with alpha are " << (info->blit_sw_A ? "" : "not ") << "accelerated." << std::endl;
133 //log_info << "Color fills are " << (info->blit_fill ? "" : "not ") << "accelerated." << std::endl;
135 // int flags = SDL_SWSURFACE | SDL_ANYFORMAT;
136 // if(g_config->use_fullscreen)
137 // flags |= SDL_FULLSCREEN;
139 log_info << "creating SDLRenderer" << std::endl;
140 int width = 800; //FIXME: config->screenwidth;
141 int height = 600; //FIXME: config->screenheight;
143 int ret = SDL_CreateWindowAndRenderer(width, height, flags,
147 std::stringstream msg;
148 msg << "Couldn't set video mode (" << width << "x" << height
149 << "): " << SDL_GetError();
150 throw std::runtime_error(msg.str());
152 SDL_SetWindowTitle(window, "SuperTux");
153 if(texture_manager == 0)
154 texture_manager = new TextureManager();
160 float xfactor = (float) config->screenwidth / SCREEN_WIDTH;
161 float yfactor = (float) config->screenheight / SCREEN_HEIGHT;
162 if(xfactor < yfactor)
164 numerator = config->screenwidth;
165 denominator = SCREEN_WIDTH;
169 numerator = config->screenheight;
170 denominator = SCREEN_HEIGHT;
176 SDLRenderer::~SDLRenderer()
178 SDL_DestroyRenderer(renderer);
179 SDL_DestroyWindow(window);
183 SDLRenderer::draw_surface(const DrawingRequest& request)
186 //FIXME: support parameters request.alpha, request.angle, request.blend
187 const Surface* surface = (const Surface*) request.request_data;
188 boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->get_texture());
189 SDLSurfaceData *surface_data = reinterpret_cast<SDLSurfaceData *>(surface->get_surface_data());
191 DrawingEffect effect = request.drawing_effect;
192 if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
194 SDL_Surface *transform = sdltexture->get_transform(request.color, effect);
196 // get and check SDL_Surface
197 if (transform == 0) {
198 std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
202 SDL_Rect *src_rect = surface_data->get_src_rect(effect);
204 dst_rect.x = (int) request.pos.x * numerator / denominator;
205 dst_rect.y = (int) request.pos.y * numerator / denominator;
208 if(request.alpha != 1.0)
210 if(!transform->format->Amask)
212 if(transform->flags & SDL_SRCALPHA)
214 alpha = transform->format->alpha;
220 SDL_SetSurfaceAlphaMod(transform, (Uint8) (request.alpha * alpha));
224 transform = apply_alpha(transform, request.alpha);
228 SDL_BlitSurface(transform, src_rect, screen, &dst_rect);
230 if(request.alpha != 1.0)
232 if(!transform->format->Amask)
236 SDL_SetSurfaceAlphaMod(transform, 0);
240 SDL_SetSurfaceAlphaMod(transform, alpha);
245 SDL_FreeSurface(transform);
252 SDLRenderer::draw_surface_part(const DrawingRequest& request)
255 const SurfacePartRequest* surfacepartrequest
256 = (SurfacePartRequest*) request.request_data;
258 const Surface* surface = surfacepartrequest->surface;
259 boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->get_texture());
261 DrawingEffect effect = request.drawing_effect;
262 if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
264 SDL_Surface *transform = sdltexture->get_transform(request.color, effect);
266 // get and check SDL_Surface
267 if (transform == 0) {
268 std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
273 if (effect == HORIZONTAL_FLIP)
275 ox = sdltexture->get_texture_width() - surface->get_x() - (int) surfacepartrequest->size.x;
279 ox = surface->get_x();
281 if (effect == VERTICAL_FLIP)
283 oy = sdltexture->get_texture_height() - surface->get_y() - (int) surfacepartrequest->size.y;
287 oy = surface->get_y();
291 src_rect.x = (ox + (int) surfacepartrequest->source.x) * numerator / denominator;
292 src_rect.y = (oy + (int) surfacepartrequest->source.y) * numerator / denominator;
293 src_rect.w = (int) surfacepartrequest->size.x * numerator / denominator;
294 src_rect.h = (int) surfacepartrequest->size.y * numerator / denominator;
297 dst_rect.x = (int) request.pos.x * numerator / denominator;
298 dst_rect.y = (int) request.pos.y * numerator / denominator;
301 if(request.alpha != 1.0)
303 if(!transform->format->Amask)
305 if(transform->flags & SDL_SRCALPHA)
307 alpha = transform->format->alpha;
313 SDL_SetSurfaceAlphaMod(transform, (Uint8) (request.alpha * alpha));
317 transform = apply_alpha(transform, request.alpha);
323 SDL_BlitSurface(transform, &src_rect, screen, &dst_rect);
327 if(request.alpha != 1.0)
329 if(!transform->format->Amask)
333 SDL_SetSurfaceAlphaMod(transform, 0);
337 SDL_SetSurfaceAlphaMod(transform, alpha);
342 SDL_FreeSurface(transform);
349 SDLRenderer::draw_gradient(const DrawingRequest& request)
352 const GradientRequest* gradientrequest
353 = (GradientRequest*) request.request_data;
354 const Color& top = gradientrequest->top;
355 const Color& bottom = gradientrequest->bottom;
357 for(int y = 0;y < screen->h;++y)
359 Uint8 r = (Uint8)((((float)(top.red-bottom.red)/(0-screen->h)) * y + top.red) * 255);
360 Uint8 g = (Uint8)((((float)(top.green-bottom.green)/(0-screen->h)) * y + top.green) * 255);
361 Uint8 b = (Uint8)((((float)(top.blue-bottom.blue)/(0-screen->h)) * y + top.blue) * 255);
362 Uint8 a = (Uint8)((((float)(top.alpha-bottom.alpha)/(0-screen->h)) * y + top.alpha) * 255);
363 Uint32 color = SDL_MapRGB(screen->format, r, g, b);
371 if(a == SDL_ALPHA_OPAQUE) {
372 SDL_FillRect(screen, &rect, color);
373 } else if(a != SDL_ALPHA_TRANSPARENT) {
374 SDL_Surface *temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
376 SDL_FillRect(temp, 0, color);
377 SDL_SetSurfaceAlphaMod(temp, a);
378 SDL_BlitSurface(temp, 0, screen, &rect);
379 SDL_FreeSurface(temp);
386 SDLRenderer::draw_filled_rect(const DrawingRequest& request)
389 const FillRectRequest* fillrectrequest
390 = (FillRectRequest*) request.request_data;
393 rect.x = (Sint16)request.pos.x * screen->w / SCREEN_WIDTH;
394 rect.y = (Sint16)request.pos.y * screen->h / SCREEN_HEIGHT;
395 rect.w = (Uint16)fillrectrequest->size.x * screen->w / SCREEN_WIDTH;
396 rect.h = (Uint16)fillrectrequest->size.y * screen->h / SCREEN_HEIGHT;
397 if((rect.w == 0) || (rect.h == 0)) {
400 Uint8 r = static_cast<Uint8>(fillrectrequest->color.red * 255);
401 Uint8 g = static_cast<Uint8>(fillrectrequest->color.green * 255);
402 Uint8 b = static_cast<Uint8>(fillrectrequest->color.blue * 255);
403 Uint8 a = static_cast<Uint8>(fillrectrequest->color.alpha * 255);
404 Uint32 color = SDL_MapRGB(screen->format, r, g, b);
405 if(a == SDL_ALPHA_OPAQUE) {
406 SDL_FillRect(screen, &rect, color);
407 } else if(a != SDL_ALPHA_TRANSPARENT) {
408 SDL_Surface *temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
410 SDL_FillRect(temp, 0, color);
411 SDL_SetSurfaceAlphaMod(temp, a);
412 SDL_BlitSurface(temp, 0, screen, &rect);
413 SDL_FreeSurface(temp);
419 SDLRenderer::draw_inverse_ellipse(const DrawingRequest&)
424 SDLRenderer::do_take_screenshot()
426 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
428 SDL_Surface *screen = SDL_GetWindowSurface(window);
431 static const std::string writeDir = PHYSFS_getWriteDir();
432 static const std::string dirSep = PHYSFS_getDirSeparator();
433 static const std::string baseName = "screenshot";
434 static const std::string fileExt = ".bmp";
435 std::string fullFilename;
436 for (int num = 0; num < 1000; num++) {
437 std::ostringstream oss;
439 oss << std::setw(3) << std::setfill('0') << num;
441 std::string fileName = oss.str();
442 fullFilename = writeDir + dirSep + fileName;
443 if (!PHYSFS_exists(fileName.c_str())) {
444 SDL_SaveBMP(screen, fullFilename.c_str());
445 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
449 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
455 SDL_RenderPresent(renderer);
459 SDLRenderer::resize(int, int)