(svn r21788) -Codechange: Enable GetRawSprite() to also load sprites not using the spritecache but a custom allocator function.

This commit is contained in:
frosch 2011-01-14 16:36:34 +00:00
parent 6cb017456a
commit 93ae848b0a
2 changed files with 28 additions and 13 deletions

View File

@ -292,7 +292,7 @@ static void *ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_ty
if (!sprite_loader.LoadSprite(&sprite, file_slot, file_pos, sprite_type)) { 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?"); 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 (void*)GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
} }
return BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, allocator); return BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, allocator);
} }
@ -530,7 +530,7 @@ static void *AllocSprite(size_t mem_req)
* @return fallback sprite * @return fallback sprite
* @note this function will do usererror() in the case the fallback sprite isn't available * @note this function will do usererror() in the case the fallback sprite isn't available
*/ */
static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc) static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc, AllocatorProc *allocator)
{ {
static const char * const sprite_types[] = { static const char * const sprite_types[] = {
"normal", // ST_NORMAL "normal", // ST_NORMAL
@ -542,7 +542,7 @@ static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, S
SpriteType available = sc->type; SpriteType available = sc->type;
if (requested == ST_FONT && available == ST_NORMAL) { if (requested == ST_FONT && available == ST_NORMAL) {
if (sc->ptr == NULL) sc->type = ST_FONT; if (sc->ptr == NULL) sc->type = ST_FONT;
return GetRawSprite(sprite, sc->type); return GetRawSprite(sprite, sc->type, allocator);
} }
byte warning_level = sc->warned ? 6 : 0; byte warning_level = sc->warned ? 6 : 0;
@ -554,10 +554,10 @@ static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, S
if (sprite == SPR_IMG_QUERY) usererror("Uhm, would you be so kind not to load a NewGRF that makes the 'query' sprite a non-normal sprite?"); if (sprite == SPR_IMG_QUERY) usererror("Uhm, would you be so kind not to load a NewGRF that makes the 'query' sprite a non-normal sprite?");
/* FALL THROUGH */ /* FALL THROUGH */
case ST_FONT: case ST_FONT:
return GetRawSprite(SPR_IMG_QUERY, ST_NORMAL); return GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
case ST_RECOLOUR: case ST_RECOLOUR:
if (sprite == PALETTE_TO_DARK_BLUE) usererror("Uhm, would you be so kind not to load a NewGRF that makes the 'PALETTE_TO_DARK_BLUE' sprite a non-remap sprite?"); if (sprite == PALETTE_TO_DARK_BLUE) usererror("Uhm, would you be so kind not to load a NewGRF that makes the 'PALETTE_TO_DARK_BLUE' sprite a non-remap sprite?");
return GetRawSprite(PALETTE_TO_DARK_BLUE, ST_RECOLOUR); return GetRawSprite(PALETTE_TO_DARK_BLUE, ST_RECOLOUR, allocator);
case ST_MAPGEN: case ST_MAPGEN:
/* this shouldn't happen, overriding of ST_MAPGEN sprites is checked in LoadNextSprite() /* this shouldn't happen, overriding of ST_MAPGEN sprites is checked in LoadNextSprite()
* (the only case the check fails is when these sprites weren't even loaded...) */ * (the only case the check fails is when these sprites weren't even loaded...) */
@ -566,7 +566,15 @@ static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, S
} }
} }
void *GetRawSprite(SpriteID sprite, SpriteType type) /**
* Reads a sprite (from disk or sprite cache).
* If the sprite is not available or of wrong type, a fallback sprite is returned.
* @param sprite Sprite to read.
* @param type Expected sprite type.
* @param allocator Allocator function to use. Set to NULL to use the usual sprite cache.
* @return Sprite raw data
*/
void *GetRawSprite(SpriteID sprite, SpriteType type, AllocatorProc *allocator)
{ {
assert(IsMapgenSpriteID(sprite) == (type == ST_MAPGEN)); assert(IsMapgenSpriteID(sprite) == (type == ST_MAPGEN));
assert(type < ST_INVALID); assert(type < ST_INVALID);
@ -580,15 +588,22 @@ void *GetRawSprite(SpriteID sprite, SpriteType type)
SpriteCache *sc = GetSpriteCache(sprite); SpriteCache *sc = GetSpriteCache(sprite);
if (sc->type != type) return HandleInvalidSpriteRequest(sprite, type, sc); if (sc->type != type) return HandleInvalidSpriteRequest(sprite, type, sc, allocator);
/* Update LRU */ if (allocator == NULL) {
sc->lru = ++_sprite_lru_counter; /* Load sprite into/from spritecache */
/* Load the sprite, if it is not loaded, yet */ /* Update LRU */
if (sc->ptr == NULL) sc->ptr = ReadSprite(sc, sprite, type, AllocSprite); sc->lru = ++_sprite_lru_counter;
return sc->ptr; /* Load the sprite, if it is not loaded, yet */
if (sc->ptr == NULL) sc->ptr = ReadSprite(sc, sprite, type, AllocSprite);
return sc->ptr;
} else {
/* Do not use the spritecache, but a different allocator. */
return ReadSprite(sc, sprite, type, allocator);
}
} }

View File

@ -27,7 +27,7 @@ extern uint _sprite_cache_size;
typedef void *AllocatorProc(size_t size); typedef void *AllocatorProc(size_t size);
void *GetRawSprite(SpriteID sprite, SpriteType type); void *GetRawSprite(SpriteID sprite, SpriteType type, AllocatorProc *allocator = NULL);
bool SpriteExists(SpriteID sprite); bool SpriteExists(SpriteID sprite);
SpriteType GetSpriteType(SpriteID sprite); SpriteType GetSpriteType(SpriteID sprite);