src/blitter/8bpp_optimized.cpp
author bjarni
Thu, 19 Jun 2008 17:54:23 +0000
changeset 9561 f236daaaf93a
parent 9542 bc9789270025
child 9573 5f9fa05daadf
permissions -rw-r--r--
(svn r13584) -Fix: [OSX] Fixed issue where 10.5 failed to switch to fullscreen
This is done by selecting the 32bpp-anim blitter by default as it seems Apple removed some 8bpp support
Since this is done at runtime the same binary will still select 8bpp on 10.3 and 10.4
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;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    74
			if (width <= 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) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    79
				case BM_COLOUR_REMAP:
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    80
					for (uint x = 0; x < pixels; x++) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    81
						if (bp->remap[*src] != 0) *dst = bp->remap[*src];
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    82
						dst++; src++;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    83
					}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    84
					break;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    85
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    86
				case BM_TRANSPARENT:
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    87
					for (uint x = 0; x < pixels; x++) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    88
						*dst = bp->remap[*dst];
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    89
						dst++; src++;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    90
					}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    91
					break;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    92
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    93
				default:
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    94
					memcpy(dst, src, pixels);
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    95
					dst += pixels; src += pixels;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    96
					break;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    97
			}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    98
		}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    99
	}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   100
}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   101
6856
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6855
diff changeset
   102
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
   103
{
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
   104
	/* 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
   105
	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
   106
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
   107
	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
   108
		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
   109
	}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   110
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   111
	/* 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
   112
	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
   113
	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
   114
	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
   115
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   116
	/* 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
   117
	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
   118
		/* 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
   119
		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
   120
		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
   121
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
   122
		/* 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
   123
		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
   124
		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
   125
		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
   126
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
   127
		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
   128
			uint trans = 0;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   129
			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
   130
			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
   131
			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
   132
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
   133
			/* 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
   134
			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
   135
			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
   136
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
			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
   138
				uint colour = 0;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   139
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   140
				/* 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
   141
				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
   142
					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
   143
					/* 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
   144
					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
   145
				}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   146
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
   147
				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
   148
					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
   149
						/* 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
   150
						*count_dst = pixels;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   151
						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
   152
						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
					}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   154
					/* 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
   155
					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
   156
						last_colour = 0;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   157
						trans++;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   158
						continue;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   159
					}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   160
					/* 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
   161
					*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
   162
					dst++;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   163
					trans = 0;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   164
					/* 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
   165
					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
   166
					dst++;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   167
				}
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
   168
				last_colour = colour;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   169
				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
   170
				*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
   171
				dst++;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   172
			}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   173
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
			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
   175
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   176
			/* 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
   177
			*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
   178
			*dst = 0; dst++;
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
	}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   181
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
   182
	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
   183
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   184
	/* 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
   185
	assert(size < memory);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   186
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   187
	/* 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
   188
	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
   189
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   190
	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
   191
	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
   192
	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
   193
	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
   194
	memcpy(dest_sprite->data, temp_dst, size);
7088
70852959cb77 (svn r10355) -Fix (r10092): memory leak when encoding (reading) sprites.
rubidium
parents: 6978
diff changeset
   195
	free(temp_dst);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   196
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   197
	return dest_sprite;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   198
}