Update INSTALL to tell users to do CMAKE_BUILD_TYPE=DEBUG rather that DEBUG=ON
[supertux.git] / src / video / sdl_renderer.cpp
1 //  $Id: sdl_renderer.cpp 5063 2007-05-27 11:32:00Z matzeb $
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #include <config.h>
20
21 #include <functional>
22 #include <algorithm>
23 #include <stdexcept>
24 #include <cassert>
25 #include <iostream>
26 #include <SDL_image.h>
27 #include <sstream>
28 #include <iomanip>
29 #include <physfs.h>
30
31 #include "glutil.hpp"
32 #include "sdl_renderer.hpp"
33 #include "sdl_texture.hpp"
34 #include "sdl_surface_data.hpp"
35 #include "drawing_context.hpp"
36 #include "drawing_request.hpp"
37 #include "surface.hpp"
38 #include "font.hpp"
39 #include "main.hpp"
40 #include "gameconfig.hpp"
41 #include "log.hpp"
42 #include "texture.hpp"
43 #include "texture_manager.hpp"
44 #include "obstack/obstackpp.hpp"
45
46 namespace
47 {
48   SDL_Surface *apply_alpha(SDL_Surface *src, float alpha_factor)
49   {
50     // FIXME: This is really slow
51     assert(src->format->Amask);
52     int alpha = (int) (alpha_factor * 256);
53     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);
54     int bpp = dst->format->BytesPerPixel;
55     if(SDL_MUSTLOCK(src))
56     {
57       SDL_LockSurface(src);
58     }
59     if(SDL_MUSTLOCK(dst))
60     {
61       SDL_LockSurface(dst);
62     }
63     for(int y = 0;y < dst->h;y++) {
64       for(int x = 0;x < dst->w;x++) {
65         Uint8 *srcpixel = (Uint8 *) src->pixels + y * src->pitch + x * bpp;
66         Uint8 *dstpixel = (Uint8 *) dst->pixels + y * dst->pitch + x * bpp;
67         Uint32 mapped = 0;
68         switch(bpp) {
69           case 1:
70             mapped = *srcpixel;
71             break;
72           case 2:
73             mapped = *(Uint16 *)srcpixel;
74             break;
75           case 3:
76 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
77             mapped |= srcpixel[0] << 16;
78             mapped |= srcpixel[1] << 8;
79             mapped |= srcpixel[2] << 0;
80 #else
81             mapped |= srcpixel[0] << 0;
82             mapped |= srcpixel[1] << 8;
83             mapped |= srcpixel[2] << 16;
84 #endif
85             break;
86           case 4:
87             mapped = *(Uint32 *)srcpixel;
88             break;
89         }
90         Uint8 r, g, b, a;
91         SDL_GetRGBA(mapped, src->format, &r, &g, &b, &a);
92         mapped = SDL_MapRGBA(dst->format, r, g, b, (a * alpha) >> 8);
93         switch(bpp) {
94           case 1:
95             *dstpixel = mapped;
96             break;
97           case 2:
98             *(Uint16 *)dstpixel = mapped;
99             break;
100           case 3:
101 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
102             dstpixel[0] = (mapped >> 16) & 0xff;
103             dstpixel[1] = (mapped >> 8) & 0xff;
104             dstpixel[2] = (mapped >> 0) & 0xff;
105 #else
106             dstpixel[0] = (mapped >> 0) & 0xff;
107             dstpixel[1] = (mapped >> 8) & 0xff;
108             dstpixel[2] = (mapped >> 16) & 0xff;
109 #endif
110             break;
111           case 4:
112             *(Uint32 *)dstpixel = mapped;
113             break;
114         }
115       }
116     }
117     if(SDL_MUSTLOCK(dst))
118     {
119       SDL_UnlockSurface(dst);
120     }
121     if(SDL_MUSTLOCK(src))
122     {
123       SDL_UnlockSurface(src);
124     }
125     return dst;
126   }
127 }
128
129 namespace SDL
130 {
131   Renderer::Renderer()
132   {
133     const SDL_VideoInfo *info = SDL_GetVideoInfo();
134     log_info << "Hardware surfaces are " << (info->hw_available ? "" : "not ") << "available." << std::endl;
135     log_info << "Hardware to hardware blits are " << (info->blit_hw ? "" : "not ") << "accelerated." << std::endl;
136     log_info << "Hardware to hardware blits with colorkey are " << (info->blit_hw_CC ? "" : "not ") << "accelerated." << std::endl;
137     log_info << "Hardware to hardware blits with alpha are " << (info->blit_hw_A ? "" : "not ") << "accelerated." << std::endl;
138     log_info << "Software to hardware blits are " << (info->blit_sw ? "" : "not ") << "accelerated." << std::endl;
139     log_info << "Software to hardware blits with colorkey are " << (info->blit_sw_CC ? "" : "not ") << "accelerated." << std::endl;
140     log_info << "Software to hardware blits with alpha are " << (info->blit_sw_A ? "" : "not ") << "accelerated." << std::endl;
141     log_info << "Color fills are " << (info->blit_fill ? "" : "not ") << "accelerated." << std::endl;
142
143     int flags = SDL_SWSURFACE | SDL_ANYFORMAT;
144     int width;
145     int height;
146     if(config->use_fullscreen)
147     {
148       flags |= SDL_FULLSCREEN;
149       width  = config->fullscreen_width;
150       height = config->fullscreen_height;
151     }
152     else
153     {
154       flags |= SDL_RESIZABLE;
155       width  = config->window_width;
156       height = config->window_height;
157     }
158
159     screen = SDL_SetVideoMode(width, height, 0, flags);
160     if(screen == 0) {
161       std::stringstream msg;
162       msg << "Couldn't set video mode (" << width << "x" << height
163           << "): " << SDL_GetError();
164       throw std::runtime_error(msg.str());
165     }
166
167     numerator   = 1;
168     denominator = 1;
169     float xfactor = (float) width / SCREEN_WIDTH;
170     float yfactor = (float) height / SCREEN_HEIGHT;
171     if(xfactor < yfactor)
172     {
173       numerator = width;
174       denominator = SCREEN_WIDTH;
175     }
176     else
177     {
178       numerator = height;
179       denominator = SCREEN_HEIGHT;
180     }
181     if(texture_manager == 0)
182       texture_manager = new TextureManager();
183   }
184
185   Renderer::~Renderer()
186   {
187   }
188
189   void
190   Renderer::draw_surface(const DrawingRequest& request)
191   {
192     //FIXME: support parameters request.alpha, request.angle, request.blend
193     const Surface* surface = (const Surface*) request.request_data;
194     SDL::Texture *sdltexture = dynamic_cast<SDL::Texture *>(surface->get_texture());
195     SDL::SurfaceData *surface_data = reinterpret_cast<SDL::SurfaceData *>(surface->get_surface_data());
196
197     DrawingEffect effect = request.drawing_effect;
198     if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
199
200     SDL_Surface *transform = sdltexture->get_transform(request.color, effect);
201
202     // get and check SDL_Surface
203     if (transform == 0) {
204       std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
205       return;
206     }
207
208     SDL_Rect *src_rect = surface_data->get_src_rect(effect);
209     SDL_Rect dst_rect;
210     dst_rect.x = (int) request.pos.x * numerator / denominator;
211     dst_rect.y = (int) request.pos.y * numerator / denominator;
212
213     Uint8 alpha = 0;
214     if(request.alpha != 1.0)
215     {
216       if(!transform->format->Amask)
217       {
218         if(transform->flags & SDL_SRCALPHA)
219         {
220           alpha = transform->format->alpha;
221         }
222         else
223         {
224           alpha = 255;
225         }
226         SDL_SetAlpha(transform, SDL_SRCALPHA, (Uint8) (request.alpha * alpha));
227       }
228       /*else
229       {
230         transform = apply_alpha(transform, request.alpha);
231       }*/
232     }
233
234     SDL_BlitSurface(transform, src_rect, screen, &dst_rect);
235
236     if(request.alpha != 1.0)
237     {
238       if(!transform->format->Amask)
239       {
240         if(alpha == 255)
241         {
242           SDL_SetAlpha(transform, SDL_RLEACCEL, 0);
243         }
244         else
245         {
246           SDL_SetAlpha(transform, SDL_SRCALPHA | SDL_RLEACCEL, alpha);
247         }
248       }
249       /*else
250       {
251         SDL_FreeSurface(transform);
252       }*/
253     }
254   }
255
256   void
257   Renderer::draw_surface_part(const DrawingRequest& request)
258   {
259     const SurfacePartRequest* surfacepartrequest
260       = (SurfacePartRequest*) request.request_data;
261
262     const Surface* surface = surfacepartrequest->surface;
263     SDL::Texture *sdltexture = dynamic_cast<SDL::Texture *>(surface->get_texture());
264
265     DrawingEffect effect = request.drawing_effect;
266     if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
267
268     SDL_Surface *transform = sdltexture->get_transform(request.color, effect);
269
270     // get and check SDL_Surface
271     if (transform == 0) {
272       std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
273       return;
274     }
275
276     int ox, oy;
277     if (effect == HORIZONTAL_FLIP)
278     {
279       ox = sdltexture->get_texture_width() - surface->get_x() - (int) surfacepartrequest->size.x;
280     }
281     else
282     {
283       ox = surface->get_x();
284     }
285     if (effect == VERTICAL_FLIP)
286     {
287       oy = sdltexture->get_texture_height() - surface->get_y() - (int) surfacepartrequest->size.y;
288     }
289     else
290     {
291       oy = surface->get_y();
292     }
293
294     SDL_Rect src_rect;
295     src_rect.x = (ox + (int) surfacepartrequest->source.x) * numerator / denominator;
296     src_rect.y = (oy + (int) surfacepartrequest->source.y) * numerator / denominator;
297     src_rect.w = (int) surfacepartrequest->size.x * numerator / denominator;
298     src_rect.h = (int) surfacepartrequest->size.y * numerator / denominator;
299
300     SDL_Rect dst_rect;
301     dst_rect.x = (int) request.pos.x * numerator / denominator;
302     dst_rect.y = (int) request.pos.y * numerator / denominator;
303
304     Uint8 alpha = 0;
305     if(request.alpha != 1.0)
306     {
307       if(!transform->format->Amask)
308       {
309         if(transform->flags & SDL_SRCALPHA)
310         {
311           alpha = transform->format->alpha;
312         }
313         else
314         {
315           alpha = 255;
316         }
317         SDL_SetAlpha(transform, SDL_SRCALPHA, (Uint8) (request.alpha * alpha));
318       }
319       /*else
320       {
321         transform = apply_alpha(transform, request.alpha);
322       }*/
323     }
324
325     SDL_BlitSurface(transform, &src_rect, screen, &dst_rect);
326
327     if(request.alpha != 1.0)
328     {
329       if(!transform->format->Amask)
330       {
331         if(alpha == 255)
332         {
333           SDL_SetAlpha(transform, SDL_RLEACCEL, 0);
334         }
335         else
336         {
337           SDL_SetAlpha(transform, SDL_SRCALPHA | SDL_RLEACCEL, alpha);
338         }
339       }
340       /*else
341       {
342         SDL_FreeSurface(transform);
343       }*/
344     }
345   }
346
347   void
348   Renderer::draw_gradient(const DrawingRequest& request)
349   {
350     const GradientRequest* gradientrequest 
351       = (GradientRequest*) request.request_data;
352     const Color& top = gradientrequest->top;
353     const Color& bottom = gradientrequest->bottom;
354
355     for(int y = 0;y < screen->h;++y)
356     {
357       Uint8 r = (Uint8)((((float)(top.red-bottom.red)/(0-screen->h)) * y + top.red) * 255);
358       Uint8 g = (Uint8)((((float)(top.green-bottom.green)/(0-screen->h)) * y + top.green) * 255);
359       Uint8 b = (Uint8)((((float)(top.blue-bottom.blue)/(0-screen->h)) * y + top.blue) * 255);
360       Uint8 a = (Uint8)((((float)(top.alpha-bottom.alpha)/(0-screen->h)) * y + top.alpha) * 255);
361       Uint32 color = SDL_MapRGB(screen->format, r, g, b);
362
363       SDL_Rect rect;
364       rect.x = 0;
365       rect.y = y;
366       rect.w = screen->w;
367       rect.h = 1;
368
369       if(a == SDL_ALPHA_OPAQUE) {
370         SDL_FillRect(screen, &rect, color);
371       } else if(a != SDL_ALPHA_TRANSPARENT) {
372         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);
373
374         SDL_FillRect(temp, 0, color);
375         SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, a);
376         SDL_BlitSurface(temp, 0, screen, &rect);
377         SDL_FreeSurface(temp);
378       }
379     }
380   }
381
382   void
383   Renderer::draw_filled_rect(const DrawingRequest& request)
384   {
385     const FillRectRequest* fillrectrequest
386       = (FillRectRequest*) request.request_data;
387
388     SDL_Rect rect;
389     rect.x = (Sint16)request.pos.x * screen->w / SCREEN_WIDTH;
390     rect.y = (Sint16)request.pos.y * screen->h / SCREEN_HEIGHT;
391     rect.w = (Uint16)fillrectrequest->size.x * screen->w / SCREEN_WIDTH;
392     rect.h = (Uint16)fillrectrequest->size.y * screen->h / SCREEN_HEIGHT;
393     Uint8 r = static_cast<Uint8>(fillrectrequest->color.red * 255);
394     Uint8 g = static_cast<Uint8>(fillrectrequest->color.green * 255);
395     Uint8 b = static_cast<Uint8>(fillrectrequest->color.blue * 255);
396     Uint8 a = static_cast<Uint8>(fillrectrequest->color.alpha * 255);
397     Uint32 color = SDL_MapRGB(screen->format, r, g, b);
398     if(a == SDL_ALPHA_OPAQUE) {
399       SDL_FillRect(screen, &rect, color);
400     } else if(a != SDL_ALPHA_TRANSPARENT) {
401       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);
402
403       SDL_FillRect(temp, 0, color);
404       SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, a);
405       SDL_BlitSurface(temp, 0, screen, &rect);
406       SDL_FreeSurface(temp);
407     }
408   }
409
410   void
411   Renderer::draw_inverse_ellipse(const DrawingRequest&)
412   {
413   }
414
415   void 
416   Renderer::do_take_screenshot()
417   {
418     // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
419
420     SDL_Surface *screen = SDL_GetVideoSurface();
421
422     // save screenshot
423     static const std::string writeDir = PHYSFS_getWriteDir();
424     static const std::string dirSep = PHYSFS_getDirSeparator();
425     static const std::string baseName = "screenshot";
426     static const std::string fileExt = ".bmp";
427     std::string fullFilename;
428     for (int num = 0; num < 1000; num++) {
429       std::ostringstream oss;
430       oss << baseName;
431       oss << std::setw(3) << std::setfill('0') << num;
432       oss << fileExt;
433       std::string fileName = oss.str();
434       fullFilename = writeDir + dirSep + fileName;
435       if (!PHYSFS_exists(fileName.c_str())) {
436         SDL_SaveBMP(screen, fullFilename.c_str());
437         log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
438         return;
439       }
440     }
441     log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
442   }
443
444   void
445   Renderer::flip()
446   {
447     SDL_Flip(screen);
448   }
449
450   void
451   Renderer::resize(int, int)
452   {
453     
454   }
455 }