1 // $Id: sdl_renderer.cpp 5063 2007-05-27 11:32:00Z matzeb $
4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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.
26 #include <SDL_image.h>
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"
40 #include "gameconfig.hpp"
42 #include "texture.hpp"
43 #include "texture_manager.hpp"
44 #include "obstack/obstackpp.hpp"
48 SDL_Surface *apply_alpha(SDL_Surface *src, float alpha_factor)
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;
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;
73 mapped = *(Uint16 *)srcpixel;
76 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
77 mapped |= srcpixel[0] << 16;
78 mapped |= srcpixel[1] << 8;
79 mapped |= srcpixel[2] << 0;
81 mapped |= srcpixel[0] << 0;
82 mapped |= srcpixel[1] << 8;
83 mapped |= srcpixel[2] << 16;
87 mapped = *(Uint32 *)srcpixel;
91 SDL_GetRGBA(mapped, src->format, &r, &g, &b, &a);
92 mapped = SDL_MapRGBA(dst->format, r, g, b, (a * alpha) >> 8);
98 *(Uint16 *)dstpixel = mapped;
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;
106 dstpixel[0] = (mapped >> 0) & 0xff;
107 dstpixel[1] = (mapped >> 8) & 0xff;
108 dstpixel[2] = (mapped >> 16) & 0xff;
112 *(Uint32 *)dstpixel = mapped;
117 if(SDL_MUSTLOCK(dst))
119 SDL_UnlockSurface(dst);
121 if(SDL_MUSTLOCK(src))
123 SDL_UnlockSurface(src);
133 ::Renderer::instance_ = this;
135 const SDL_VideoInfo *info = SDL_GetVideoInfo();
136 log_info << "Hardware surfaces are " << (info->hw_available ? "" : "not ") << "available." << std::endl;
137 log_info << "Hardware to hardware blits are " << (info->blit_hw ? "" : "not ") << "accelerated." << std::endl;
138 log_info << "Hardware to hardware blits with colorkey are " << (info->blit_hw_CC ? "" : "not ") << "accelerated." << std::endl;
139 log_info << "Hardware to hardware blits with alpha are " << (info->blit_hw_A ? "" : "not ") << "accelerated." << std::endl;
140 log_info << "Software to hardware blits are " << (info->blit_sw ? "" : "not ") << "accelerated." << std::endl;
141 log_info << "Software to hardware blits with colorkey are " << (info->blit_sw_CC ? "" : "not ") << "accelerated." << std::endl;
142 log_info << "Software to hardware blits with alpha are " << (info->blit_sw_A ? "" : "not ") << "accelerated." << std::endl;
143 log_info << "Color fills are " << (info->blit_fill ? "" : "not ") << "accelerated." << std::endl;
145 int flags = SDL_SWSURFACE | SDL_ANYFORMAT;
146 if(config->use_fullscreen)
147 flags |= SDL_FULLSCREEN;
149 int width = 800; //FIXME: config->screenwidth;
150 int height = 600; //FIXME: config->screenheight;
152 screen = SDL_SetVideoMode(width, height, 0, flags);
154 std::stringstream msg;
155 msg << "Couldn't set video mode (" << width << "x" << height
156 << "): " << SDL_GetError();
157 throw std::runtime_error(msg.str());
163 float xfactor = (float) config->screenwidth / SCREEN_WIDTH;
164 float yfactor = (float) config->screenheight / SCREEN_HEIGHT;
165 if(xfactor < yfactor)
167 numerator = config->screenwidth;
168 denominator = SCREEN_WIDTH;
172 numerator = config->screenheight;
173 denominator = SCREEN_HEIGHT;
176 if(texture_manager == 0)
177 texture_manager = new TextureManager();
180 Renderer::~Renderer()
185 Renderer::draw_surface(const DrawingRequest& request)
187 //FIXME: support parameters request.alpha, request.angle, request.blend
188 const Surface* surface = (const Surface*) request.request_data;
189 SDL::Texture *sdltexture = dynamic_cast<SDL::Texture *>(surface->get_texture());
190 SDL::SurfaceData *surface_data = reinterpret_cast<SDL::SurfaceData *>(surface->get_surface_data());
192 DrawingEffect effect = request.drawing_effect;
193 if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
195 SDL_Surface *transform = sdltexture->get_transform(request.color, effect);
197 // get and check SDL_Surface
198 if (transform == 0) {
199 std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
203 SDL_Rect *src_rect = surface_data->get_src_rect(effect);
205 dst_rect.x = (int) request.pos.x * numerator / denominator;
206 dst_rect.y = (int) request.pos.y * numerator / denominator;
209 if(request.alpha != 1.0)
211 if(!transform->format->Amask)
213 if(transform->flags & SDL_SRCALPHA)
215 alpha = transform->format->alpha;
221 SDL_SetAlpha(transform, SDL_SRCALPHA, (Uint8) (request.alpha * alpha));
225 transform = apply_alpha(transform, request.alpha);
229 SDL_BlitSurface(transform, src_rect, screen, &dst_rect);
231 if(request.alpha != 1.0)
233 if(!transform->format->Amask)
237 SDL_SetAlpha(transform, SDL_RLEACCEL, 0);
241 SDL_SetAlpha(transform, SDL_SRCALPHA | SDL_RLEACCEL, alpha);
246 SDL_FreeSurface(transform);
252 Renderer::draw_surface_part(const DrawingRequest& request)
254 const SurfacePartRequest* surfacepartrequest
255 = (SurfacePartRequest*) request.request_data;
257 const Surface* surface = surfacepartrequest->surface;
258 SDL::Texture *sdltexture = dynamic_cast<SDL::Texture *>(surface->get_texture());
260 DrawingEffect effect = request.drawing_effect;
261 if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
263 SDL_Surface *transform = sdltexture->get_transform(Color(1.0, 1.0, 1.0), effect);
265 // get and check SDL_Surface
266 if (transform == 0) {
267 std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
272 if (effect == HORIZONTAL_FLIP)
274 ox = sdltexture->get_texture_width() - surface->get_x() - (int) surfacepartrequest->size.x;
278 ox = surface->get_x();
280 if (effect == VERTICAL_FLIP)
282 oy = sdltexture->get_texture_height() - surface->get_y() - (int) surfacepartrequest->size.y;
286 oy = surface->get_y();
290 src_rect.x = (ox + (int) surfacepartrequest->source.x) * numerator / denominator;
291 src_rect.y = (oy + (int) surfacepartrequest->source.y) * numerator / denominator;
292 src_rect.w = (int) surfacepartrequest->size.x * numerator / denominator;
293 src_rect.h = (int) surfacepartrequest->size.y * numerator / denominator;
296 dst_rect.x = (int) request.pos.x * numerator / denominator;
297 dst_rect.y = (int) request.pos.y * numerator / denominator;
300 if(request.alpha != 1.0)
302 if(!transform->format->Amask)
304 if(transform->flags & SDL_SRCALPHA)
306 alpha = transform->format->alpha;
312 SDL_SetAlpha(transform, SDL_SRCALPHA, (Uint8) (request.alpha * alpha));
316 transform = apply_alpha(transform, request.alpha);
320 SDL_BlitSurface(transform, &src_rect, screen, &dst_rect);
322 if(request.alpha != 1.0)
324 if(!transform->format->Amask)
328 SDL_SetAlpha(transform, SDL_RLEACCEL, 0);
332 SDL_SetAlpha(transform, SDL_SRCALPHA | SDL_RLEACCEL, alpha);
337 SDL_FreeSurface(transform);
343 Renderer::draw_gradient(const DrawingRequest& request)
345 const GradientRequest* gradientrequest
346 = (GradientRequest*) request.request_data;
347 const Color& top = gradientrequest->top;
348 const Color& bottom = gradientrequest->bottom;
350 for(int y = 0;y < screen->h;++y)
352 Uint8 r = (Uint8)((((float)(top.red-bottom.red)/(0-screen->h)) * y + top.red) * 255);
353 Uint8 g = (Uint8)((((float)(top.green-bottom.green)/(0-screen->h)) * y + top.green) * 255);
354 Uint8 b = (Uint8)((((float)(top.blue-bottom.blue)/(0-screen->h)) * y + top.blue) * 255);
355 Uint8 a = (Uint8)((((float)(top.alpha-bottom.alpha)/(0-screen->h)) * y + top.alpha) * 255);
356 Uint32 color = SDL_MapRGB(screen->format, r, g, b);
364 if(a == SDL_ALPHA_OPAQUE) {
365 SDL_FillRect(screen, &rect, color);
366 } else if(a != SDL_ALPHA_TRANSPARENT) {
367 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);
369 SDL_FillRect(temp, 0, color);
370 SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, a);
371 SDL_BlitSurface(temp, 0, screen, &rect);
372 SDL_FreeSurface(temp);
378 Renderer::draw_filled_rect(const DrawingRequest& request)
380 const FillRectRequest* fillrectrequest
381 = (FillRectRequest*) request.request_data;
384 rect.x = (Sint16)request.pos.x * screen->w / SCREEN_WIDTH;
385 rect.y = (Sint16)request.pos.y * screen->h / SCREEN_HEIGHT;
386 rect.w = (Uint16)fillrectrequest->size.x * screen->w / SCREEN_WIDTH;
387 rect.h = (Uint16)fillrectrequest->size.y * screen->h / SCREEN_HEIGHT;
388 Uint8 r = static_cast<Uint8>(fillrectrequest->color.red * 255);
389 Uint8 g = static_cast<Uint8>(fillrectrequest->color.green * 255);
390 Uint8 b = static_cast<Uint8>(fillrectrequest->color.blue * 255);
391 Uint8 a = static_cast<Uint8>(fillrectrequest->color.alpha * 255);
392 Uint32 color = SDL_MapRGB(screen->format, r, g, b);
393 if(a == SDL_ALPHA_OPAQUE) {
394 SDL_FillRect(screen, &rect, color);
395 } else if(a != SDL_ALPHA_TRANSPARENT) {
396 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);
398 SDL_FillRect(temp, 0, color);
399 SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, a);
400 SDL_BlitSurface(temp, 0, screen, &rect);
401 SDL_FreeSurface(temp);
406 Renderer::draw_inverse_ellipse(const DrawingRequest&)
411 Renderer::do_take_screenshot()
413 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
415 SDL_Surface *screen = SDL_GetVideoSurface();
418 static const std::string writeDir = PHYSFS_getWriteDir();
419 static const std::string dirSep = PHYSFS_getDirSeparator();
420 static const std::string baseName = "screenshot";
421 static const std::string fileExt = ".bmp";
422 std::string fullFilename;
423 for (int num = 0; num < 1000; num++) {
424 std::ostringstream oss;
426 oss << std::setw(3) << std::setfill('0') << num;
428 std::string fileName = oss.str();
429 fullFilename = writeDir + dirSep + fileName;
430 if (!PHYSFS_exists(fileName.c_str())) {
431 SDL_SaveBMP(screen, fullFilename.c_str());
432 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
436 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
446 Renderer::resize(int, int)