src/spritecache.cpp
branchcustombridgeheads
changeset 5650 aefc131bf5ce
parent 5649 55c8267c933f
equal deleted inserted replaced
5649:55c8267c933f 5650:aefc131bf5ce
     6 #include "functions.h"
     6 #include "functions.h"
     7 #include "macros.h"
     7 #include "macros.h"
     8 #include "spritecache.h"
     8 #include "spritecache.h"
     9 #include "table/sprites.h"
     9 #include "table/sprites.h"
    10 #include "fileio.h"
    10 #include "fileio.h"
       
    11 #include "helpers.hpp"
    11 
    12 
    12 #define SPRITE_CACHE_SIZE 1024*1024
    13 #define SPRITE_CACHE_SIZE 1024*1024
    13 
    14 
    14 
    15 
    15 typedef struct SpriteCache {
    16 typedef struct SpriteCache {
    35 		/* Add another 1024 items to the 'pool' */
    36 		/* Add another 1024 items to the 'pool' */
    36 		uint items = ALIGN(index + 1, 1024);
    37 		uint items = ALIGN(index + 1, 1024);
    37 
    38 
    38 		DEBUG(sprite, 4, "Increasing sprite cache to %d items (%d bytes)", items, items * sizeof(*_spritecache));
    39 		DEBUG(sprite, 4, "Increasing sprite cache to %d items (%d bytes)", items, items * sizeof(*_spritecache));
    39 
    40 
    40 		_spritecache = realloc(_spritecache, items * sizeof(*_spritecache));
    41 		ReallocT(&_spritecache, items);
    41 
    42 
    42 		if (_spritecache == NULL) {
    43 		if (_spritecache == NULL) {
    43 			error("Unable to allocate sprite cache of %d items (%d bytes)", items, items * sizeof(*_spritecache));
    44 			error("Unable to allocate sprite cache of %d items (%d bytes)", items, items * sizeof(*_spritecache));
    44 		}
    45 		}
    45 
    46 
   130 	FioSeekToFile(sc->file_pos);
   131 	FioSeekToFile(sc->file_pos);
   131 
   132 
   132 	num  = FioReadWord();
   133 	num  = FioReadWord();
   133 	type = FioReadByte();
   134 	type = FioReadByte();
   134 	if (type == 0xFF) {
   135 	if (type == 0xFF) {
   135 		byte* dest = AllocSprite(num);
   136 		byte* dest = (byte*)AllocSprite(num);
   136 
   137 
   137 		sc->ptr = dest;
   138 		sc->ptr = dest;
   138 		FioReadBlock(dest, num);
   139 		FioReadBlock(dest, num);
   139 
   140 
   140 		return dest;
   141 		return dest;
   143 		uint width  = FioReadWord();
   144 		uint width  = FioReadWord();
   144 		Sprite* sprite;
   145 		Sprite* sprite;
   145 		byte* dest;
   146 		byte* dest;
   146 
   147 
   147 		num = (type & 0x02) ? width * height : num - 8;
   148 		num = (type & 0x02) ? width * height : num - 8;
   148 		sprite = AllocSprite(sizeof(*sprite) + num);
   149 		sprite = (Sprite*)AllocSprite(sizeof(*sprite) + num);
   149 		sc->ptr = sprite;
   150 		sc->ptr = sprite;
   150 		sprite->info   = type;
   151 		sprite->info   = type;
   151 		sprite->height = (id != 142) ? height : 10; // Compensate for a TTD bug
   152 		sprite->height = (id != 142) ? height : 10; // Compensate for a TTD bug
   152 		sprite->width  = width;
   153 		sprite->width  = width;
   153 		sprite->x_offs = FioReadWord();
   154 		sprite->x_offs = FioReadWord();
   193 
   194 
   194 	return true;
   195 	return true;
   195 }
   196 }
   196 
   197 
   197 
   198 
   198 void DupSprite(SpriteID old, SpriteID new)
   199 void DupSprite(SpriteID old_spr, SpriteID new_spr)
   199 {
   200 {
   200 	SpriteCache *scold = GetSpriteCache(old);
   201 	SpriteCache *scold = GetSpriteCache(old_spr);
   201 	SpriteCache *scnew = AllocateSpriteCache(new);
   202 	SpriteCache *scnew = AllocateSpriteCache(new_spr);
   202 
   203 
   203 	scnew->file_pos = scold->file_pos;
   204 	scnew->file_pos = scold->file_pos;
   204 	scnew->ptr = NULL;
   205 	scnew->ptr = NULL;
   205 }
   206 }
   206 
   207 
   304 }
   305 }
   305 
   306 
   306 static void DeleteEntryFromSpriteCache(void)
   307 static void DeleteEntryFromSpriteCache(void)
   307 {
   308 {
   308 	SpriteID i;
   309 	SpriteID i;
   309 	uint best = -1;
   310 	uint best = UINT_MAX;
   310 	MemBlock* s;
   311 	MemBlock* s;
   311 	int cur_lru;
   312 	int cur_lru;
   312 
   313 
   313 	DEBUG(sprite, 3, "DeleteEntryFromSpriteCache, inuse=%d", GetSpriteCacheUsage());
   314 	DEBUG(sprite, 3, "DeleteEntryFromSpriteCache, inuse=%d", GetSpriteCacheUsage());
   314 
   315 
   401 
   402 
   402 
   403 
   403 void GfxInitSpriteMem(void)
   404 void GfxInitSpriteMem(void)
   404 {
   405 {
   405 	// initialize sprite cache heap
   406 	// initialize sprite cache heap
   406 	if (_spritecache_ptr == NULL) _spritecache_ptr = malloc(SPRITE_CACHE_SIZE);
   407 	if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)malloc(SPRITE_CACHE_SIZE);
   407 
   408 
   408 	// A big free block
   409 	// A big free block
   409 	_spritecache_ptr->size = (SPRITE_CACHE_SIZE - sizeof(MemBlock)) | S_FREE_MASK;
   410 	_spritecache_ptr->size = (SPRITE_CACHE_SIZE - sizeof(MemBlock)) | S_FREE_MASK;
   410 	// Sentinel block (identified by size == 0)
   411 	// Sentinel block (identified by size == 0)
   411 	NextBlock(_spritecache_ptr)->size = 0;
   412 	NextBlock(_spritecache_ptr)->size = 0;