src/blitter/32bpp_simple.cpp
author rubidium
Sun, 13 Jan 2008 01:21:35 +0000
changeset 8264 b1e85998c7d3
parent 8123 ce31d2843a95
child 9111 48ce04029fe4
permissions -rw-r--r--
(svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
8091
674be8638d74 (svn r11652) -Codechange: add the svn $ header for several files
smatz
parents: 7552
diff changeset
     1
/* $Id$ */
674be8638d74 (svn r11652) -Codechange: add the svn $ header for several files
smatz
parents: 7552
diff changeset
     2
6889
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
     3
#include "../stdafx.h"
8123
ce31d2843a95 (svn r11684) -Codechange: split gfx.h in a type and functional header.
rubidium
parents: 8091
diff changeset
     4
#include "../gfx_func.h"
ce31d2843a95 (svn r11684) -Codechange: split gfx.h in a type and functional header.
rubidium
parents: 8091
diff changeset
     5
#include "../zoom_func.h"
6889
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
     6
#include "../debug.h"
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8123
diff changeset
     7
#include "32bpp_simple.hpp"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8123
diff changeset
     8
6889
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
     9
#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
    10
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    11
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
    12
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    13
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
    14
{
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    15
	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
    16
	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
    17
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    18
	/* 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
    19
	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
    20
	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
    21
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    22
	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
    23
		dst = dst_line;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    24
		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
    25
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    26
		src = src_line;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    27
		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
    28
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    29
		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
    30
			switch (mode) {
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    31
				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
    32
					/* 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
    33
					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
    34
						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
    35
					} 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
    36
						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
    37
					}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    38
					break;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    39
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    40
				case BM_TRANSPARENT:
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    41
					/* 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
    42
					 *  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
    43
					 *  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
    44
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    45
					/* 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
    46
					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
    47
					break;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    48
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    49
				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
    50
					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
    51
					break;
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
			dst++;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    54
			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
    55
		}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    56
	}
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
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    59
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
    60
{
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    61
	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
    62
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    63
	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
    64
		do {
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    65
			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
    66
				*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
    67
				udst++;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    68
			}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    69
			udst = udst - width + _screen.pitch;
6959
8c5ef1825831 (svn r10215) -Fix r10214: forgot 2 cases of the same mistake
truelight
parents: 6937
diff changeset
    70
		} while (--height);
6889
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    71
		return;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    72
	}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    73
	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
    74
		do {
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    75
			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
    76
				*udst = MakeGrey(*udst);
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    77
				udst++;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    78
			}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    79
			udst = udst - width + _screen.pitch;
6959
8c5ef1825831 (svn r10215) -Fix r10214: forgot 2 cases of the same mistake
truelight
parents: 6937
diff changeset
    80
		} while (--height);
6889
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    81
		return;
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
	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
    85
}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    86
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    87
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
    88
{
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    89
	Sprite *dest_sprite;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    90
	SpriteLoader::CommonPixel *dst;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    91
	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
    92
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    93
	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
    94
	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
    95
	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
    96
	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
    97
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
    98
	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
    99
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
   100
	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
   101
	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
   102
		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
   103
			/* 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
   104
			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
   105
			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
   106
			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
   107
			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
   108
		}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
   109
	}
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
   110
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
   111
	return dest_sprite;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents:
diff changeset
   112
}