author | glx |
Mon, 24 Sep 2007 03:08:47 +0000 | |
branch | 0.5 |
changeset 5545 | f42dc59a45f5 |
parent 5513 | 0d3227e250eb |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
0 | 3 |
#include "stdafx.h" |
1891
92a3b0aa0946
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1844
diff
changeset
|
4 |
#include "openttd.h" |
1299
0a6510cc889b
(svn r1803) Move debugging stuff into files of it's own
tron
parents:
1250
diff
changeset
|
5 |
#include "debug.h" |
2163
637ec3c361f5
(svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents:
2159
diff
changeset
|
6 |
#include "functions.h" |
2548
97ada3bd2702
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2407
diff
changeset
|
7 |
#include "macros.h" |
1349
07514c2cc6d1
(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
|
8 |
#include "spritecache.h" |
1363
01d3de5d8039
(svn r1867) Include tables/sprites.h only in files which need it
tron
parents:
1361
diff
changeset
|
9 |
#include "table/sprites.h" |
0 | 10 |
#include "fileio.h" |
11 |
||
5421
96c2e24bfe30
(svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents:
5150
diff
changeset
|
12 |
#ifndef SPRITE_CACHE_SIZE |
96c2e24bfe30
(svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents:
5150
diff
changeset
|
13 |
# define SPRITE_CACHE_SIZE 2*1024*1024 |
96c2e24bfe30
(svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents:
5150
diff
changeset
|
14 |
#endif /* SPRITE_CACHE_SIZE */ |
0 | 15 |
|
16 |
#define WANT_NEW_LRU |
|
17 |
||
18 |
||
2187
2a51f8925eeb
(svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents:
2186
diff
changeset
|
19 |
static void* _sprite_ptr[MAX_SPRITES]; |
2a51f8925eeb
(svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents:
2186
diff
changeset
|
20 |
static uint32 _sprite_file_pos[MAX_SPRITES]; |
0 | 21 |
|
22 |
#if defined(WANT_NEW_LRU) |
|
2187
2a51f8925eeb
(svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents:
2186
diff
changeset
|
23 |
static int16 _sprite_lru_new[MAX_SPRITES]; |
0 | 24 |
#else |
2187
2a51f8925eeb
(svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents:
2186
diff
changeset
|
25 |
static uint16 _sprite_lru[MAX_SPRITES]; |
2a51f8925eeb
(svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents:
2186
diff
changeset
|
26 |
static uint16 _sprite_lru_cur[MAX_SPRITES]; |
0 | 27 |
#endif |
28 |
||
1353
48b59d472641
(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
|
29 |
typedef struct MemBlock { |
48b59d472641
(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
|
30 |
uint32 size; |
48b59d472641
(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
|
31 |
byte data[VARARRAY_SIZE]; |
48b59d472641
(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
|
32 |
} MemBlock; |
48b59d472641
(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
|
33 |
|
0 | 34 |
static uint _sprite_lru_counter; |
1353
48b59d472641
(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
|
35 |
static MemBlock *_spritecache_ptr; |
0 | 36 |
static int _compact_cache_counter; |
37 |
||
1093
e8d26c7dc42f
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1070
diff
changeset
|
38 |
static void CompactSpriteCache(void); |
0 | 39 |
|
2342
c19fb4f2df30
(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
|
40 |
static bool ReadSpriteHeaderSkipData(void) |
0 | 41 |
{ |
2329
f68428464540
(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
|
42 |
uint16 num = FioReadWord(); |
0 | 43 |
byte type; |
44 |
||
2329
f68428464540
(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
|
45 |
if (num == 0) return false; |
f68428464540
(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
|
46 |
|
0 | 47 |
type = FioReadByte(); |
48 |
if (type == 0xFF) { |
|
2342
c19fb4f2df30
(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
|
49 |
FioSkipBytes(num); |
5150
247343201280
(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
|
50 |
/* Some NewGRF files have "empty" pseudo-sprites which are 1 |
247343201280
(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
|
51 |
* byte long. Catch these so the sprites won't be displayed. */ |
247343201280
(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
|
52 |
return num != 1; |
0 | 53 |
} |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
54 |
|
0 | 55 |
FioSkipBytes(7); |
56 |
num -= 8; |
|
2329
f68428464540
(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
|
57 |
if (num == 0) return true; |
0 | 58 |
|
59 |
if (type & 2) { |
|
60 |
FioSkipBytes(num); |
|
1355 | 61 |
} else { |
62 |
while (num > 0) { |
|
63 |
int8 i = FioReadByte(); |
|
64 |
if (i >= 0) { |
|
65 |
num -= i; |
|
66 |
FioSkipBytes(i); |
|
67 |
} else { |
|
68 |
i = -(i >> 3); |
|
69 |
num -= i; |
|
70 |
FioReadByte(); |
|
71 |
} |
|
0 | 72 |
} |
73 |
} |
|
2329
f68428464540
(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
|
74 |
|
f68428464540
(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
|
75 |
return true; |
0 | 76 |
} |
77 |
||
3565
03d870cc3dcd
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
78 |
/* Check if the given Sprite ID exists */ |
03d870cc3dcd
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
79 |
bool SpriteExists(SpriteID id) |
03d870cc3dcd
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
80 |
{ |
03d870cc3dcd
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
81 |
/* Special case for Sprite ID zero -- its position is also 0... */ |
03d870cc3dcd
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
82 |
return _sprite_file_pos[id] != 0 || id == 0; |
03d870cc3dcd
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
83 |
} |
03d870cc3dcd
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
84 |
|
2014
0230ed9186bc
(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
|
85 |
static void* AllocSprite(size_t); |
0230ed9186bc
(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
|
86 |
|
5513
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
87 |
static void* ReadSprite(SpriteID id, bool real_sprite) |
0 | 88 |
{ |
2321
455694cdbada
(svn r2847) Don't remember the size of sprites during initialisation. Since the sprite loading was altered this is no longer necessary.
tron
parents:
2319
diff
changeset
|
89 |
uint num; |
0 | 90 |
byte type; |
5509
0b111f4e6dc9
(svn r10274) [0.5] -Backport from trunk (r9961, r10023, r10028, r10030, r10038, r10147):
rubidium
parents:
5433
diff
changeset
|
91 |
uint32 filepos = _sprite_file_pos[id]; |
2014
0230ed9186bc
(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
|
92 |
|
0230ed9186bc
(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
|
93 |
DEBUG(spritecache, 9) ("load sprite %d", id); |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
94 |
|
3565
03d870cc3dcd
(svn r4446) - Add function to determine if a Sprite ID exists.
peter1138
parents:
2548
diff
changeset
|
95 |
if (!SpriteExists(id)) { |
5433
c136db538201
(svn r8636) [0.5] -Backport from trunk (8634, 8635):
rubidium
parents:
5421
diff
changeset
|
96 |
DEBUG(spritecache, 1) ("Tried to load non-existing sprite #%d. Probable cause: Wrong/missing NewGRFs", id); |
c136db538201
(svn r8636) [0.5] -Backport from trunk (8634, 8635):
rubidium
parents:
5421
diff
changeset
|
97 |
|
c136db538201
(svn r8636) [0.5] -Backport from trunk (8634, 8635):
rubidium
parents:
5421
diff
changeset
|
98 |
/* SPR_IMG_QUERY is a BIG FAT RED ? */ |
5509
0b111f4e6dc9
(svn r10274) [0.5] -Backport from trunk (r9961, r10023, r10028, r10030, r10038, r10147):
rubidium
parents:
5433
diff
changeset
|
99 |
filepos = _sprite_file_pos[SPR_IMG_QUERY]; |
1378
ebb8d52f0352
(svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents:
1363
diff
changeset
|
100 |
} |
ebb8d52f0352
(svn r1882) Add a basic check if a non-existent sprite gets accessed.
tron
parents:
1363
diff
changeset
|
101 |
|
5509
0b111f4e6dc9
(svn r10274) [0.5] -Backport from trunk (r9961, r10023, r10028, r10030, r10038, r10147):
rubidium
parents:
5433
diff
changeset
|
102 |
FioSeekToFile(filepos); |
1354
5e5c89b9b169
(svn r1858) Let ReadSprite() handle the subtleties of loading a sprite, not its caller
tron
parents:
1353
diff
changeset
|
103 |
|
2321
455694cdbada
(svn r2847) Don't remember the size of sprites during initialisation. Since the sprite loading was altered this is no longer necessary.
tron
parents:
2319
diff
changeset
|
104 |
num = FioReadWord(); |
0 | 105 |
type = FioReadByte(); |
2014
0230ed9186bc
(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
|
106 |
if (type == 0xFF) { |
5513
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
107 |
byte* dest; |
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
108 |
|
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
109 |
if (real_sprite) { |
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
110 |
static byte warning_level = 0; |
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
111 |
DEBUG(misc, warning_level) ("Tried to load non sprite #%d as a real sprite. Probable cause: NewGRF interference", id); |
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
112 |
warning_level = 6; |
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
113 |
if (id == SPR_IMG_QUERY) error("Uhm, would you be so kind not to load a NewGRF that makes the 'query' sprite a non- sprite?"); |
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
114 |
return (void*)GetSprite(SPR_IMG_QUERY); |
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
115 |
} |
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
116 |
|
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
117 |
dest = AllocSprite(num); |
2014
0230ed9186bc
(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
|
118 |
|
0230ed9186bc
(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
|
119 |
_sprite_ptr[id] = dest; |
0230ed9186bc
(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
|
120 |
FioReadBlock(dest, num); |
0230ed9186bc
(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
|
121 |
|
0230ed9186bc
(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
|
122 |
return dest; |
0230ed9186bc
(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
|
123 |
} else { |
0230ed9186bc
(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
|
124 |
uint height = FioReadByte(); |
0230ed9186bc
(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
|
125 |
uint width = FioReadWord(); |
0230ed9186bc
(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
|
126 |
Sprite* sprite; |
0230ed9186bc
(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
|
127 |
byte* dest; |
0230ed9186bc
(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
|
128 |
|
5513
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
129 |
if (!real_sprite) { |
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
130 |
static byte warning_level = 0; |
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
131 |
DEBUG(misc, warning_level) ("Tried to load real sprite #%d as a non sprite. Probable cause: NewGRF interference", id); |
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
132 |
warning_level = 6; |
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
133 |
} |
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
134 |
|
2014
0230ed9186bc
(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
|
135 |
num = (type & 0x02) ? width * height : num - 8; |
0230ed9186bc
(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 |
sprite = AllocSprite(sizeof(*sprite) + num); |
0230ed9186bc
(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
|
137 |
_sprite_ptr[id] = sprite; |
0230ed9186bc
(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
|
138 |
sprite->info = type; |
2015 | 139 |
sprite->height = (id != 142) ? height : 10; // Compensate for a TTD bug |
2014
0230ed9186bc
(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
|
140 |
sprite->width = width; |
1351
3e7aa0d35f8f
(svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents:
1350
diff
changeset
|
141 |
sprite->x_offs = FioReadWord(); |
3e7aa0d35f8f
(svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents:
1350
diff
changeset
|
142 |
sprite->y_offs = FioReadWord(); |
2014
0230ed9186bc
(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
|
143 |
|
1351
3e7aa0d35f8f
(svn r1855) Handle endianness of sprite headers when loading a sprite, not everytime when accessing it
tron
parents:
1350
diff
changeset
|
144 |
dest = sprite->data; |
1355 | 145 |
while (num > 0) { |
146 |
int8 i = FioReadByte(); |
|
0 | 147 |
|
1355 | 148 |
if (i >= 0) { |
149 |
num -= i; |
|
2014
0230ed9186bc
(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
|
150 |
for (; i > 0; --i) *dest++ = FioReadByte(); |
1355 | 151 |
} else { |
152 |
const byte* rel = dest - (((i & 7) << 8) | FioReadByte()); |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
153 |
|
1355 | 154 |
i = -(i >> 3); |
155 |
num -= i; |
|
156 |
||
2014
0230ed9186bc
(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
|
157 |
for (; i > 0; --i) *dest++ = *rel++; |
1355 | 158 |
} |
0 | 159 |
} |
2014
0230ed9186bc
(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
|
160 |
|
0230ed9186bc
(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
|
161 |
return sprite; |
0 | 162 |
} |
163 |
} |
|
164 |
||
165 |
||
2340
0a9f3eeccb96
(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
|
166 |
bool LoadNextSprite(int load_index, byte file_index) |
0 | 167 |
{ |
2321
455694cdbada
(svn r2847) Don't remember the size of sprites during initialisation. Since the sprite loading was altered this is no longer necessary.
tron
parents:
2319
diff
changeset
|
168 |
uint32 file_pos = FioGetPos() | (file_index << 24); |
0 | 169 |
|
2342
c19fb4f2df30
(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
|
170 |
if (!ReadSpriteHeaderSkipData()) return false; |
361
ad7a042ee0eb
(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
|
171 |
|
3591
05a4b88d875f
(svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents:
3565
diff
changeset
|
172 |
if (load_index >= MAX_SPRITES) { |
05a4b88d875f
(svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents:
3565
diff
changeset
|
173 |
error("Tried to load too many sprites (#%d; max %d)", load_index, MAX_SPRITES); |
05a4b88d875f
(svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents:
3565
diff
changeset
|
174 |
} |
05a4b88d875f
(svn r4481) - Fix: Validate the given sprite ID when loading a sprite.
peter1138
parents:
3565
diff
changeset
|
175 |
|
361
ad7a042ee0eb
(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
|
176 |
_sprite_file_pos[load_index] = file_pos; |
ad7a042ee0eb
(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
|
177 |
|
0 | 178 |
_sprite_ptr[load_index] = NULL; |
179 |
||
180 |
#if defined(WANT_NEW_LRU) |
|
181 |
_sprite_lru_new[load_index] = 0; |
|
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
182 |
#else |
0 | 183 |
_sprite_lru[load_index] = 0xFFFF; |
184 |
_sprite_lru_cur[load_index] = 0; |
|
185 |
#endif |
|
186 |
||
187 |
return true; |
|
188 |
} |
|
189 |
||
2407 | 190 |
|
191 |
void DupSprite(SpriteID old, SpriteID new) |
|
192 |
{ |
|
193 |
_sprite_file_pos[new] = _sprite_file_pos[old]; |
|
194 |
_sprite_ptr[new] = NULL; |
|
195 |
} |
|
196 |
||
197 |
||
2340
0a9f3eeccb96
(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
|
198 |
void SkipSprites(uint count) |
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
199 |
{ |
2329
f68428464540
(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
|
200 |
for (; count > 0; --count) { |
2342
c19fb4f2df30
(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
|
201 |
if (!ReadSpriteHeaderSkipData()) return; |
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
202 |
} |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
203 |
} |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
204 |
|
0 | 205 |
|
206 |
#define S_FREE_MASK 1 |
|
1353
48b59d472641
(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
|
207 |
|
48b59d472641
(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
|
208 |
static inline MemBlock* NextBlock(MemBlock* block) |
48b59d472641
(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
|
209 |
{ |
48b59d472641
(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
|
210 |
return (MemBlock*)((byte*)block + (block->size & ~S_FREE_MASK)); |
48b59d472641
(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
|
211 |
} |
0 | 212 |
|
1093
e8d26c7dc42f
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1070
diff
changeset
|
213 |
static uint32 GetSpriteCacheUsage(void) |
0 | 214 |
{ |
4321
b763b7007162
(svn r5974) -Codechange: added casts all around the place to make Windows 64bit happy (michi_cc)
truelight
parents:
3591
diff
changeset
|
215 |
uint32 tot_size = 0; |
1353
48b59d472641
(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
|
216 |
MemBlock* s; |
48b59d472641
(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
|
217 |
|
48b59d472641
(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
|
218 |
for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) |
48b59d472641
(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
|
219 |
if (!(s->size & S_FREE_MASK)) tot_size += s->size; |
0 | 220 |
|
221 |
return tot_size; |
|
222 |
} |
|
223 |
||
224 |
||
1093
e8d26c7dc42f
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1070
diff
changeset
|
225 |
void IncreaseSpriteLRU(void) |
0 | 226 |
{ |
227 |
int i; |
|
228 |
||
229 |
// Increase all LRU values |
|
230 |
#if defined(WANT_NEW_LRU) |
|
231 |
if (_sprite_lru_counter > 16384) { |
|
232 |
DEBUG(spritecache, 2) ("fixing lru %d, inuse=%d", _sprite_lru_counter, GetSpriteCacheUsage()); |
|
233 |
||
2187
2a51f8925eeb
(svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents:
2186
diff
changeset
|
234 |
for (i = 0; i != MAX_SPRITES; i++) |
0 | 235 |
if (_sprite_ptr[i] != NULL) { |
236 |
if (_sprite_lru_new[i] >= 0) { |
|
237 |
_sprite_lru_new[i] = -1; |
|
238 |
} else if (_sprite_lru_new[i] != -32768) { |
|
239 |
_sprite_lru_new[i]--; |
|
240 |
} |
|
241 |
} |
|
242 |
_sprite_lru_counter = 0; |
|
243 |
} |
|
244 |
#else |
|
2187
2a51f8925eeb
(svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents:
2186
diff
changeset
|
245 |
for (i = 0; i != MAX_SPRITES; i++) |
0 | 246 |
if (_sprite_ptr[i] != NULL && _sprite_lru[i] != 65535) |
247 |
_sprite_lru[i]++; |
|
248 |
// Reset the lru counter. |
|
249 |
_sprite_lru_counter = 0; |
|
250 |
#endif |
|
251 |
||
252 |
// Compact sprite cache every now and then. |
|
253 |
if (++_compact_cache_counter >= 740) { |
|
254 |
CompactSpriteCache(); |
|
255 |
_compact_cache_counter = 0; |
|
256 |
} |
|
257 |
} |
|
258 |
||
259 |
// Called when holes in the sprite cache should be removed. |
|
260 |
// That is accomplished by moving the cached data. |
|
1093
e8d26c7dc42f
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1070
diff
changeset
|
261 |
static void CompactSpriteCache(void) |
0 | 262 |
{ |
1353
48b59d472641
(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
|
263 |
MemBlock *s; |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
264 |
|
1353
48b59d472641
(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
|
265 |
DEBUG(spritecache, 2) ( |
48b59d472641
(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
|
266 |
"compacting sprite cache, inuse=%d", GetSpriteCacheUsage() |
48b59d472641
(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
|
267 |
); |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
268 |
|
1353
48b59d472641
(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
|
269 |
for (s = _spritecache_ptr; s->size != 0;) { |
48b59d472641
(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
|
270 |
if (s->size & S_FREE_MASK) { |
48b59d472641
(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
|
271 |
MemBlock* next = NextBlock(s); |
48b59d472641
(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
|
272 |
MemBlock temp; |
2014
0230ed9186bc
(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
|
273 |
void** i; |
1353
48b59d472641
(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
|
274 |
|
0 | 275 |
// Since free blocks are automatically coalesced, this should hold true. |
1353
48b59d472641
(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
|
276 |
assert(!(next->size & S_FREE_MASK)); |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
277 |
|
0 | 278 |
// If the next block is the sentinel block, we can safely return |
1353
48b59d472641
(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
|
279 |
if (next->size == 0) |
0 | 280 |
break; |
281 |
||
1353
48b59d472641
(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
|
282 |
// Locate the sprite belonging to the next pointer. |
48b59d472641
(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
|
283 |
for (i = _sprite_ptr; *i != next->data; ++i) { |
48b59d472641
(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
|
284 |
assert(i != endof(_sprite_ptr)); |
48b59d472641
(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
|
285 |
} |
0 | 286 |
|
1353
48b59d472641
(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
|
287 |
*i = s->data; // Adjust sprite array entry |
48b59d472641
(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
|
288 |
// Swap this and the next block |
48b59d472641
(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
|
289 |
temp = *s; |
48b59d472641
(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
|
290 |
memmove(s, next, next->size); |
48b59d472641
(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
|
291 |
s = NextBlock(s); |
48b59d472641
(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
|
292 |
*s = temp; |
48b59d472641
(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
|
293 |
|
48b59d472641
(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
|
294 |
// Coalesce free blocks |
48b59d472641
(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
|
295 |
while (NextBlock(s)->size & S_FREE_MASK) { |
48b59d472641
(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
|
296 |
s->size += NextBlock(s)->size & ~S_FREE_MASK; |
0 | 297 |
} |
1353
48b59d472641
(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
|
298 |
} else { |
48b59d472641
(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
|
299 |
s = NextBlock(s); |
0 | 300 |
} |
301 |
} |
|
302 |
} |
|
303 |
||
1093
e8d26c7dc42f
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1070
diff
changeset
|
304 |
static void DeleteEntryFromSpriteCache(void) |
0 | 305 |
{ |
306 |
int i; |
|
307 |
int best = -1; |
|
1353
48b59d472641
(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
|
308 |
MemBlock* s; |
0 | 309 |
int cur_lru; |
310 |
||
311 |
DEBUG(spritecache, 2) ("DeleteEntryFromSpriteCache, inuse=%d", GetSpriteCacheUsage()); |
|
312 |
||
313 |
#if defined(WANT_NEW_LRU) |
|
314 |
cur_lru = 0xffff; |
|
2187
2a51f8925eeb
(svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents:
2186
diff
changeset
|
315 |
for (i = 0; i != MAX_SPRITES; i++) { |
2305
f0856e98f7a5
(svn r2829) Remove sprite locking, it was never used anyway
tron
parents:
2244
diff
changeset
|
316 |
if (_sprite_ptr[i] != NULL && _sprite_lru_new[i] < cur_lru) { |
0 | 317 |
cur_lru = _sprite_lru_new[i]; |
318 |
best = i; |
|
319 |
} |
|
320 |
} |
|
321 |
#else |
|
322 |
{ |
|
323 |
uint16 cur_lru = 0, cur_lru_cur = 0xffff; |
|
2187
2a51f8925eeb
(svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents:
2186
diff
changeset
|
324 |
for (i = 0; i != MAX_SPRITES; i++) { |
2305
f0856e98f7a5
(svn r2829) Remove sprite locking, it was never used anyway
tron
parents:
2244
diff
changeset
|
325 |
if (_sprite_ptr[i] == NULL || _sprite_lru[i] < cur_lru) continue; |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
326 |
|
0 | 327 |
// Found a sprite with a higher LRU value, then remember it. |
328 |
if (_sprite_lru[i] != cur_lru) { |
|
329 |
cur_lru = _sprite_lru[i]; |
|
330 |
best = i; |
|
331 |
||
332 |
// Else if both sprites were very recently referenced, compare by the cur value instead. |
|
333 |
} else if (cur_lru == 0 && _sprite_lru_cur[i] <= cur_lru_cur) { |
|
334 |
cur_lru_cur = _sprite_lru_cur[i]; |
|
335 |
cur_lru = _sprite_lru[i]; |
|
336 |
best = i; |
|
337 |
} |
|
338 |
} |
|
339 |
} |
|
340 |
#endif |
|
341 |
||
342 |
// Display an error message and die, in case we found no sprite at all. |
|
343 |
// This shouldn't really happen, unless all sprites are locked. |
|
344 |
if (best == -1) |
|
345 |
error("Out of sprite memory"); |
|
346 |
||
347 |
// Mark the block as free (the block must be in use) |
|
1353
48b59d472641
(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
|
348 |
s = (MemBlock*)_sprite_ptr[best] - 1; |
48b59d472641
(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
|
349 |
assert(!(s->size & S_FREE_MASK)); |
48b59d472641
(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
|
350 |
s->size |= S_FREE_MASK; |
0 | 351 |
_sprite_ptr[best] = NULL; |
352 |
||
353 |
// And coalesce adjacent free blocks |
|
1353
48b59d472641
(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
|
354 |
for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) { |
48b59d472641
(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
|
355 |
if (s->size & S_FREE_MASK) { |
48b59d472641
(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
|
356 |
while (NextBlock(s)->size & S_FREE_MASK) { |
48b59d472641
(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
|
357 |
s->size += NextBlock(s)->size & ~S_FREE_MASK; |
0 | 358 |
} |
359 |
} |
|
360 |
} |
|
361 |
} |
|
362 |
||
2014
0230ed9186bc
(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
|
363 |
static void* AllocSprite(size_t mem_req) |
0 | 364 |
{ |
2014
0230ed9186bc
(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
|
365 |
mem_req += sizeof(MemBlock); |
0 | 366 |
|
1353
48b59d472641
(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
|
367 |
/* Align this to an uint32 boundary. This also makes sure that the 2 least |
48b59d472641
(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
|
368 |
* bits are not used, so we could use those for other things. */ |
2548
97ada3bd2702
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2407
diff
changeset
|
369 |
mem_req = ALIGN(mem_req, sizeof(uint32)); |
0 | 370 |
|
1353
48b59d472641
(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
|
371 |
for (;;) { |
48b59d472641
(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
|
372 |
MemBlock* s; |
0 | 373 |
|
1353
48b59d472641
(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
|
374 |
for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) { |
48b59d472641
(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
|
375 |
if (s->size & S_FREE_MASK) { |
48b59d472641
(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
|
376 |
size_t cur_size = s->size & ~S_FREE_MASK; |
0 | 377 |
|
1353
48b59d472641
(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 |
/* Is the block exactly the size we need or |
48b59d472641
(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 |
* big enough for an additional free block? */ |
48b59d472641
(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 |
if (cur_size == mem_req || |
48b59d472641
(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
|
381 |
cur_size >= mem_req + sizeof(MemBlock)) { |
48b59d472641
(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 |
// Set size and in use |
48b59d472641
(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
|
383 |
s->size = mem_req; |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
384 |
|
1353
48b59d472641
(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
|
385 |
// Do we need to inject a free block too? |
48b59d472641
(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
|
386 |
if (cur_size != mem_req) { |
48b59d472641
(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
|
387 |
NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK; |
48b59d472641
(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
|
388 |
} |
0 | 389 |
|
1353
48b59d472641
(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
|
390 |
return s->data; |
48b59d472641
(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
|
391 |
} |
48b59d472641
(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 | 393 |
} |
394 |
||
1353
48b59d472641
(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
|
395 |
// Reached sentinel, but no block found yet. Delete some old entry. |
48b59d472641
(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 |
DeleteEntryFromSpriteCache(); |
0 | 397 |
} |
398 |
} |
|
399 |
||
400 |
#if defined(NEW_ROTATION) |
|
401 |
#define X15(x) else if (s >= x && s < (x+15)) { s = _rotate_tile_sprite[s - x] + x; } |
|
402 |
#define X19(x) else if (s >= x && s < (x+19)) { s = _rotate_tile_sprite[s - x] + x; } |
|
403 |
#define MAP(from,to,map) else if (s >= from && s <= to) { s = map[s - from] + from; } |
|
404 |
||
405 |
||
410 | 406 |
static uint RotateSprite(uint s) |
0 | 407 |
{ |
4344
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4321
diff
changeset
|
408 |
static const byte _rotate_tile_sprite[19] = { 0, 2, 4, 6, 8, 10, 12, 14, 1, 3, 5, 7, 9, 11, 13, 17, 18, 16, 15 }; |
0 | 409 |
static const byte _coast_map[9] = {0, 4, 3, 1, 2, 6, 8, 5, 7}; |
410 |
static const byte _fence_map[6] = {1, 0, 5, 4, 3, 2}; |
|
411 |
||
412 |
if (0); |
|
413 |
X19(752) |
|
414 |
X15(990-1) |
|
415 |
X19(3924) |
|
416 |
X19(3943) |
|
417 |
X19(3962) |
|
418 |
X19(3981) |
|
419 |
X19(4000) |
|
420 |
X19(4023) |
|
421 |
X19(4042) |
|
4344
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4321
diff
changeset
|
422 |
MAP(4061, 4069, _coast_map) |
0 | 423 |
X19(4126) |
424 |
X19(4145) |
|
425 |
X19(4164) |
|
426 |
X19(4183) |
|
427 |
X19(4202) |
|
428 |
X19(4221) |
|
429 |
X19(4240) |
|
430 |
X19(4259) |
|
431 |
X19(4259) |
|
432 |
X19(4278) |
|
433 |
MAP(4090, 4095, _fence_map) |
|
434 |
MAP(4096, 4101, _fence_map) |
|
435 |
MAP(4102, 4107, _fence_map) |
|
436 |
MAP(4108, 4113, _fence_map) |
|
437 |
MAP(4114, 4119, _fence_map) |
|
438 |
MAP(4120, 4125, _fence_map) |
|
439 |
return s; |
|
440 |
} |
|
441 |
#endif |
|
442 |
||
5513
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
443 |
const void *GetRawSprite(SpriteID sprite, bool real_sprite) |
0 | 444 |
{ |
2014
0230ed9186bc
(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
|
445 |
void* p; |
0 | 446 |
|
2187
2a51f8925eeb
(svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents:
2186
diff
changeset
|
447 |
assert(sprite < MAX_SPRITES); |
0 | 448 |
|
449 |
#if defined(NEW_ROTATION) |
|
450 |
sprite = RotateSprite(sprite); |
|
451 |
#endif |
|
452 |
||
453 |
// Update LRU |
|
454 |
#if defined(WANT_NEW_LRU) |
|
455 |
_sprite_lru_new[sprite] = ++_sprite_lru_counter; |
|
456 |
#else |
|
2026 | 457 |
_sprite_lru_cur[sprite] = ++_sprite_lru_counter; |
0 | 458 |
_sprite_lru[sprite] = 0; |
459 |
#endif |
|
460 |
||
461 |
p = _sprite_ptr[sprite]; |
|
2014
0230ed9186bc
(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
|
462 |
// Load the sprite, if it is not loaded, yet |
5513
0d3227e250eb
(svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents:
5509
diff
changeset
|
463 |
if (p == NULL) p = ReadSprite(sprite, real_sprite); |
184
dbeaaaf8b2bb
(svn r185) -Fix: [1016954] Cached_sprites does now work again
truelight
parents:
182
diff
changeset
|
464 |
return p; |
0 | 465 |
} |
466 |
||
961 | 467 |
|
2340
0a9f3eeccb96
(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
|
468 |
void GfxInitSpriteMem(void) |
0 | 469 |
{ |
470 |
// initialize sprite cache heap |
|
2339
1c64119d5a3b
(svn r2865) Push the responsibility for allocating the sprite heap into GfxInitSpriteMem()
tron
parents:
2329
diff
changeset
|
471 |
if (_spritecache_ptr == NULL) _spritecache_ptr = malloc(SPRITE_CACHE_SIZE); |
0 | 472 |
|
473 |
// A big free block |
|
2339
1c64119d5a3b
(svn r2865) Push the responsibility for allocating the sprite heap into GfxInitSpriteMem()
tron
parents:
2329
diff
changeset
|
474 |
_spritecache_ptr->size = (SPRITE_CACHE_SIZE - sizeof(MemBlock)) | S_FREE_MASK; |
1353
48b59d472641
(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
|
475 |
// Sentinel block (identified by size == 0) |
48b59d472641
(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 |
NextBlock(_spritecache_ptr)->size = 0; |
0 | 477 |
|
478 |
memset(_sprite_ptr, 0, sizeof(_sprite_ptr)); |
|
479 |
||
2340
0a9f3eeccb96
(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
|
480 |
_compact_cache_counter = 0; |
0 | 481 |
} |