2005-07-24 14:12:37 +00:00
/* $Id$ */
2008-05-06 15:11:33 +00:00
/** @file spritecache.cpp Caching of sprites. */
2007-04-04 01:35:16 +00:00
2004-08-09 17:04:08 +00:00
# include "stdafx.h"
2009-01-31 20:16:06 +00:00
# include "gfx_type.h"
2008-08-31 10:50:05 +00:00
# include "fileio_func.h"
2007-06-11 11:50:49 +00:00
# include "spriteloader/grf.hpp"
2007-12-25 09:48:53 +00:00
# include "core/alloc_func.hpp"
2008-09-02 18:45:15 +00:00
# include "gfx_func.h"
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"
2009-01-11 22:28:30 +00:00
# include "core/math_func.hpp"
2004-08-09 17:04:08 +00:00
2008-01-13 01:21:35 +00:00
# include "table/sprites.h"
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
2009-01-12 17:31:43 +00:00
typedef SimpleTinyEnumT < SpriteType , byte > SpriteTypeByte ;
2004-08-09 17:04:08 +00:00
2007-03-07 12:11:48 +00:00
struct SpriteCache {
2008-03-26 19:18:30 +00:00
void * ptr ;
2008-05-27 21:41:00 +00:00
size_t file_pos ;
2008-07-04 14:45:51 +00:00
uint32 id ;
2008-01-21 23:55:57 +00:00
uint16 file_slot ;
2008-03-26 19:18:30 +00:00
int16 lru ;
2009-01-12 17:31:43 +00:00
SpriteTypeByte type ; ///< In some cases a single sprite is misused by two NewGRFs. Once as real sprite and once as recolour sprite. If the recolour sprite gets into the cache it might be drawn as real sprite which causes enormous trouble.
bool warned ; ///< True iff the user has been warned about incorrect use of this sprite
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 ] ;
}
2009-01-11 22:28:30 +00:00
static inline bool IsMapgenSpriteID ( SpriteID sprite )
{
return IsInsideMM ( sprite , 4845 , 4882 ) ;
}
2007-01-03 14:42:08 +00:00
static SpriteCache * AllocateSpriteCache ( uint index )
{
if ( index > = _spritecache_items ) {
/* Add another 1024 items to the 'pool' */
2007-11-19 20:40:14 +00:00
uint items = Align ( index + 1 , 1024 ) ;
2007-01-03 14:42:08 +00:00
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
/* 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 {
2008-05-08 13:20:54 +00:00
size_t size ;
2005-02-11 13:35:27 +00:00
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
2008-08-30 09:46:52 +00:00
/**
* Skip the given amount of sprite graphics data .
* @ param type the type of sprite ( compressed etc )
* @ param num the amount of sprites to skip
*/
void SkipSpriteData ( byte type , uint16 num )
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 ) {
2008-08-30 12:14:56 +00:00
int size = ( i = = 0 ) ? 0x80 : i ;
num - = size ;
FioSkipBytes ( size ) ;
2005-02-11 14:33:43 +00:00
} else {
i = - ( i > > 3 ) ;
num - = i ;
FioReadByte ( ) ;
}
2004-08-09 17:04:08 +00:00
}
}
2008-08-30 09:46:52 +00:00
}
/**
* Read the sprite header data and then skip the real payload .
2009-01-11 22:28:30 +00:00
* @ return type of sprite ; ST_INVALID if the sprite is a pseudo - or unusable sprite
2008-08-30 09:46:52 +00:00
*/
2009-01-11 22:28:30 +00:00
static SpriteType ReadSpriteHeaderSkipData ( )
2008-08-30 09:46:52 +00:00
{
uint16 num = FioReadWord ( ) ;
2009-01-11 22:28:30 +00:00
if ( num = = 0 ) return ST_INVALID ;
2008-08-30 09:46:52 +00:00
2009-01-11 22:28:30 +00:00
byte type = FioReadByte ( ) ;
2008-08-30 09:46:52 +00:00
if ( type = = 0xFF ) {
FioSkipBytes ( num ) ;
/* Some NewGRF files have "empty" pseudo-sprites which are 1
* byte long . Catch these so the sprites won ' t be displayed . */
2009-01-11 22:28:30 +00:00
return ( num = = 1 ) ? ST_INVALID : ST_RECOLOUR ;
2008-08-30 09:46:52 +00:00
}
FioSkipBytes ( 7 ) ;
SkipSpriteData ( type , num - 8 ) ;
2005-08-11 13:09:12 +00:00
2009-01-11 22:28:30 +00:00
return ST_NORMAL ;
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-09-13 22:48:11 +00:00
return ! ( GetSpriteCache ( id ) - > file_pos = = 0 & & GetSpriteCache ( id ) - > file_slot = = 0 ) ;
2006-04-16 11:26:23 +00:00
}
2009-01-10 00:31:47 +00:00
void * AllocSprite ( size_t ) ;
2005-07-05 19:54:35 +00:00
2009-01-10 00:31:47 +00:00
static void * ReadSprite ( SpriteCache * sc , SpriteID id , SpriteType sprite_type )
2004-08-09 17:04:08 +00:00
{
2007-09-13 18:22:34 +00:00
uint8 file_slot = sc - > file_slot ;
2008-05-27 21:41:00 +00:00
size_t file_pos = sc - > file_pos ;
2005-07-05 19:54:35 +00:00
2009-01-11 22:28:30 +00:00
assert ( IsMapgenSpriteID ( id ) = = ( sprite_type = = ST_MAPGEN ) ) ;
assert ( sc - > type = = sprite_type ) ;
2007-02-08 14:02:12 +00:00
2009-01-11 22:28:30 +00:00
DEBUG ( sprite , 9 , " Load sprite %d " , id ) ;
2005-02-17 15:53:47 +00:00
2008-09-02 15:20:38 +00:00
if ( sprite_type = = ST_NORMAL & & 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 ;
2008-09-02 15:20:38 +00:00
if ( sprite_loader . LoadSprite ( & sprite , file_slot , sc - > id , sprite_type ) ) {
2007-06-13 16:21:11 +00:00
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-09-13 18:22:34 +00:00
FioSeekToFile ( file_slot , 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 ( ) ;
2009-01-11 22:28:30 +00:00
2009-02-09 02:57:15 +00:00
/* Type 0xFF indicates either a colourmap or some other non-sprite info */
2009-01-11 22:28:30 +00:00
assert ( ( type = = 0xFF ) = = ( sprite_type = = ST_RECOLOUR ) ) ;
2005-07-05 19:54:35 +00:00
if ( type = = 0xFF ) {
2008-09-02 18:45:15 +00:00
/* "Normal" recolour sprites are ALWAYS 257 bytes. Then there is a small
* number of recolour sprites that are 17 bytes that only exist in DOS
* GRFs which are the same as 257 byte recolour sprites , but with the last
* 240 bytes zeroed . */
static const int RECOLOUR_SPRITE_SIZE = 257 ;
byte * dest = ( byte * ) AllocSprite ( max ( RECOLOUR_SPRITE_SIZE , num ) ) ;
2005-07-05 19:54:35 +00:00
2007-01-03 14:42:08 +00:00
sc - > ptr = dest ;
2008-09-02 18:45:15 +00:00
if ( _palette_remap_grf [ sc - > file_slot ] ) {
byte * dest_tmp = AllocaM ( byte , max ( RECOLOUR_SPRITE_SIZE , num ) ) ;
/* Only a few recolour sprites are less than 257 bytes */
if ( num < RECOLOUR_SPRITE_SIZE ) memset ( dest_tmp , 0 , RECOLOUR_SPRITE_SIZE ) ;
FioReadBlock ( dest_tmp , num ) ;
/* The data of index 0 is never used; "literal 00" according to the (New)GRF specs. */
for ( int i = 1 ; i < RECOLOUR_SPRITE_SIZE ; i + + ) {
dest [ i ] = _palette_remap [ dest_tmp [ _palette_reverse_remap [ i - 1 ] + 1 ] ] ;
}
} else {
FioReadBlock ( dest , num ) ;
}
2005-07-05 19:54:35 +00:00
2007-06-11 18:49:55 +00:00
return sc - > ptr ;
}
2009-01-11 22:28:30 +00:00
2007-06-11 18:49:55 +00:00
/* 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 ) . */
2008-09-02 15:20:38 +00:00
if ( sprite_type = = ST_MAPGEN ) {
2007-06-11 18:49:55 +00:00
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 {
2009-01-10 00:31:47 +00:00
const byte * rel = dest - ( ( ( i & 7 ) < < 8 ) | FioReadByte ( ) ) ;
2007-06-11 18:49:55 +00:00
i = - ( i > > 3 ) ;
num - = i ;
for ( ; i > 0 ; - - i ) * dest + + = * rel + + ;
}
}
2008-09-02 15:20:38 +00:00
sc - > type = sprite_type ;
2008-07-04 14:45:51 +00:00
2007-06-11 18:49:55 +00:00
return sc - > ptr ;
2007-06-11 11:50:49 +00:00
}
2005-02-11 14:33:43 +00:00
2009-02-07 02:29:08 +00:00
assert ( sprite_type = = ST_NORMAL | | sprite_type = = ST_FONT ) ;
2007-06-12 09:40:50 +00:00
2007-06-11 11:50:49 +00:00
SpriteLoaderGrf sprite_loader ;
SpriteLoader : : Sprite sprite ;
2004-09-08 18:05:49 +00:00
2008-11-23 13:42:05 +00:00
if ( ! sprite_loader . LoadSprite ( & sprite , file_slot , file_pos , sprite_type ) ) {
if ( id = = SPR_IMG_QUERY ) usererror ( " Okay... something went horribly wrong. I couldn't load the fallback sprite. What should I do? " ) ;
return ( void * ) GetRawSprite ( SPR_IMG_QUERY , ST_NORMAL ) ;
}
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-09-13 18:22:34 +00:00
bool LoadNextSprite ( int load_index , byte file_slot , uint file_sprite_id )
2004-08-09 17:04:08 +00:00
{
2008-05-27 21:41:00 +00:00
size_t file_pos = FioGetPos ( ) ;
2004-08-09 17:04:08 +00:00
2009-01-11 22:28:30 +00:00
SpriteType type = ReadSpriteHeaderSkipData ( ) ;
if ( type = = ST_INVALID ) return false ;
2004-11-12 18:47:19 +00:00
2006-04-20 05:57:47 +00:00
if ( load_index > = MAX_SPRITES ) {
2008-06-05 20:54:52 +00:00
usererror ( " Tried to load too many sprites (#%d; max %d) " , load_index , MAX_SPRITES ) ;
2006-04-20 05:57:47 +00:00
}
2009-01-11 22:28:30 +00:00
bool is_mapgen = IsMapgenSpriteID ( load_index ) ;
if ( is_mapgen ) {
if ( type ! = ST_NORMAL ) usererror ( " Uhm, would you be so kind not to load a NewGRF that changes the type of the map generator sprites? " ) ;
type = ST_MAPGEN ;
}
SpriteCache * sc = AllocateSpriteCache ( load_index ) ;
2007-09-13 18:22:34 +00:00
sc - > file_slot = file_slot ;
2007-01-03 14:42:08 +00:00
sc - > file_pos = file_pos ;
sc - > ptr = NULL ;
sc - > lru = 0 ;
2007-06-14 14:31:48 +00:00
sc - > id = file_sprite_id ;
2009-01-11 22:28:30 +00:00
sc - > type = type ;
2009-01-11 23:49:51 +00:00
sc - > warned = false ;
2007-06-13 16:21:11 +00:00
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
{
2008-01-29 00:29:28 +00:00
SpriteCache * scnew = AllocateSpriteCache ( new_spr ) ; // may reallocate: so put it first
2007-01-10 18:56:51 +00:00
SpriteCache * scold = GetSpriteCache ( old_spr ) ;
2007-01-03 14:42:08 +00:00
2007-09-13 18:22:34 +00:00
scnew - > file_slot = scold - > file_slot ;
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 ;
2008-09-02 15:20:38 +00:00
scnew - > type = scold - > type ;
2009-01-11 23:49:51 +00:00
scnew - > warned = false ;
2005-09-10 08:17:30 +00:00
}
2004-08-09 17:04:08 +00:00
# define S_FREE_MASK 1
2005-02-11 13:35:27 +00:00
2009-01-10 00:31:47 +00:00
static inline MemBlock * NextBlock ( MemBlock * block )
2005-02-11 13:35:27 +00:00
{
return ( MemBlock * ) ( ( byte * ) block + ( block - > size & ~ S_FREE_MASK ) ) ;
}
2004-08-09 17:04:08 +00:00
2008-05-27 21:41:00 +00:00
static size_t GetSpriteCacheUsage ( )
2004-08-09 17:04:08 +00:00
{
2008-05-27 21:41:00 +00:00
size_t tot_size = 0 ;
2009-01-10 00:31:47 +00:00
MemBlock * s ;
2005-02-11 13:35:27 +00:00
2008-02-14 15:13:36 +00:00
for ( s = _spritecache_ptr ; s - > size ! = 0 ; s = NextBlock ( s ) ) {
2005-02-11 13:35:27 +00:00
if ( ! ( s - > size & S_FREE_MASK ) ) tot_size + = s - > size ;
2008-02-14 15:13:36 +00:00
}
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 ) {
2009-01-10 00:31:47 +00:00
MemBlock * next = NextBlock ( s ) ;
2005-02-11 13:35:27 +00:00
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 */
2008-03-26 19:18:30 +00:00
if ( next - > size = = 0 ) break ;
2004-08-09 17:04:08 +00:00
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 ;
2009-01-10 00:31:47 +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 . */
2008-10-14 18:38:51 +00:00
if ( best = = UINT_MAX ) error ( " Out of sprite memory " ) ;
2004-08-09 17:04:08 +00:00
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
}
}
}
}
2009-01-10 00:31:47 +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 . */
2007-11-19 20:40:14 +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 ( ; ; ) {
2009-01-10 00:31:47 +00:00
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
}
}
2009-01-11 22:28:30 +00:00
/** Handles the case when a sprite of different type is requested than is present in the SpriteCache.
* For ST_FONT sprites , it is normal . In other cases , default sprite is loaded instead .
* @ param sprite ID of loaded sprite
* @ param requested requested sprite type
* @ param available available sprite type
* @ return fallback sprite
* @ note this function will do usererror ( ) in the case the fallback sprite isn ' t available */
2009-01-11 23:49:51 +00:00
static const void * HandleInvalidSpriteRequest ( SpriteID sprite , SpriteType requested , SpriteCache * sc )
2009-01-11 22:28:30 +00:00
{
static const char * sprite_types [ ] = {
" normal " , // ST_NORMAL
" map generator " , // ST_MAPGEN
" character " , // ST_FONT
" recolour " , // ST_RECOLOUR
} ;
2009-01-11 23:49:51 +00:00
SpriteType available = sc - > type ;
2009-02-07 02:29:08 +00:00
if ( requested = = ST_FONT & & available = = ST_NORMAL ) {
if ( sc - > ptr = = NULL ) sc - > type = ST_FONT ;
return GetRawSprite ( sprite , sc - > type ) ;
}
2009-01-11 22:28:30 +00:00
2009-01-11 23:49:51 +00:00
byte warning_level = sc - > warned ? 6 : 0 ;
sc - > warned = true ;
2009-01-11 22:28:30 +00:00
DEBUG ( sprite , warning_level , " Tried to load %s sprite #%d as a %s sprite. Probable cause: NewGRF interference " , sprite_types [ available ] , sprite , sprite_types [ requested ] ) ;
switch ( requested ) {
case ST_NORMAL :
if ( sprite = = SPR_IMG_QUERY ) usererror ( " Uhm, would you be so kind not to load a NewGRF that makes the 'query' sprite a non-normal sprite? " ) ;
/* FALLTHROUGH */
case ST_FONT :
return GetRawSprite ( SPR_IMG_QUERY , ST_NORMAL ) ;
case ST_RECOLOUR :
if ( sprite = = PALETTE_TO_DARK_BLUE ) usererror ( " Uhm, would you be so kind not to load a NewGRF that makes the 'PALETTE_TO_DARK_BLUE' sprite a non-remap sprite? " ) ;
return GetRawSprite ( PALETTE_TO_DARK_BLUE , ST_RECOLOUR ) ;
case ST_MAPGEN :
/* this shouldn't happen, overriding of ST_MAPGEN sprites is checked in LoadNextSprite()
* ( the only case the check fails is when these sprites weren ' t even loaded . . . ) */
default :
NOT_REACHED ( ) ;
}
}
2004-08-09 17:04:08 +00:00
2008-09-02 15:20:38 +00:00
const void * GetRawSprite ( SpriteID sprite , SpriteType type )
2004-08-09 17:04:08 +00:00
{
2009-01-11 22:28:30 +00:00
assert ( IsMapgenSpriteID ( sprite ) = = ( type = = ST_MAPGEN ) ) ;
assert ( type < ST_INVALID ) ;
if ( ! SpriteExists ( sprite ) ) {
DEBUG ( sprite , 1 , " Tried to load non-existing sprite #%d. Probable cause: Wrong/missing NewGRFs " , sprite ) ;
/* SPR_IMG_QUERY is a BIG FAT RED ? */
sprite = SPR_IMG_QUERY ;
}
2004-08-09 17:04:08 +00:00
2009-01-11 22:28:30 +00:00
SpriteCache * sc = GetSpriteCache ( sprite ) ;
2004-08-09 17:04:08 +00:00
2009-01-11 23:49:51 +00:00
if ( sc - > type ! = type ) return HandleInvalidSpriteRequest ( sprite , type , sc ) ;
2007-01-03 14:42:08 +00:00
2007-04-04 01:35:16 +00:00
/* Update LRU */
2007-01-03 14:42:08 +00:00
sc - > lru = + + _sprite_lru_counter ;
2009-01-11 22:28:30 +00:00
void * 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 */
2009-01-11 22:28:30 +00:00
if ( p = = NULL ) p = ReadSprite ( sc , sprite , type ) ;
2008-02-14 15:13:36 +00:00
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-12-08 14:50:41 +00:00
if ( _spritecache_ptr = = NULL ) _spritecache_ptr = ( MemBlock * ) MallocT < byte > ( _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
}