changeset 17051:dff31b533b77 draft

(svn r21788) -Codechange: Enable GetRawSprite() to also load sprites not using the spritecache but a custom allocator function.
author frosch <frosch@openttd.org>
date Fri, 14 Jan 2011 16:36:34 +0000
parents 0d6fb8c23e11
children 5c5c1a23c651
files src/spritecache.cpp src/spritecache.h
diffstat 2 files changed, 28 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/spritecache.cpp
+++ b/src/spritecache.cpp
@@ -292,7 +292,7 @@
 
 	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 @@
  * @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 @@
 	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 @@
 			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 @@
 	}
 }
 
-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 @@
 
 	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 */
+
+		/* 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, AllocSprite);
+		/* Load the sprite, if it is not loaded, yet */
+		if (sc->ptr == NULL) sc->ptr = ReadSprite(sc, sprite, type, AllocSprite);
 
-	return sc->ptr;
+		return sc->ptr;
+	} else {
+		/* Do not use the spritecache, but a different allocator. */
+		return ReadSprite(sc, sprite, type, allocator);
+	}
 }
 
 
--- a/src/spritecache.h
+++ b/src/spritecache.h
@@ -27,7 +27,7 @@
 
 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);