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-10-22 06:39:32 +00:00
|
|
|
#include "macros.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"
|
2007-01-10 18:56:51 +00:00
|
|
|
#include "helpers.hpp"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-01-17 23:25:19 +00:00
|
|
|
#ifndef SPRITE_CACHE_SIZE
|
|
|
|
# define SPRITE_CACHE_SIZE 2*1024*1024
|
|
|
|
#endif /* SPRITE_CACHE_SIZE */
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
|
2007-01-03 14:42:08 +00:00
|
|
|
typedef struct SpriteCache {
|
|
|
|
void *ptr;
|
|
|
|
uint32 file_pos;
|
|
|
|
int16 lru;
|
|
|
|
} SpriteCache;
|
|
|
|
|
|
|
|
|
|
|
|
static uint _spritecache_items = 0;
|
|
|
|
static SpriteCache *_spritecache = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
static inline SpriteCache *GetSpriteCache(uint index)
|
|
|
|
{
|
|
|
|
return &_spritecache[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static SpriteCache *AllocateSpriteCache(uint index)
|
|
|
|
{
|
|
|
|
if (index >= _spritecache_items) {
|
|
|
|
/* Add another 1024 items to the 'pool' */
|
|
|
|
uint items = ALIGN(index + 1, 1024);
|
|
|
|
|
|
|
|
DEBUG(sprite, 4, "Increasing sprite cache to %d items (%d bytes)", items, items * sizeof(*_spritecache));
|
|
|
|
|
2007-01-11 17:29:39 +00:00
|
|
|
_spritecache = ReallocT(_spritecache, items);
|
2007-01-03 14:42:08 +00:00
|
|
|
|
|
|
|
if (_spritecache == NULL) {
|
|
|
|
error("Unable to allocate sprite cache of %d items (%d bytes)", items, items * sizeof(*_spritecache));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the new items and update the count */
|
|
|
|
memset(_spritecache + _spritecache_items, 0, (items - _spritecache_items) * sizeof(*_spritecache));
|
|
|
|
_spritecache_items = items;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetSpriteCache(index);
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
|
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);
|
2006-11-23 21:19:43 +00:00
|
|
|
/* Some NewGRF files have "empty" pseudo-sprites which are 1
|
|
|
|
* byte long. Catch these so the sprites won't be displayed. */
|
|
|
|
return num != 1;
|
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
|
|
|
}
|
|
|
|
|
2006-04-16 11:26:23 +00:00
|
|
|
/* Check if the given Sprite ID exists */
|
|
|
|
bool SpriteExists(SpriteID id)
|
|
|
|
{
|
|
|
|
/* Special case for Sprite ID zero -- its position is also 0... */
|
2007-01-03 14:42:08 +00:00
|
|
|
if (id == 0) return true;
|
2007-01-16 22:10:35 +00:00
|
|
|
if (id >= _spritecache_items) return false;
|
2007-01-03 14:42:08 +00:00
|
|
|
return GetSpriteCache(id)->file_pos != 0;
|
2006-04-16 11:26:23 +00:00
|
|
|
}
|
|
|
|
|
2005-07-05 19:54:35 +00:00
|
|
|
static void* AllocSprite(size_t);
|
|
|
|
|
2007-01-03 14:42:08 +00:00
|
|
|
static void* ReadSprite(SpriteCache *sc, 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
|
|
|
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(sprite, 9, "Load sprite %d", id);
|
2004-09-08 18:05:49 +00:00
|
|
|
|
2006-04-16 11:26:23 +00:00
|
|
|
if (!SpriteExists(id)) {
|
2007-02-08 14:02:12 +00:00
|
|
|
DEBUG(sprite, 1, "Tried to load non-existing sprite #%d. Probable cause: Wrong/missing NewGRFs", id);
|
|
|
|
|
|
|
|
/* SPR_IMG_QUERY is a BIG FAT RED ? */
|
|
|
|
id = SPR_IMG_QUERY;
|
|
|
|
sc = GetSpriteCache(SPR_IMG_QUERY);
|
2005-02-17 15:53:47 +00:00
|
|
|
}
|
|
|
|
|
2007-01-03 14:42:08 +00:00
|
|
|
FioSeekToFile(sc->file_pos);
|
2005-02-11 13:46:25 +00:00
|
|
|
|
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) {
|
2007-01-10 18:56:51 +00:00
|
|
|
byte* dest = (byte*)AllocSprite(num);
|
2005-07-05 19:54:35 +00:00
|
|
|
|
2007-01-03 14:42:08 +00:00
|
|
|
sc->ptr = dest;
|
2005-07-05 19:54:35 +00:00
|
|
|
FioReadBlock(dest, num);
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
} else {
|
|
|
|
uint height = FioReadByte();
|
|
|
|
uint width = FioReadWord();
|
|
|
|
Sprite* sprite;
|
|
|
|
byte* dest;
|
|
|
|
|
|
|
|
num = (type & 0x02) ? width * height : num - 8;
|
2007-01-10 18:56:51 +00:00
|
|
|
sprite = (Sprite*)AllocSprite(sizeof(*sprite) + num);
|
2007-01-03 14:42:08 +00:00
|
|
|
sc->ptr = sprite;
|
2005-07-05 19:54:35 +00:00
|
|
|
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
|
|
|
{
|
2007-01-03 14:42:08 +00:00
|
|
|
SpriteCache *sc;
|
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
|
|
|
|
2006-04-20 05:57:47 +00:00
|
|
|
if (load_index >= MAX_SPRITES) {
|
|
|
|
error("Tried to load too many sprites (#%d; max %d)", load_index, MAX_SPRITES);
|
|
|
|
}
|
|
|
|
|
2007-01-03 14:42:08 +00:00
|
|
|
sc = AllocateSpriteCache(load_index);
|
|
|
|
sc->file_pos = file_pos;
|
|
|
|
sc->ptr = NULL;
|
|
|
|
sc->lru = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-09-10 08:17:30 +00:00
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
void DupSprite(SpriteID old_spr, SpriteID new_spr)
|
2005-09-10 08:17:30 +00:00
|
|
|
{
|
2007-01-10 18:56:51 +00:00
|
|
|
SpriteCache *scold = GetSpriteCache(old_spr);
|
|
|
|
SpriteCache *scnew = AllocateSpriteCache(new_spr);
|
2007-01-03 14:42:08 +00:00
|
|
|
|
|
|
|
scnew->file_pos = scold->file_pos;
|
|
|
|
scnew->ptr = NULL;
|
2005-09-10 08:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2006-08-20 12:09:32 +00:00
|
|
|
uint32 tot_size = 0;
|
2005-02-11 13:35:27 +00:00
|
|
|
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
|
|
|
{
|
|
|
|
// Increase all LRU values
|
|
|
|
if (_sprite_lru_counter > 16384) {
|
2007-01-03 14:42:08 +00:00
|
|
|
SpriteID i;
|
|
|
|
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(sprite, 3, "Fixing lru %d, inuse=%d", _sprite_lru_counter, GetSpriteCacheUsage());
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-01-03 14:42:08 +00:00
|
|
|
for (i = 0; i != _spritecache_items; i++) {
|
|
|
|
SpriteCache *sc = GetSpriteCache(i);
|
|
|
|
if (sc->ptr != NULL) {
|
|
|
|
if (sc->lru >= 0) {
|
|
|
|
sc->lru = -1;
|
|
|
|
} else if (sc->lru != -32768) {
|
|
|
|
sc->lru--;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2007-01-03 14:42:08 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
_sprite_lru_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(sprite, 3, "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;
|
2007-01-03 14:42:08 +00:00
|
|
|
SpriteID 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.
|
2007-01-03 14:42:08 +00:00
|
|
|
for (i = 0; GetSpriteCache(i)->ptr != next->data; i++) {
|
|
|
|
assert(i != _spritecache_items);
|
2005-02-11 13:35:27 +00:00
|
|
|
}
|
2004-09-08 18:05:49 +00:00
|
|
|
|
2007-01-03 14:42:08 +00:00
|
|
|
GetSpriteCache(i)->ptr = s->data; // Adjust sprite array entry
|
2005-02-11 13:35:27 +00:00
|
|
|
// 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
|
|
|
{
|
2007-01-03 14:42:08 +00:00
|
|
|
SpriteID i;
|
2007-01-10 18:56:51 +00:00
|
|
|
uint best = UINT_MAX;
|
2005-02-11 13:35:27 +00:00
|
|
|
MemBlock* s;
|
2004-08-09 17:04:08 +00:00
|
|
|
int cur_lru;
|
|
|
|
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(sprite, 3, "DeleteEntryFromSpriteCache, inuse=%d", GetSpriteCacheUsage());
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
cur_lru = 0xffff;
|
2007-01-03 14:42:08 +00:00
|
|
|
for (i = 0; i != _spritecache_items; i++) {
|
|
|
|
SpriteCache *sc = GetSpriteCache(i);
|
|
|
|
if (sc->ptr != NULL && sc->lru < cur_lru) {
|
|
|
|
cur_lru = sc->lru;
|
2004-08-09 17:04:08 +00:00
|
|
|
best = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display an error message and die, in case we found no sprite at all.
|
|
|
|
// This shouldn't really happen, unless all sprites are locked.
|
2007-01-03 14:42:08 +00:00
|
|
|
if (best == (uint)-1)
|
2004-08-09 17:04:08 +00:00
|
|
|
error("Out of sprite memory");
|
|
|
|
|
|
|
|
// Mark the block as free (the block must be in use)
|
2007-01-03 14:42:08 +00:00
|
|
|
s = (MemBlock*)GetSpriteCache(best)->ptr - 1;
|
2005-02-11 13:35:27 +00:00
|
|
|
assert(!(s->size & S_FREE_MASK));
|
|
|
|
s->size |= S_FREE_MASK;
|
2007-01-03 14:42:08 +00:00
|
|
|
GetSpriteCache(best)->ptr = NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// 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. */
|
2005-10-22 06:39:32 +00:00
|
|
|
mem_req = ALIGN(mem_req, sizeof(uint32));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-13 08:12:03 +00:00
|
|
|
const void *GetRawSprite(SpriteID sprite)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-01-03 14:42:08 +00:00
|
|
|
SpriteCache *sc;
|
2005-07-05 19:54:35 +00:00
|
|
|
void* p;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-01-16 22:10:35 +00:00
|
|
|
assert(sprite < _spritecache_items);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-01-03 14:42:08 +00:00
|
|
|
sc = GetSpriteCache(sprite);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Update LRU
|
2007-01-03 14:42:08 +00:00
|
|
|
sc->lru = ++_sprite_lru_counter;
|
|
|
|
|
|
|
|
p = sc->ptr;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-05 19:54:35 +00:00
|
|
|
// Load the sprite, if it is not loaded, yet
|
2007-01-03 14:42:08 +00:00
|
|
|
if (p == NULL) p = ReadSprite(sc, 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
|
2007-01-10 18:56:51 +00:00
|
|
|
if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)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
|
|
|
|
2007-01-03 14:42:08 +00:00
|
|
|
/* Reset the spritecache 'pool' */
|
|
|
|
free(_spritecache);
|
|
|
|
_spritecache_items = 0;
|
|
|
|
_spritecache = NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-08-14 18:10:18 +00:00
|
|
|
_compact_cache_counter = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|