src/blitter/32bpp_simple.cpp
author rubidium
Sun, 14 Oct 2007 19:57:15 +0000
changeset 7726 a9c8beebf2c1
parent 7552 56aa62dba204
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.
6889
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
     1
#include "../stdafx.h"
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
     2
#include "../zoom.hpp"
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
     3
#include "../gfx.h"
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
     4
#include "../debug.h"
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
     5
#include "../table/sprites.h"
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
     6
#include "32bpp_simple.hpp"
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
     7
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
     8
static FBlitter_32bppSimple iFBlitter_32bppSimple;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
     9
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    10
void Blitter_32bppSimple::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    11
{
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    12
	const SpriteLoader::CommonPixel *src, *src_line;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    13
	uint32 *dst, *dst_line;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    14
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    15
	/* Find where to start reading in the source sprite */
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    16
	src_line = (const SpriteLoader::CommonPixel *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom);
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    17
	dst_line = (uint32 *)bp->dst + bp->top * bp->pitch + bp->left;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    18
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    19
	for (int y = 0; y < bp->height; y++) {
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    20
		dst = dst_line;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    21
		dst_line += bp->pitch;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    22
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    23
		src = src_line;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    24
		src_line += bp->sprite_width * ScaleByZoom(1, zoom);
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    25
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    26
		for (int x = 0; x < bp->width; x++) {
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    27
			switch (mode) {
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    28
				case BM_COLOUR_REMAP:
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    29
					/* In case the m-channel is zero, do not remap this pixel in any way */
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    30
					if (src->m == 0) {
6969
3d6722f0e3bd (svn r10225) -Codechange: move common Colour routines for 32bpp to the base class (and nick it colour, not color)
truelight
parents: 6959
diff changeset
    31
						if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
6889
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    32
					} else {
6969
3d6722f0e3bd (svn r10225) -Codechange: move common Colour routines for 32bpp to the base class (and nick it colour, not color)
truelight
parents: 6959
diff changeset
    33
						if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->LookupColourInPalette(bp->remap[src->m]), src->a, *dst);
6889
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    34
					}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    35
					break;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    36
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    37
				case BM_TRANSPARENT:
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    38
					/* TODO -- We make an assumption here that the remap in fact is transparency, not some color.
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    39
					 *  This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    40
					 *  we produce a result the newgrf maker didn't expect ;) */
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    41
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    42
					/* Make the current color a bit more black, so it looks like this image is transparent */
7552
56aa62dba204 (svn r11076) -Fix: MakeTransparent of 32bpp blitter used 0..100; using 0..255 makes it much faster (frosch)
truelight
parents: 6969
diff changeset
    43
					if (src->a != 0) *dst = MakeTransparent(*dst, 192);
6889
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    44
					break;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    45
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    46
				default:
6969
3d6722f0e3bd (svn r10225) -Codechange: move common Colour routines for 32bpp to the base class (and nick it colour, not color)
truelight
parents: 6959
diff changeset
    47
					if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
6889
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    48
					break;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    49
			}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    50
			dst++;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    51
			src += ScaleByZoom(1, zoom);
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    52
		}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    53
	}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    54
}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    55
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    56
void Blitter_32bppSimple::DrawColorMappingRect(void *dst, int width, int height, int pal)
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    57
{
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    58
	uint32 *udst = (uint32 *)dst;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    59
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    60
	if (pal == PALETTE_TO_TRANSPARENT) {
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    61
		do {
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    62
			for (int i = 0; i != width; i++) {
7552
56aa62dba204 (svn r11076) -Fix: MakeTransparent of 32bpp blitter used 0..100; using 0..255 makes it much faster (frosch)
truelight
parents: 6969
diff changeset
    63
				*udst = MakeTransparent(*udst, 154);
6889
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    64
				udst++;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    65
			}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    66
			udst = udst - width + _screen.pitch;
6959
8c5ef1825831 (svn r10215) -Fix r10214: forgot 2 cases of the same mistake
truelight
parents: 6937
diff changeset
    67
		} while (--height);
6889
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    68
		return;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    69
	}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    70
	if (pal == PALETTE_TO_STRUCT_GREY) {
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    71
		do {
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    72
			for (int i = 0; i != width; i++) {
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    73
				*udst = MakeGrey(*udst);
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    74
				udst++;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    75
			}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    76
			udst = udst - width + _screen.pitch;
6959
8c5ef1825831 (svn r10215) -Fix r10214: forgot 2 cases of the same mistake
truelight
parents: 6937
diff changeset
    77
		} while (--height);
6889
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    78
		return;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    79
	}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    80
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    81
	DEBUG(misc, 0, "32bpp blitter doesn't know how to draw this color table ('%d')", pal);
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    82
}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    83
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    84
Sprite *Blitter_32bppSimple::Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator)
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    85
{
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    86
	Sprite *dest_sprite;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    87
	SpriteLoader::CommonPixel *dst;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    88
	dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sprite->height * sprite->width * sizeof(SpriteLoader::CommonPixel));
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    89
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    90
	dest_sprite->height = sprite->height;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    91
	dest_sprite->width  = sprite->width;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    92
	dest_sprite->x_offs = sprite->x_offs;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    93
	dest_sprite->y_offs = sprite->y_offs;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    94
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    95
	dst = (SpriteLoader::CommonPixel *)dest_sprite->data;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    96
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    97
	memcpy(dst, sprite->data, sprite->height * sprite->width * sizeof(SpriteLoader::CommonPixel));
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    98
	for (int i = 0; i < sprite->height * sprite->width; i++) {
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    99
		if (dst[i].m != 0) {
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
   100
			/* Pre-convert the mapping channel to a RGB value */
6937
40c760fcf1f6 (svn r10190) -Codechange: merged renderer and blitter to one single class API: blitter
truelight
parents: 6895
diff changeset
   101
			uint color = this->LookupColourInPalette(dst[i].m);
6889
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
   102
			dst[i].r = GB(color, 16, 8);
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
   103
			dst[i].g = GB(color, 8,  8);
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
   104
			dst[i].b = GB(color, 0,  8);
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
   105
		}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
   106
	}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
   107
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
   108
	return dest_sprite;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
   109
}