Changeset - r2014:c6a34c339dda
[Not reviewed]
master
0 3 0
tron - 19 years ago 2005-07-05 19:54:35
tron@openttd.org
(svn r2522) Reorganize sprite load and decompression in order to remove a special case from the sprite blitter, which decompressed certain sprites every time when blitting them
3 files changed with 36 insertions and 74 deletions:
0 comments (0 inline, 0 general)
gfx.c
Show inline comments
 
@@ -1376,38 +1376,6 @@ static void GfxMainBlitter(const Sprite*
 
		}
 
		bp.start_x = start_x;
 

	
 
		if (info&2) {
 
			int totpix = bp.height_org * bp.width_org;
 
			byte *dst = (byte*)alloca(totpix);
 
			const byte *src = bp.sprite_org;
 

	
 
			bp.sprite = dst + (bp.sprite - bp.sprite_org);
 

	
 
			while (totpix != 0) {
 
				signed char b;
 

	
 
				assert(totpix > 0);
 

	
 
				b = *src++;
 
				if (b >= 0) {
 
					uint count = b;
 
					uint i;
 

	
 
					for (i = 0; i != count; i++) dst[i] = src[i];
 
					dst += count;
 
					src += count;
 
					totpix -= count;
 
				} else {
 
					const byte *tmp = dst - (((b & 7) << 8) | *src++);
 
					uint count = -(b >> 3);
 
					uint i;
 

	
 
					for (i = 0; i != count; i++) dst[i] = tmp[i];
 
					dst += count;
 
					totpix -= count;
 
				}
 
			}
 
		}
 
		zf_uncomp[dpi->zoom](&bp);
 
	}
 
}
spritecache.c
Show inline comments
 
@@ -31,7 +31,7 @@ uint16 _custom_sprites_base;
 
static Sprite _cur_sprite;
 

	
 

	
 
static byte *_sprite_ptr[NUM_SPRITES];
 
static void* _sprite_ptr[NUM_SPRITES];
 
static uint16 _sprite_size[NUM_SPRITES];
 
static uint32 _sprite_file_pos[NUM_SPRITES];
 

	
 
@@ -148,11 +148,14 @@ static void ReadSpriteHeaderSkipData(int
 
	}
 
}
 

	
 
static void ReadSprite(SpriteID id, void *buffer)
 
static void* AllocSprite(size_t);
 

	
 
static void* ReadSprite(SpriteID id)
 
{
 
	uint num = _sprite_size[id];
 
	byte type;
 
	byte* dest;
 

	
 
	DEBUG(spritecache, 9) ("load sprite %d", id);
 

	
 
	if (_sprite_file_pos[id] == 0) {
 
		error(
 
@@ -165,43 +168,46 @@ static void ReadSprite(SpriteID id, void
 
	FioSeekToFile(_sprite_file_pos[id]);
 

	
 
	type = FioReadByte();
 
	/* We've decoded special sprites when reading headers. */
 
	if (type != 0xFF) {
 
		/* read sprite hdr */
 
		Sprite* sprite = buffer;
 
		sprite->info = type;
 
		sprite->height = FioReadByte();
 
		if (id == 142) sprite->height = 10; // Compensate for a TTD bug
 
		sprite->width = FioReadWord();
 
	if (type == 0xFF) {
 
		byte* dest = AllocSprite(num);
 

	
 
		_sprite_ptr[id] = dest;
 
		FioReadBlock(dest, num);
 

	
 
		return dest;
 
	} else {
 
		uint height = FioReadByte();
 
		uint width  = FioReadWord();
 
		Sprite* sprite;
 
		byte* dest;
 

	
 
		num = (type & 0x02) ? width * height : num - 8;
 
		sprite = AllocSprite(sizeof(*sprite) + num);
 
		_sprite_ptr[id] = sprite;
 
		sprite->info   = type;
 
		sprite->height = (id != 142) ? height : 10;
 
		sprite->width  = width;
 
		sprite->x_offs = FioReadWord();
 
		sprite->y_offs = FioReadWord();
 

	
 
		dest = sprite->data;
 
		num -= 8;
 
	} else {
 
		dest = buffer;
 
	}
 

	
 
	if (type & 2) {
 
		for (; num > 0; --num)
 
			*dest++ = FioReadByte();
 
	} else {
 
		while (num > 0) {
 
			int8 i = FioReadByte();
 

	
 
			if (i >= 0) {
 
				num -= i;
 
				for (; i > 0; --i)
 
					*dest++ = FioReadByte();
 
				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++;
 
				for (; i > 0; --i) *dest++ = *rel++;
 
			}
 
		}
 

	
 
		return sprite;
 
	}
 
}
 

	
 
@@ -499,7 +505,7 @@ static void CompactSpriteCache(void)
 
		if (s->size & S_FREE_MASK) {
 
			MemBlock* next = NextBlock(s);
 
			MemBlock temp;
 
			byte** i;
 
			void** i;
 

	
 
			// Since free blocks are automatically coalesced, this should hold true.
 
			assert(!(next->size & S_FREE_MASK));
 
@@ -606,14 +612,9 @@ static void DeleteEntryFromSpriteCache(v
 
	}
 
}
 

	
 
static byte *LoadSpriteToMem(SpriteID sprite)
 
static void* AllocSprite(size_t mem_req)
 
{
 
	size_t mem_req;
 

	
 
	DEBUG(spritecache, 9) ("load sprite %d", sprite);
 

	
 
	// Number of needed bytes
 
	mem_req = sizeof(MemBlock) + _sprite_size[sprite];
 
	mem_req += sizeof(MemBlock);
 

	
 
	/* 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. */
 
@@ -638,11 +639,6 @@ static byte *LoadSpriteToMem(SpriteID sp
 
						NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK;
 
					}
 

	
 
					_sprite_ptr[sprite] = s->data;
 

	
 
					ReadSprite(sprite, s->data);
 

	
 
					// Return sprite ptr
 
					return s->data;
 
				}
 
			}
 
@@ -698,7 +694,7 @@ static uint RotateSprite(uint s)
 

	
 
const void *GetRawSprite(SpriteID sprite)
 
{
 
	byte *p;
 
	void* p;
 

	
 
	assert(sprite < NUM_SPRITES);
 

	
 
@@ -714,10 +710,9 @@ const void *GetRawSprite(SpriteID sprite
 
	_sprite_lru[sprite] = 0;
 
#endif
 

	
 
	// Check if the sprite is loaded already?
 
	p = _sprite_ptr[sprite];
 
	if (p == NULL)
 
		p = LoadSpriteToMem(sprite);  // No, need to load it.
 
	// Load the sprite, if it is not loaded, yet
 
	if (p == NULL) p = ReadSprite(sprite);
 
	return p;
 
}
 

	
spritecache.h
Show inline comments
 
@@ -9,7 +9,6 @@ typedef struct Sprite {
 
	int16 y_offs;
 
	byte data[VARARRAY_SIZE];
 
} Sprite;
 
assert_compile(sizeof(Sprite) == 8);
 

	
 
typedef struct {
 
	int xoffs, yoffs;
0 comments (0 inline, 0 general)