src/spriteloader/grf.cpp
author peter1138
Sun, 03 Aug 2008 17:35:08 +0000
changeset 9834 7474149531c1
parent 9390 88d36f907e96
child 10031 a787bda1be6f
permissions -rw-r--r--
(svn r13977) -Codechange: Let ResizeWindowForWidget() handle hidden (zero height or zero width) widgets.
6872
0a4a20ef71c3 (svn r10113) -Fix (r10092): Missing svn properties and some Id/@file comments
peter1138
parents: 6852
diff changeset
     1
/* $Id$ */
0a4a20ef71c3 (svn r10113) -Fix (r10092): Missing svn properties and some Id/@file comments
peter1138
parents: 6852
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: 8374
diff changeset
     3
/** @file grf.cpp Reading graphics data from (New)GRF files. */
6872
0a4a20ef71c3 (svn r10113) -Fix (r10092): Missing svn properties and some Id/@file comments
peter1138
parents: 6852
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: 7570
diff changeset
     6
#include "../gfx_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 "../fileio.h"
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
     8
#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
     9
#include "../core/alloc_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 "grf.hpp"
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    11
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9111
diff changeset
    12
bool SpriteLoaderGrf::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos)
6852
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
	/* Open the right file and go to the correct position */
7570
5d5d9b6af0ef (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 6896
diff changeset
    15
	FioSeekToFile(file_slot, file_pos);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    16
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    17
	/* Read the size and type */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    18
	int num = FioReadWord();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    19
	byte type = FioReadByte();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    20
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    21
	/* Type 0xFF indicates either a colormap or some other non-sprite info; we do not handle them here */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    22
	if (type == 0xFF) return false;
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
	sprite->height = FioReadByte();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    25
	sprite->width  = FioReadWord();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    26
	sprite->x_offs = FioReadWord();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    27
	sprite->y_offs = FioReadWord();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    28
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    29
	/* 0x02 indicates it is a compressed sprite, so we can't rely on 'num' to be valid.
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    30
	 *  In case it is uncompressed, the size is 'num' - 8 (header-size). */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    31
	num = (type & 0x02) ? sprite->width * sprite->height : num - 8;
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
	/* XXX -- We should use a pre-located memory segment for this, malloc/free is pretty expensive */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    34
	byte *dest_orig = MallocT<byte>(num);
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    35
	byte *dest = dest_orig;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    36
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    37
	/* Read the file, which has some kind of compression */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    38
	while (num > 0) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    39
		int8 code = FioReadByte();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    40
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    41
		if (code >= 0) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    42
			/* Plain bytes to read */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    43
			int size = (code == 0) ? 0x80 : code;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    44
			num -= size;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    45
			for (; size > 0; size--) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    46
				*dest = FioReadByte();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    47
				dest++;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    48
			}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    49
		} else {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    50
			/* Copy bytes from earlier in the sprite */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    51
			const uint data_offset = ((code & 7) << 8) | FioReadByte();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    52
			int size = -(code >> 3);
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    53
			num -= size;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    54
			for (; size > 0; size--) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    55
				*dest = *(dest - data_offset);
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    56
				dest++;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    57
			}
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
	}
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    60
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    61
	assert(num == 0);
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    62
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    63
	sprite->data = CallocT<SpriteLoader::CommonPixel>(sprite->width * sprite->height);
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    64
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    65
	/* When there are transparency pixels, this format has an other trick.. decode it */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    66
	if (type & 0x08) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    67
		for (int y = 0; y < sprite->height; y++) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    68
			bool last_item = false;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    69
			/* Look up in the header-table where the real data is stored for this row */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    70
			int offset = (dest_orig[y * 2 + 1] << 8) | dest_orig[y * 2];
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    71
			/* Go to that row */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    72
			dest = &dest_orig[offset];
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    73
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    74
			do {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    75
				SpriteLoader::CommonPixel *data;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    76
				/* Read the header:
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    77
				 *  0 .. 14  - length
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    78
				 *  15       - last_item
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    79
				 *  16 .. 31 - transparency bytes */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    80
				last_item  = ((*dest) & 0x80) != 0;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    81
				int length =  (*dest++) & 0x7F;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    82
				int skip   =   *dest++;
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
				data = &sprite->data[y * sprite->width + skip];
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
				for (int x = 0; x < length; x++) {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    87
					data->m = *dest;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    88
					dest++;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    89
					data++;
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
			} while (!last_item);
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
	} else {
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    94
		dest = dest_orig;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    95
		for (int i = 0; i < sprite->width * sprite->height; i++)
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
    96
			sprite->data[i].m = dest[i];
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
6889
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents: 6872
diff changeset
    99
	/* Make sure to mark all transparent pixels transparent on the alpha channel too */
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents: 6872
diff changeset
   100
	for (int i = 0; i < sprite->width * sprite->height; i++)
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents: 6872
diff changeset
   101
		if (sprite->data[i].m != 0) sprite->data[i].a = 0xFF;
2160bf28040b (svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
truelight
parents: 6872
diff changeset
   102
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   103
	free(dest_orig);
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   104
	return true;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents:
diff changeset
   105
}