1 // $Id: sdl_lightmap.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.
25 #include <SDL_image.h>
31 #include "sdl_lightmap.hpp"
32 #include "sdl_texture.hpp"
33 #include "sdl_surface_data.hpp"
34 #include "drawing_context.hpp"
35 #include "drawing_request.hpp"
36 #include "renderer.hpp"
37 #include "surface.hpp"
40 #include "gameconfig.hpp"
41 #include "texture.hpp"
42 #include "texture_manager.hpp"
43 #include "obstack/obstackpp.hpp"
49 screen = SDL_GetVideoSurface();
51 float xfactor = (float) config->screenwidth / SCREEN_WIDTH;
52 float yfactor = (float) config->screenheight / SCREEN_HEIGHT;
55 numerator = config->screenwidth;
56 denominator = SCREEN_WIDTH;
60 numerator = config->screenheight;
61 denominator = SCREEN_HEIGHT;
64 LIGHTMAP_DIV = 8 * numerator / denominator;
66 width = screen->w / LIGHTMAP_DIV;
67 height = screen->h / LIGHTMAP_DIV;
69 red_channel = (Uint8 *)malloc(width * height * sizeof(Uint8));
70 green_channel = (Uint8 *)malloc(width * height * sizeof(Uint8));
71 blue_channel = (Uint8 *)malloc(width * height * sizeof(Uint8));
82 Lightmap::start_draw(const Color &ambient_color)
84 memset(red_channel, (Uint8) (ambient_color.red * 255), width * height * sizeof(Uint8));
85 memset(green_channel, (Uint8) (ambient_color.green * 255), width * height * sizeof(Uint8));
86 memset(blue_channel, (Uint8) (ambient_color.blue * 255), width * height * sizeof(Uint8));
99 void merge(Uint8 color[3], Uint8 color0[3], Uint8 color1[3], int rem, int total)
101 color[0] = (color0[0] * (total - rem) + color1[0] * rem) / total;
102 color[1] = (color0[1] * (total - rem) + color1[1] * rem) / total;
103 color[2] = (color0[2] * (total - rem) + color1[2] * rem) / total;
111 // FIXME: This is really slow
112 if(LIGHTMAP_DIV == 1)
114 int bpp = screen->format->BytesPerPixel;
115 if(SDL_MUSTLOCK(screen))
117 SDL_LockSurface(screen);
119 Uint8 *pixel = (Uint8 *) screen->pixels;
121 for(int y = 0;y < height;y++) {
122 for(int x = 0;x < width;x++, pixel += bpp, loc++) {
123 if(red_channel[loc] == 0xff && green_channel[loc] == 0xff && blue_channel[loc] == 0xff)
133 mapped = *(Uint16 *)pixel;
136 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
137 mapped |= pixel[0] << 16;
138 mapped |= pixel[1] << 8;
139 mapped |= pixel[2] << 0;
141 mapped |= pixel[0] << 0;
142 mapped |= pixel[1] << 8;
143 mapped |= pixel[2] << 16;
147 mapped = *(Uint32 *)pixel;
150 Uint8 red, green, blue, alpha;
151 SDL_GetRGBA(mapped, screen->format, &red, &green, &blue, &alpha);
152 red = (red * red_channel[loc]) >> 8;
153 green = (green * green_channel[loc]) >> 8;
154 blue = (blue * blue_channel[loc]) >> 8;
155 mapped = SDL_MapRGBA(screen->format, red, green, blue, alpha);
161 *(Uint16 *)pixel = mapped;
164 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
165 pixel[0] = (mapped >> 16) & 0xff;
166 pixel[1] = (mapped >> 8) & 0xff;
167 pixel[2] = (mapped >> 0) & 0xff;
169 pixel[0] = (mapped >> 0) & 0xff;
170 pixel[1] = (mapped >> 8) & 0xff;
171 pixel[2] = (mapped >> 16) & 0xff;
175 *(Uint32 *)pixel = mapped;
179 pixel += screen->pitch - width * bpp;
181 if(SDL_MUSTLOCK(screen))
183 SDL_UnlockSurface(screen);
188 int bpp = screen->format->BytesPerPixel;
189 if(SDL_MUSTLOCK(screen))
191 SDL_LockSurface(screen);
193 Uint8 *div_pixel = (Uint8 *) screen->pixels;
195 for(int y = 0;y < height;y++) {
196 for(int x = 0;x < width;x++, div_pixel += bpp * LIGHTMAP_DIV, loc++) {
197 if(red_channel[loc] == 0xff && green_channel[loc] == 0xff && blue_channel[loc] == 0xff)
201 Uint8 *pixel = div_pixel;
202 for(int div_y = 0;div_y < LIGHTMAP_DIV;div_y++) {
203 for(int div_x = 0;div_x < LIGHTMAP_DIV;pixel += bpp, div_x++) {
210 mapped = *(Uint16 *)pixel;
213 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
214 mapped |= pixel[0] << 16;
215 mapped |= pixel[1] << 8;
216 mapped |= pixel[2] << 0;
218 mapped |= pixel[0] << 0;
219 mapped |= pixel[1] << 8;
220 mapped |= pixel[2] << 16;
224 mapped = *(Uint32 *)pixel;
227 Uint8 red, green, blue, alpha;
228 SDL_GetRGBA(mapped, screen->format, &red, &green, &blue, &alpha);
231 int xinc = (x + 1 != width ? 1 : 0);
232 int yinc = (y + 1 != height ? width : 0);
233 Uint8 color00[3], color01[3], color10[3], color11[3];
235 color00[0] = red_channel[loc];
236 color00[1] = green_channel[loc];
237 color00[2] = blue_channel[loc];
240 color01[0] = red_channel[loc + xinc];
241 color01[1] = green_channel[loc + xinc];
242 color01[2] = blue_channel[loc + xinc];
245 color10[0] = red_channel[loc + yinc];
246 color10[1] = green_channel[loc + yinc];
247 color10[2] = blue_channel[loc + yinc];
250 color11[0] = red_channel[loc + yinc + xinc];
251 color11[1] = green_channel[loc + yinc + xinc];
252 color11[2] = blue_channel[loc + yinc + xinc];
254 Uint8 color0[3], color1[3], color[3];
255 merge(color0, color00, color01, div_x, LIGHTMAP_DIV);
256 merge(color1, color10, color11, div_x, LIGHTMAP_DIV);
257 merge(color, color0, color1, div_y, LIGHTMAP_DIV);
258 red = (red * color[0]) >> 8;
259 green = (green * color[1]) >> 8;
260 blue = (blue * color[2]) >> 8;
262 red = (red * red_channel[loc]) >> 8;
263 green = (green * green_channel[loc]) >> 8;
264 blue = (blue * blue_channel[loc]) >> 8;
267 mapped = SDL_MapRGBA(screen->format, red, green, blue, alpha);
273 *(Uint16 *)pixel = mapped;
276 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
277 pixel[0] = (mapped >> 16) & 0xff;
278 pixel[1] = (mapped >> 8) & 0xff;
279 pixel[2] = (mapped >> 0) & 0xff;
281 pixel[0] = (mapped >> 0) & 0xff;
282 pixel[1] = (mapped >> 8) & 0xff;
283 pixel[2] = (mapped >> 16) & 0xff;
287 *(Uint32 *)pixel = mapped;
291 pixel += screen->pitch - LIGHTMAP_DIV * bpp;
294 div_pixel += (screen->pitch - width * bpp) * LIGHTMAP_DIV;
296 if(SDL_MUSTLOCK(screen))
298 SDL_UnlockSurface(screen);
303 void Lightmap::light_blit(SDL_Surface *src, SDL_Rect *src_rect, int dstx, int dsty)
305 dstx /= LIGHTMAP_DIV;
306 dsty /= LIGHTMAP_DIV;
307 int srcx = src_rect->x / LIGHTMAP_DIV;
308 int srcy = src_rect->y / LIGHTMAP_DIV;
309 int blit_width = src_rect->w / LIGHTMAP_DIV;
310 int blit_height = src_rect->h / LIGHTMAP_DIV;
311 int bpp = src->format->BytesPerPixel;
312 if(SDL_MUSTLOCK(src))
314 SDL_LockSurface(src);
316 Uint8 *pixel = (Uint8 *) src->pixels + srcy * src->pitch + srcx * bpp;
317 int loc = dsty * width + dstx;
318 for(int y = 0;y < blit_height;y++) {
319 for(int x = 0;x < blit_width;x++, pixel += bpp * LIGHTMAP_DIV, loc++) {
320 if(x + dstx < 0 || y + dsty < 0 || x + dstx >= width || y + dsty >= height)
324 if(red_channel[loc] == 0xff && green_channel[loc] == 0xff && blue_channel[loc] == 0xff)
335 mapped = *(Uint16 *)pixel;
338 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
339 mapped |= pixel[0] << 16;
340 mapped |= pixel[1] << 8;
341 mapped |= pixel[2] << 0;
343 mapped |= pixel[0] << 0;
344 mapped |= pixel[1] << 8;
345 mapped |= pixel[2] << 16;
349 mapped = *(Uint32 *)pixel;
352 Uint8 red, green, blue, alpha;
353 SDL_GetRGBA(mapped, src->format, &red, &green, &blue, &alpha);
357 int redsum = red_channel[loc] + (red * alpha >> 8);
358 red_channel[loc] = redsum & ~0xff ? 0xff : redsum;
362 int greensum = green_channel[loc] + (green * alpha >> 8);
363 green_channel[loc] = greensum & ~0xff ? 0xff : greensum;
367 int bluesum = blue_channel[loc] + (blue * alpha >> 8);
368 blue_channel[loc] = bluesum & ~0xff ? 0xff : bluesum;
371 pixel += (src->pitch - blit_width * bpp) * LIGHTMAP_DIV;
372 loc += width - blit_width;
374 if(SDL_MUSTLOCK(src))
376 SDL_UnlockSurface(src);
380 /*void Lightmap::light_blit(SDL_Surface *src, SDL_Rect *src_rect, int dstx, int dsty)
382 int bpp = src->format->BytesPerPixel;
383 if(SDL_MUSTLOCK(src))
385 SDL_LockSurface(src);
387 Uint8 *pixel = (Uint8 *) src->pixels + src_rect->y * src->pitch + src_rect->x * bpp;
388 int loc = dsty * width + dstx;
389 for(int y = 0;y < src_rect->h;y++) {
390 for(int x = 0;x < src_rect->w;x++, pixel += bpp, loc++) {
391 if(x + dstx < 0 || y + dsty < 0 || x + dstx >= width || y + dsty >= height)
395 if(red_channel[loc] == 0xff && green_channel[loc] == 0xff && blue_channel[loc] == 0xff)
406 mapped = *(Uint16 *)pixel;
409 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
410 mapped |= pixel[0] << 16;
411 mapped |= pixel[1] << 8;
412 mapped |= pixel[2] << 0;
414 mapped |= pixel[0] << 0;
415 mapped |= pixel[1] << 8;
416 mapped |= pixel[2] << 16;
420 mapped = *(Uint32 *)pixel;
423 Uint8 red, green, blue, alpha;
424 SDL_GetRGBA(mapped, src->format, &red, &green, &blue, &alpha);
428 int redsum = red_channel[loc] + (red * alpha >> 8);
429 red_channel[loc] = redsum & ~0xff ? 0xff : redsum;
433 int greensum = green_channel[loc] + (green * alpha >> 8);
434 green_channel[loc] = greensum & ~0xff ? 0xff : greensum;
438 int bluesum = blue_channel[loc] + (blue * alpha >> 8);
439 blue_channel[loc] = bluesum & ~0xff ? 0xff : bluesum;
442 pixel += src->pitch - src_rect->w * bpp;
443 loc += width - src_rect->w;
445 if(SDL_MUSTLOCK(src))
447 SDL_UnlockSurface(src);
452 Lightmap::draw_surface(const DrawingRequest& request)
454 if((request.color.red == 0.0 && request.color.green == 0.0 && request.color.blue == 0.0) || request.color.alpha == 0.0 || request.alpha == 0.0)
458 //FIXME: support parameters request.alpha, request.angle, request.blend
460 const Surface* surface = (const Surface*) request.request_data;
461 SDL::Texture *sdltexture = dynamic_cast<SDL::Texture *>(surface->get_texture());
462 SDL::SurfaceData *surface_data = reinterpret_cast<SDL::SurfaceData *>(surface->get_surface_data());
464 DrawingEffect effect = request.drawing_effect;
465 if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
467 SDL_Surface *transform = sdltexture->get_transform(request.color, effect);
469 // get and check SDL_Surface
470 if (transform == 0) {
471 std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
475 SDL_Rect *src_rect = surface_data->get_src_rect(effect);
476 int dstx = (int) request.pos.x * numerator / denominator;
477 int dsty = (int) request.pos.y * numerator / denominator;
478 light_blit(transform, src_rect, dstx, dsty);
482 Lightmap::draw_surface_part(const DrawingRequest& request)
484 const SurfacePartRequest* surfacepartrequest
485 = (SurfacePartRequest*) request.request_data;
487 const Surface* surface = surfacepartrequest->surface;
488 SDL::Texture *sdltexture = dynamic_cast<SDL::Texture *>(surface->get_texture());
490 DrawingEffect effect = request.drawing_effect;
491 if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
493 SDL_Surface *transform = sdltexture->get_transform(Color(1.0, 1.0, 1.0), effect);
495 // get and check SDL_Surface
496 if (transform == 0) {
497 std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
502 if (effect == HORIZONTAL_FLIP)
504 ox = sdltexture->get_texture_width() - surface->get_x() - (int) surfacepartrequest->size.x;
508 ox = surface->get_x();
510 if (effect == VERTICAL_FLIP)
512 oy = sdltexture->get_texture_height() - surface->get_y() - (int) surfacepartrequest->size.y;
516 oy = surface->get_y();
520 src_rect.x = (ox + (int) surfacepartrequest->source.x) * numerator / denominator;
521 src_rect.y = (oy + (int) surfacepartrequest->source.y) * numerator / denominator;
522 src_rect.w = (int) surfacepartrequest->size.x * numerator / denominator;
523 src_rect.h = (int) surfacepartrequest->size.y * numerator / denominator;
524 int dstx = (int) request.pos.x * numerator / denominator;
525 int dsty = (int) request.pos.y * numerator / denominator;
526 light_blit(transform, &src_rect, dstx, dsty);
530 Lightmap::draw_gradient(const DrawingRequest& request)
532 const GradientRequest* gradientrequest
533 = (GradientRequest*) request.request_data;
534 const Color& top = gradientrequest->top;
535 const Color& bottom = gradientrequest->bottom;
538 for(int y = 0;y < height;++y)
540 Uint8 red = (Uint8)((((float)(top.red-bottom.red)/(0-height)) * y + top.red) * 255);
541 Uint8 green = (Uint8)((((float)(top.green-bottom.green)/(0-height)) * y + top.green) * 255);
542 Uint8 blue = (Uint8)((((float)(top.blue-bottom.blue)/(0-height)) * y + top.blue) * 255);
543 Uint8 alpha = (Uint8)((((float)(top.alpha-bottom.alpha)/(0-height)) * y + top.alpha) * 255);
544 for(int x = 0;x < width;x++, loc++) {
547 int redsum = red_channel[loc] + (red * alpha >> 8);
548 red_channel[loc] = redsum & ~0xff ? 0xff : redsum;
552 int greensum = green_channel[loc] + (green * alpha >> 8);
553 green_channel[loc] = greensum & ~0xff ? 0xff : greensum;
557 int bluesum = blue_channel[loc] + (blue * alpha >> 8);
558 blue_channel[loc] = bluesum & ~0xff ? 0xff : bluesum;
565 Lightmap::draw_filled_rect(const DrawingRequest& request)
567 const FillRectRequest* fillrectrequest
568 = (FillRectRequest*) request.request_data;
570 int rect_x = (int) (request.pos.x * width / SCREEN_WIDTH);
571 int rect_y = (int) (request.pos.y * height / SCREEN_HEIGHT);
572 int rect_w = (int) (fillrectrequest->size.x * width / SCREEN_WIDTH);
573 int rect_h = (int) (fillrectrequest->size.y * height / SCREEN_HEIGHT);
574 Uint8 red = (Uint8) (fillrectrequest->color.red * fillrectrequest->color.alpha * 255);
575 Uint8 green = (Uint8) (fillrectrequest->color.green * fillrectrequest->color.alpha * 255);
576 Uint8 blue = (Uint8) (fillrectrequest->color.blue * fillrectrequest->color.alpha * 255);
577 if(red == 0 && green == 0 && blue == 0)
581 for(int y = rect_y;y < rect_y + rect_h;y++) {
582 for(int x = rect_x;x < rect_x + rect_w;x++) {
583 int loc = y * width + x;
586 int redsum = red_channel[loc] + red;
587 red_channel[loc] = redsum & ~0xff ? 0xff : redsum;
591 int greensum = green_channel[loc] + green;
592 green_channel[loc] = greensum & ~0xff ? 0xff : greensum;
596 int bluesum = blue_channel[loc] + blue;
597 blue_channel[loc] = bluesum & ~0xff ? 0xff : bluesum;
604 Lightmap::get_light(const DrawingRequest& request) const
606 const GetLightRequest* getlightrequest
607 = (GetLightRequest*) request.request_data;
609 int x = (int) (request.pos.x * width / SCREEN_WIDTH);
610 int y = (int) (request.pos.y * height / SCREEN_HEIGHT);
611 int loc = y * width + x;
612 *(getlightrequest->color_ptr) = Color(((float)red_channel[loc])/255, ((float)green_channel[loc])/255, ((float)blue_channel[loc])/255);