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