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