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 "drawing_context.hpp"
34 #include "drawing_request.hpp"
35 #include "renderer.hpp"
36 #include "surface.hpp"
39 #include "gameconfig.hpp"
40 #include "texture.hpp"
41 #include "texture_manager.hpp"
42 #include "obstack/obstackpp.hpp"
48 screen = SDL_GetVideoSurface();
53 red_channel = (Uint8 *)malloc(width * height * sizeof(Uint8));
54 green_channel = (Uint8 *)malloc(width * height * sizeof(Uint8));
55 blue_channel = (Uint8 *)malloc(width * height * sizeof(Uint8));
66 Lightmap::start_draw(const Color &ambient_color)
68 memset(red_channel, (Uint8) (ambient_color.red * 255), width * height * sizeof(Uint8));
69 memset(green_channel, (Uint8) (ambient_color.green * 255), width * height * sizeof(Uint8));
70 memset(blue_channel, (Uint8) (ambient_color.blue * 255), width * height * sizeof(Uint8));
81 // FIXME: This is really slow
82 int bpp = screen->format->BytesPerPixel;
83 for(int y = 0;y < height;y++) {
84 for(int x = 0;x < width;x++) {
85 int loc = y * width + x;
86 if(red_channel[loc] == 255 && green_channel[loc] == 255 && blue_channel[loc] == 255)
90 Uint8 *pixel = (Uint8 *) screen->pixels + y * screen->pitch + x * bpp;
97 mapped = *(Uint16 *)pixel;
100 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
101 mapped |= pixel[0] << 16;
102 mapped |= pixel[1] << 8;
103 mapped |= pixel[2] << 0;
105 mapped |= pixel[0] << 0;
106 mapped |= pixel[1] << 8;
107 mapped |= pixel[2] << 16;
111 mapped = *(Uint32 *)pixel;
114 Uint8 red, green, blue, alpha;
115 SDL_GetRGBA(mapped, screen->format, &red, &green, &blue, &alpha);
116 red = (red * red_channel[loc]) / 255;
117 green = (green * green_channel[loc]) / 255;
118 blue = (blue * blue_channel[loc]) / 255;
119 mapped = SDL_MapRGBA(screen->format, red, green, blue, alpha);
125 *(Uint16 *)pixel = mapped;
128 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
129 pixel[0] = (mapped >> 16) & 0xff;
130 pixel[1] = (mapped >> 8) & 0xff;
131 pixel[2] = (mapped >> 0) & 0xff;
133 pixel[0] = (mapped >> 0) & 0xff;
134 pixel[1] = (mapped >> 8) & 0xff;
135 pixel[2] = (mapped >> 16) & 0xff;
139 *(Uint32 *)pixel = mapped;
146 void Lightmap::light_blit(SDL_Surface *src, int dstx, int dsty,
147 int srcx, int srcy, int blit_width, int blit_height)
149 for(int y = 0;y < blit_height;y++) {
150 for(int x = 0;x < blit_width;x++) {
151 if(x + dstx < 0 || y + dsty < 0 || x + dstx >= width || y + dsty >= height)
155 int loc = (y + dsty) * width + (x + dstx);
156 if(red_channel[loc] == 255 && green_channel[loc] == 255 && blue_channel[loc])
161 Uint8 *pixel = (Uint8 *) src->pixels + (y + srcy) * src->pitch + (x + srcx) * src->format->BytesPerPixel;
163 switch(src->format->BytesPerPixel) {
168 mapped = *(Uint16 *)pixel;
171 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
172 mapped |= pixel[0] << 16;
173 mapped |= pixel[1] << 8;
174 mapped |= pixel[2] << 0;
176 mapped |= pixel[0] << 0;
177 mapped |= pixel[1] << 8;
178 mapped |= pixel[2] << 16;
182 mapped = *(Uint32 *)pixel;
185 Uint8 red, green, blue, alpha;
186 SDL_GetRGBA(mapped, src->format, &red, &green, &blue, &alpha);
188 red_channel[loc] = std::min(red_channel[loc] + (red * alpha / 255), 255);
189 green_channel[loc] = std::min(green_channel[loc] + (green * alpha / 255), 255);
190 blue_channel[loc] = std::min(blue_channel[loc] + (blue * alpha / 255), 255);
196 Lightmap::draw_surface(const DrawingRequest& request)
198 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)
202 //FIXME: support parameters request.alpha, request.angle, request.blend
204 const Surface* surface = (const Surface*) request.request_data;
205 SDL::Texture *sdltexture = dynamic_cast<SDL::Texture *>(surface->get_texture());
206 DrawingEffect effect = request.drawing_effect;
207 if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
209 SDL_Surface *transform = sdltexture->get_transform(request.color, effect);
211 // get and check SDL_Surface
212 if (transform == 0) {
213 std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
218 if (effect == HORIZONTAL_FLIP)
220 ox = sdltexture->get_texture_width() - surface->get_x() - surface->get_width();
224 ox = surface->get_x();
226 if (effect == VERTICAL_FLIP)
228 oy = sdltexture->get_texture_height() - surface->get_y() - surface->get_height();
232 oy = surface->get_y();
235 int numerator, denominator;
236 float xfactor = (float) config->screenwidth / SCREEN_WIDTH;
237 float yfactor = (float) config->screenheight / SCREEN_HEIGHT;
238 if(xfactor < yfactor)
240 numerator = config->screenwidth;
241 denominator = SCREEN_WIDTH;
245 numerator = config->screenheight;
246 denominator = SCREEN_HEIGHT;
249 int dstx = (int) request.pos.x * numerator / denominator;
250 int dsty = (int) request.pos.y * numerator / denominator;
251 int srcx = ox * numerator / denominator;
252 int srcy = oy * numerator / denominator;
253 int blit_width = surface->get_width() * numerator / denominator;
254 int blit_height = surface->get_height() * numerator / denominator;
255 light_blit(transform, dstx, dsty, srcx, srcy, blit_width, blit_height);
259 Lightmap::draw_surface_part(const DrawingRequest& request)
261 const SurfacePartRequest* surfacepartrequest
262 = (SurfacePartRequest*) request.request_data;
264 const Surface* surface = surfacepartrequest->surface;
265 SDL::Texture *sdltexture = dynamic_cast<SDL::Texture *>(surface->get_texture());
266 DrawingEffect effect = request.drawing_effect;
267 if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
269 SDL_Surface *transform = sdltexture->get_transform(Color(1.0, 1.0, 1.0), effect);
271 // get and check SDL_Surface
272 if (transform == 0) {
273 std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
278 if (effect == HORIZONTAL_FLIP)
280 ox = sdltexture->get_texture_width() - surface->get_x() - (int) surfacepartrequest->size.x;
284 ox = surface->get_x();
286 if (effect == VERTICAL_FLIP)
288 oy = sdltexture->get_texture_height() - surface->get_y() - (int) surfacepartrequest->size.y;
292 oy = surface->get_y();
295 int numerator, denominator;
296 float xfactor = (float) config->screenwidth / SCREEN_WIDTH;
297 float yfactor = (float) config->screenheight / SCREEN_HEIGHT;
298 if(xfactor < yfactor)
300 numerator = config->screenwidth;
301 denominator = SCREEN_WIDTH;
305 numerator = config->screenheight;
306 denominator = SCREEN_HEIGHT;
309 int dstx = (int) request.pos.x * numerator / denominator;
310 int dsty = (int) request.pos.y * numerator / denominator;
311 int srcx = (ox + (int) surfacepartrequest->source.x) * numerator / denominator;
312 int srcy = (oy + (int) surfacepartrequest->source.y) * numerator / denominator;
313 int blit_width = (int) surfacepartrequest->size.x * numerator / denominator;
314 int blit_height = (int) surfacepartrequest->size.y * numerator / denominator;
315 light_blit(transform, dstx, dsty, srcx, srcy, blit_width, blit_height);
319 Lightmap::draw_gradient(const DrawingRequest& request)
321 const GradientRequest* gradientrequest
322 = (GradientRequest*) request.request_data;
323 const Color& top = gradientrequest->top;
324 const Color& bottom = gradientrequest->bottom;
326 for(int y = 0;y < height;++y)
328 Uint8 r = (Uint8)((((float)(top.red-bottom.red)/(0-height)) * y + top.red) * 255);
329 Uint8 g = (Uint8)((((float)(top.green-bottom.green)/(0-height)) * y + top.green) * 255);
330 Uint8 b = (Uint8)((((float)(top.blue-bottom.blue)/(0-height)) * y + top.blue) * 255);
332 //Uint8 a = (Uint8)((((float)(top.alpha-bottom.alpha)/(0-height)) * y + top.alpha) * 255);
333 for(int x = 0;x < width;x++) {
334 int loc = y * width + x;
335 red_channel[loc] = std::min(red_channel[loc] + r, 255);
336 green_channel[loc] = std::min(green_channel[loc] + g, 255);
337 blue_channel[loc] = std::min(blue_channel[loc] + b, 255);
343 Lightmap::draw_text(const DrawingRequest& /*request*/)
345 //const TextRequest* textrequest = (TextRequest*) request.request_data;
347 //textrequest->font->draw(textrequest->text, request.pos,
348 // textrequest->alignment, request.drawing_effect, request.alpha);
352 Lightmap::draw_filled_rect(const DrawingRequest& request)
354 const FillRectRequest* fillrectrequest
355 = (FillRectRequest*) request.request_data;
357 int rect_x = (int) (request.pos.x * width / SCREEN_WIDTH);
358 int rect_y = (int) (request.pos.y * height / SCREEN_HEIGHT);
359 int rect_w = (int) (fillrectrequest->size.x * width / SCREEN_WIDTH);
360 int rect_h = (int) (fillrectrequest->size.y * height / SCREEN_HEIGHT);
361 Uint8 red = (Uint8) (fillrectrequest->color.red * fillrectrequest->color.alpha * 255);
362 Uint8 green = (Uint8) (fillrectrequest->color.green * fillrectrequest->color.alpha * 255);
363 Uint8 blue = (Uint8) (fillrectrequest->color.blue * fillrectrequest->color.alpha * 255);
364 if(red == 0 && green == 0 && blue == 0)
368 for(int y = rect_y;y < rect_y + rect_h;y++) {
369 for(int x = rect_x;x < rect_x + rect_w;x++) {
370 int loc = y * width + x;
371 red_channel[loc] = std::min(red_channel[loc] + red, 255);
372 green_channel[loc] = std::min(green_channel[loc] + green, 255);
373 blue_channel[loc] = std::min(blue_channel[loc] + blue, 255);
379 Lightmap::get_light(const DrawingRequest& request) const
381 const GetLightRequest* getlightrequest
382 = (GetLightRequest*) request.request_data;
384 int x = (int) (request.pos.x * width / SCREEN_WIDTH);
385 int y = (int) (request.pos.y * height / SCREEN_HEIGHT);
386 int loc = y * width + x;
387 *(getlightrequest->color_ptr) = Color(((float)red_channel[loc])/255, ((float)green_channel[loc])/255, ((float)blue_channel[loc])/255);