smatz@8091: /* $Id$ */ smatz@8091: truelight@7553: #include "../stdafx.h" rubidium@8123: #include "../zoom_func.h" rubidium@8123: #include "../gfx_func.h" truelight@7553: #include "../debug.h" truelight@7553: #include "../table/sprites.h" truelight@7553: #include "32bpp_optimized.hpp" truelight@7553: truelight@7553: static FBlitter_32bppOptimized iFBlitter_32bppOptimized; truelight@7553: truelight@7553: void Blitter_32bppOptimized::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) truelight@7553: { truelight@7553: const SpriteLoader::CommonPixel *src, *src_line; truelight@7553: uint32 *dst, *dst_line; truelight@7553: truelight@7553: /* Find where to start reading in the source sprite */ truelight@7553: src_line = (const SpriteLoader::CommonPixel *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom); truelight@7553: dst_line = (uint32 *)bp->dst + bp->top * bp->pitch + bp->left; truelight@7553: truelight@7553: for (int y = 0; y < bp->height; y++) { truelight@7553: dst = dst_line; truelight@7553: dst_line += bp->pitch; truelight@7553: truelight@7553: src = src_line; truelight@7553: src_line += bp->sprite_width * ScaleByZoom(1, zoom); truelight@7553: truelight@7553: for (int x = 0; x < bp->width; x++) { truelight@7553: if (src->a == 0) { truelight@7553: /* src->r is 'misused' here to indicate how much more pixels are following with an alpha of 0 */ truelight@7553: int skip = UnScaleByZoom(src->r, zoom); truelight@7553: truelight@7553: dst += skip; truelight@7553: x += skip - 1; truelight@7553: src += ScaleByZoom(1, zoom) * skip; truelight@7553: continue; truelight@7553: } truelight@7553: truelight@7553: switch (mode) { truelight@7553: case BM_COLOUR_REMAP: truelight@7553: /* In case the m-channel is zero, do not remap this pixel in any way */ truelight@7553: if (src->m == 0) { truelight@7553: *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst); truelight@7553: } else { truelight@7553: if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->LookupColourInPalette(bp->remap[src->m]), src->a, *dst); truelight@7553: } truelight@7553: break; truelight@7553: truelight@7553: case BM_TRANSPARENT: truelight@7553: /* TODO -- We make an assumption here that the remap in fact is transparency, not some color. truelight@7553: * This is never a problem with the code we produce, but newgrfs can make it fail... or at least: truelight@7553: * we produce a result the newgrf maker didn't expect ;) */ truelight@7553: truelight@7553: /* Make the current color a bit more black, so it looks like this image is transparent */ truelight@7553: *dst = MakeTransparent(*dst, 192); truelight@7553: break; truelight@7553: truelight@7553: default: truelight@7553: *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst); truelight@7553: break; truelight@7553: } truelight@7553: dst++; truelight@7553: src += ScaleByZoom(1, zoom); truelight@7553: } truelight@7553: } truelight@7553: } truelight@7553: truelight@7553: Sprite *Blitter_32bppOptimized::Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator) truelight@7553: { truelight@7553: Sprite *dest_sprite; truelight@7553: SpriteLoader::CommonPixel *dst; truelight@7553: dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sprite->height * sprite->width * sizeof(SpriteLoader::CommonPixel)); truelight@7553: truelight@7553: dest_sprite->height = sprite->height; truelight@7553: dest_sprite->width = sprite->width; truelight@7553: dest_sprite->x_offs = sprite->x_offs; truelight@7553: dest_sprite->y_offs = sprite->y_offs; truelight@7553: truelight@7553: dst = (SpriteLoader::CommonPixel *)dest_sprite->data; truelight@7553: truelight@7553: memcpy(dst, sprite->data, sprite->height * sprite->width * sizeof(SpriteLoader::CommonPixel)); truelight@7553: /* Skip to the end of the array, and work backwards to find transparent blocks */ truelight@7553: dst = dst + sprite->height * sprite->width - 1; truelight@7553: truelight@7553: for (uint y = sprite->height; y > 0; y--) { truelight@7553: int trans = 0; truelight@7553: /* Process sprite line backwards, to compute lengths of transparent blocks */ truelight@7553: for (uint x = sprite->width; x > 0; x--) { truelight@7553: if (dst->a == 0) { truelight@7553: /* Save transparent block length in red channel; max value is 255 the red channel can contain */ truelight@7553: if (trans < 255) trans++; truelight@7553: dst->r = trans; truelight@7553: dst->g = 0; truelight@7553: dst->b = 0; truelight@7553: dst->m = 0; truelight@7553: } else { truelight@7553: trans = 0; truelight@7553: if (dst->m != 0) { truelight@7553: /* Pre-convert the mapping channel to a RGB value */ truelight@7553: uint color = this->LookupColourInPalette(dst->m); truelight@7553: dst->r = GB(color, 16, 8); truelight@7553: dst->g = GB(color, 8, 8); truelight@7553: dst->b = GB(color, 0, 8); truelight@7553: } truelight@7553: } truelight@7553: dst--; truelight@7553: } truelight@7553: } truelight@7553: return dest_sprite; truelight@7553: }