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 "video/drawing_request.hpp"
21 #include "video/sdl/sdl_surface_data.hpp"
22 #include "video/sdl/sdl_texture.hpp"
29 #include "SDL2/SDL_video.h"
30 //#include "SDL/SDL.h"
31 //#include "SDL/SDL_opengl.h"
36 SDL_Surface *apply_alpha(SDL_Surface *src, float alpha_factor)
38 // FIXME: This is really slow
39 assert(src->format->Amask);
40 int alpha = (int) (alpha_factor * 256);
41 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);
42 int bpp = dst->format->BytesPerPixel;
51 for(int y = 0;y < dst->h;y++) {
52 for(int x = 0;x < dst->w;x++) {
53 Uint8 *srcpixel = (Uint8 *) src->pixels + y * src->pitch + x * bpp;
54 Uint8 *dstpixel = (Uint8 *) dst->pixels + y * dst->pitch + x * bpp;
61 mapped = *(Uint16 *)srcpixel;
64 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
65 mapped |= srcpixel[0] << 16;
66 mapped |= srcpixel[1] << 8;
67 mapped |= srcpixel[2] << 0;
69 mapped |= srcpixel[0] << 0;
70 mapped |= srcpixel[1] << 8;
71 mapped |= srcpixel[2] << 16;
75 mapped = *(Uint32 *)srcpixel;
79 SDL_GetRGBA(mapped, src->format, &r, &g, &b, &a);
80 mapped = SDL_MapRGBA(dst->format, r, g, b, (a * alpha) >> 8);
86 *(Uint16 *)dstpixel = mapped;
89 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
90 dstpixel[0] = (mapped >> 16) & 0xff;
91 dstpixel[1] = (mapped >> 8) & 0xff;
92 dstpixel[2] = (mapped >> 0) & 0xff;
94 dstpixel[0] = (mapped >> 0) & 0xff;
95 dstpixel[1] = (mapped >> 8) & 0xff;
96 dstpixel[2] = (mapped >> 16) & 0xff;
100 *(Uint32 *)dstpixel = mapped;
105 if(SDL_MUSTLOCK(dst))
107 SDL_UnlockSurface(dst);
109 if(SDL_MUSTLOCK(src))
111 SDL_UnlockSurface(src);
118 SDLRenderer::SDLRenderer() :
123 Renderer::instance_ = this;
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 int width = 800; //FIXME: config->screenwidth;
140 int height = 600; //FIXME: config->screenheight;
142 SDL_Window *window; // Declare a pointer to an SDL_Window
144 SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
146 window = SDL_CreateWindow("SuperTux",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,width, height, 0, SDL_WINDOW_OPENGL );
147 SDL_GLContext glcontext = SDL_GL_CreateContext(window);
150 std::stringstream msg;
151 msg << "Couldn't set video mode (" << width << "x" << height
152 << "): " << SDL_GetError();
153 throw std::runtime_error(msg.str());
159 float xfactor = (float) config->screenwidth / SCREEN_WIDTH;
160 float yfactor = (float) config->screenheight / SCREEN_HEIGHT;
161 if(xfactor < yfactor)
163 numerator = config->screenwidth;
164 denominator = SCREEN_WIDTH;
168 numerator = config->screenheight;
169 denominator = SCREEN_HEIGHT;
172 if(texture_manager == 0)
173 texture_manager = new TextureManager();
176 SDLRenderer::~SDLRenderer()
181 SDLRenderer::draw_surface(const DrawingRequest& request)
183 //FIXME: support parameters request.alpha, request.angle, request.blend
184 const Surface* surface = (const Surface*) request.request_data;
185 boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->get_texture());
186 SDLSurfaceData *surface_data = reinterpret_cast<SDLSurfaceData *>(surface->get_surface_data());
188 DrawingEffect effect = request.drawing_effect;
189 if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
191 SDL_Surface *transform = sdltexture->get_transform(request.color, effect);
193 // get and check SDL_Surface
194 if (transform == 0) {
195 std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
199 SDL_Rect *src_rect = surface_data->get_src_rect(effect);
201 dst_rect.x = (int) request.pos.x * numerator / denominator;
202 dst_rect.y = (int) request.pos.y * numerator / denominator;
205 if(request.alpha != 1.0)
207 if(!transform->format->Amask)
209 if(transform->flags & SDL_SRCALPHA)
211 alpha = transform->format->alpha;
217 SDL_SetAlpha(transform, SDL_SRCALPHA, (Uint8) (request.alpha * alpha));
221 transform = apply_alpha(transform, request.alpha);
225 SDL_BlitSurface(transform, src_rect, screen, &dst_rect);
227 if(request.alpha != 1.0)
229 if(!transform->format->Amask)
233 SDL_SetAlpha(transform, SDL_RLEACCEL, 0);
237 SDL_SetAlpha(transform, SDL_SRCALPHA | SDL_RLEACCEL, alpha);
242 SDL_FreeSurface(transform);
248 SDLRenderer::draw_surface_part(const DrawingRequest& request)
250 const SurfacePartRequest* surfacepartrequest
251 = (SurfacePartRequest*) request.request_data;
253 const Surface* surface = surfacepartrequest->surface;
254 boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->get_texture());
256 DrawingEffect effect = request.drawing_effect;
257 if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
259 SDL_Surface *transform = sdltexture->get_transform(request.color, effect);
261 // get and check SDL_Surface
262 if (transform == 0) {
263 std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
268 if (effect == HORIZONTAL_FLIP)
270 ox = sdltexture->get_texture_width() - surface->get_x() - (int) surfacepartrequest->size.x;
274 ox = surface->get_x();
276 if (effect == VERTICAL_FLIP)
278 oy = sdltexture->get_texture_height() - surface->get_y() - (int) surfacepartrequest->size.y;
282 oy = surface->get_y();
286 src_rect.x = (ox + (int) surfacepartrequest->source.x) * numerator / denominator;
287 src_rect.y = (oy + (int) surfacepartrequest->source.y) * numerator / denominator;
288 src_rect.w = (int) surfacepartrequest->size.x * numerator / denominator;
289 src_rect.h = (int) surfacepartrequest->size.y * numerator / denominator;
292 dst_rect.x = (int) request.pos.x * numerator / denominator;
293 dst_rect.y = (int) request.pos.y * numerator / denominator;
296 if(request.alpha != 1.0)
298 if(!transform->format->Amask)
300 if(transform->flags & SDL_SRCALPHA)
302 alpha = transform->format->alpha;
308 SDL_SetAlpha(transform, SDL_SRCALPHA, (Uint8) (request.alpha * alpha));
312 transform = apply_alpha(transform, request.alpha);
316 SDL_BlitSurface(transform, &src_rect, screen, &dst_rect);
318 if(request.alpha != 1.0)
320 if(!transform->format->Amask)
324 SDL_SetAlpha(transform, SDL_RLEACCEL, 0);
328 SDL_SetAlpha(transform, SDL_SRCALPHA | SDL_RLEACCEL, alpha);
333 SDL_FreeSurface(transform);
339 SDLRenderer::draw_gradient(const DrawingRequest& request)
341 const GradientRequest* gradientrequest
342 = (GradientRequest*) request.request_data;
343 const Color& top = gradientrequest->top;
344 const Color& bottom = gradientrequest->bottom;
346 for(int y = 0;y < screen->h;++y)
348 Uint8 r = (Uint8)((((float)(top.red-bottom.red)/(0-screen->h)) * y + top.red) * 255);
349 Uint8 g = (Uint8)((((float)(top.green-bottom.green)/(0-screen->h)) * y + top.green) * 255);
350 Uint8 b = (Uint8)((((float)(top.blue-bottom.blue)/(0-screen->h)) * y + top.blue) * 255);
351 Uint8 a = (Uint8)((((float)(top.alpha-bottom.alpha)/(0-screen->h)) * y + top.alpha) * 255);
352 Uint32 color = SDL_MapRGB(screen->format, r, g, b);
360 if(a == SDL_ALPHA_OPAQUE) {
361 SDL_FillRect(screen, &rect, color);
362 } else if(a != SDL_ALPHA_TRANSPARENT) {
363 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);
365 SDL_FillRect(temp, 0, color);
366 SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, a);
367 SDL_BlitSurface(temp, 0, screen, &rect);
368 SDL_FreeSurface(temp);
374 SDLRenderer::draw_filled_rect(const DrawingRequest& request)
376 const FillRectRequest* fillrectrequest
377 = (FillRectRequest*) request.request_data;
380 rect.x = (Sint16)request.pos.x * screen->w / SCREEN_WIDTH;
381 rect.y = (Sint16)request.pos.y * screen->h / SCREEN_HEIGHT;
382 rect.w = (Uint16)fillrectrequest->size.x * screen->w / SCREEN_WIDTH;
383 rect.h = (Uint16)fillrectrequest->size.y * screen->h / SCREEN_HEIGHT;
384 if((rect.w == 0) || (rect.h == 0)) {
387 Uint8 r = static_cast<Uint8>(fillrectrequest->color.red * 255);
388 Uint8 g = static_cast<Uint8>(fillrectrequest->color.green * 255);
389 Uint8 b = static_cast<Uint8>(fillrectrequest->color.blue * 255);
390 Uint8 a = static_cast<Uint8>(fillrectrequest->color.alpha * 255);
391 Uint32 color = SDL_MapRGB(screen->format, r, g, b);
392 if(a == SDL_ALPHA_OPAQUE) {
393 SDL_FillRect(screen, &rect, color);
394 } else if(a != SDL_ALPHA_TRANSPARENT) {
395 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);
397 SDL_FillRect(temp, 0, color);
398 SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, a);
399 SDL_BlitSurface(temp, 0, screen, &rect);
400 SDL_FreeSurface(temp);
405 SDLRenderer::draw_inverse_ellipse(const DrawingRequest&)
410 SDLRenderer::do_take_screenshot()
412 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
414 SDL_Surface *screen = SDL_GetVideoSurface();
417 static const std::string writeDir = PHYSFS_getWriteDir();
418 static const std::string dirSep = PHYSFS_getDirSeparator();
419 static const std::string baseName = "screenshot";
420 static const std::string fileExt = ".bmp";
421 std::string fullFilename;
422 for (int num = 0; num < 1000; num++) {
423 std::ostringstream oss;
425 oss << std::setw(3) << std::setfill('0') << num;
427 std::string fileName = oss.str();
428 fullFilename = writeDir + dirSep + fileName;
429 if (!PHYSFS_exists(fileName.c_str())) {
430 SDL_SaveBMP(screen, fullFilename.c_str());
431 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
435 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
445 SDLRenderer::resize(int, int)