2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2005-02-05 15:58:59 +00:00
|
|
|
#include "debug.h"
|
2005-07-22 07:02:20 +00:00
|
|
|
#include "functions.h"
|
2005-02-10 05:43:30 +00:00
|
|
|
#include "spritecache.h"
|
2005-02-13 11:18:02 +00:00
|
|
|
#include "table/sprites.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "fileio.h"
|
|
|
|
|
|
|
|
#define SPRITE_CACHE_SIZE 1024*1024
|
|
|
|
|
|
|
|
#define WANT_NEW_LRU
|
|
|
|
|
|
|
|
|
2005-07-24 15:56:31 +00:00
|
|
|
static void* _sprite_ptr[MAX_SPRITES];
|
|
|
|
static uint32 _sprite_file_pos[MAX_SPRITES];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
#if defined(WANT_NEW_LRU)
|
2005-07-24 15:56:31 +00:00
|
|
|
static int16 _sprite_lru_new[MAX_SPRITES];
|
2004-08-09 17:04:08 +00:00
|
|
|
#else
|
2005-07-24 15:56:31 +00:00
|
|
|
static uint16 _sprite_lru[MAX_SPRITES];
|
|
|
|
static uint16 _sprite_lru_cur[MAX_SPRITES];
|
2004-08-09 17:04:08 +00:00
|
|
|
#endif
|
|
|
|
|
2005-02-11 13:35:27 +00:00
|
|
|
typedef struct MemBlock {
|
|
|
|
uint32 size;
|
|
|
|
byte data[VARARRAY_SIZE];
|
|
|
|
} MemBlock;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static uint _sprite_lru_counter;
|
2005-02-11 13:35:27 +00:00
|
|
|
static MemBlock *_spritecache_ptr;
|
2004-08-09 17:04:08 +00:00
|
|
|
static int _compact_cache_counter;
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static void CompactSpriteCache(void);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-08-15 11:39:13 +00:00
|
|
|
static bool ReadSpriteHeaderSkipData(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-08-11 13:09:12 +00:00
|
|
|
uint16 num = FioReadWord();
|
2004-08-09 17:04:08 +00:00
|
|
|
byte type;
|
|
|
|
|
2005-08-11 13:09:12 +00:00
|
|
|
if (num == 0) return false;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
type = FioReadByte();
|
|
|
|
if (type == 0xFF) {
|
2005-08-15 11:39:13 +00:00
|
|
|
FioSkipBytes(num);
|
2005-08-11 13:09:12 +00:00
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-08 18:05:49 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
FioSkipBytes(7);
|
|
|
|
num -= 8;
|
2005-08-11 13:09:12 +00:00
|
|
|
if (num == 0) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (type & 2) {
|
|
|
|
FioSkipBytes(num);
|
2005-02-11 14:33:43 +00:00
|
|
|
} else {
|
|
|
|
while (num > 0) {
|
|
|
|
int8 i = FioReadByte();
|
|
|
|
if (i >= 0) {
|
|
|
|
num -= i;
|
|
|
|
FioSkipBytes(i);
|
|
|
|
} else {
|
|
|
|
i = -(i >> 3);
|
|
|
|
num -= i;
|
|
|
|
FioReadByte();
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2005-08-11 13:09:12 +00:00
|
|
|
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-07-05 19:54:35 +00:00
|
|
|
static void* AllocSprite(size_t);
|
|
|
|
|
|
|
|
static void* ReadSprite(SpriteID id)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-08-09 12:58:22 +00:00
|
|
|
uint num;
|
2004-08-09 17:04:08 +00:00
|
|
|
byte type;
|
2005-07-05 19:54:35 +00:00
|
|
|
|
|
|
|
DEBUG(spritecache, 9) ("load sprite %d", id);
|
2004-09-08 18:05:49 +00:00
|
|
|
|
2005-08-09 12:58:22 +00:00
|
|
|
if (_sprite_file_pos[id] == 0 && id != 0) {
|
2005-02-17 15:53:47 +00:00
|
|
|
error(
|
|
|
|
"Tried to load non-existing sprite #%d.\n"
|
|
|
|
"Probable cause: Wrong/missing NewGRFs",
|
|
|
|
id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2005-02-11 13:46:25 +00:00
|
|
|
FioSeekToFile(_sprite_file_pos[id]);
|
|
|
|
|
2005-08-09 12:58:22 +00:00
|
|
|
num = FioReadWord();
|
2004-08-09 17:04:08 +00:00
|
|
|
type = FioReadByte();
|
2005-07-05 19:54:35 +00:00
|
|
|
if (type == 0xFF) {
|
|
|
|
byte* dest = AllocSprite(num);
|
|
|
|
|
|
|
|
_sprite_ptr[id] = dest;
|
|
|
|
FioReadBlock(dest, num);
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
} else {
|
|
|
|
uint height = FioReadByte();
|
|
|
|
uint width = FioReadWord();
|
|
|
|
Sprite* sprite;
|
|
|
|
byte* dest;
|
|
|
|
|
|
|
|
num = (type & 0x02) ? width * height : num - 8;
|
|
|
|
sprite = AllocSprite(sizeof(*sprite) + num);
|
|
|
|
_sprite_ptr[id] = sprite;
|
|
|
|
sprite->info = type;
|
2005-07-06 06:18:53 +00:00
|
|
|
sprite->height = (id != 142) ? height : 10; // Compensate for a TTD bug
|
2005-07-05 19:54:35 +00:00
|
|
|
sprite->width = width;
|
2005-02-10 12:26:41 +00:00
|
|
|
sprite->x_offs = FioReadWord();
|
|
|
|
sprite->y_offs = FioReadWord();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-05 19:54:35 +00:00
|
|
|
dest = sprite->data;
|
2005-02-11 14:33:43 +00:00
|
|
|
while (num > 0) {
|
|
|
|
int8 i = FioReadByte();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-02-11 14:33:43 +00:00
|
|
|
if (i >= 0) {
|
|
|
|
num -= i;
|
2005-07-05 19:54:35 +00:00
|
|
|
for (; i > 0; --i) *dest++ = FioReadByte();
|
2005-02-11 14:33:43 +00:00
|
|
|
} else {
|
|
|
|
const byte* rel = dest - (((i & 7) << 8) | FioReadByte());
|
|
|
|
|
|
|
|
i = -(i >> 3);
|
|
|
|
num -= i;
|
2004-09-08 18:05:49 +00:00
|
|
|
|
2005-07-05 19:54:35 +00:00
|
|
|
for (; i > 0; --i) *dest++ = *rel++;
|
2005-02-11 14:33:43 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-07-05 19:54:35 +00:00
|
|
|
|
|
|
|
return sprite;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-14 18:10:18 +00:00
|
|
|
bool LoadNextSprite(int load_index, byte file_index)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-08-09 12:58:22 +00:00
|
|
|
uint32 file_pos = FioGetPos() | (file_index << 24);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-08-15 11:39:13 +00:00
|
|
|
if (!ReadSpriteHeaderSkipData()) return false;
|
2004-11-12 18:47:19 +00:00
|
|
|
|
|
|
|
_sprite_file_pos[load_index] = file_pos;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
_sprite_ptr[load_index] = NULL;
|
|
|
|
|
|
|
|
#if defined(WANT_NEW_LRU)
|
|
|
|
_sprite_lru_new[load_index] = 0;
|
2004-09-08 18:05:49 +00:00
|
|
|
#else
|
2004-08-09 17:04:08 +00:00
|
|
|
_sprite_lru[load_index] = 0xFFFF;
|
|
|
|
_sprite_lru_cur[load_index] = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-08-14 18:10:18 +00:00
|
|
|
void SkipSprites(uint count)
|
2004-08-13 11:28:59 +00:00
|
|
|
{
|
2005-08-11 13:09:12 +00:00
|
|
|
for (; count > 0; --count) {
|
2005-08-15 11:39:13 +00:00
|
|
|
if (!ReadSpriteHeaderSkipData()) return;
|
2004-08-13 11:28:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
#define S_FREE_MASK 1
|
2005-02-11 13:35:27 +00:00
|
|
|
|
|
|
|
static inline MemBlock* NextBlock(MemBlock* block)
|
|
|
|
{
|
|
|
|
return (MemBlock*)((byte*)block + (block->size & ~S_FREE_MASK));
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static uint32 GetSpriteCacheUsage(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-02-11 13:35:27 +00:00
|
|
|
size_t tot_size = 0;
|
|
|
|
MemBlock* s;
|
|
|
|
|
|
|
|
for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s))
|
|
|
|
if (!(s->size & S_FREE_MASK)) tot_size += s->size;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return tot_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void IncreaseSpriteLRU(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// Increase all LRU values
|
|
|
|
#if defined(WANT_NEW_LRU)
|
|
|
|
if (_sprite_lru_counter > 16384) {
|
|
|
|
DEBUG(spritecache, 2) ("fixing lru %d, inuse=%d", _sprite_lru_counter, GetSpriteCacheUsage());
|
|
|
|
|
2005-07-24 15:56:31 +00:00
|
|
|
for (i = 0; i != MAX_SPRITES; i++)
|
2004-08-09 17:04:08 +00:00
|
|
|
if (_sprite_ptr[i] != NULL) {
|
|
|
|
if (_sprite_lru_new[i] >= 0) {
|
|
|
|
_sprite_lru_new[i] = -1;
|
|
|
|
} else if (_sprite_lru_new[i] != -32768) {
|
|
|
|
_sprite_lru_new[i]--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_sprite_lru_counter = 0;
|
|
|
|
}
|
|
|
|
#else
|
2005-07-24 15:56:31 +00:00
|
|
|
for (i = 0; i != MAX_SPRITES; i++)
|
2004-08-09 17:04:08 +00:00
|
|
|
if (_sprite_ptr[i] != NULL && _sprite_lru[i] != 65535)
|
|
|
|
_sprite_lru[i]++;
|
|
|
|
// Reset the lru counter.
|
|
|
|
_sprite_lru_counter = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Compact sprite cache every now and then.
|
|
|
|
if (++_compact_cache_counter >= 740) {
|
|
|
|
CompactSpriteCache();
|
|
|
|
_compact_cache_counter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when holes in the sprite cache should be removed.
|
|
|
|
// That is accomplished by moving the cached data.
|
2005-01-22 20:23:18 +00:00
|
|
|
static void CompactSpriteCache(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-02-11 13:35:27 +00:00
|
|
|
MemBlock *s;
|
2004-09-08 18:05:49 +00:00
|
|
|
|
2005-02-11 13:35:27 +00:00
|
|
|
DEBUG(spritecache, 2) (
|
|
|
|
"compacting sprite cache, inuse=%d", GetSpriteCacheUsage()
|
|
|
|
);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-02-11 13:35:27 +00:00
|
|
|
for (s = _spritecache_ptr; s->size != 0;) {
|
|
|
|
if (s->size & S_FREE_MASK) {
|
|
|
|
MemBlock* next = NextBlock(s);
|
|
|
|
MemBlock temp;
|
2005-07-05 19:54:35 +00:00
|
|
|
void** i;
|
2004-09-08 18:05:49 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Since free blocks are automatically coalesced, this should hold true.
|
2005-02-11 13:35:27 +00:00
|
|
|
assert(!(next->size & S_FREE_MASK));
|
2004-09-08 18:05:49 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// If the next block is the sentinel block, we can safely return
|
2005-02-11 13:35:27 +00:00
|
|
|
if (next->size == 0)
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
|
2005-02-11 13:35:27 +00:00
|
|
|
// Locate the sprite belonging to the next pointer.
|
|
|
|
for (i = _sprite_ptr; *i != next->data; ++i) {
|
|
|
|
assert(i != endof(_sprite_ptr));
|
|
|
|
}
|
2004-09-08 18:05:49 +00:00
|
|
|
|
2005-02-11 13:35:27 +00:00
|
|
|
*i = s->data; // Adjust sprite array entry
|
|
|
|
// Swap this and the next block
|
|
|
|
temp = *s;
|
|
|
|
memmove(s, next, next->size);
|
|
|
|
s = NextBlock(s);
|
|
|
|
*s = temp;
|
|
|
|
|
|
|
|
// Coalesce free blocks
|
|
|
|
while (NextBlock(s)->size & S_FREE_MASK) {
|
|
|
|
s->size += NextBlock(s)->size & ~S_FREE_MASK;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s = NextBlock(s);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static void DeleteEntryFromSpriteCache(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int best = -1;
|
2005-02-11 13:35:27 +00:00
|
|
|
MemBlock* s;
|
2004-08-09 17:04:08 +00:00
|
|
|
int cur_lru;
|
|
|
|
|
|
|
|
DEBUG(spritecache, 2) ("DeleteEntryFromSpriteCache, inuse=%d", GetSpriteCacheUsage());
|
|
|
|
|
|
|
|
#if defined(WANT_NEW_LRU)
|
|
|
|
cur_lru = 0xffff;
|
2005-07-24 15:56:31 +00:00
|
|
|
for (i = 0; i != MAX_SPRITES; i++) {
|
2005-08-07 11:01:31 +00:00
|
|
|
if (_sprite_ptr[i] != NULL && _sprite_lru_new[i] < cur_lru) {
|
2004-08-09 17:04:08 +00:00
|
|
|
cur_lru = _sprite_lru_new[i];
|
|
|
|
best = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
uint16 cur_lru = 0, cur_lru_cur = 0xffff;
|
2005-07-24 15:56:31 +00:00
|
|
|
for (i = 0; i != MAX_SPRITES; i++) {
|
2005-08-07 11:01:31 +00:00
|
|
|
if (_sprite_ptr[i] == NULL || _sprite_lru[i] < cur_lru) continue;
|
2004-09-08 18:05:49 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Found a sprite with a higher LRU value, then remember it.
|
|
|
|
if (_sprite_lru[i] != cur_lru) {
|
|
|
|
cur_lru = _sprite_lru[i];
|
|
|
|
best = i;
|
|
|
|
|
|
|
|
// Else if both sprites were very recently referenced, compare by the cur value instead.
|
|
|
|
} else if (cur_lru == 0 && _sprite_lru_cur[i] <= cur_lru_cur) {
|
|
|
|
cur_lru_cur = _sprite_lru_cur[i];
|
|
|
|
cur_lru = _sprite_lru[i];
|
|
|
|
best = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Display an error message and die, in case we found no sprite at all.
|
|
|
|
// This shouldn't really happen, unless all sprites are locked.
|
|
|
|
if (best == -1)
|
|
|
|
error("Out of sprite memory");
|
|
|
|
|
|
|
|
// Mark the block as free (the block must be in use)
|
2005-02-11 13:35:27 +00:00
|
|
|
s = (MemBlock*)_sprite_ptr[best] - 1;
|
|
|
|
assert(!(s->size & S_FREE_MASK));
|
|
|
|
s->size |= S_FREE_MASK;
|
2004-08-09 17:04:08 +00:00
|
|
|
_sprite_ptr[best] = NULL;
|
|
|
|
|
|
|
|
// And coalesce adjacent free blocks
|
2005-02-11 13:35:27 +00:00
|
|
|
for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
|
|
|
|
if (s->size & S_FREE_MASK) {
|
|
|
|
while (NextBlock(s)->size & S_FREE_MASK) {
|
|
|
|
s->size += NextBlock(s)->size & ~S_FREE_MASK;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-05 19:54:35 +00:00
|
|
|
static void* AllocSprite(size_t mem_req)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-07-05 19:54:35 +00:00
|
|
|
mem_req += sizeof(MemBlock);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-02-11 13:35:27 +00:00
|
|
|
/* Align this to an uint32 boundary. This also makes sure that the 2 least
|
|
|
|
* bits are not used, so we could use those for other things. */
|
2004-08-09 17:04:08 +00:00
|
|
|
mem_req = (mem_req + sizeof(uint32) - 1) & ~(sizeof(uint32) - 1);
|
|
|
|
|
2005-02-11 13:35:27 +00:00
|
|
|
for (;;) {
|
|
|
|
MemBlock* s;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-02-11 13:35:27 +00:00
|
|
|
for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
|
|
|
|
if (s->size & S_FREE_MASK) {
|
|
|
|
size_t cur_size = s->size & ~S_FREE_MASK;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-02-11 13:35:27 +00:00
|
|
|
/* Is the block exactly the size we need or
|
|
|
|
* big enough for an additional free block? */
|
|
|
|
if (cur_size == mem_req ||
|
|
|
|
cur_size >= mem_req + sizeof(MemBlock)) {
|
|
|
|
// Set size and in use
|
|
|
|
s->size = mem_req;
|
2004-09-08 18:05:49 +00:00
|
|
|
|
2005-02-11 13:35:27 +00:00
|
|
|
// Do we need to inject a free block too?
|
|
|
|
if (cur_size != mem_req) {
|
|
|
|
NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-02-11 13:35:27 +00:00
|
|
|
return s->data;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-08 18:05:49 +00:00
|
|
|
|
2005-02-11 13:35:27 +00:00
|
|
|
// Reached sentinel, but no block found yet. Delete some old entry.
|
|
|
|
DeleteEntryFromSpriteCache();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(NEW_ROTATION)
|
|
|
|
#define X15(x) else if (s >= x && s < (x+15)) { s = _rotate_tile_sprite[s - x] + x; }
|
|
|
|
#define X19(x) else if (s >= x && s < (x+19)) { s = _rotate_tile_sprite[s - x] + x; }
|
|
|
|
#define MAP(from,to,map) else if (s >= from && s <= to) { s = map[s - from] + from; }
|
|
|
|
|
|
|
|
|
2004-11-14 19:44:06 +00:00
|
|
|
static uint RotateSprite(uint s)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
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 };
|
|
|
|
static const byte _coast_map[9] = {0, 4, 3, 1, 2, 6, 8, 5, 7};
|
|
|
|
static const byte _fence_map[6] = {1, 0, 5, 4, 3, 2};
|
|
|
|
|
|
|
|
if (0);
|
|
|
|
X19(752)
|
|
|
|
X15(990-1)
|
|
|
|
X19(3924)
|
|
|
|
X19(3943)
|
|
|
|
X19(3962)
|
|
|
|
X19(3981)
|
|
|
|
X19(4000)
|
|
|
|
X19(4023)
|
|
|
|
X19(4042)
|
|
|
|
MAP(4061,4069,_coast_map)
|
|
|
|
X19(4126)
|
|
|
|
X19(4145)
|
|
|
|
X19(4164)
|
|
|
|
X19(4183)
|
|
|
|
X19(4202)
|
|
|
|
X19(4221)
|
|
|
|
X19(4240)
|
|
|
|
X19(4259)
|
|
|
|
X19(4259)
|
|
|
|
X19(4278)
|
|
|
|
MAP(4090, 4095, _fence_map)
|
|
|
|
MAP(4096, 4101, _fence_map)
|
|
|
|
MAP(4102, 4107, _fence_map)
|
|
|
|
MAP(4108, 4113, _fence_map)
|
|
|
|
MAP(4114, 4119, _fence_map)
|
|
|
|
MAP(4120, 4125, _fence_map)
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-02-13 08:12:03 +00:00
|
|
|
const void *GetRawSprite(SpriteID sprite)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-07-05 19:54:35 +00:00
|
|
|
void* p;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-24 15:56:31 +00:00
|
|
|
assert(sprite < MAX_SPRITES);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
#if defined(NEW_ROTATION)
|
|
|
|
sprite = RotateSprite(sprite);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Update LRU
|
|
|
|
#if defined(WANT_NEW_LRU)
|
|
|
|
_sprite_lru_new[sprite] = ++_sprite_lru_counter;
|
|
|
|
#else
|
2005-07-08 22:25:24 +00:00
|
|
|
_sprite_lru_cur[sprite] = ++_sprite_lru_counter;
|
2004-08-09 17:04:08 +00:00
|
|
|
_sprite_lru[sprite] = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
p = _sprite_ptr[sprite];
|
2005-07-05 19:54:35 +00:00
|
|
|
// Load the sprite, if it is not loaded, yet
|
|
|
|
if (p == NULL) p = ReadSprite(sprite);
|
2004-09-08 18:05:49 +00:00
|
|
|
return p;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-14 18:10:18 +00:00
|
|
|
void GfxInitSpriteMem(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
// initialize sprite cache heap
|
2005-08-13 21:40:22 +00:00
|
|
|
if (_spritecache_ptr == NULL) _spritecache_ptr = malloc(SPRITE_CACHE_SIZE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// A big free block
|
2005-08-13 21:40:22 +00:00
|
|
|
_spritecache_ptr->size = (SPRITE_CACHE_SIZE - sizeof(MemBlock)) | S_FREE_MASK;
|
2005-02-11 13:35:27 +00:00
|
|
|
// Sentinel block (identified by size == 0)
|
|
|
|
NextBlock(_spritecache_ptr)->size = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
memset(_sprite_ptr, 0, sizeof(_sprite_ptr));
|
|
|
|
|
2005-08-14 18:10:18 +00:00
|
|
|
_compact_cache_counter = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|