SDL: Remove the alpha channel and approximate with a colorkey if the texture doesn...
[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     for(int y = 0;y < dst->h;y++) {
56       for(int x = 0;x < dst->w;x++) {
57         Uint8 *srcpixel = (Uint8 *) src->pixels + y * src->pitch + x * bpp;
58         Uint8 *dstpixel = (Uint8 *) dst->pixels + y * dst->pitch + x * bpp;
59         Uint32 mapped = 0;
60         switch(bpp) {
61           case 1:
62             mapped = *srcpixel;
63             break;
64           case 2:
65             mapped = *(Uint16 *)srcpixel;
66             break;
67           case 3:
68 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
69             mapped |= srcpixel[0] << 16;
70             mapped |= srcpixel[1] << 8;
71             mapped |= srcpixel[2] << 0;
72 #else
73             mapped |= srcpixel[0] << 0;
74             mapped |= srcpixel[1] << 8;
75             mapped |= srcpixel[2] << 16;
76 #endif
77             break;
78           case 4:
79             mapped = *(Uint32 *)srcpixel;
80             break;
81         }
82         Uint8 r, g, b, a;
83         SDL_GetRGBA(mapped, src->format, &r, &g, &b, &a);
84         mapped = SDL_MapRGBA(dst->format, r, g, b, (a * alpha) >> 8);
85         switch(bpp) {
86           case 1:
87             *dstpixel = mapped;
88             break;
89           case 2:
90             *(Uint16 *)dstpixel = mapped;
91             break;
92           case 3:
93 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
94             dstpixel[0] = (mapped >> 16) & 0xff;
95             dstpixel[1] = (mapped >> 8) & 0xff;
96             dstpixel[2] = (mapped >> 0) & 0xff;
97 #else
98             dstpixel[0] = (mapped >> 0) & 0xff;
99             dstpixel[1] = (mapped >> 8) & 0xff;
100             dstpixel[2] = (mapped >> 16) & 0xff;
101 #endif
102             break;
103           case 4:
104             *(Uint32 *)dstpixel = mapped;
105             break;
106         }
107       }
108     }
109     return dst;
110   }
111 }
112
113 namespace SDL
114 {
115   Renderer::Renderer()
116   {
117     const SDL_VideoInfo *info = SDL_GetVideoInfo();
118     log_info << "Hardware surfaces are " << (info->hw_available ? "" : "not ") << "available." << std::endl;
119     log_info << "Hardware to hardware blits are " << (info->blit_hw ? "" : "not ") << "accelerated." << std::endl;
120     log_info << "Hardware to hardware blits with colorkey are " << (info->blit_hw_CC ? "" : "not ") << "accelerated." << std::endl;
121     log_info << "Hardware to hardware blits with alpha are " << (info->blit_hw_A ? "" : "not ") << "accelerated." << std::endl;
122     log_info << "Software to hardware blits are " << (info->blit_sw ? "" : "not ") << "accelerated." << std::endl;
123     log_info << "Software to hardware blits with colorkey are " << (info->blit_sw_CC ? "" : "not ") << "accelerated." << std::endl;
124     log_info << "Software to hardware blits with alpha are " << (info->blit_sw_A ? "" : "not ") << "accelerated." << std::endl;
125     log_info << "Color fills are " << (info->blit_fill ? "" : "not ") << "accelerated." << std::endl;
126
127     int flags = SDL_SWSURFACE | SDL_ANYFORMAT;
128     if(config->use_fullscreen)
129       flags |= SDL_FULLSCREEN;
130     int width = config->screenwidth;
131     int height = config->screenheight;
132
133     screen = SDL_SetVideoMode(width, height, 0, flags);
134     if(screen == 0) {
135       std::stringstream msg;
136       msg << "Couldn't set video mode (" << width << "x" << height
137           << "): " << SDL_GetError();
138       throw std::runtime_error(msg.str());
139     }
140
141     float xfactor = (float) config->screenwidth / SCREEN_WIDTH;
142     float yfactor = (float) config->screenheight / SCREEN_HEIGHT;
143     if(xfactor < yfactor)
144     {
145       numerator = config->screenwidth;
146       denominator = SCREEN_WIDTH;
147     }
148     else
149     {
150       numerator = config->screenheight;
151       denominator = SCREEN_HEIGHT;
152     }
153
154     if(texture_manager == 0)
155       texture_manager = new TextureManager();
156   }
157
158   Renderer::~Renderer()
159   {
160   }
161
162   void
163   Renderer::draw_surface(const DrawingRequest& request)
164   {
165     //FIXME: support parameters request.alpha, request.angle, request.blend
166     const Surface* surface = (const Surface*) request.request_data;
167     SDL::Texture *sdltexture = dynamic_cast<SDL::Texture *>(surface->get_texture());
168     SDL::SurfaceData *surface_data = reinterpret_cast<SDL::SurfaceData *>(surface->get_surface_data());
169
170     DrawingEffect effect = request.drawing_effect;
171     if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
172
173     SDL_Surface *transform = sdltexture->get_transform(request.color, effect);
174
175     // get and check SDL_Surface
176     if (transform == 0) {
177       std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
178       return;
179     }   
180
181     SDL_Rect *src_rect = surface_data->get_src_rect(effect);
182     SDL_Rect dst_rect;
183     dst_rect.x = (int) request.pos.x * numerator / denominator;
184     dst_rect.y = (int) request.pos.y * numerator / denominator;
185
186     Uint8 alpha = 0;
187     if(request.alpha != 1.0)
188     {
189       if(!transform->format->Amask)
190       {
191         if(transform->flags & SDL_SRCALPHA)
192         {
193           alpha = transform->format->alpha;
194         }
195         else
196         {
197           alpha = 255;
198         }
199         SDL_SetAlpha(transform, SDL_SRCALPHA, (Uint8) (request.alpha * alpha));
200       }
201       /*else
202       {
203         transform = apply_alpha(transform, request.alpha);
204       }*/
205     }
206
207     SDL_BlitSurface(transform, src_rect, screen, &dst_rect);
208
209     if(request.alpha != 1.0)
210     {
211       if(!transform->format->Amask)
212       {
213         if(alpha == 255)
214         {
215           SDL_SetAlpha(transform, 0, 0);
216         }
217         else
218         {
219           SDL_SetAlpha(transform, SDL_SRCALPHA, alpha);
220         }
221       }
222       /*else
223       {
224         SDL_FreeSurface(transform);
225       }*/
226     }
227   }
228
229   void
230   Renderer::draw_surface_part(const DrawingRequest& request)
231   {
232     const SurfacePartRequest* surfacepartrequest
233       = (SurfacePartRequest*) request.request_data;
234
235     const Surface* surface = surfacepartrequest->surface;
236     SDL::Texture *sdltexture = dynamic_cast<SDL::Texture *>(surface->get_texture());
237
238     DrawingEffect effect = request.drawing_effect;
239     if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
240
241     SDL_Surface *transform = sdltexture->get_transform(Color(1.0, 1.0, 1.0), effect);
242
243     // get and check SDL_Surface
244     if (transform == 0) {
245       std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
246       return;
247     }   
248
249     int ox, oy;
250     if (effect == HORIZONTAL_FLIP)
251     {
252       ox = sdltexture->get_texture_width() - surface->get_x() - (int) surfacepartrequest->size.x;
253     }
254     else
255     {
256       ox = surface->get_x();
257     }
258     if (effect == VERTICAL_FLIP)
259     {
260       oy = sdltexture->get_texture_height() - surface->get_y() - (int) surfacepartrequest->size.y;
261     }
262     else
263     {
264       oy = surface->get_y();
265     }
266
267     SDL_Rect src_rect;
268     src_rect.x = (ox + (int) surfacepartrequest->source.x) * numerator / denominator;
269     src_rect.y = (oy + (int) surfacepartrequest->source.y) * numerator / denominator;
270     src_rect.w = (int) surfacepartrequest->size.x * numerator / denominator;
271     src_rect.h = (int) surfacepartrequest->size.y * numerator / denominator;
272
273     SDL_Rect dst_rect;
274     dst_rect.x = (int) request.pos.x * numerator / denominator;
275     dst_rect.y = (int) request.pos.y * numerator / denominator;
276
277     Uint8 alpha = 0;
278     if(!(transform->flags & SDL_SRCALPHA))
279     {
280       alpha = 255;
281       SDL_SetAlpha(transform, SDL_SRCALPHA, (Uint8) (request.alpha * 255));
282     }
283     else if(!transform->format->Amask)
284     {
285       alpha = transform->format->alpha;
286       SDL_SetAlpha(transform, SDL_SRCALPHA, (Uint8) (request.alpha * alpha));
287     }
288
289     SDL_BlitSurface(transform, &src_rect, screen, &dst_rect);
290
291     if(alpha == 255)
292     {
293       SDL_SetAlpha(transform, 0, 0);
294     }
295     else if(!transform->format->Amask)
296     {
297       SDL_SetAlpha(transform, SDL_SRCALPHA, alpha);
298     }
299   }
300
301   void
302   Renderer::draw_gradient(const DrawingRequest& request)
303   {
304     const GradientRequest* gradientrequest 
305       = (GradientRequest*) request.request_data;
306     const Color& top = gradientrequest->top;
307     const Color& bottom = gradientrequest->bottom;
308
309     for(int y = 0;y < screen->h;++y)
310     {
311       Uint8 r = (Uint8)((((float)(top.red-bottom.red)/(0-screen->h)) * y + top.red) * 255);
312       Uint8 g = (Uint8)((((float)(top.green-bottom.green)/(0-screen->h)) * y + top.green) * 255);
313       Uint8 b = (Uint8)((((float)(top.blue-bottom.blue)/(0-screen->h)) * y + top.blue) * 255);
314       Uint8 a = (Uint8)((((float)(top.alpha-bottom.alpha)/(0-screen->h)) * y + top.alpha) * 255);
315       Uint32 color = SDL_MapRGB(screen->format, r, g, b);
316
317       SDL_Rect rect;
318       rect.x = 0;
319       rect.y = y;
320       rect.w = screen->w;
321       rect.h = 1;
322
323       if(a == SDL_ALPHA_OPAQUE) {
324         SDL_FillRect(screen, &rect, color);
325       } else if(a != SDL_ALPHA_TRANSPARENT) {
326         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);
327
328         SDL_FillRect(temp, 0, color);
329         SDL_SetAlpha(temp, SDL_SRCALPHA, a);
330         SDL_BlitSurface(temp, 0, screen, &rect);
331         SDL_FreeSurface(temp);
332       }
333     }
334   }
335
336   void
337   Renderer::draw_text(const DrawingRequest& request)
338   {
339     const TextRequest* textrequest = (TextRequest*) request.request_data;
340
341     textrequest->font->draw(this, textrequest->text, request.pos,
342         textrequest->alignment, request.drawing_effect, request.alpha);
343   }
344
345   void
346   Renderer::draw_filled_rect(const DrawingRequest& request)
347   {
348     const FillRectRequest* fillrectrequest
349       = (FillRectRequest*) request.request_data;
350
351     SDL_Rect rect;
352     rect.x = (Sint16)request.pos.x * screen->w / SCREEN_WIDTH;
353     rect.y = (Sint16)request.pos.y * screen->h / SCREEN_HEIGHT;
354     rect.w = (Uint16)fillrectrequest->size.x * screen->w / SCREEN_WIDTH;
355     rect.h = (Uint16)fillrectrequest->size.y * screen->h / SCREEN_HEIGHT;
356     Uint8 r = static_cast<Uint8>(fillrectrequest->color.red * 255);
357     Uint8 g = static_cast<Uint8>(fillrectrequest->color.green * 255);
358     Uint8 b = static_cast<Uint8>(fillrectrequest->color.blue * 255);
359     Uint8 a = static_cast<Uint8>(fillrectrequest->color.alpha * 255);
360     Uint32 color = SDL_MapRGB(screen->format, r, g, b);
361     if(a == SDL_ALPHA_OPAQUE) {
362       SDL_FillRect(screen, &rect, color);
363     } else if(a != SDL_ALPHA_TRANSPARENT) {
364       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
366       SDL_FillRect(temp, 0, color);
367       SDL_SetAlpha(temp, SDL_SRCALPHA, a);
368       SDL_BlitSurface(temp, 0, screen, &rect);
369       SDL_FreeSurface(temp);
370     }
371   }
372
373   void 
374   Renderer::do_take_screenshot()
375   {
376     // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
377
378     SDL_Surface *screen = SDL_GetVideoSurface();
379
380     // save screenshot
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;
388       oss << baseName;
389       oss << std::setw(3) << std::setfill('0') << num;
390       oss << fileExt;
391       std::string fileName = oss.str();
392       fullFilename = writeDir + dirSep + fileName;
393       if (!PHYSFS_exists(fileName.c_str())) {
394         SDL_SaveBMP(screen, fullFilename.c_str());
395         log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
396         return;
397       }
398     }
399     log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
400   }
401
402   void
403   Renderer::flip()
404   {
405     SDL_Flip(screen);
406   }
407 }