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
 
@@ -172,15 +172,21 @@ uint GetOriginFileSlot(SpriteID sprite)
 
 */
 
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);
 
@@ -191,13 +197,13 @@ static void *ReadSprite(const SpriteCach
 
#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");
 
@@ -217,13 +223,13 @@ static void *ReadSprite(const SpriteCach
 
	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);
 
@@ -253,13 +259,13 @@ static void *ReadSprite(const SpriteCach
 
		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;
 
@@ -285,13 +291,13 @@ static void *ReadSprite(const SpriteCach
 
	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();
 
@@ -577,13 +583,13 @@ void *GetRawSprite(SpriteID sprite, Spri
 
	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()
0 comments (0 inline, 0 general)