From 93ae848b0ad844ff597793e88b5f032f696c5361 Mon Sep 17 00:00:00 2001 From: frosch Date: Fri, 14 Jan 2011 16:36:34 +0000 Subject: [PATCH] (svn r21788) -Codechange: Enable GetRawSprite() to also load sprites not using the spritecache but a custom allocator function. --- src/spritecache.cpp | 39 +++++++++++++++++++++++++++------------ src/spritecache.h | 2 +- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/spritecache.cpp b/src/spritecache.cpp index ace3bccbd7..f11882d5ec 100644 --- a/src/spritecache.cpp +++ b/src/spritecache.cpp @@ -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 (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); } @@ -530,7 +530,7 @@ static void *AllocSprite(size_t mem_req) * @return fallback sprite * @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[] = { "normal", // ST_NORMAL @@ -542,7 +542,7 @@ static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, S SpriteType available = sc->type; if (requested == ST_FONT && available == ST_NORMAL) { 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; @@ -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?"); /* FALL THROUGH */ case ST_FONT: - return GetRawSprite(SPR_IMG_QUERY, ST_NORMAL); + return GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator); 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?"); - return GetRawSprite(PALETTE_TO_DARK_BLUE, ST_RECOLOUR); + return GetRawSprite(PALETTE_TO_DARK_BLUE, ST_RECOLOUR, allocator); case ST_MAPGEN: /* 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...) */ @@ -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(type < ST_INVALID); @@ -580,15 +588,22 @@ void *GetRawSprite(SpriteID sprite, SpriteType type) 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 */ - sc->lru = ++_sprite_lru_counter; + if (allocator == NULL) { + /* Load sprite into/from spritecache */ - /* Load the sprite, if it is not loaded, yet */ - if (sc->ptr == NULL) sc->ptr = ReadSprite(sc, sprite, type, AllocSprite); + /* Update LRU */ + 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); + } } diff --git a/src/spritecache.h b/src/spritecache.h index 91fda9b531..f0c124f5ac 100644 --- a/src/spritecache.h +++ b/src/spritecache.h @@ -27,7 +27,7 @@ extern uint _sprite_cache_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); SpriteType GetSpriteType(SpriteID sprite);