2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-04-17 20:23:13 +00:00
|
|
|
/** @file spritecache.cpp */
|
2007-04-04 01:35:16 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2007-06-13 16:21:11 +00:00
|
|
|
#include "variables.h"
|
|
|
|
#include "string.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"
|
2007-06-11 11:50:49 +00:00
|
|
|
#include "spriteloader/grf.hpp"
|
2007-06-13 18:52:06 +00:00
|
|
|
#ifdef WITH_PNG
|
2007-06-13 16:21:11 +00:00
|
|
|
#include "spriteloader/png.hpp"
|
2007-06-13 18:52:06 +00:00
|
|
|
#endif /* WITH_PNG */
|
2007-06-17 20:30:28 +00:00
|
|
|
#include "blitter/factory.hpp"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-11 11:50:49 +00:00
|
|
|
/* Default of 4MB spritecache */
|
|
|
|
uint _sprite_cache_size = 4;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct SpriteCache {
|
2007-01-03 14:42:08 +00:00
|
|
|
void *ptr;
|
|
|
|
uint32 file_pos;
|
|
|
|
int16 lru;
|
2007-06-13 16:21:11 +00:00
|
|
|
uint32 id;
|
|
|
|
const char *grf_name;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2007-01-03 14:42:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct MemBlock {
|
2005-02-11 13:35:27 +00:00
|
|
|
uint32 size;
|
|
|
|
byte data[VARARRAY_SIZE];
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2005-02-11 13:35:27 +00:00
|
|
|
|
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;
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void CompactSpriteCache();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static bool ReadSpriteHeaderSkipData()
|
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
|
|
|
}
|
|
|
|
|
2007-06-11 11:50:49 +00:00
|
|
|
void* AllocSprite(size_t);
|
2005-07-05 19:54:35 +00:00
|
|
|
|
2007-06-12 09:40:50 +00:00
|
|
|
static void* ReadSprite(SpriteCache *sc, SpriteID id, bool real_sprite)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-06-04 20:42:49 +00:00
|
|
|
uint32 file_pos = sc->file_pos;
|
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;
|
2007-06-04 20:42:49 +00:00
|
|
|
file_pos = GetSpriteCache(SPR_IMG_QUERY)->file_pos;
|
2005-02-17 15:53:47 +00:00
|
|
|
}
|
|
|
|
|
2007-06-13 16:21:11 +00:00
|
|
|
if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 32) {
|
2007-06-13 19:34:33 +00:00
|
|
|
#ifdef WITH_PNG
|
2007-06-13 16:21:11 +00:00
|
|
|
/* Try loading 32bpp graphics in case we are 32bpp output */
|
|
|
|
SpriteLoaderPNG sprite_loader;
|
|
|
|
SpriteLoader::Sprite sprite;
|
|
|
|
|
|
|
|
if (sprite_loader.LoadSprite(&sprite, sc->grf_name, sc->id)) {
|
|
|
|
sc->ptr = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, &AllocSprite);
|
|
|
|
free(sprite.data);
|
|
|
|
|
|
|
|
return sc->ptr;
|
|
|
|
}
|
|
|
|
/* If the PNG couldn't be loaded, fall back to 8bpp grfs */
|
2007-06-13 19:34:33 +00:00
|
|
|
#else
|
|
|
|
static bool show_once = true;
|
|
|
|
if (show_once) {
|
|
|
|
DEBUG(misc, 0, "You are running a 32bpp blitter, but this build is without libpng support; falling back to 8bpp graphics");
|
|
|
|
show_once = false;
|
|
|
|
}
|
2007-06-13 18:52:06 +00:00
|
|
|
#endif /* WITH_PNG */
|
2007-06-13 19:34:33 +00:00
|
|
|
}
|
2007-06-13 16:21:11 +00:00
|
|
|
|
2007-06-04 20:42:49 +00:00
|
|
|
FioSeekToFile(file_pos);
|
2005-02-11 13:46:25 +00:00
|
|
|
|
2007-06-11 11:50:49 +00:00
|
|
|
/* Read the size and type */
|
|
|
|
int num = FioReadWord();
|
|
|
|
byte type = FioReadByte();
|
|
|
|
/* Type 0xFF indicates either a colormap or some other non-sprite info */
|
2005-07-05 19:54:35 +00:00
|
|
|
if (type == 0xFF) {
|
2007-06-12 09:40:50 +00:00
|
|
|
if (real_sprite) {
|
|
|
|
static byte warning_level = 0;
|
|
|
|
DEBUG(sprite, warning_level, "Tried to load non sprite #%d as a real sprite. Probable cause: NewGRF interference", id);
|
|
|
|
warning_level = 6;
|
|
|
|
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?");
|
|
|
|
return (void*)GetSprite(SPR_IMG_QUERY);
|
|
|
|
}
|
|
|
|
|
2007-06-11 11:50:49 +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);
|
|
|
|
|
2007-06-11 18:49:55 +00:00
|
|
|
return sc->ptr;
|
|
|
|
}
|
|
|
|
/* Ugly hack to work around the problem that the old landscape
|
|
|
|
* generator assumes that those sprites are stored uncompressed in
|
|
|
|
* the memory, and they are only read directly by the code, never
|
|
|
|
* send to the blitter. So do not send it to the blitter (which will
|
|
|
|
* result in a data array in the format the blitter likes most), but
|
|
|
|
* read the data directly from disk and store that as sprite.
|
|
|
|
* Ugly: yes. Other solution: no. Blame the original author or
|
|
|
|
* something ;) The image should really have been a data-stream
|
|
|
|
* (so type = 0xFF basicly). */
|
|
|
|
if (id >= 4845 && id <= 4881) {
|
|
|
|
uint height = FioReadByte();
|
|
|
|
uint width = FioReadWord();
|
|
|
|
Sprite *sprite;
|
|
|
|
byte *dest;
|
|
|
|
|
|
|
|
num = width * height;
|
|
|
|
sprite = (Sprite *)AllocSprite(sizeof(*sprite) + num);
|
|
|
|
sc->ptr = sprite;
|
|
|
|
sprite->height = height;
|
|
|
|
sprite->width = width;
|
|
|
|
sprite->x_offs = FioReadWord();
|
|
|
|
sprite->y_offs = FioReadWord();
|
|
|
|
|
|
|
|
dest = sprite->data;
|
|
|
|
while (num > 0) {
|
|
|
|
int8 i = FioReadByte();
|
|
|
|
if (i >= 0) {
|
|
|
|
num -= i;
|
|
|
|
for (; i > 0; --i) *dest++ = FioReadByte();
|
|
|
|
} else {
|
|
|
|
const byte* rel = dest - (((i & 7) << 8) | FioReadByte());
|
|
|
|
i = -(i >> 3);
|
|
|
|
num -= i;
|
|
|
|
for (; i > 0; --i) *dest++ = *rel++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sc->ptr;
|
2007-06-11 11:50:49 +00:00
|
|
|
}
|
2005-02-11 14:33:43 +00:00
|
|
|
|
2007-06-12 09:40:50 +00:00
|
|
|
if (!real_sprite) {
|
|
|
|
static byte warning_level = 0;
|
|
|
|
DEBUG(sprite, warning_level, "Tried to load real sprite #%d as a non sprite. Probable cause: NewGRF interference", id);
|
|
|
|
warning_level = 6;
|
|
|
|
}
|
|
|
|
|
2007-06-11 11:50:49 +00:00
|
|
|
SpriteLoaderGrf sprite_loader;
|
|
|
|
SpriteLoader::Sprite sprite;
|
2004-09-08 18:05:49 +00:00
|
|
|
|
2007-06-13 16:21:11 +00:00
|
|
|
if (!sprite_loader.LoadSprite(&sprite, sc->grf_name, file_pos)) return NULL;
|
2007-06-11 11:50:49 +00:00
|
|
|
if (id == 142) sprite.height = 10; // Compensate for a TTD bug
|
2007-06-11 13:38:11 +00:00
|
|
|
sc->ptr = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, &AllocSprite);
|
2007-06-11 11:50:49 +00:00
|
|
|
free(sprite.data);
|
2005-07-05 19:54:35 +00:00
|
|
|
|
2007-06-11 11:50:49 +00:00
|
|
|
return sc->ptr;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-14 14:31:48 +00:00
|
|
|
bool LoadNextSprite(int load_index, byte file_index, uint file_sprite_id)
|
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;
|
2007-06-14 14:31:48 +00:00
|
|
|
sc->id = file_sprite_id;
|
2007-06-13 16:21:11 +00:00
|
|
|
|
2007-06-13 19:05:42 +00:00
|
|
|
const char *fio_grf_name = FioGetFilename();
|
|
|
|
const char *t = strrchr(fio_grf_name, PATHSEPCHAR);
|
|
|
|
char *grf_name;
|
|
|
|
if (t == NULL) grf_name = strdup(fio_grf_name);
|
|
|
|
else grf_name = strdup(t);
|
|
|
|
/* Make the string lowercase and strip extension */
|
|
|
|
char *t2 = strrchr(grf_name, '.');
|
|
|
|
if (t2 != NULL) *t2 = '\0';
|
2007-06-13 16:21:11 +00:00
|
|
|
strtolower(grf_name);
|
2007-06-13 19:05:42 +00:00
|
|
|
|
2007-06-13 16:21:11 +00:00
|
|
|
free((char *)sc->grf_name);
|
|
|
|
sc->grf_name = grf_name;
|
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;
|
2007-06-13 16:21:11 +00:00
|
|
|
scnew->id = scold->id;
|
|
|
|
scnew->grf_name = strdup(scold->grf_name);
|
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
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static uint32 GetSpriteCacheUsage()
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void IncreaseSpriteLRU()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Increase all LRU values */
|
2004-08-09 17:04:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Compact sprite cache every now and then. */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (++_compact_cache_counter >= 740) {
|
|
|
|
CompactSpriteCache();
|
|
|
|
_compact_cache_counter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/** Called when holes in the sprite cache should be removed.
|
|
|
|
* That is accomplished by moving the cached data. */
|
2007-03-07 11:47:46 +00:00
|
|
|
static void CompactSpriteCache()
|
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
|
|
|
|
2007-04-04 01:35:16 +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
|
|
|
|
2007-04-04 01:35:16 +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;
|
|
|
|
|
2007-04-04 01:35:16 +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
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Swap this and the next block */
|
2005-02-11 13:35:27 +00:00
|
|
|
temp = *s;
|
|
|
|
memmove(s, next, next->size);
|
|
|
|
s = NextBlock(s);
|
|
|
|
*s = temp;
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Coalesce free blocks */
|
2005-02-11 13:35:27 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void DeleteEntryFromSpriteCache()
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* 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");
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* 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
|
|
|
|
2007-04-04 01:35:16 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-11 11:50:49 +00:00
|
|
|
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)) {
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Set size and in use */
|
2005-02-11 13:35:27 +00:00
|
|
|
s->size = mem_req;
|
2004-09-08 18:05:49 +00:00
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Do we need to inject a free block too? */
|
2005-02-11 13:35:27 +00:00
|
|
|
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
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Reached sentinel, but no block found yet. Delete some old entry. */
|
2005-02-11 13:35:27 +00:00
|
|
|
DeleteEntryFromSpriteCache();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-12 09:40:50 +00:00
|
|
|
const void *GetRawSprite(SpriteID sprite, bool real_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);
|
|
|
|
|
2007-04-04 01:35:16 +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
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Load the sprite, if it is not loaded, yet */
|
2007-06-12 09:40:50 +00:00
|
|
|
if (p == NULL) p = ReadSprite(sc, sprite, real_sprite);
|
2004-09-08 18:05:49 +00:00
|
|
|
return p;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void GfxInitSpriteMem()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-04-04 01:35:16 +00:00
|
|
|
/* initialize sprite cache heap */
|
2007-06-05 10:40:29 +00:00
|
|
|
if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)malloc(_sprite_cache_size * 1024 * 1024);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* A big free block */
|
2007-06-05 10:40:29 +00:00
|
|
|
_spritecache_ptr->size = ((_sprite_cache_size * 1024 * 1024) - sizeof(MemBlock)) | S_FREE_MASK;
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Sentinel block (identified by size == 0) */
|
2005-02-11 13:35:27 +00:00
|
|
|
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
|
|
|
}
|