src/blitter/32bpp_optimized.cpp
author rubidium
Sun, 14 Oct 2007 19:57:15 +0000
changeset 7726 a9c8beebf2c1
parent 7553 63d4424de5d7
child 8091 674be8638d74
permissions -rw-r--r--
(svn r11261) -Codechange: Draw selection sprites (HT_RECT, HT_POINT, HT_RAIL) on foundations as ChildSprite of the foundation, not as single ParentSprite. Patch by frosch.
7553
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
     1
#include "../stdafx.h"
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
     2
#include "../zoom.hpp"
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
     3
#include "../gfx.h"
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
     4
#include "../debug.h"
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
     5
#include "../table/sprites.h"
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
     6
#include "32bpp_optimized.hpp"
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
     7
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
     8
static FBlitter_32bppOptimized iFBlitter_32bppOptimized;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
     9
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    10
void Blitter_32bppOptimized::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    11
{
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    12
	const SpriteLoader::CommonPixel *src, *src_line;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    13
	uint32 *dst, *dst_line;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    14
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    15
	/* Find where to start reading in the source sprite */
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    16
	src_line = (const SpriteLoader::CommonPixel *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom);
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    17
	dst_line = (uint32 *)bp->dst + bp->top * bp->pitch + bp->left;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    18
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    19
	for (int y = 0; y < bp->height; y++) {
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    20
		dst = dst_line;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    21
		dst_line += bp->pitch;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    22
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    23
		src = src_line;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    24
		src_line += bp->sprite_width * ScaleByZoom(1, zoom);
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    25
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    26
		for (int x = 0; x < bp->width; x++) {
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    27
			if (src->a == 0) {
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    28
				/* src->r is 'misused' here to indicate how much more pixels are following with an alpha of 0 */
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    29
				int skip = UnScaleByZoom(src->r, zoom);
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    30
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    31
				dst += skip;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    32
				x   += skip - 1;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    33
				src += ScaleByZoom(1, zoom) * skip;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    34
				continue;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    35
			}
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    36
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    37
			switch (mode) {
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    38
				case BM_COLOUR_REMAP:
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    39
					/* In case the m-channel is zero, do not remap this pixel in any way */
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    40
					if (src->m == 0) {
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    41
						*dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    42
					} else {
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    43
						if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->LookupColourInPalette(bp->remap[src->m]), src->a, *dst);
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    44
					}
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    45
					break;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    46
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    47
				case BM_TRANSPARENT:
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    48
					/* TODO -- We make an assumption here that the remap in fact is transparency, not some color.
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    49
					 *  This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    50
					 *  we produce a result the newgrf maker didn't expect ;) */
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    51
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    52
					/* Make the current color a bit more black, so it looks like this image is transparent */
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    53
					*dst = MakeTransparent(*dst, 192);
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    54
					break;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    55
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    56
				default:
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    57
					*dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    58
					break;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    59
			}
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    60
			dst++;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    61
			src += ScaleByZoom(1, zoom);
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    62
		}
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    63
	}
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    64
}
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    65
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    66
Sprite *Blitter_32bppOptimized::Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator)
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    67
{
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    68
	Sprite *dest_sprite;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    69
	SpriteLoader::CommonPixel *dst;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    70
	dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sprite->height * sprite->width * sizeof(SpriteLoader::CommonPixel));
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    71
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    72
	dest_sprite->height = sprite->height;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    73
	dest_sprite->width  = sprite->width;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    74
	dest_sprite->x_offs = sprite->x_offs;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    75
	dest_sprite->y_offs = sprite->y_offs;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    76
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    77
	dst = (SpriteLoader::CommonPixel *)dest_sprite->data;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    78
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    79
	memcpy(dst, sprite->data, sprite->height * sprite->width * sizeof(SpriteLoader::CommonPixel));
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    80
	/* Skip to the end of the array, and work backwards to find transparent blocks */
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    81
	dst = dst + sprite->height * sprite->width - 1;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    82
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    83
	for (uint y = sprite->height; y > 0; y--) {
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    84
		int trans = 0;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    85
		/* Process sprite line backwards, to compute lengths of transparent blocks */
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    86
		for (uint x = sprite->width; x > 0; x--) {
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    87
			if (dst->a == 0) {
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    88
				/* Save transparent block length in red channel; max value is 255 the red channel can contain */
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    89
				if (trans < 255) trans++;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    90
				dst->r = trans;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    91
				dst->g = 0;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    92
				dst->b = 0;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    93
				dst->m = 0;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    94
			} else {
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    95
				trans = 0;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    96
				if (dst->m != 0) {
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    97
					/* Pre-convert the mapping channel to a RGB value */
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    98
					uint color = this->LookupColourInPalette(dst->m);
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
    99
					dst->r = GB(color, 16, 8);
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
   100
					dst->g = GB(color, 8,  8);
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
   101
					dst->b = GB(color, 0,  8);
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
   102
				}
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
   103
			}
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
   104
			dst--;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
   105
		}
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
   106
	}
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
   107
	return dest_sprite;
63d4424de5d7 (svn r11078) -Add: added 32bpp-optimized, which is almost twice as fast as 32bpp-simple (based on the work of frosch)
truelight
parents:
diff changeset
   108
}