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 Uint8 *pixel = (Uint8 *) screen->pixels;
117 for(int y = 0;y < height;y++) {
118 for(int x = 0;x < width;x++, pixel += bpp, loc++) {
119 if(red_channel[loc] == 0xff && green_channel[loc] == 0xff && blue_channel[loc] == 0xff)
129 mapped = *(Uint16 *)pixel;
132 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
133 mapped |= pixel[0] << 16;
134 mapped |= pixel[1] << 8;
135 mapped |= pixel[2] << 0;
137 mapped |= pixel[0] << 0;
138 mapped |= pixel[1] << 8;
139 mapped |= pixel[2] << 16;
143 mapped = *(Uint32 *)pixel;
146 Uint8 red, green, blue, alpha;
147 SDL_GetRGBA(mapped, screen->format, &red, &green, &blue, &alpha);
148 red = (red * red_channel[loc]) >> 8;
149 green = (green * green_channel[loc]) >> 8;
150 blue = (blue * blue_channel[loc]) >> 8;
151 mapped = SDL_MapRGBA(screen->format, red, green, blue, alpha);
157 *(Uint16 *)pixel = mapped;
160 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
161 pixel[0] = (mapped >> 16) & 0xff;
162 pixel[1] = (mapped >> 8) & 0xff;
163 pixel[2] = (mapped >> 0) & 0xff;
165 pixel[0] = (mapped >> 0) & 0xff;
166 pixel[1] = (mapped >> 8) & 0xff;
167 pixel[2] = (mapped >> 16) & 0xff;
171 *(Uint32 *)pixel = mapped;
175 pixel += screen->pitch - width * bpp;
180 int bpp = screen->format->BytesPerPixel;
181 Uint8 *div_pixel = (Uint8 *) screen->pixels;
183 for(int y = 0;y < height;y++) {
184 for(int x = 0;x < width;x++, div_pixel += bpp * LIGHTMAP_DIV, loc++) {
185 if(red_channel[loc] == 0xff && green_channel[loc] == 0xff && blue_channel[loc] == 0xff)
189 Uint8 *pixel = div_pixel;
190 for(int div_y = 0;div_y < LIGHTMAP_DIV;div_y++) {
191 for(int div_x = 0;div_x < LIGHTMAP_DIV;pixel += bpp, div_x++) {
198 mapped = *(Uint16 *)pixel;
201 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
202 mapped |= pixel[0] << 16;
203 mapped |= pixel[1] << 8;
204 mapped |= pixel[2] << 0;
206 mapped |= pixel[0] << 0;
207 mapped |= pixel[1] << 8;
208 mapped |= pixel[2] << 16;
212 mapped = *(Uint32 *)pixel;
215 Uint8 red, green, blue, alpha;
216 SDL_GetRGBA(mapped, screen->format, &red, &green, &blue, &alpha);
219 int xinc = (x + 1 != width ? 1 : 0);
220 int yinc = (y + 1 != height ? width : 0);
221 Uint8 color00[3], color01[3], color10[3], color11[3];
223 color00[0] = red_channel[loc];
224 color00[1] = green_channel[loc];
225 color00[2] = blue_channel[loc];
228 color01[0] = red_channel[loc + xinc];
229 color01[1] = green_channel[loc + xinc];
230 color01[2] = blue_channel[loc + xinc];
233 color10[0] = red_channel[loc + yinc];
234 color10[1] = green_channel[loc + yinc];
235 color10[2] = blue_channel[loc + yinc];
238 color11[0] = red_channel[loc + yinc + xinc];
239 color11[1] = green_channel[loc + yinc + xinc];
240 color11[2] = blue_channel[loc + yinc + xinc];
242 Uint8 color0[3], color1[3], color[3];
243 merge(color0, color00, color01, div_x, LIGHTMAP_DIV);
244 merge(color1, color10, color11, div_x, LIGHTMAP_DIV);
245 merge(color, color0, color1, div_y, LIGHTMAP_DIV);
246 red = (red * color[0]) >> 8;
247 green = (green * color[1]) >> 8;
248 blue = (blue * color[2]) >> 8;
250 red = (red * red_channel[loc]) >> 8;
251 green = (green * green_channel[loc]) >> 8;
252 blue = (blue * blue_channel[loc]) >> 8;
255 mapped = SDL_MapRGBA(screen->format, red, green, blue, alpha);
261 *(Uint16 *)pixel = mapped;
264 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
265 pixel[0] = (mapped >> 16) & 0xff;
266 pixel[1] = (mapped >> 8) & 0xff;
267 pixel[2] = (mapped >> 0) & 0xff;
269 pixel[0] = (mapped >> 0) & 0xff;
270 pixel[1] = (mapped >> 8) & 0xff;
271 pixel[2] = (mapped >> 16) & 0xff;
275 *(Uint32 *)pixel = mapped;
279 pixel += screen->pitch - LIGHTMAP_DIV * bpp;
282 div_pixel += (screen->pitch - width * bpp) * LIGHTMAP_DIV;
287 void Lightmap::light_blit(SDL_Surface *src, SDL_Rect *src_rect, int dstx, int dsty)
289 dstx /= LIGHTMAP_DIV;
290 dsty /= LIGHTMAP_DIV;
291 int srcx = src_rect->x / LIGHTMAP_DIV;
292 int srcy = src_rect->y / LIGHTMAP_DIV;
293 int blit_width = src_rect->w / LIGHTMAP_DIV;
294 int blit_height = src_rect->h / LIGHTMAP_DIV;
295 int bpp = src->format->BytesPerPixel;
296 Uint8 *pixel = (Uint8 *) src->pixels + srcy * src->pitch + srcx * bpp;
297 int loc = dsty * width + dstx;
298 for(int y = 0;y < blit_height;y++) {
299 for(int x = 0;x < blit_width;x++, pixel += bpp * LIGHTMAP_DIV, loc++) {
300 if(x + dstx < 0 || y + dsty < 0 || x + dstx >= width || y + dsty >= height)
304 if(red_channel[loc] == 0xff && green_channel[loc] == 0xff && blue_channel[loc] == 0xff)
315 mapped = *(Uint16 *)pixel;
318 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
319 mapped |= pixel[0] << 16;
320 mapped |= pixel[1] << 8;
321 mapped |= pixel[2] << 0;
323 mapped |= pixel[0] << 0;
324 mapped |= pixel[1] << 8;
325 mapped |= pixel[2] << 16;
329 mapped = *(Uint32 *)pixel;
332 Uint8 red, green, blue, alpha;
333 SDL_GetRGBA(mapped, src->format, &red, &green, &blue, &alpha);
337 int redsum = red_channel[loc] + (red * alpha >> 8);
338 red_channel[loc] = redsum & ~0xff ? 0xff : redsum;
342 int greensum = green_channel[loc] + (green * alpha >> 8);
343 green_channel[loc] = greensum & ~0xff ? 0xff : greensum;
347 int bluesum = blue_channel[loc] + (blue * alpha >> 8);
348 blue_channel[loc] = bluesum & ~0xff ? 0xff : bluesum;
351 pixel += (src->pitch - blit_width * bpp) * LIGHTMAP_DIV;
352 loc += width - blit_width;
356 /*void Lightmap::light_blit(SDL_Surface *src, SDL_Rect *src_rect, int dstx, int dsty)
358 int bpp = src->format->BytesPerPixel;
359 Uint8 *pixel = (Uint8 *) src->pixels + src_rect->y * src->pitch + src_rect->x * bpp;
360 int loc = dsty * width + dstx;
361 for(int y = 0;y < src_rect->h;y++) {
362 for(int x = 0;x < src_rect->w;x++, pixel += bpp, loc++) {
363 if(x + dstx < 0 || y + dsty < 0 || x + dstx >= width || y + dsty >= height)
367 if(red_channel[loc] == 0xff && green_channel[loc] == 0xff && blue_channel[loc] == 0xff)
378 mapped = *(Uint16 *)pixel;
381 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
382 mapped |= pixel[0] << 16;
383 mapped |= pixel[1] << 8;
384 mapped |= pixel[2] << 0;
386 mapped |= pixel[0] << 0;
387 mapped |= pixel[1] << 8;
388 mapped |= pixel[2] << 16;
392 mapped = *(Uint32 *)pixel;
395 Uint8 red, green, blue, alpha;
396 SDL_GetRGBA(mapped, src->format, &red, &green, &blue, &alpha);
400 int redsum = red_channel[loc] + (red * alpha >> 8);
401 red_channel[loc] = redsum & ~0xff ? 0xff : redsum;
405 int greensum = green_channel[loc] + (green * alpha >> 8);
406 green_channel[loc] = greensum & ~0xff ? 0xff : greensum;
410 int bluesum = blue_channel[loc] + (blue * alpha >> 8);
411 blue_channel[loc] = bluesum & ~0xff ? 0xff : bluesum;
414 pixel += src->pitch - src_rect->w * bpp;
415 loc += width - src_rect->w;
420 Lightmap::draw_surface(const DrawingRequest& request)
422 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)
426 //FIXME: support parameters request.alpha, request.angle, request.blend
428 const Surface* surface = (const Surface*) request.request_data;
429 SDL::Texture *sdltexture = dynamic_cast<SDL::Texture *>(surface->get_texture());
430 SDL::SurfaceData *surface_data = reinterpret_cast<SDL::SurfaceData *>(surface->get_surface_data());
432 DrawingEffect effect = request.drawing_effect;
433 if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
435 SDL_Surface *transform = sdltexture->get_transform(request.color, effect);
437 // get and check SDL_Surface
438 if (transform == 0) {
439 std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
443 SDL_Rect *src_rect = surface_data->get_src_rect(effect);
444 int dstx = (int) request.pos.x * numerator / denominator;
445 int dsty = (int) request.pos.y * numerator / denominator;
446 light_blit(transform, src_rect, dstx, dsty);
450 Lightmap::draw_surface_part(const DrawingRequest& request)
452 const SurfacePartRequest* surfacepartrequest
453 = (SurfacePartRequest*) request.request_data;
455 const Surface* surface = surfacepartrequest->surface;
456 SDL::Texture *sdltexture = dynamic_cast<SDL::Texture *>(surface->get_texture());
458 DrawingEffect effect = request.drawing_effect;
459 if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
461 SDL_Surface *transform = sdltexture->get_transform(Color(1.0, 1.0, 1.0), effect);
463 // get and check SDL_Surface
464 if (transform == 0) {
465 std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
470 if (effect == HORIZONTAL_FLIP)
472 ox = sdltexture->get_texture_width() - surface->get_x() - (int) surfacepartrequest->size.x;
476 ox = surface->get_x();
478 if (effect == VERTICAL_FLIP)
480 oy = sdltexture->get_texture_height() - surface->get_y() - (int) surfacepartrequest->size.y;
484 oy = surface->get_y();
488 src_rect.x = (ox + (int) surfacepartrequest->source.x) * numerator / denominator;
489 src_rect.y = (oy + (int) surfacepartrequest->source.y) * numerator / denominator;
490 src_rect.w = (int) surfacepartrequest->size.x * numerator / denominator;
491 src_rect.h = (int) surfacepartrequest->size.y * numerator / denominator;
492 int dstx = (int) request.pos.x * numerator / denominator;
493 int dsty = (int) request.pos.y * numerator / denominator;
494 light_blit(transform, &src_rect, dstx, dsty);
498 Lightmap::draw_gradient(const DrawingRequest& request)
500 const GradientRequest* gradientrequest
501 = (GradientRequest*) request.request_data;
502 const Color& top = gradientrequest->top;
503 const Color& bottom = gradientrequest->bottom;
506 for(int y = 0;y < height;++y)
508 Uint8 red = (Uint8)((((float)(top.red-bottom.red)/(0-height)) * y + top.red) * 255);
509 Uint8 green = (Uint8)((((float)(top.green-bottom.green)/(0-height)) * y + top.green) * 255);
510 Uint8 blue = (Uint8)((((float)(top.blue-bottom.blue)/(0-height)) * y + top.blue) * 255);
511 Uint8 alpha = (Uint8)((((float)(top.alpha-bottom.alpha)/(0-height)) * y + top.alpha) * 255);
512 for(int x = 0;x < width;x++, loc++) {
515 int redsum = red_channel[loc] + (red * alpha >> 8);
516 red_channel[loc] = redsum & ~0xff ? 0xff : redsum;
520 int greensum = green_channel[loc] + (green * alpha >> 8);
521 green_channel[loc] = greensum & ~0xff ? 0xff : greensum;
525 int bluesum = blue_channel[loc] + (blue * alpha >> 8);
526 blue_channel[loc] = bluesum & ~0xff ? 0xff : bluesum;
533 Lightmap::draw_text(const DrawingRequest& /*request*/)
535 //const TextRequest* textrequest = (TextRequest*) request.request_data;
537 //textrequest->font->draw(textrequest->text, request.pos,
538 // textrequest->alignment, request.drawing_effect, request.alpha);
542 Lightmap::draw_filled_rect(const DrawingRequest& request)
544 const FillRectRequest* fillrectrequest
545 = (FillRectRequest*) request.request_data;
547 int rect_x = (int) (request.pos.x * width / SCREEN_WIDTH);
548 int rect_y = (int) (request.pos.y * height / SCREEN_HEIGHT);
549 int rect_w = (int) (fillrectrequest->size.x * width / SCREEN_WIDTH);
550 int rect_h = (int) (fillrectrequest->size.y * height / SCREEN_HEIGHT);
551 Uint8 red = (Uint8) (fillrectrequest->color.red * fillrectrequest->color.alpha * 255);
552 Uint8 green = (Uint8) (fillrectrequest->color.green * fillrectrequest->color.alpha * 255);
553 Uint8 blue = (Uint8) (fillrectrequest->color.blue * fillrectrequest->color.alpha * 255);
554 if(red == 0 && green == 0 && blue == 0)
558 for(int y = rect_y;y < rect_y + rect_h;y++) {
559 for(int x = rect_x;x < rect_x + rect_w;x++) {
560 int loc = y * width + x;
563 int redsum = red_channel[loc] + red;
564 red_channel[loc] = redsum & ~0xff ? 0xff : redsum;
568 int greensum = green_channel[loc] + green;
569 green_channel[loc] = greensum & ~0xff ? 0xff : greensum;
573 int bluesum = blue_channel[loc] + blue;
574 blue_channel[loc] = bluesum & ~0xff ? 0xff : bluesum;
581 Lightmap::get_light(const DrawingRequest& request) const
583 const GetLightRequest* getlightrequest
584 = (GetLightRequest*) request.request_data;
586 int x = (int) (request.pos.x * width / SCREEN_WIDTH);
587 int y = (int) (request.pos.y * height / SCREEN_HEIGHT);
588 int loc = y * width + x;
589 *(getlightrequest->color_ptr) = Color(((float)red_channel[loc])/255, ((float)green_channel[loc])/255, ((float)blue_channel[loc])/255);