+ if(LIGHTMAP_DIV == 1)
+ {
+ int bpp = screen->format->BytesPerPixel;
+ if(SDL_MUSTLOCK(screen))
+ {
+ SDL_LockSurface(screen);
+ }
+ Uint8 *pixel = (Uint8 *) screen->pixels;
+ int loc = 0;
+ for(int y = 0;y < height;y++) {
+ for(int x = 0;x < width;x++, pixel += bpp, loc++) {
+ if(red_channel[loc] == 0xff && green_channel[loc] == 0xff && blue_channel[loc] == 0xff)
+ {
+ continue;
+ }
+ Uint32 mapped = 0;
+ switch(bpp) {
+ case 1:
+ mapped = *pixel;
+ break;
+ case 2:
+ mapped = *(Uint16 *)pixel;
+ break;
+ case 3:
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+ mapped |= pixel[0] << 16;
+ mapped |= pixel[1] << 8;
+ mapped |= pixel[2] << 0;
+#else
+ mapped |= pixel[0] << 0;
+ mapped |= pixel[1] << 8;
+ mapped |= pixel[2] << 16;
+#endif
+ break;
+ case 4:
+ mapped = *(Uint32 *)pixel;
+ break;
+ }
+ Uint8 red, green, blue, alpha;
+ SDL_GetRGBA(mapped, screen->format, &red, &green, &blue, &alpha);
+ red = (red * red_channel[loc]) >> 8;
+ green = (green * green_channel[loc]) >> 8;
+ blue = (blue * blue_channel[loc]) >> 8;
+ mapped = SDL_MapRGBA(screen->format, red, green, blue, alpha);
+ switch(bpp) {
+ case 1:
+ *pixel = mapped;
+ break;
+ case 2:
+ *(Uint16 *)pixel = mapped;
+ break;
+ case 3:
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+ pixel[0] = (mapped >> 16) & 0xff;
+ pixel[1] = (mapped >> 8) & 0xff;
+ pixel[2] = (mapped >> 0) & 0xff;
+#else
+ pixel[0] = (mapped >> 0) & 0xff;
+ pixel[1] = (mapped >> 8) & 0xff;
+ pixel[2] = (mapped >> 16) & 0xff;
+#endif
+ break;
+ case 4:
+ *(Uint32 *)pixel = mapped;
+ break;
+ }
+ }
+ pixel += screen->pitch - width * bpp;
+ }
+ if(SDL_MUSTLOCK(screen))
+ {
+ SDL_UnlockSurface(screen);
+ }
+ }
+ else
+ {
+ int bpp = screen->format->BytesPerPixel;
+ if(SDL_MUSTLOCK(screen))
+ {
+ SDL_LockSurface(screen);
+ }
+ Uint8 *div_pixel = (Uint8 *) screen->pixels;
+ int loc = 0;
+ for(int y = 0;y < height;y++) {
+ for(int x = 0;x < width;x++, div_pixel += bpp * LIGHTMAP_DIV, loc++) {
+ if(red_channel[loc] == 0xff && green_channel[loc] == 0xff && blue_channel[loc] == 0xff)
+ {
+ continue;
+ }
+ Uint8 *pixel = div_pixel;
+ for(int div_y = 0;div_y < LIGHTMAP_DIV;div_y++) {
+ for(int div_x = 0;div_x < LIGHTMAP_DIV;pixel += bpp, div_x++) {
+ Uint32 mapped = 0;
+ switch(bpp) {
+ case 1:
+ mapped = *pixel;
+ break;
+ case 2:
+ mapped = *(Uint16 *)pixel;
+ break;
+ case 3:
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+ mapped |= pixel[0] << 16;
+ mapped |= pixel[1] << 8;
+ mapped |= pixel[2] << 0;
+#else
+ mapped |= pixel[0] << 0;
+ mapped |= pixel[1] << 8;
+ mapped |= pixel[2] << 16;
+#endif
+ break;
+ case 4:
+ mapped = *(Uint32 *)pixel;
+ break;
+ }
+ Uint8 red, green, blue, alpha;
+ SDL_GetRGBA(mapped, screen->format, &red, &green, &blue, &alpha);
+
+#ifdef BILINEAR
+ int xinc = (x + 1 != width ? 1 : 0);
+ int yinc = (y + 1 != height ? width : 0);
+ Uint8 color00[3], color01[3], color10[3], color11[3];
+ {
+ color00[0] = red_channel[loc];
+ color00[1] = green_channel[loc];
+ color00[2] = blue_channel[loc];
+ }
+ {
+ color01[0] = red_channel[loc + xinc];
+ color01[1] = green_channel[loc + xinc];
+ color01[2] = blue_channel[loc + xinc];
+ }
+ {
+ color10[0] = red_channel[loc + yinc];
+ color10[1] = green_channel[loc + yinc];
+ color10[2] = blue_channel[loc + yinc];
+ }
+ {
+ color11[0] = red_channel[loc + yinc + xinc];
+ color11[1] = green_channel[loc + yinc + xinc];
+ color11[2] = blue_channel[loc + yinc + xinc];
+ }
+ Uint8 color0[3], color1[3], color[3];
+ merge(color0, color00, color01, div_x, LIGHTMAP_DIV);
+ merge(color1, color10, color11, div_x, LIGHTMAP_DIV);
+ merge(color, color0, color1, div_y, LIGHTMAP_DIV);
+ red = (red * color[0]) >> 8;
+ green = (green * color[1]) >> 8;
+ blue = (blue * color[2]) >> 8;
+#else
+ red = (red * red_channel[loc]) >> 8;
+ green = (green * green_channel[loc]) >> 8;
+ blue = (blue * blue_channel[loc]) >> 8;
+#endif
+
+ mapped = SDL_MapRGBA(screen->format, red, green, blue, alpha);
+ switch(bpp) {
+ case 1:
+ *pixel = mapped;
+ break;
+ case 2:
+ *(Uint16 *)pixel = mapped;
+ break;
+ case 3:
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+ pixel[0] = (mapped >> 16) & 0xff;
+ pixel[1] = (mapped >> 8) & 0xff;
+ pixel[2] = (mapped >> 0) & 0xff;
+#else
+ pixel[0] = (mapped >> 0) & 0xff;
+ pixel[1] = (mapped >> 8) & 0xff;
+ pixel[2] = (mapped >> 16) & 0xff;
+#endif
+ break;
+ case 4:
+ *(Uint32 *)pixel = mapped;
+ break;
+ }
+ }
+ pixel += screen->pitch - LIGHTMAP_DIV * bpp;
+ }
+ }
+ div_pixel += (screen->pitch - width * bpp) * LIGHTMAP_DIV;
+ }
+ if(SDL_MUSTLOCK(screen))
+ {
+ SDL_UnlockSurface(screen);
+ }
+ }
+ }
+
+ void Lightmap::light_blit(SDL_Surface *src, SDL_Rect *src_rect, int dstx, int dsty)
+ {
+ dstx /= LIGHTMAP_DIV;
+ dsty /= LIGHTMAP_DIV;
+ int srcx = src_rect->x / LIGHTMAP_DIV;
+ int srcy = src_rect->y / LIGHTMAP_DIV;
+ int blit_width = src_rect->w / LIGHTMAP_DIV;
+ int blit_height = src_rect->h / LIGHTMAP_DIV;
+ int bpp = src->format->BytesPerPixel;
+ if(SDL_MUSTLOCK(src))
+ {
+ SDL_LockSurface(src);
+ }
+ Uint8 *pixel = (Uint8 *) src->pixels + srcy * src->pitch + srcx * bpp;
+ int loc = dsty * width + dstx;
+ for(int y = 0;y < blit_height;y++) {
+ for(int x = 0;x < blit_width;x++, pixel += bpp * LIGHTMAP_DIV, loc++) {
+ if(x + dstx < 0 || y + dsty < 0 || x + dstx >= width || y + dsty >= height)
+ {
+ continue;
+ }