Changeset - r17040:516f3959e3ff
[Not reviewed]
master
0 1 0
frosch - 13 years ago 2011-01-14 16:28:23
frosch@openttd.org
(svn r21787) -Codechange: Pass the allocator function to use to ReadSprite().
1 file changed with 14 insertions and 8 deletions:
0 comments (0 inline, 0 general)
src/spritecache.cpp
Show inline comments
 
@@ -166,44 +166,50 @@ uint GetOriginFileSlot(SpriteID sprite)
 
 * Get a reasonable (upper bound) estimate of the maximum
 
 * SpriteID used in OpenTTD; there will be no sprites with
 
 * a higher SpriteID, although there might be up to roughly
 
 * a thousand unused SpriteIDs below this number.
 
 * @note It's actually the number of spritecache items.
 
 * @return maximum SpriteID
 
 */
 
uint GetMaxSpriteID()
 
{
 
	return _spritecache_items;
 
}
 

	
 
static void *AllocSprite(size_t);
 

	
 
static void *ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_type)
 
/**
 
 * Read a sprite from disk.
 
 * @param sc          Location of sprite.
 
 * @param id          Sprite number.
 
 * @param sprite_type Type of sprite.
 
 * @param allocator   Allocator function to use.
 
 * @return Read sprite data.
 
 */
 
static void *ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_type, AllocatorProc *allocator)
 
{
 
	uint8 file_slot = sc->file_slot;
 
	size_t file_pos = sc->file_pos;
 

	
 
	assert(IsMapgenSpriteID(id) == (sprite_type == ST_MAPGEN));
 
	assert(sc->type == sprite_type);
 

	
 
	DEBUG(sprite, 9, "Load sprite %d", id);
 

	
 
	if (sprite_type == ST_NORMAL && BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 32) {
 
#ifdef WITH_PNG
 
		/* Try loading 32bpp graphics in case we are 32bpp output */
 
		SpriteLoaderPNG sprite_loader;
 
		SpriteLoader::Sprite sprite;
 

	
 
		if (sprite_loader.LoadSprite(&sprite, file_slot, sc->id, sprite_type)) {
 
			return BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, &AllocSprite);
 
			return BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, allocator);
 
		}
 
		/* If the PNG couldn't be loaded, fall back to 8bpp grfs */
 
#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;
 
		}
 
#endif /* WITH_PNG */
 
	}
 

	
 
	FioSeekToFile(file_slot, file_pos);
 
@@ -211,25 +217,25 @@ static void *ReadSprite(const SpriteCach
 
	/* Read the size and type */
 
	int num  = FioReadWord();
 
	byte type = FioReadByte();
 

	
 
	/* Type 0xFF indicates either a colourmap or some other non-sprite info */
 
	assert((type == 0xFF) == (sprite_type == ST_RECOLOUR));
 
	if (type == 0xFF) {
 
		/* "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));
 
		byte *dest = (byte *)allocator(max(RECOLOUR_SPRITE_SIZE, num));
 

	
 
		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]];
 
			}
 
@@ -247,25 +253,25 @@ static void *ReadSprite(const SpriteCach
 
	 *  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 (sprite_type == ST_MAPGEN) {
 
		uint height = FioReadByte();
 
		uint width  = FioReadWord();
 
		Sprite *sprite;
 
		byte *dest;
 

	
 
		num = width * height;
 
		sprite = (Sprite *)AllocSprite(sizeof(*sprite) + num);
 
		sprite = (Sprite *)allocator(sizeof(*sprite) + num);
 
		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 {
 
@@ -279,25 +285,25 @@ static void *ReadSprite(const SpriteCach
 
		return sprite;
 
	}
 

	
 
	assert(sprite_type == ST_NORMAL || sprite_type == ST_FONT);
 

	
 
	SpriteLoaderGrf sprite_loader;
 
	SpriteLoader::Sprite sprite;
 

	
 
	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);
 
	}
 
	return BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, &AllocSprite);
 
	return BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, allocator);
 
}
 

	
 

	
 
bool LoadNextSprite(int load_index, byte file_slot, uint file_sprite_id)
 
{
 
	size_t file_pos = FioGetPos();
 

	
 
	SpriteType type = ReadSpriteHeaderSkipData();
 

	
 
	if (type == ST_INVALID) return false;
 

	
 
	if (load_index >= MAX_SPRITES) {
 
@@ -571,25 +577,25 @@ void *GetRawSprite(SpriteID sprite, Spri
 
		/* SPR_IMG_QUERY is a BIG FAT RED ? */
 
		sprite = SPR_IMG_QUERY;
 
	}
 

	
 
	SpriteCache *sc = GetSpriteCache(sprite);
 

	
 
	if (sc->type != type) return HandleInvalidSpriteRequest(sprite, type, sc);
 

	
 
	/* Update LRU */
 
	sc->lru = ++_sprite_lru_counter;
 

	
 
	/* Load the sprite, if it is not loaded, yet */
 
	if (sc->ptr == NULL) sc->ptr = ReadSprite(sc, sprite, type);
 
	if (sc->ptr == NULL) sc->ptr = ReadSprite(sc, sprite, type, AllocSprite);
 

	
 
	return sc->ptr;
 
}
 

	
 

	
 
void GfxInitSpriteMem()
 
{
 
	/* initialize sprite cache heap */
 
	if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)MallocT<byte>(_sprite_cache_size * 1024 * 1024);
 

	
 
	/* A big free block */
 
	_spritecache_ptr->size = ((_sprite_cache_size * 1024 * 1024) - sizeof(MemBlock)) | S_FREE_MASK;
0 comments (0 inline, 0 general)