src/spritecache.cpp
author rubidium
Thu, 18 Dec 2008 12:23:08 +0000
changeset 10436 8d3a9fbe8f19
parent 10359 64e5bdedbbfa
permissions -rw-r--r--
(svn r14689) -Change: make configure die on commonly made user mistakes, like not having SDL development files or zlib headers installed; you can still compile a dedicated server or a binary without zlib, but you have to explicitly force it.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
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: 8741
diff changeset
     3
/** @file spritecache.cpp Caching of sprites. */
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1844
diff changeset
     6
#include "openttd.h"
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6869
diff changeset
     7
#include "variables.h"
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1250
diff changeset
     8
#include "debug.h"
1349
15979a2e9001 (svn r1853) Move spritecache function declarations into a header of their own and use SpriteID as parameter type where appropriate
tron
parents: 1348
diff changeset
     9
#include "spritecache.h"
10039
1f236afd6cd1 (svn r14199) -Codechange: split fileio.h into fileio_type.h and fileio_func.h so not everything that includes saveload.h needs to include everything else too.
rubidium
parents: 10034
diff changeset
    10
#include "fileio_func.h"
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents: 6803
diff changeset
    11
#include "spriteloader/grf.hpp"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8113
diff changeset
    12
#include "core/alloc_func.hpp"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
    13
#include "core/math_func.hpp"
10062
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
    14
#include "gfx_func.h"
6901
ad8e8b93060d (svn r10148) -Fix r10143: make --without-png to compile again
truelight
parents: 6896
diff changeset
    15
#ifdef WITH_PNG
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6869
diff changeset
    16
#include "spriteloader/png.hpp"
6901
ad8e8b93060d (svn r10148) -Fix r10143: make --without-png to compile again
truelight
parents: 6896
diff changeset
    17
#endif /* WITH_PNG */
6937
40c760fcf1f6 (svn r10190) -Codechange: merged renderer and blitter to one single class API: blitter
truelight
parents: 6908
diff changeset
    18
#include "blitter/factory.hpp"
6803
9803e56a8d9c (svn r10042) -Codechange: Replace hardcoded spritecache size with a configuration
peter1138
parents: 6799
diff changeset
    19
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8214
diff changeset
    20
#include "table/sprites.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8214
diff changeset
    21
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents: 6803
diff changeset
    22
/* Default of 4MB spritecache */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents: 6803
diff changeset
    23
uint _sprite_cache_size = 4;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    26
struct SpriteCache {
8741
57b96b8b0d99 (svn r12436) -Cleanup: minor coding style
peter1138
parents: 8562
diff changeset
    27
	void *ptr;
9618
2a191ec24807 (svn r13674) -Fix [FS#2127]: crash when drawing a non-real sprite. The drawing of the non-real sprite is caused when two NewGRFs replace the same sprite and the first replaces it with a real sprite (and thus assumes it remains a real sprite) and the second replaces it with a non-real sprite. OpenTTD already looked at whether the sprite to load should be seen as a real or non-real sprite, but it failed to replace non-real sprites with a substitute real sprite when getting the sprite from the cache.
rubidium
parents: 9470
diff changeset
    28
	size_t file_pos;
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6869
diff changeset
    29
	uint32 id;
8373
7431d91527f2 (svn r11939) -Codechange: some type fixes and very initial steps into supporting NDS by default. Based on work by Dominik.
rubidium
parents: 8264
diff changeset
    30
	uint16 file_slot;
8741
57b96b8b0d99 (svn r12436) -Cleanup: minor coding style
peter1138
parents: 8562
diff changeset
    31
	int16 lru;
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
    32
	SpriteType type; ///< In some cases a single sprite is misused by two NewGRFs. Once as real sprite and once as recolour sprite. If the recolour sprite gets into the cache it might be drawn as real sprite which causes enormous trouble.
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    33
};
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    34
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    35
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    36
static uint _spritecache_items = 0;
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    37
static SpriteCache *_spritecache = NULL;
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    38
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    39
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    40
static inline SpriteCache *GetSpriteCache(uint index)
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    41
{
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    42
	return &_spritecache[index];
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    43
}
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    44
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    45
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    46
static SpriteCache *AllocateSpriteCache(uint index)
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    47
{
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    48
	if (index >= _spritecache_items) {
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    49
		/* Add another 1024 items to the 'pool' */
7927
3a3289a049f9 (svn r11480) -Codechange: Rename the function ALIGN fitting to the naming style
skidd13
parents: 7576
diff changeset
    50
		uint items = Align(index + 1, 1024);
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    51
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    52
		DEBUG(sprite, 4, "Increasing sprite cache to %d items (%d bytes)", items, items * sizeof(*_spritecache));
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    53
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
    54
		_spritecache = ReallocT(_spritecache, items);
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    55
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    56
		/* Reset the new items and update the count */
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    57
		memset(_spritecache + _spritecache_items, 0, (items - _spritecache_items) * sizeof(*_spritecache));
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    58
		_spritecache_items = items;
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    59
	}
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    60
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    61
	return GetSpriteCache(index);
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    62
}
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
    63
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    64
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    65
struct MemBlock {
9146
dbe2317185eb (svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx
parents: 9111
diff changeset
    66
	size_t size;
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    67
	byte data[VARARRAY_SIZE];
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    68
};
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    69
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
static uint _sprite_lru_counter;
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
    71
static MemBlock *_spritecache_ptr;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
static int _compact_cache_counter;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5962
diff changeset
    74
static void CompactSpriteCache();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
10032
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    76
/**
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    77
 * Skip the given amount of sprite graphics data.
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    78
 * @param type the type of sprite (compressed etc)
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    79
 * @param num the amount of sprites to skip
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    80
 */
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    81
void SkipSpriteData(byte type, uint16 num)
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    82
{
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    83
	if (type & 2) {
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    84
		FioSkipBytes(num);
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    85
	} else {
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    86
		while (num > 0) {
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    87
			int8 i = FioReadByte();
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    88
			if (i >= 0) {
10034
31baa7c207b8 (svn r14193) -Fix (r14191): don't put an unsigned 0x80 in a signed int8 as that's not what you want.
rubidium
parents: 10032
diff changeset
    89
				int size = (i == 0) ? 0x80 : i;
31baa7c207b8 (svn r14193) -Fix (r14191): don't put an unsigned 0x80 in a signed int8 as that's not what you want.
rubidium
parents: 10032
diff changeset
    90
				num -= size;
31baa7c207b8 (svn r14193) -Fix (r14191): don't put an unsigned 0x80 in a signed int8 as that's not what you want.
rubidium
parents: 10032
diff changeset
    91
				FioSkipBytes(size);
10032
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    92
			} else {
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    93
				i = -(i >> 3);
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    94
				num -= i;
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    95
				FioReadByte();
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    96
			}
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    97
		}
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    98
	}
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
    99
}
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
   100
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
   101
/**
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
   102
 * Read the sprite header data and then skip the real payload.
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
   103
 * @return true if the sprite is a pseudo sprite.
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
   104
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5962
diff changeset
   105
static bool ReadSpriteHeaderSkipData()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
{
2329
c4d9fc4006b1 (svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents: 2321
diff changeset
   107
	uint16 num = FioReadWord();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
	byte type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
2329
c4d9fc4006b1 (svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents: 2321
diff changeset
   110
	if (num == 0) return false;
c4d9fc4006b1 (svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents: 2321
diff changeset
   111
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   112
	type = FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
	if (type == 0xFF) {
2342
aae24c9661ba (svn r2868) Change the way NewGRFs are loaded: The loading process i no longer bolted onto the normal graphics loading.
tron
parents: 2340
diff changeset
   114
		FioSkipBytes(num);
5150
fe410b84b302 (svn r7242) -Fix: Don't load 1-byte pseudo sprites, as used in some NewGRF sets. If the sprite is ever drawn this will result in a "missing sprite" error instead of undefined misbehaviour leading to segmentation faults...
peter1138
parents: 4522
diff changeset
   115
		/* Some NewGRF files have "empty" pseudo-sprites which are 1
fe410b84b302 (svn r7242) -Fix: Don't load 1-byte pseudo sprites, as used in some NewGRF sets. If the sprite is ever drawn this will result in a "missing sprite" error instead of undefined misbehaviour leading to segmentation faults...
peter1138
parents: 4522
diff changeset
   116
		 * byte long. Catch these so the sprites won't be displayed. */
fe410b84b302 (svn r7242) -Fix: Don't load 1-byte pseudo sprites, as used in some NewGRF sets. If the sprite is ever drawn this will result in a "missing sprite" error instead of undefined misbehaviour leading to segmentation faults...
peter1138
parents: 4522
diff changeset
   117
		return num != 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   118
	}
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   119
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
	FioSkipBytes(7);
10032
1f6c6f3bea28 (svn r14191) -Codechange: unify the code to skip sprite payload (i.e. not the header).
rubidium
parents: 9737
diff changeset
   121
	SkipSpriteData(type, num - 8);
2329
c4d9fc4006b1 (svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents: 2321
diff changeset
   122
c4d9fc4006b1 (svn r2855) Make ReadSpriteHeaderSkipData() responsible for detecting the end of a grf file instead of its callers - this simplifies the code a bit
tron
parents: 2321
diff changeset
   123
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   125
3565
ef0a9ef56fa0 (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   126
/* Check if the given Sprite ID exists */
ef0a9ef56fa0 (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   127
bool SpriteExists(SpriteID id)
ef0a9ef56fa0 (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   128
{
ef0a9ef56fa0 (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   129
	/* Special case for Sprite ID zero -- its position is also 0... */
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   130
	if (id == 0) return true;
5702
f98beec96c66 (svn r8166) -Fix (r7797): Protect against out of bounds access to the sprite ptr
peter1138
parents: 5609
diff changeset
   131
	if (id >= _spritecache_items) return false;
7576
702396fd6908 (svn r11101) -Fix r11099: the check: file_pos == 0, no longer works; adjust the check with file_slot. This solves the ? sprites with autorail (nice catch Progman)
truelight
parents: 7570
diff changeset
   132
	return !(GetSpriteCache(id)->file_pos == 0 && GetSpriteCache(id)->file_slot == 0);
3565
ef0a9ef56fa0 (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   133
}
ef0a9ef56fa0 (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   134
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents: 6803
diff changeset
   135
void* AllocSprite(size_t);
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   136
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   137
static void* ReadSprite(SpriteCache *sc, SpriteID id, SpriteType sprite_type)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   138
{
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: 7089
diff changeset
   139
	uint8 file_slot = sc->file_slot;
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9146
diff changeset
   140
	size_t file_pos = sc->file_pos;
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   141
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5150
diff changeset
   142
	DEBUG(sprite, 9, "Load sprite %d", id);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   143
3565
ef0a9ef56fa0 (svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents: 2548
diff changeset
   144
	if (!SpriteExists(id)) {
5962
c861a188a760 (svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents: 5720
diff changeset
   145
		DEBUG(sprite, 1, "Tried to load non-existing sprite #%d. Probable cause: Wrong/missing NewGRFs", id);
c861a188a760 (svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents: 5720
diff changeset
   146
c861a188a760 (svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents: 5720
diff changeset
   147
		/* SPR_IMG_QUERY is a BIG FAT RED ? */
c861a188a760 (svn r8634) -Codechange: replace missing sprites with a red question mark.
rubidium
parents: 5720
diff changeset
   148
		id = SPR_IMG_QUERY;
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: 7089
diff changeset
   149
		file_slot = GetSpriteCache(SPR_IMG_QUERY)->file_slot;
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: 7089
diff changeset
   150
		file_pos  = GetSpriteCache(SPR_IMG_QUERY)->file_pos;
1378
23d4f642c989 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
   151
	}
23d4f642c989 (svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents: 1363
diff changeset
   152
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   153
	if (sprite_type == ST_NORMAL && BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 32) {
6901
ad8e8b93060d (svn r10148) -Fix r10143: make --without-png to compile again
truelight
parents: 6896
diff changeset
   154
#ifdef WITH_PNG
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6869
diff changeset
   155
		/* Try loading 32bpp graphics in case we are 32bpp output */
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6869
diff changeset
   156
		SpriteLoaderPNG sprite_loader;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6869
diff changeset
   157
		SpriteLoader::Sprite sprite;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6869
diff changeset
   158
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   159
		if (sprite_loader.LoadSprite(&sprite, file_slot, sc->id, sprite_type)) {
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6869
diff changeset
   160
			sc->ptr = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, &AllocSprite);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6869
diff changeset
   161
			free(sprite.data);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6869
diff changeset
   162
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   163
			sc->type = sprite_type;
9629
a5d908f61ae4 (svn r13692) -Fix (r13674): loading PNG sprites could very quickly fill the sprite cache.
rubidium
parents: 9618
diff changeset
   164
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6869
diff changeset
   165
			return sc->ptr;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6869
diff changeset
   166
		}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6869
diff changeset
   167
		/* If the PNG couldn't be loaded, fall back to 8bpp grfs */
6903
091fb25052ac (svn r10150) -Fix r10148: show a message to users when using 32bpp blitter and no libpng available
truelight
parents: 6902
diff changeset
   168
#else
091fb25052ac (svn r10150) -Fix r10148: show a message to users when using 32bpp blitter and no libpng available
truelight
parents: 6902
diff changeset
   169
		static bool show_once = true;
091fb25052ac (svn r10150) -Fix r10148: show a message to users when using 32bpp blitter and no libpng available
truelight
parents: 6902
diff changeset
   170
		if (show_once) {
091fb25052ac (svn r10150) -Fix r10148: show a message to users when using 32bpp blitter and no libpng available
truelight
parents: 6902
diff changeset
   171
			DEBUG(misc, 0, "You are running a 32bpp blitter, but this build is without libpng support; falling back to 8bpp graphics");
091fb25052ac (svn r10150) -Fix r10148: show a message to users when using 32bpp blitter and no libpng available
truelight
parents: 6902
diff changeset
   172
			show_once = false;
091fb25052ac (svn r10150) -Fix r10148: show a message to users when using 32bpp blitter and no libpng available
truelight
parents: 6902
diff changeset
   173
		}
091fb25052ac (svn r10150) -Fix r10148: show a message to users when using 32bpp blitter and no libpng available
truelight
parents: 6902
diff changeset
   174
#endif /* WITH_PNG */
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6869
diff changeset
   175
	}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6869
diff changeset
   176
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: 7089
diff changeset
   177
	FioSeekToFile(file_slot, file_pos);
1354
4f9416233d06 (svn r1858) Let ReadSprite() handle the subtleties of loading a sprite, not its caller
tron
parents: 1353
diff changeset
   178
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents: 6803
diff changeset
   179
	/* Read the size and type */
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents: 6803
diff changeset
   180
	int num  = FioReadWord();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents: 6803
diff changeset
   181
	byte type = FioReadByte();
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents: 6803
diff changeset
   182
	/* Type 0xFF indicates either a colormap or some other non-sprite info */
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   183
	if (type == 0xFF) {
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   184
		if (sprite_type != ST_RECOLOUR) {
6869
cd04f1d7cad7 (svn r10109) -Fix [FS#838]: some NewGRFs use the same (unused in the "current" climate) sprite IDs. Normally this gives some artefacts, but when one NewGRF expects it to be a sprite and another NewGRF overwrites it with a non-sprite nasty things happen (drawing a non-sprite crashes OTTD).
rubidium
parents: 6865
diff changeset
   185
			static byte warning_level = 0;
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   186
			DEBUG(sprite, warning_level, "Tried to load recolour sprite #%d as a real sprite. Probable cause: NewGRF interference", id);
6869
cd04f1d7cad7 (svn r10109) -Fix [FS#838]: some NewGRFs use the same (unused in the "current" climate) sprite IDs. Normally this gives some artefacts, but when one NewGRF expects it to be a sprite and another NewGRF overwrites it with a non-sprite nasty things happen (drawing a non-sprite crashes OTTD).
rubidium
parents: 6865
diff changeset
   187
			warning_level = 6;
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   188
			if (id == SPR_IMG_QUERY) usererror("Uhm, would you be so kind not to load a NewGRF that makes the 'query' sprite a recolour-sprite?");
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   189
			return (void*)GetRawSprite(SPR_IMG_QUERY, ST_NORMAL);
6869
cd04f1d7cad7 (svn r10109) -Fix [FS#838]: some NewGRFs use the same (unused in the "current" climate) sprite IDs. Normally this gives some artefacts, but when one NewGRF expects it to be a sprite and another NewGRF overwrites it with a non-sprite nasty things happen (drawing a non-sprite crashes OTTD).
rubidium
parents: 6865
diff changeset
   190
		}
cd04f1d7cad7 (svn r10109) -Fix [FS#838]: some NewGRFs use the same (unused in the "current" climate) sprite IDs. Normally this gives some artefacts, but when one NewGRF expects it to be a sprite and another NewGRF overwrites it with a non-sprite nasty things happen (drawing a non-sprite crashes OTTD).
rubidium
parents: 6865
diff changeset
   191
10062
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   192
		/* "Normal" recolour sprites are ALWAYS 257 bytes. Then there is a small
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   193
		 * number of recolour sprites that are 17 bytes that only exist in DOS
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   194
		 * GRFs which are the same as 257 byte recolour sprites, but with the last
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   195
		 * 240 bytes zeroed.  */
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   196
		static const int RECOLOUR_SPRITE_SIZE = 257;
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   197
		byte *dest = (byte *)AllocSprite(max(RECOLOUR_SPRITE_SIZE, num));
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   198
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   199
		sc->ptr = dest;
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   200
		sc->type = sprite_type;
10062
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   201
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   202
		if (_palette_remap_grf[sc->file_slot]) {
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   203
			byte *dest_tmp = AllocaM(byte, max(RECOLOUR_SPRITE_SIZE, num));
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   204
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   205
			/* Only a few recolour sprites are less than 257 bytes */
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   206
			if (num < RECOLOUR_SPRITE_SIZE) memset(dest_tmp, 0, RECOLOUR_SPRITE_SIZE);
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   207
			FioReadBlock(dest_tmp, num);
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   208
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   209
			/* The data of index 0 is never used; "literal 00" according to the (New)GRF specs. */
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   210
			for (int i = 1; i < RECOLOUR_SPRITE_SIZE; i++) {
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   211
				dest[i] = _palette_remap[dest_tmp[_palette_reverse_remap[i - 1] + 1]];
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   212
			}
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   213
		} else {
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   214
			FioReadBlock(dest, num);
8ec5e3f048b7 (svn r14229) -Feature: allow overriding the palette of the base GRFs. This way you can play with NewGRFs made for the Windows palette with the DOS palettes base GRFs (and vice versa). Note that for this to work correctly ALL NewGRFs must use the same palette; mix and match is not yet supported.
rubidium
parents: 10056
diff changeset
   215
		}
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   216
6865
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   217
		return sc->ptr;
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   218
	}
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   219
	/* Ugly hack to work around the problem that the old landscape
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   220
	 *  generator assumes that those sprites are stored uncompressed in
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   221
	 *  the memory, and they are only read directly by the code, never
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   222
	 *  send to the blitter. So do not send it to the blitter (which will
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   223
	 *  result in a data array in the format the blitter likes most), but
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   224
	 *  read the data directly from disk and store that as sprite.
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   225
	 * Ugly: yes. Other solution: no. Blame the original author or
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   226
	 *  something ;) The image should really have been a data-stream
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   227
	 *  (so type = 0xFF basicly). */
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   228
	assert((id >= 4845 && id <= 4881) == (sprite_type == ST_MAPGEN));
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   229
	if (sprite_type == ST_MAPGEN) {
6865
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   230
		uint height = FioReadByte();
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   231
		uint width  = FioReadWord();
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   232
		Sprite *sprite;
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   233
		byte *dest;
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   234
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   235
		num = width * height;
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   236
		sprite = (Sprite *)AllocSprite(sizeof(*sprite) + num);
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   237
		sc->ptr = sprite;
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   238
		sprite->height = height;
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   239
		sprite->width  = width;
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   240
		sprite->x_offs = FioReadWord();
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   241
		sprite->y_offs = FioReadWord();
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   242
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   243
		dest = sprite->data;
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   244
		while (num > 0) {
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   245
			int8 i = FioReadByte();
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   246
			if (i >= 0) {
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   247
				num -= i;
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   248
				for (; i > 0; --i) *dest++ = FioReadByte();
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   249
			} else {
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   250
				const byte* rel = dest - (((i & 7) << 8) | FioReadByte());
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   251
				i = -(i >> 3);
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   252
				num -= i;
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   253
				for (; i > 0; --i) *dest++ = *rel++;
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   254
			}
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   255
		}
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   256
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   257
		sc->type = sprite_type;
9618
2a191ec24807 (svn r13674) -Fix [FS#2127]: crash when drawing a non-real sprite. The drawing of the non-real sprite is caused when two NewGRFs replace the same sprite and the first replaces it with a real sprite (and thus assumes it remains a real sprite) and the second replaces it with a non-real sprite. OpenTTD already looked at whether the sprite to load should be seen as a real or non-real sprite, but it failed to replace non-real sprites with a substitute real sprite when getting the sprite from the cache.
rubidium
parents: 9470
diff changeset
   258
6865
60e668c0ed7c (svn r10105) -Fix r10092: fix sprite 4845 till 4881 (inclusive), so they store the data as on the disk in the memory, as the old landscape generate assumes this. Talking about ugly hacks...
truelight
parents: 6856
diff changeset
   259
		return sc->ptr;
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents: 6803
diff changeset
   260
	}
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   261
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   262
	if (sprite_type == ST_RECOLOUR) {
6869
cd04f1d7cad7 (svn r10109) -Fix [FS#838]: some NewGRFs use the same (unused in the "current" climate) sprite IDs. Normally this gives some artefacts, but when one NewGRF expects it to be a sprite and another NewGRF overwrites it with a non-sprite nasty things happen (drawing a non-sprite crashes OTTD).
rubidium
parents: 6865
diff changeset
   263
		static byte warning_level = 0;
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   264
		DEBUG(sprite, warning_level, "Tried to load real sprite #%d as a recolour sprite. Probable cause: NewGRF interference", id);
6869
cd04f1d7cad7 (svn r10109) -Fix [FS#838]: some NewGRFs use the same (unused in the "current" climate) sprite IDs. Normally this gives some artefacts, but when one NewGRF expects it to be a sprite and another NewGRF overwrites it with a non-sprite nasty things happen (drawing a non-sprite crashes OTTD).
rubidium
parents: 6865
diff changeset
   265
		warning_level = 6;
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   266
		return (void*)GetRawSprite(id, ST_NORMAL);
6869
cd04f1d7cad7 (svn r10109) -Fix [FS#838]: some NewGRFs use the same (unused in the "current" climate) sprite IDs. Normally this gives some artefacts, but when one NewGRF expects it to be a sprite and another NewGRF overwrites it with a non-sprite nasty things happen (drawing a non-sprite crashes OTTD).
rubidium
parents: 6865
diff changeset
   267
	}
cd04f1d7cad7 (svn r10109) -Fix [FS#838]: some NewGRFs use the same (unused in the "current" climate) sprite IDs. Normally this gives some artefacts, but when one NewGRF expects it to be a sprite and another NewGRF overwrites it with a non-sprite nasty things happen (drawing a non-sprite crashes OTTD).
rubidium
parents: 6865
diff changeset
   268
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents: 6803
diff changeset
   269
	SpriteLoaderGrf sprite_loader;
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents: 6803
diff changeset
   270
	SpriteLoader::Sprite sprite;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   271
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   272
	sc->type = sprite_type;
9737
bd9703b254c2 (svn r13869) -Fix [FS#2153] (r13674): in some cases the sprite cache could be filled with unremovable items.
rubidium
parents: 9629
diff changeset
   273
10359
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10236
diff changeset
   274
	if (!sprite_loader.LoadSprite(&sprite, file_slot, file_pos, sprite_type)) {
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10236
diff changeset
   275
		if (id == SPR_IMG_QUERY) usererror("Okay... something went horribly wrong. I couldn't load the fallback sprite. What should I do?");
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10236
diff changeset
   276
		return (void*)GetRawSprite(SPR_IMG_QUERY, ST_NORMAL);
64e5bdedbbfa (svn r14610) -Fix [FS#2415]: possible stack corruption when reading corrupted sprites.
rubidium
parents: 10236
diff changeset
   277
	}
6856
aa95d0fd29f1 (svn r10096) -Fix r10092: freetype bypassed the Blitter::Encode, making fonts look weird
truelight
parents: 6852
diff changeset
   278
	sc->ptr = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, &AllocSprite);
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents: 6803
diff changeset
   279
	free(sprite.data);
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   280
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents: 6803
diff changeset
   281
	return sc->ptr;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   283
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   284
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: 7089
diff changeset
   285
bool LoadNextSprite(int load_index, byte file_slot, uint file_sprite_id)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   286
{
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   287
	SpriteCache *sc;
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9146
diff changeset
   288
	size_t file_pos = FioGetPos();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   289
2342
aae24c9661ba (svn r2868) Change the way NewGRFs are loaded: The loading process i no longer bolted onto the normal graphics loading.
tron
parents: 2340
diff changeset
   290
	if (!ReadSpriteHeaderSkipData()) return false;
361
95307fb1c3f8 (svn r549) -newgrf: Support for action 0xd (change a parameter (sorta variable for the GRF scripts)). Based on patch by octo, heavy changes by pasky.
darkvater
parents: 184
diff changeset
   291
3591
a5febb93bc11 (svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents: 3565
diff changeset
   292
	if (load_index >= MAX_SPRITES) {
9470
08424e2e79e4 (svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents: 9390
diff changeset
   293
		usererror("Tried to load too many sprites (#%d; max %d)", load_index, MAX_SPRITES);
3591
a5febb93bc11 (svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents: 3565
diff changeset
   294
	}
a5febb93bc11 (svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents: 3565
diff changeset
   295
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   296
	sc = AllocateSpriteCache(load_index);
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: 7089
diff changeset
   297
	sc->file_slot = file_slot;
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   298
	sc->file_pos = file_pos;
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   299
	sc->ptr = NULL;
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   300
	sc->lru = 0;
6908
6b1324d7a2c9 (svn r10157) -Fix: use as indentified for PNGs, the place of the image as it was in the grf, not the internal SpriteID
truelight
parents: 6903
diff changeset
   301
	sc->id = file_sprite_id;
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   302
	sc->type = ST_NORMAL;
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6869
diff changeset
   303
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   304
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   305
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   306
2407
6c378ce4d469 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   307
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   308
void DupSprite(SpriteID old_spr, SpriteID new_spr)
2407
6c378ce4d469 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   309
{
8435
6425e2ecd2a6 (svn r12005) -Fix [FS#1717]: possible reading from an invalid pointer. Patch by PhilSophus.
rubidium
parents: 8432
diff changeset
   310
	SpriteCache *scnew = AllocateSpriteCache(new_spr); // may reallocate: so put it first
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   311
	SpriteCache *scold = GetSpriteCache(old_spr);
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   312
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: 7089
diff changeset
   313
	scnew->file_slot = scold->file_slot;
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   314
	scnew->file_pos = scold->file_pos;
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   315
	scnew->ptr = NULL;
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6869
diff changeset
   316
	scnew->id = scold->id;
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   317
	scnew->type = scold->type;
2407
6c378ce4d469 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   318
}
6c378ce4d469 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   319
6c378ce4d469 (svn r2933) Implement the non-breaking space
tron
parents: 2342
diff changeset
   320
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   321
#define S_FREE_MASK 1
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   322
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   323
static inline MemBlock* NextBlock(MemBlock* block)
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   324
{
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   325
	return (MemBlock*)((byte*)block + (block->size & ~S_FREE_MASK));
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   326
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   327
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9146
diff changeset
   328
static size_t GetSpriteCacheUsage()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   329
{
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9146
diff changeset
   330
	size_t tot_size = 0;
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   331
	MemBlock* s;
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   332
8562
7419166c16bd (svn r12140) -Codechange: A bit of code style fixes(Geektoo).
belugas
parents: 8435
diff changeset
   333
	for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   334
		if (!(s->size & S_FREE_MASK)) tot_size += s->size;
8562
7419166c16bd (svn r12140) -Codechange: A bit of code style fixes(Geektoo).
belugas
parents: 8435
diff changeset
   335
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   336
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   337
	return tot_size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   338
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   339
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   340
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5962
diff changeset
   341
void IncreaseSpriteLRU()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   342
{
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   343
	/* Increase all LRU values */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   344
	if (_sprite_lru_counter > 16384) {
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   345
		SpriteID i;
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   346
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5150
diff changeset
   347
		DEBUG(sprite, 3, "Fixing lru %d, inuse=%d", _sprite_lru_counter, GetSpriteCacheUsage());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   348
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   349
		for (i = 0; i != _spritecache_items; i++) {
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   350
			SpriteCache *sc = GetSpriteCache(i);
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   351
			if (sc->ptr != NULL) {
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   352
				if (sc->lru >= 0) {
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   353
					sc->lru = -1;
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   354
				} else if (sc->lru != -32768) {
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   355
					sc->lru--;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   356
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
			}
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   358
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   359
		_sprite_lru_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   360
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   361
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   362
	/* Compact sprite cache every now and then. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   363
	if (++_compact_cache_counter >= 740) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   364
		CompactSpriteCache();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   365
		_compact_cache_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   366
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   367
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   368
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   369
/** Called when holes in the sprite cache should be removed.
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   370
 * That is accomplished by moving the cached data. */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5962
diff changeset
   371
static void CompactSpriteCache()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   372
{
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   373
	MemBlock *s;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   374
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5150
diff changeset
   375
	DEBUG(sprite, 3, "Compacting sprite cache, inuse=%d", GetSpriteCacheUsage());
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   376
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   377
	for (s = _spritecache_ptr; s->size != 0;) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   378
		if (s->size & S_FREE_MASK) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   379
			MemBlock* next = NextBlock(s);
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   380
			MemBlock temp;
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   381
			SpriteID i;
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   382
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   383
			/* Since free blocks are automatically coalesced, this should hold true. */
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   384
			assert(!(next->size & S_FREE_MASK));
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   385
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   386
			/* If the next block is the sentinel block, we can safely return */
8741
57b96b8b0d99 (svn r12436) -Cleanup: minor coding style
peter1138
parents: 8562
diff changeset
   387
			if (next->size == 0) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   389
			/* Locate the sprite belonging to the next pointer. */
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   390
			for (i = 0; GetSpriteCache(i)->ptr != next->data; i++) {
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   391
				assert(i != _spritecache_items);
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   392
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   393
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   394
			GetSpriteCache(i)->ptr = s->data; // Adjust sprite array entry
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   395
			/* Swap this and the next block */
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   396
			temp = *s;
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   397
			memmove(s, next, next->size);
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   398
			s = NextBlock(s);
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   399
			*s = temp;
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   400
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   401
			/* Coalesce free blocks */
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   402
			while (NextBlock(s)->size & S_FREE_MASK) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   403
				s->size += NextBlock(s)->size & ~S_FREE_MASK;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   404
			}
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   405
		} else {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   406
			s = NextBlock(s);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   407
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   408
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   409
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5962
diff changeset
   411
static void DeleteEntryFromSpriteCache()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   412
{
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   413
	SpriteID i;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   414
	uint best = UINT_MAX;
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   415
	MemBlock* s;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   416
	int cur_lru;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   417
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5150
diff changeset
   418
	DEBUG(sprite, 3, "DeleteEntryFromSpriteCache, inuse=%d", GetSpriteCacheUsage());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   419
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   420
	cur_lru = 0xffff;
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   421
	for (i = 0; i != _spritecache_items; i++) {
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   422
		SpriteCache *sc = GetSpriteCache(i);
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   423
		if (sc->ptr != NULL && sc->lru < cur_lru) {
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   424
			cur_lru = sc->lru;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   425
			best = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   426
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   427
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   428
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   429
	/* Display an error message and die, in case we found no sprite at all.
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   430
	 * This shouldn't really happen, unless all sprites are locked. */
10236
50afe9dd466e (svn r14464) -Codechange: replace (uint)-1 with UINT_MAX (PhilSophus)
rubidium
parents: 10062
diff changeset
   431
	if (best == UINT_MAX) error("Out of sprite memory");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   433
	/* Mark the block as free (the block must be in use) */
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   434
	s = (MemBlock*)GetSpriteCache(best)->ptr - 1;
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   435
	assert(!(s->size & S_FREE_MASK));
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   436
	s->size |= S_FREE_MASK;
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   437
	GetSpriteCache(best)->ptr = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   439
	/* And coalesce adjacent free blocks */
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   440
	for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   441
		if (s->size & S_FREE_MASK) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   442
			while (NextBlock(s)->size & S_FREE_MASK) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   443
				s->size += NextBlock(s)->size & ~S_FREE_MASK;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   444
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   447
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   448
6852
439563b70fd3 (svn r10092) -Codechange: code-seperated the spriteloader and blitter from the rest of the code
truelight
parents: 6803
diff changeset
   449
void* AllocSprite(size_t mem_req)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   450
{
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   451
	mem_req += sizeof(MemBlock);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   452
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   453
	/* Align this to an uint32 boundary. This also makes sure that the 2 least
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   454
	 * bits are not used, so we could use those for other things. */
7927
3a3289a049f9 (svn r11480) -Codechange: Rename the function ALIGN fitting to the naming style
skidd13
parents: 7576
diff changeset
   455
	mem_req = Align(mem_req, sizeof(uint32));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   456
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   457
	for (;;) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   458
		MemBlock* s;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   459
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   460
		for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   461
			if (s->size & S_FREE_MASK) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   462
				size_t cur_size = s->size & ~S_FREE_MASK;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   463
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   464
				/* Is the block exactly the size we need or
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   465
				 * big enough for an additional free block? */
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   466
				if (cur_size == mem_req ||
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   467
						cur_size >= mem_req + sizeof(MemBlock)) {
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   468
					/* Set size and in use */
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   469
					s->size = mem_req;
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   470
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   471
					/* Do we need to inject a free block too? */
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   472
					if (cur_size != mem_req) {
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   473
						NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK;
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   474
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   475
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   476
					return s->data;
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   477
				}
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   478
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   479
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   480
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   481
		/* Reached sentinel, but no block found yet. Delete some old entry. */
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   482
		DeleteEntryFromSpriteCache();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   483
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   484
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   485
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   486
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   487
const void *GetRawSprite(SpriteID sprite, SpriteType type)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   488
{
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   489
	SpriteCache *sc;
2014
ccfe4fa81a14 (svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
tron
parents: 1891
diff changeset
   490
	void* p;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   491
5702
f98beec96c66 (svn r8166) -Fix (r7797): Protect against out of bounds access to the sprite ptr
peter1138
parents: 5609
diff changeset
   492
	assert(sprite < _spritecache_items);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   493
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   494
	sc = GetSpriteCache(sprite);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   495
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   496
	/* Update LRU */
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   497
	sc->lru = ++_sprite_lru_counter;
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   498
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   499
	p = sc->ptr;
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   500
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   501
	/* Load the sprite, if it is not loaded, yet */
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10052
diff changeset
   502
	if (p == NULL || sc->type != type) p = ReadSprite(sc, sprite, type);
8562
7419166c16bd (svn r12140) -Codechange: A bit of code style fixes(Geektoo).
belugas
parents: 8435
diff changeset
   503
184
dbeaaaf8b2bb (svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents: 182
diff changeset
   504
	return p;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   505
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
961
e9abf6f087f8 (svn r1453) Feature: MD5 hash check for TTD files
dominik
parents: 884
diff changeset
   507
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5962
diff changeset
   508
void GfxInitSpriteMem()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   509
{
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   510
	/* initialize sprite cache heap */
8037
8aa4ace04383 (svn r11597) -Change: replace all remaining instances of (re|m|c)alloc with (Re|M|C)allocT and add a check for out-of-memory situations to the *allocT functions.
rubidium
parents: 7927
diff changeset
   511
	if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)MallocT<byte>(_sprite_cache_size * 1024 * 1024);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   512
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   513
	/* A big free block */
6803
9803e56a8d9c (svn r10042) -Codechange: Replace hardcoded spritecache size with a configuration
peter1138
parents: 6799
diff changeset
   514
	_spritecache_ptr->size = ((_sprite_cache_size * 1024 * 1024) - sizeof(MemBlock)) | S_FREE_MASK;
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6248
diff changeset
   515
	/* Sentinel block (identified by size == 0) */
1353
c5892a0dadad (svn r1857) Rewrite parts of the sprite heap. It's functionally equivalent but should be easier to read and maintain.
tron
parents: 1352
diff changeset
   516
	NextBlock(_spritecache_ptr)->size = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   517
5504
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   518
	/* Reset the spritecache 'pool' */
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   519
	free(_spritecache);
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   520
	_spritecache_items = 0;
a6ef917aa095 (svn r7797) -Codechange: Replace static _sprite_ptr and associated arrays with dynamic array to allow variable number of sprites. This does not change the sprite limit.
peter1138
parents: 5501
diff changeset
   521
	_spritecache = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   522
2340
e18ef06bc59a (svn r2866) Move all functions and tables which aren't directly involved in managing the sprite heap to a new file gfxinit.c.
tron
parents: 2339
diff changeset
   523
	_compact_cache_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   524
}