src/blitter/8bpp_optimized.cpp
author rubidium
Wed, 17 Dec 2008 23:08:11 +0000
changeset 10434 3659467c844c
parent 9573 5f9fa05daadf
permissions -rw-r--r--
(svn r14687) -Change: log all configure errors to config.log
6872
0a4a20ef71c3 (svn r10113) -Fix (r10092): Missing svn properties and some Id/@file comments
peter1138
parents: 6860
diff changeset
     1
/* $Id$ */
0a4a20ef71c3 (svn r10113) -Fix (r10092): Missing svn properties and some Id/@file comments
peter1138
parents: 6860
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 8130
diff changeset
     3
/** @file 8bpp_optimized.cpp Implementation of the optimized 8 bpp blitter. */
6872
0a4a20ef71c3 (svn r10113) -Fix (r10092): Missing svn properties and some Id/@file comments
peter1138
parents: 6860
diff changeset
     4
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
     5
#include "../stdafx.h"
8123
ce31d2843a95 (svn r11684) -Codechange: split gfx.h in a type and functional header.
rubidium
parents: 8095
diff changeset
     6
#include "../zoom_func.h"
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
     7
#include "../debug.h"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
     8
#include "../core/alloc_func.hpp"
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
     9
#include "../core/math_func.hpp"
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    10
#include "8bpp_optimized.hpp"
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    11
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    12
static FBlitter_8bppOptimized iFBlitter_8bppOptimized;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    13
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    14
void Blitter_8bppOptimized::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    15
{
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    16
	/* Find the offset of this zoom-level */
9542
bc9789270025 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 9437
diff changeset
    17
	const SpriteData *sprite_src = (const SpriteData *)bp->sprite;
bc9789270025 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 9437
diff changeset
    18
	uint offset = sprite_src->offset[zoom];
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    19
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    20
	/* Find where to start reading in the source sprite */
9542
bc9789270025 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 9437
diff changeset
    21
	const uint8 *src = sprite_src->data + offset;
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
    22
	uint8 *dst_line = (uint8 *)bp->dst + bp->top * bp->pitch + bp->left;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    23
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    24
	/* Skip over the top lines in the source image */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    25
	for (int y = 0; y < bp->skip_top; y++) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    26
		for (;;) {
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
    27
			uint trans = *src++;
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
    28
			uint pixels = *src++;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    29
			if (trans == 0 && pixels == 0) break;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    30
			src += pixels;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    31
		}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    32
	}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    33
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
    34
	const uint8 *src_next = src;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    35
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    36
	for (int y = 0; y < bp->height; y++) {
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
    37
		uint8 *dst = dst_line;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    38
		dst_line += bp->pitch;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    39
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    40
		uint skip_left = bp->skip_left;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    41
		int width = bp->width;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    42
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    43
		for (;;) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    44
			src = src_next;
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
    45
			uint trans = *src++;
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
    46
			uint pixels = *src++;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    47
			src_next = src + pixels;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    48
			if (trans == 0 && pixels == 0) break;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    49
			if (width <= 0) continue;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    50
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    51
			if (skip_left != 0) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    52
				if (skip_left < trans) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    53
					trans -= skip_left;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    54
					skip_left = 0;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    55
				} else {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    56
					skip_left -= trans;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    57
					trans = 0;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    58
				}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    59
				if (skip_left < pixels) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    60
					src += skip_left;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    61
					pixels -= skip_left;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    62
					skip_left = 0;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    63
				} else {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    64
					src += pixels;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    65
					skip_left -= pixels;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    66
					pixels = 0;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    67
				}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    68
			}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    69
			if (skip_left != 0) continue;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    70
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    71
			/* Skip transparent pixels */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    72
			dst += trans;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    73
			width -= trans;
9573
5f9fa05daadf (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 9542
diff changeset
    74
			if (width <= 0 || pixels == 0) continue;
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
    75
			pixels = min<uint>(pixels, (uint)width);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    76
			width -= pixels;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    77
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    78
			switch (mode) {
9573
5f9fa05daadf (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 9542
diff changeset
    79
				case BM_COLOUR_REMAP: {
5f9fa05daadf (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 9542
diff changeset
    80
					const uint8 *remap = bp->remap;
5f9fa05daadf (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 9542
diff changeset
    81
					do {
5f9fa05daadf (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 9542
diff changeset
    82
						uint m = remap[*src];
5f9fa05daadf (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 9542
diff changeset
    83
						if (m != 0) *dst = m;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    84
						dst++; src++;
9573
5f9fa05daadf (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 9542
diff changeset
    85
					} while (--pixels != 0);
5f9fa05daadf (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 9542
diff changeset
    86
				} break;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    87
9573
5f9fa05daadf (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 9542
diff changeset
    88
				case BM_TRANSPARENT: {
5f9fa05daadf (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 9542
diff changeset
    89
					const uint8 *remap = bp->remap;
5f9fa05daadf (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 9542
diff changeset
    90
					src += pixels;
5f9fa05daadf (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 9542
diff changeset
    91
					do {
5f9fa05daadf (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 9542
diff changeset
    92
						*dst = remap[*dst];
5f9fa05daadf (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 9542
diff changeset
    93
						dst++;
5f9fa05daadf (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 9542
diff changeset
    94
					} while (--pixels != 0);
5f9fa05daadf (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 9542
diff changeset
    95
				} break;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    96
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    97
				default:
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    98
					memcpy(dst, src, pixels);
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    99
					dst += pixels; src += pixels;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   100
					break;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   101
			}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   102
		}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   103
	}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   104
}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   105
6856
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6855
diff changeset
   106
Sprite *Blitter_8bppOptimized::Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator)
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   107
{
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   108
	/* Make memory for all zoom-levels */
9542
bc9789270025 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 9437
diff changeset
   109
	uint memory = sizeof(SpriteData);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   110
8095
f834186120af (svn r11656) -Codechange: add ZOOM_LVL_BEGIN and postfix operators so ZoomLevel can be used in some iterations
smatz
parents: 7088
diff changeset
   111
	for (ZoomLevel i = ZOOM_LVL_BEGIN; i < ZOOM_LVL_END; i++) {
f834186120af (svn r11656) -Codechange: add ZOOM_LVL_BEGIN and postfix operators so ZoomLevel can be used in some iterations
smatz
parents: 7088
diff changeset
   112
		memory += UnScaleByZoom(sprite->height, i) * UnScaleByZoom(sprite->width, i);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   113
	}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   114
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   115
	/* We have no idea how much memory we really need, so just guess something */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   116
	memory *= 5;
9542
bc9789270025 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 9437
diff changeset
   117
	SpriteData *temp_dst = (SpriteData *)MallocT<byte>(memory);
bc9789270025 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 9437
diff changeset
   118
	byte *dst = temp_dst->data;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   119
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   120
	/* Make the sprites per zoom-level */
8095
f834186120af (svn r11656) -Codechange: add ZOOM_LVL_BEGIN and postfix operators so ZoomLevel can be used in some iterations
smatz
parents: 7088
diff changeset
   121
	for (ZoomLevel i = ZOOM_LVL_BEGIN; i < ZOOM_LVL_END; i++) {
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   122
		/* Store the index table */
9542
bc9789270025 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 9437
diff changeset
   123
		uint offset = dst - temp_dst->data;
bc9789270025 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 9437
diff changeset
   124
		temp_dst->offset[i] = offset;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   125
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   126
		/* cache values, because compiler can't cache it */
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   127
		int scaled_height = UnScaleByZoom(sprite->height, i);
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   128
		int scaled_width  = UnScaleByZoom(sprite->width,  i);
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   129
		int scaled_1      =   ScaleByZoom(1,              i);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   130
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   131
		for (int y = 0; y < scaled_height; y++) {
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   132
			uint trans = 0;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   133
			uint pixels = 0;
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   134
			uint last_colour = 0;
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   135
			byte *count_dst = NULL;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   136
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   137
			/* Store the scaled image */
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   138
			const SpriteLoader::CommonPixel *src = &sprite->data[ScaleByZoom(y, i) * sprite->width];
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   139
			const SpriteLoader::CommonPixel *src_end = &src[sprite->width];
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   140
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   141
			for (int x = 0; x < scaled_width; x++) {
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   142
				uint colour = 0;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   143
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   144
				/* Get the color keeping in mind the zoom-level */
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   145
				for (int j = 0; j < scaled_1; j++) {
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   146
					if (src->m != 0) colour = src->m;
6855
ac13e9ecd231 (svn r10095) -Fix: avoid reading outside the buffer because of scaling problems
truelight
parents: 6852
diff changeset
   147
					/* Because of the scaling it might happen we read outside the buffer. Avoid that. */
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   148
					if (++src == src_end) break;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   149
				}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   150
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   151
				if (last_colour == 0 || colour == 0 || pixels == 255) {
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   152
					if (count_dst != NULL) {
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   153
						/* Write how many non-transparent bytes we get */
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   154
						*count_dst = pixels;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   155
						pixels = 0;
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   156
						count_dst = NULL;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   157
					}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   158
					/* As long as we find transparency bytes, keep counting */
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   159
					if (colour == 0) {
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   160
						last_colour = 0;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   161
						trans++;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   162
						continue;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   163
					}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   164
					/* No longer transparency, so write the amount of transparent bytes */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   165
					*dst = trans;
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   166
					dst++;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   167
					trans = 0;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   168
					/* Reserve a byte for the pixel counter */
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   169
					count_dst = dst;
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   170
					dst++;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   171
				}
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   172
				last_colour = colour;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   173
				pixels++;
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   174
				*dst = colour;
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   175
				dst++;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   176
			}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   177
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   178
			if (count_dst != NULL) *count_dst = pixels;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   179
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   180
			/* Write line-ending */
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   181
			*dst = 0; dst++;
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   182
			*dst = 0; dst++;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   183
		}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   184
	}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   185
9542
bc9789270025 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 9437
diff changeset
   186
	uint size = dst - (byte *)temp_dst;
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   187
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   188
	/* Safety check, to make sure we guessed the size correctly */
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   189
	assert(size < memory);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   190
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   191
	/* Allocate the exact amount of memory we need */
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   192
	Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + size);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   193
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   194
	dest_sprite->height = sprite->height;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   195
	dest_sprite->width  = sprite->width;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   196
	dest_sprite->x_offs = sprite->x_offs;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   197
	dest_sprite->y_offs = sprite->y_offs;
9437
d822addc9d2a (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 9111
diff changeset
   198
	memcpy(dest_sprite->data, temp_dst, size);
7088
70852959cb77 (svn r10355) -Fix (r10092): memory leak when encoding (reading) sprites.
rubidium
parents: 6978
diff changeset
   199
	free(temp_dst);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   200
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   201
	return dest_sprite;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   202
}