2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #include "video/sdl/sdl_renderer.hpp"
23 #include "video/drawing_request.hpp"
24 #include "video/sdl/sdl_surface_data.hpp"
25 #include "video/sdl/sdl_texture.hpp"
29 SDL_Surface *apply_alpha(SDL_Surface *src, float alpha_factor)
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;
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;
54 mapped = *(Uint16 *)srcpixel;
57 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
58 mapped |= srcpixel[0] << 16;
59 mapped |= srcpixel[1] << 8;
60 mapped |= srcpixel[2] << 0;
62 mapped |= srcpixel[0] << 0;
63 mapped |= srcpixel[1] << 8;
64 mapped |= srcpixel[2] << 16;
68 mapped = *(Uint32 *)srcpixel;
72 SDL_GetRGBA(mapped, src->format, &r, &g, &b, &a);
73 mapped = SDL_MapRGBA(dst->format, r, g, b, (a * alpha) >> 8);
79 *(Uint16 *)dstpixel = mapped;
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;
87 dstpixel[0] = (mapped >> 0) & 0xff;
88 dstpixel[1] = (mapped >> 8) & 0xff;
89 dstpixel[2] = (mapped >> 16) & 0xff;
93 *(Uint32 *)dstpixel = mapped;
100 SDL_UnlockSurface(dst);
102 if(SDL_MUSTLOCK(src))
104 SDL_UnlockSurface(src);
111 SDLRenderer::SDLRenderer() :
116 Renderer::instance_ = this;
118 const SDL_VideoInfo *info = SDL_GetVideoInfo();
119 log_info << "Hardware surfaces are " << (info->hw_available ? "" : "not ") << "available." << std::endl;
120 log_info << "Hardware to hardware blits are " << (info->blit_hw ? "" : "not ") << "accelerated." << std::endl;
121 log_info << "Hardware to hardware blits with colorkey are " << (info->blit_hw_CC ? "" : "not ") << "accelerated." << std::endl;
122 log_info << "Hardware to hardware blits with alpha are " << (info->blit_hw_A ? "" : "not ") << "accelerated." << std::endl;
123 log_info << "Software to hardware blits are " << (info->blit_sw ? "" : "not ") << "accelerated." << std::endl;
124 log_info << "Software to hardware blits with colorkey are " << (info->blit_sw_CC ? "" : "not ") << "accelerated." << std::endl;
125 log_info << "Software to hardware blits with alpha are " << (info->blit_sw_A ? "" : "not ") << "accelerated." << std::endl;
126 log_info << "Color fills are " << (info->blit_fill ? "" : "not ") << "accelerated." << std::endl;
128 int flags = SDL_SWSURFACE | SDL_ANYFORMAT;
129 if(g_config->use_fullscreen)
130 flags |= SDL_FULLSCREEN;
132 int width = 800; //FIXME: config->screenwidth;
133 int height = 600; //FIXME: config->screenheight;
135 screen = SDL_SetVideoMode(width, height, 0, flags);
137 std::stringstream msg;
138 msg << "Couldn't set video mode (" << width << "x" << height
139 << "): " << SDL_GetError();
140 throw std::runtime_error(msg.str());
146 float xfactor = (float) config->screenwidth / SCREEN_WIDTH;
147 float yfactor = (float) config->screenheight / SCREEN_HEIGHT;
148 if(xfactor < yfactor)
150 numerator = config->screenwidth;
151 denominator = SCREEN_WIDTH;
155 numerator = config->screenheight;
156 denominator = SCREEN_HEIGHT;
159 if(texture_manager == 0)
160 texture_manager = new TextureManager();
163 SDLRenderer::~SDLRenderer()
168 SDLRenderer::draw_surface(const DrawingRequest& request)
170 //FIXME: support parameters request.alpha, request.angle, request.blend
171 const Surface* surface = (const Surface*) request.request_data;
172 SDLTexture *sdltexture = dynamic_cast<SDLTexture *>(surface->get_texture());
173 SDLSurfaceData *surface_data = reinterpret_cast<SDLSurfaceData *>(surface->get_surface_data());
175 DrawingEffect effect = request.drawing_effect;
176 if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
178 SDL_Surface *transform = sdltexture->get_transform(request.color, effect);
180 // get and check SDL_Surface
181 if (transform == 0) {
182 std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
186 SDL_Rect *src_rect = surface_data->get_src_rect(effect);
188 dst_rect.x = (int) request.pos.x * numerator / denominator;
189 dst_rect.y = (int) request.pos.y * numerator / denominator;
192 if(request.alpha != 1.0)
194 if(!transform->format->Amask)
196 if(transform->flags & SDL_SRCALPHA)
198 alpha = transform->format->alpha;
204 SDL_SetAlpha(transform, SDL_SRCALPHA, (Uint8) (request.alpha * alpha));
208 transform = apply_alpha(transform, request.alpha);
212 SDL_BlitSurface(transform, src_rect, screen, &dst_rect);
214 if(request.alpha != 1.0)
216 if(!transform->format->Amask)
220 SDL_SetAlpha(transform, SDL_RLEACCEL, 0);
224 SDL_SetAlpha(transform, SDL_SRCALPHA | SDL_RLEACCEL, alpha);
229 SDL_FreeSurface(transform);
235 SDLRenderer::draw_surface_part(const DrawingRequest& request)
237 const SurfacePartRequest* surfacepartrequest
238 = (SurfacePartRequest*) request.request_data;
240 const Surface* surface = surfacepartrequest->surface;
241 SDLTexture *sdltexture = dynamic_cast<SDLTexture*>(surface->get_texture());
243 DrawingEffect effect = request.drawing_effect;
244 if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
246 SDL_Surface *transform = sdltexture->get_transform(request.color, effect);
248 // get and check SDL_Surface
249 if (transform == 0) {
250 std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
255 if (effect == HORIZONTAL_FLIP)
257 ox = sdltexture->get_texture_width() - surface->get_x() - (int) surfacepartrequest->size.x;
261 ox = surface->get_x();
263 if (effect == VERTICAL_FLIP)
265 oy = sdltexture->get_texture_height() - surface->get_y() - (int) surfacepartrequest->size.y;
269 oy = surface->get_y();
273 src_rect.x = (ox + (int) surfacepartrequest->source.x) * numerator / denominator;
274 src_rect.y = (oy + (int) surfacepartrequest->source.y) * numerator / denominator;
275 src_rect.w = (int) surfacepartrequest->size.x * numerator / denominator;
276 src_rect.h = (int) surfacepartrequest->size.y * numerator / denominator;
279 dst_rect.x = (int) request.pos.x * numerator / denominator;
280 dst_rect.y = (int) request.pos.y * numerator / denominator;
283 if(request.alpha != 1.0)
285 if(!transform->format->Amask)
287 if(transform->flags & SDL_SRCALPHA)
289 alpha = transform->format->alpha;
295 SDL_SetAlpha(transform, SDL_SRCALPHA, (Uint8) (request.alpha * alpha));
299 transform = apply_alpha(transform, request.alpha);
303 SDL_BlitSurface(transform, &src_rect, screen, &dst_rect);
305 if(request.alpha != 1.0)
307 if(!transform->format->Amask)
311 SDL_SetAlpha(transform, SDL_RLEACCEL, 0);
315 SDL_SetAlpha(transform, SDL_SRCALPHA | SDL_RLEACCEL, alpha);
320 SDL_FreeSurface(transform);
326 SDLRenderer::draw_gradient(const DrawingRequest& request)
328 const GradientRequest* gradientrequest
329 = (GradientRequest*) request.request_data;
330 const Color& top = gradientrequest->top;
331 const Color& bottom = gradientrequest->bottom;
333 for(int y = 0;y < screen->h;++y)
335 Uint8 r = (Uint8)((((float)(top.red-bottom.red)/(0-screen->h)) * y + top.red) * 255);
336 Uint8 g = (Uint8)((((float)(top.green-bottom.green)/(0-screen->h)) * y + top.green) * 255);
337 Uint8 b = (Uint8)((((float)(top.blue-bottom.blue)/(0-screen->h)) * y + top.blue) * 255);
338 Uint8 a = (Uint8)((((float)(top.alpha-bottom.alpha)/(0-screen->h)) * y + top.alpha) * 255);
339 Uint32 color = SDL_MapRGB(screen->format, r, g, b);
347 if(a == SDL_ALPHA_OPAQUE) {
348 SDL_FillRect(screen, &rect, color);
349 } else if(a != SDL_ALPHA_TRANSPARENT) {
350 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);
352 SDL_FillRect(temp, 0, color);
353 SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, a);
354 SDL_BlitSurface(temp, 0, screen, &rect);
355 SDL_FreeSurface(temp);
361 SDLRenderer::draw_filled_rect(const DrawingRequest& request)
363 const FillRectRequest* fillrectrequest
364 = (FillRectRequest*) request.request_data;
367 rect.x = (Sint16)request.pos.x * screen->w / SCREEN_WIDTH;
368 rect.y = (Sint16)request.pos.y * screen->h / SCREEN_HEIGHT;
369 rect.w = (Uint16)fillrectrequest->size.x * screen->w / SCREEN_WIDTH;
370 rect.h = (Uint16)fillrectrequest->size.y * screen->h / SCREEN_HEIGHT;
371 Uint8 r = static_cast<Uint8>(fillrectrequest->color.red * 255);
372 Uint8 g = static_cast<Uint8>(fillrectrequest->color.green * 255);
373 Uint8 b = static_cast<Uint8>(fillrectrequest->color.blue * 255);
374 Uint8 a = static_cast<Uint8>(fillrectrequest->color.alpha * 255);
375 Uint32 color = SDL_MapRGB(screen->format, r, g, b);
376 if(a == SDL_ALPHA_OPAQUE) {
377 SDL_FillRect(screen, &rect, color);
378 } else if(a != SDL_ALPHA_TRANSPARENT) {
379 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);
381 SDL_FillRect(temp, 0, color);
382 SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, a);
383 SDL_BlitSurface(temp, 0, screen, &rect);
384 SDL_FreeSurface(temp);
389 SDLRenderer::draw_inverse_ellipse(const DrawingRequest&)
394 SDLRenderer::do_take_screenshot()
396 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
398 SDL_Surface *screen = SDL_GetVideoSurface();
401 static const std::string writeDir = PHYSFS_getWriteDir();
402 static const std::string dirSep = PHYSFS_getDirSeparator();
403 static const std::string baseName = "screenshot";
404 static const std::string fileExt = ".bmp";
405 std::string fullFilename;
406 for (int num = 0; num < 1000; num++) {
407 std::ostringstream oss;
409 oss << std::setw(3) << std::setfill('0') << num;
411 std::string fileName = oss.str();
412 fullFilename = writeDir + dirSep + fileName;
413 if (!PHYSFS_exists(fileName.c_str())) {
414 SDL_SaveBMP(screen, fullFilename.c_str());
415 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
419 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
429 SDLRenderer::resize(int, int)