src/blitter/8bpp_optimized.cpp
author Tero Marttila <terom@fixme.fi>
Fri, 18 Jul 2008 22:41:08 +0300
changeset 11177 6d9a43c48924
parent 11046 b26cfd2a54f6
permissions -rw-r--r--
set the GRFConfig's next ptr to NULL
7368
c8585746a177 (svn r10113) -Fix (r10092): Missing svn properties and some Id/@file comments
peter1138
parents: 7356
diff changeset
     1
/* $Id$ */
c8585746a177 (svn r10113) -Fix (r10092): Missing svn properties and some Id/@file comments
peter1138
parents: 7356
diff changeset
     2
10429
1b99254f9607 (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: 8626
diff changeset
     3
/** @file 8bpp_optimized.cpp Implementation of the optimized 8 bpp blitter. */
7368
c8585746a177 (svn r10113) -Fix (r10092): Missing svn properties and some Id/@file comments
peter1138
parents: 7356
diff changeset
     4
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
     5
#include "../stdafx.h"
8619
c2434269c3eb (svn r11684) -Codechange: split gfx.h in a type and functional header.
rubidium
parents: 8591
diff changeset
     6
#include "../zoom_func.h"
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
     7
#include "../debug.h"
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
     8
#include "../core/alloc_func.hpp"
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
     9
#include "../core/math_func.hpp"
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    10
#include "8bpp_optimized.hpp"
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    11
becce3f57dc7 (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;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    13
becce3f57dc7 (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)
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    15
{
becce3f57dc7 (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 */
10997
968df7476121 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 10802
diff changeset
    17
	const SpriteData *sprite_src = (const SpriteData *)bp->sprite;
968df7476121 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 10802
diff changeset
    18
	uint offset = sprite_src->offset[zoom];
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    19
becce3f57dc7 (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 */
10997
968df7476121 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 10802
diff changeset
    21
	const uint8 *src = sprite_src->data + offset;
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
    22
	uint8 *dst_line = (uint8 *)bp->dst + bp->top * bp->pitch + bp->left;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    23
becce3f57dc7 (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 */
becce3f57dc7 (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++) {
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    26
		for (;;) {
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
    27
			uint trans = *src++;
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
    28
			uint pixels = *src++;
7348
becce3f57dc7 (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;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    30
			src += pixels;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    31
		}
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    32
	}
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    33
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
    34
	const uint8 *src_next = src;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    35
becce3f57dc7 (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++) {
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
    37
		uint8 *dst = dst_line;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    38
		dst_line += bp->pitch;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    39
becce3f57dc7 (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;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    41
		int width = bp->width;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    42
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    43
		for (;;) {
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    44
			src = src_next;
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
    45
			uint trans = *src++;
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
    46
			uint pixels = *src++;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    47
			src_next = src + pixels;
becce3f57dc7 (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;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    49
			if (width <= 0) continue;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    50
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    51
			if (skip_left != 0) {
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    52
				if (skip_left < trans) {
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    53
					trans -= skip_left;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    54
					skip_left = 0;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    55
				} else {
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    56
					skip_left -= trans;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    57
					trans = 0;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    58
				}
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    59
				if (skip_left < pixels) {
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    60
					src += skip_left;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    61
					pixels -= skip_left;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    62
					skip_left = 0;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    63
				} else {
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    64
					src += pixels;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    65
					skip_left -= pixels;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    66
					pixels = 0;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    67
				}
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    68
			}
becce3f57dc7 (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;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    70
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    71
			/* Skip transparent pixels */
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    72
			dst += trans;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    73
			width -= trans;
11046
b26cfd2a54f6 (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 10997
diff changeset
    74
			if (width <= 0 || pixels == 0) continue;
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
    75
			pixels = min<uint>(pixels, (uint)width);
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    76
			width -= pixels;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    77
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    78
			switch (mode) {
11046
b26cfd2a54f6 (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 10997
diff changeset
    79
				case BM_COLOUR_REMAP: {
b26cfd2a54f6 (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 10997
diff changeset
    80
					const uint8 *remap = bp->remap;
b26cfd2a54f6 (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 10997
diff changeset
    81
					do {
b26cfd2a54f6 (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 10997
diff changeset
    82
						uint m = remap[*src];
b26cfd2a54f6 (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 10997
diff changeset
    83
						if (m != 0) *dst = m;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    84
						dst++; src++;
11046
b26cfd2a54f6 (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 10997
diff changeset
    85
					} while (--pixels != 0);
b26cfd2a54f6 (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 10997
diff changeset
    86
				} break;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    87
11046
b26cfd2a54f6 (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 10997
diff changeset
    88
				case BM_TRANSPARENT: {
b26cfd2a54f6 (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 10997
diff changeset
    89
					const uint8 *remap = bp->remap;
b26cfd2a54f6 (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 10997
diff changeset
    90
					src += pixels;
b26cfd2a54f6 (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 10997
diff changeset
    91
					do {
b26cfd2a54f6 (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 10997
diff changeset
    92
						*dst = remap[*dst];
b26cfd2a54f6 (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 10997
diff changeset
    93
						dst++;
b26cfd2a54f6 (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 10997
diff changeset
    94
					} while (--pixels != 0);
b26cfd2a54f6 (svn r13602) -Codechange: little speedup for 8bpp-optimized blitter
smatz
parents: 10997
diff changeset
    95
				} break;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    96
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    97
				default:
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    98
					memcpy(dst, src, pixels);
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    99
					dst += pixels; src += pixels;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   100
					break;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   101
			}
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   102
		}
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   103
	}
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   104
}
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   105
7352
e2e8432018f6 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 7351
diff changeset
   106
Sprite *Blitter_8bppOptimized::Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator)
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   107
{
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   108
	/* Make memory for all zoom-levels */
10997
968df7476121 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 10802
diff changeset
   109
	uint memory = sizeof(SpriteData);
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   110
8591
46eca2eb57ba (svn r11656) -Codechange: add ZOOM_LVL_BEGIN and postfix operators so ZoomLevel can be used in some iterations
smatz
parents: 7584
diff changeset
   111
	for (ZoomLevel i = ZOOM_LVL_BEGIN; i < ZOOM_LVL_END; i++) {
46eca2eb57ba (svn r11656) -Codechange: add ZOOM_LVL_BEGIN and postfix operators so ZoomLevel can be used in some iterations
smatz
parents: 7584
diff changeset
   112
		memory += UnScaleByZoom(sprite->height, i) * UnScaleByZoom(sprite->width, i);
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   113
	}
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   114
becce3f57dc7 (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 */
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   116
	memory *= 5;
10997
968df7476121 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 10802
diff changeset
   117
	SpriteData *temp_dst = (SpriteData *)MallocT<byte>(memory);
968df7476121 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 10802
diff changeset
   118
	byte *dst = temp_dst->data;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   119
becce3f57dc7 (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 */
8591
46eca2eb57ba (svn r11656) -Codechange: add ZOOM_LVL_BEGIN and postfix operators so ZoomLevel can be used in some iterations
smatz
parents: 7584
diff changeset
   121
	for (ZoomLevel i = ZOOM_LVL_BEGIN; i < ZOOM_LVL_END; i++) {
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   122
		/* Store the index table */
10997
968df7476121 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 10802
diff changeset
   123
		uint offset = dst - temp_dst->data;
968df7476121 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 10802
diff changeset
   124
		temp_dst->offset[i] = offset;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   125
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   126
		/* cache values, because compiler can't cache it */
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   127
		int scaled_height = UnScaleByZoom(sprite->height, i);
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   128
		int scaled_width  = UnScaleByZoom(sprite->width,  i);
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   129
		int scaled_1      =   ScaleByZoom(1,              i);
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   130
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   131
		for (int y = 0; y < scaled_height; y++) {
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   132
			uint trans = 0;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   133
			uint pixels = 0;
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   134
			uint last_colour = 0;
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   135
			byte *count_dst = NULL;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   136
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   137
			/* Store the scaled image */
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   138
			const SpriteLoader::CommonPixel *src = &sprite->data[ScaleByZoom(y, i) * sprite->width];
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   139
			const SpriteLoader::CommonPixel *src_end = &src[sprite->width];
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   140
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   141
			for (int x = 0; x < scaled_width; x++) {
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   142
				uint colour = 0;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   143
becce3f57dc7 (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 */
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   145
				for (int j = 0; j < scaled_1; j++) {
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   146
					if (src->m != 0) colour = src->m;
7351
c2828be69292 (svn r10095) -Fix: avoid reading outside the buffer because of scaling problems
truelight
parents: 7348
diff changeset
   147
					/* Because of the scaling it might happen we read outside the buffer. Avoid that. */
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   148
					if (++src == src_end) break;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   149
				}
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   150
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   151
				if (last_colour == 0 || colour == 0 || pixels == 255) {
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   152
					if (count_dst != NULL) {
7348
becce3f57dc7 (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 */
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   154
						*count_dst = pixels;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   155
						pixels = 0;
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   156
						count_dst = NULL;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   157
					}
becce3f57dc7 (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 */
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   159
					if (colour == 0) {
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   160
						last_colour = 0;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   161
						trans++;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   162
						continue;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   163
					}
becce3f57dc7 (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 */
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   165
					*dst = trans;
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   166
					dst++;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   167
					trans = 0;
becce3f57dc7 (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 */
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   169
					count_dst = dst;
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   170
					dst++;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   171
				}
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   172
				last_colour = colour;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   173
				pixels++;
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   174
				*dst = colour;
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   175
				dst++;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   176
			}
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   177
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   178
			if (count_dst != NULL) *count_dst = pixels;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   179
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   180
			/* Write line-ending */
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   181
			*dst = 0; dst++;
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   182
			*dst = 0; dst++;
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   183
		}
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   184
	}
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   185
10997
968df7476121 (svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents: 10802
diff changeset
   186
	uint size = dst - (byte *)temp_dst;
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   187
7348
becce3f57dc7 (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 */
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   189
	assert(size < memory);
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   190
becce3f57dc7 (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 */
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   192
	Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + size);
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   193
becce3f57dc7 (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;
becce3f57dc7 (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;
becce3f57dc7 (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;
becce3f57dc7 (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;
10802
ea3bd4482e56 (svn r13354) -Codechange: make 8bpp_optimized blitter ~25% faster in encoding and ~15% faster in drawing (depends on architecture)
smatz
parents: 10429
diff changeset
   198
	memcpy(dest_sprite->data, temp_dst, size);
7584
5b7981270971 (svn r10355) -Fix (r10092): memory leak when encoding (reading) sprites.
rubidium
parents: 7474
diff changeset
   199
	free(temp_dst);
7348
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   200
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   201
	return dest_sprite;
becce3f57dc7 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   202
}