Precalculated renderer specific surface data, better management of multiple renderers...
[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 SDL
47 {
48   Renderer::Renderer()
49   {
50     const SDL_VideoInfo *info = SDL_GetVideoInfo();
51     log_info << "Hardware surfaces are " << (info->hw_available ? "" : "not ") << "available." << std::endl;
52     log_info << "Hardware to hardware blits are " << (info->blit_hw ? "" : "not ") << "accelerated." << std::endl;
53     log_info << "Hardware to hardware blits with colorkey are " << (info->blit_hw_CC ? "" : "not ") << "accelerated." << std::endl;
54     log_info << "Hardware to hardware blits with alpha are " << (info->blit_hw_A ? "" : "not ") << "accelerated." << std::endl;
55     log_info << "Software to hardware blits are " << (info->blit_sw ? "" : "not ") << "accelerated." << std::endl;
56     log_info << "Software to hardware blits with colorkey are " << (info->blit_sw_CC ? "" : "not ") << "accelerated." << std::endl;
57     log_info << "Software to hardware blits with alpha are " << (info->blit_sw_A ? "" : "not ") << "accelerated." << std::endl;
58     log_info << "Color fills are " << (info->blit_fill ? "" : "not ") << "accelerated." << std::endl;
59
60     int flags = SDL_SWSURFACE | SDL_ANYFORMAT;
61     if(config->use_fullscreen)
62       flags |= SDL_FULLSCREEN;
63     int width = config->screenwidth;
64     int height = config->screenheight;
65
66     screen = SDL_SetVideoMode(width, height, 0, flags);
67     if(screen == 0) {
68       std::stringstream msg;
69       msg << "Couldn't set video mode (" << width << "x" << height
70           << "): " << SDL_GetError();
71       throw std::runtime_error(msg.str());
72     }
73
74     float xfactor = (float) config->screenwidth / SCREEN_WIDTH;
75     float yfactor = (float) config->screenheight / SCREEN_HEIGHT;
76     if(xfactor < yfactor)
77     {
78       numerator = config->screenwidth;
79       denominator = SCREEN_WIDTH;
80     }
81     else
82     {
83       numerator = config->screenheight;
84       denominator = SCREEN_HEIGHT;
85     }
86
87     if(texture_manager == 0)
88       texture_manager = new TextureManager();
89   }
90
91   Renderer::~Renderer()
92   {
93   }
94
95   void
96   Renderer::draw_surface(const DrawingRequest& request)
97   {
98     //FIXME: support parameters request.alpha, request.angle, request.blend
99     const Surface* surface = (const Surface*) request.request_data;
100     SDL::Texture *sdltexture = dynamic_cast<SDL::Texture *>(surface->get_texture());
101     SDL::SurfaceData *surface_data = reinterpret_cast<SDL::SurfaceData *>(surface->get_surface_data());
102
103     DrawingEffect effect = request.drawing_effect;
104     if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
105
106     SDL_Surface *transform = sdltexture->get_transform(request.color, effect);
107
108     // get and check SDL_Surface
109     if (transform == 0) {
110       std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
111       return;
112     }   
113
114     SDL_Rect *src_rect = surface_data->get_src_rect(effect);
115     SDL_Rect dst_rect;
116     dst_rect.x = (int) request.pos.x * numerator / denominator;
117     dst_rect.y = (int) request.pos.y * numerator / denominator;
118
119     SDL_BlitSurface(transform, src_rect, screen, &dst_rect);
120   }
121
122   void
123   Renderer::draw_surface_part(const DrawingRequest& request)
124   {
125     const SurfacePartRequest* surfacepartrequest
126       = (SurfacePartRequest*) request.request_data;
127
128     const Surface* surface = surfacepartrequest->surface;
129     SDL::Texture *sdltexture = dynamic_cast<SDL::Texture *>(surface->get_texture());
130
131     DrawingEffect effect = request.drawing_effect;
132     if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
133
134     SDL_Surface *transform = sdltexture->get_transform(Color(1.0, 1.0, 1.0), effect);
135
136     // get and check SDL_Surface
137     if (transform == 0) {
138       std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
139       return;
140     }   
141
142     int ox, oy;
143     if (effect == HORIZONTAL_FLIP)
144     {
145       ox = sdltexture->get_texture_width() - surface->get_x() - (int) surfacepartrequest->size.x;
146     }
147     else
148     {
149       ox = surface->get_x();
150     }
151     if (effect == VERTICAL_FLIP)
152     {
153       oy = sdltexture->get_texture_height() - surface->get_y() - (int) surfacepartrequest->size.y;
154     }
155     else
156     {
157       oy = surface->get_y();
158     }
159
160     SDL_Rect src_rect;
161     src_rect.x = (ox + (int) surfacepartrequest->source.x) * numerator / denominator;
162     src_rect.y = (oy + (int) surfacepartrequest->source.y) * numerator / denominator;
163     src_rect.w = (int) surfacepartrequest->size.x * numerator / denominator;
164     src_rect.h = (int) surfacepartrequest->size.y * numerator / denominator;
165
166     SDL_Rect dst_rect;
167     dst_rect.x = (int) request.pos.x * numerator / denominator;
168     dst_rect.y = (int) request.pos.y * numerator / denominator;
169
170     SDL_BlitSurface(transform, &src_rect, screen, &dst_rect);
171   }
172
173   void
174   Renderer::draw_gradient(const DrawingRequest& request)
175   {
176     const GradientRequest* gradientrequest 
177       = (GradientRequest*) request.request_data;
178     const Color& top = gradientrequest->top;
179     const Color& bottom = gradientrequest->bottom;
180
181     for(int y = 0;y < screen->h;++y)
182     {
183       Uint8 r = (Uint8)((((float)(top.red-bottom.red)/(0-screen->h)) * y + top.red) * 255);
184       Uint8 g = (Uint8)((((float)(top.green-bottom.green)/(0-screen->h)) * y + top.green) * 255);
185       Uint8 b = (Uint8)((((float)(top.blue-bottom.blue)/(0-screen->h)) * y + top.blue) * 255);
186       Uint8 a = (Uint8)((((float)(top.alpha-bottom.alpha)/(0-screen->h)) * y + top.alpha) * 255);
187       Uint32 color = SDL_MapRGB(screen->format, r, g, b);
188
189       SDL_Rect rect;
190       rect.x = 0;
191       rect.y = y;
192       rect.w = screen->w;
193       rect.h = 1;
194
195       if(a == SDL_ALPHA_OPAQUE) {
196         SDL_FillRect(screen, &rect, color);
197       } else if(a != SDL_ALPHA_TRANSPARENT) {
198         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);
199
200         SDL_FillRect(temp, 0, color);
201         SDL_SetAlpha(temp, SDL_SRCALPHA, a);
202         SDL_BlitSurface(temp, 0, screen, &rect);
203         SDL_FreeSurface(temp);
204       }
205     }
206   }
207
208   void
209   Renderer::draw_text(const DrawingRequest& request)
210   {
211     const TextRequest* textrequest = (TextRequest*) request.request_data;
212
213     textrequest->font->draw(this, textrequest->text, request.pos,
214         textrequest->alignment, request.drawing_effect, request.alpha);
215   }
216
217   void
218   Renderer::draw_filled_rect(const DrawingRequest& request)
219   {
220     const FillRectRequest* fillrectrequest
221       = (FillRectRequest*) request.request_data;
222
223     SDL_Rect rect;
224     rect.x = (Sint16)request.pos.x * screen->w / SCREEN_WIDTH;
225     rect.y = (Sint16)request.pos.y * screen->h / SCREEN_HEIGHT;
226     rect.w = (Uint16)fillrectrequest->size.x * screen->w / SCREEN_WIDTH;
227     rect.h = (Uint16)fillrectrequest->size.y * screen->h / SCREEN_HEIGHT;
228     Uint8 r = static_cast<Uint8>(fillrectrequest->color.red * 255);
229     Uint8 g = static_cast<Uint8>(fillrectrequest->color.green * 255);
230     Uint8 b = static_cast<Uint8>(fillrectrequest->color.blue * 255);
231     Uint8 a = static_cast<Uint8>(fillrectrequest->color.alpha * 255);
232     Uint32 color = SDL_MapRGB(screen->format, r, g, b);
233     if(a == SDL_ALPHA_OPAQUE) {
234       SDL_FillRect(screen, &rect, color);
235     } else if(a != SDL_ALPHA_TRANSPARENT) {
236       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);
237
238       SDL_FillRect(temp, 0, color);
239       SDL_SetAlpha(temp, SDL_SRCALPHA, a);
240       SDL_BlitSurface(temp, 0, screen, &rect);
241       SDL_FreeSurface(temp);
242     }
243   }
244
245   void 
246   Renderer::do_take_screenshot()
247   {
248     // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
249
250     SDL_Surface *screen = SDL_GetVideoSurface();
251
252     // save screenshot
253     static const std::string writeDir = PHYSFS_getWriteDir();
254     static const std::string dirSep = PHYSFS_getDirSeparator();
255     static const std::string baseName = "screenshot";
256     static const std::string fileExt = ".bmp";
257     std::string fullFilename;
258     for (int num = 0; num < 1000; num++) {
259       std::ostringstream oss;
260       oss << baseName;
261       oss << std::setw(3) << std::setfill('0') << num;
262       oss << fileExt;
263       std::string fileName = oss.str();
264       fullFilename = writeDir + dirSep + fileName;
265       if (!PHYSFS_exists(fileName.c_str())) {
266         SDL_SaveBMP(screen, fullFilename.c_str());
267         log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
268         return;
269       }
270     }
271     log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
272   }
273
274   void
275   Renderer::flip()
276   {
277     SDL_Flip(screen);
278   }
279 }