src/spritecache.cpp
branchgamebalance
changeset 9895 7bd07f43b0e3
parent 6213 164d84e1c5b4
child 6307 f40e88cff863
equal deleted inserted replaced
9894:70d78ac95d6c 9895:7bd07f43b0e3
    13 #ifndef SPRITE_CACHE_SIZE
    13 #ifndef SPRITE_CACHE_SIZE
    14 # define SPRITE_CACHE_SIZE 2*1024*1024
    14 # define SPRITE_CACHE_SIZE 2*1024*1024
    15 #endif /* SPRITE_CACHE_SIZE */
    15 #endif /* SPRITE_CACHE_SIZE */
    16 
    16 
    17 
    17 
    18 typedef struct SpriteCache {
    18 struct SpriteCache {
    19 	void *ptr;
    19 	void *ptr;
    20 	uint32 file_pos;
    20 	uint32 file_pos;
    21 	int16 lru;
    21 	int16 lru;
    22 } SpriteCache;
    22 };
    23 
    23 
    24 
    24 
    25 static uint _spritecache_items = 0;
    25 static uint _spritecache_items = 0;
    26 static SpriteCache *_spritecache = NULL;
    26 static SpriteCache *_spritecache = NULL;
    27 
    27 
    53 
    53 
    54 	return GetSpriteCache(index);
    54 	return GetSpriteCache(index);
    55 }
    55 }
    56 
    56 
    57 
    57 
    58 typedef struct MemBlock {
    58 struct MemBlock {
    59 	uint32 size;
    59 	uint32 size;
    60 	byte data[VARARRAY_SIZE];
    60 	byte data[VARARRAY_SIZE];
    61 } MemBlock;
    61 };
    62 
    62 
    63 static uint _sprite_lru_counter;
    63 static uint _sprite_lru_counter;
    64 static MemBlock *_spritecache_ptr;
    64 static MemBlock *_spritecache_ptr;
    65 static int _compact_cache_counter;
    65 static int _compact_cache_counter;
    66 
    66 
    67 static void CompactSpriteCache(void);
    67 static void CompactSpriteCache();
    68 
    68 
    69 static bool ReadSpriteHeaderSkipData(void)
    69 static bool ReadSpriteHeaderSkipData()
    70 {
    70 {
    71 	uint16 num = FioReadWord();
    71 	uint16 num = FioReadWord();
    72 	byte type;
    72 	byte type;
    73 
    73 
    74 	if (num == 0) return false;
    74 	if (num == 0) return false;
   221 static inline MemBlock* NextBlock(MemBlock* block)
   221 static inline MemBlock* NextBlock(MemBlock* block)
   222 {
   222 {
   223 	return (MemBlock*)((byte*)block + (block->size & ~S_FREE_MASK));
   223 	return (MemBlock*)((byte*)block + (block->size & ~S_FREE_MASK));
   224 }
   224 }
   225 
   225 
   226 static uint32 GetSpriteCacheUsage(void)
   226 static uint32 GetSpriteCacheUsage()
   227 {
   227 {
   228 	uint32 tot_size = 0;
   228 	uint32 tot_size = 0;
   229 	MemBlock* s;
   229 	MemBlock* s;
   230 
   230 
   231 	for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s))
   231 	for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s))
   233 
   233 
   234 	return tot_size;
   234 	return tot_size;
   235 }
   235 }
   236 
   236 
   237 
   237 
   238 void IncreaseSpriteLRU(void)
   238 void IncreaseSpriteLRU()
   239 {
   239 {
   240 	// Increase all LRU values
   240 	// Increase all LRU values
   241 	if (_sprite_lru_counter > 16384) {
   241 	if (_sprite_lru_counter > 16384) {
   242 		SpriteID i;
   242 		SpriteID i;
   243 
   243 
   263 	}
   263 	}
   264 }
   264 }
   265 
   265 
   266 // Called when holes in the sprite cache should be removed.
   266 // Called when holes in the sprite cache should be removed.
   267 // That is accomplished by moving the cached data.
   267 // That is accomplished by moving the cached data.
   268 static void CompactSpriteCache(void)
   268 static void CompactSpriteCache()
   269 {
   269 {
   270 	MemBlock *s;
   270 	MemBlock *s;
   271 
   271 
   272 	DEBUG(sprite, 3, "Compacting sprite cache, inuse=%d", GetSpriteCacheUsage());
   272 	DEBUG(sprite, 3, "Compacting sprite cache, inuse=%d", GetSpriteCacheUsage());
   273 
   273 
   304 			s = NextBlock(s);
   304 			s = NextBlock(s);
   305 		}
   305 		}
   306 	}
   306 	}
   307 }
   307 }
   308 
   308 
   309 static void DeleteEntryFromSpriteCache(void)
   309 static void DeleteEntryFromSpriteCache()
   310 {
   310 {
   311 	SpriteID i;
   311 	SpriteID i;
   312 	uint best = UINT_MAX;
   312 	uint best = UINT_MAX;
   313 	MemBlock* s;
   313 	MemBlock* s;
   314 	int cur_lru;
   314 	int cur_lru;
   401 	if (p == NULL) p = ReadSprite(sc, sprite);
   401 	if (p == NULL) p = ReadSprite(sc, sprite);
   402 	return p;
   402 	return p;
   403 }
   403 }
   404 
   404 
   405 
   405 
   406 void GfxInitSpriteMem(void)
   406 void GfxInitSpriteMem()
   407 {
   407 {
   408 	// initialize sprite cache heap
   408 	// initialize sprite cache heap
   409 	if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)malloc(SPRITE_CACHE_SIZE);
   409 	if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)malloc(SPRITE_CACHE_SIZE);
   410 
   410 
   411 	// A big free block
   411 	// A big free block