changeset 19028:ad1b8d2738e9 draft

(svn r23885) -Codechange: Use the GRF sprite loader for then mapgen sprites as well.
author michi_cc <michi_cc@openttd.org>
date Sat, 04 Feb 2012 13:28:56 +0000
parents 65f4f5efa5de
children c43f1ddb84ec
files src/landscape.cpp src/spritecache.cpp
diffstat 2 files changed, 31 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/src/landscape.cpp
+++ b/src/landscape.cpp
@@ -769,6 +769,7 @@
 	uint32 r = Random();
 
 	const Sprite *templ = GetSprite((((r >> 24) * _genterrain_tbl_1[type]) >> 8) + _genterrain_tbl_2[type] + 4845, ST_MAPGEN);
+	if (templ == NULL) usererror("Map generator sprites could not be loaded");
 
 	uint x = r & MapMaxX();
 	uint y = (r >> MapLogX()) & MapMaxY();
--- a/src/spritecache.cpp
+++ b/src/spritecache.cpp
@@ -227,65 +227,44 @@
 #endif /* WITH_PNG */
 	}
 
-	FioSeekToFile(file_slot, file_pos);
-
-	/* Read the size and type */
-	int num  = FioReadWord();
-	byte type = FioReadByte();
-
-	/* Type 0xFF indicates either a colourmap or some other non-sprite info
-	 * which we should have already handled during GRF loading. */
-	assert(type != 0xFF);
-
-	/* Ugly hack to work around the problem that the old landscape
-	 *  generator assumes that those sprites are stored uncompressed in
-	 *  the memory, and they are only read directly by the code, never
-	 *  send to the blitter. So do not send it to the blitter (which will
-	 *  result in a data array in the format the blitter likes most), but
-	 *  read the data directly from disk and store that as sprite.
-	 * Ugly: yes. Other solution: no. Blame the original author or
-	 *  something ;) The image should really have been a data-stream
-	 *  (so type = 0xFF basicly). */
-	if (sprite_type == ST_MAPGEN) {
-		uint height = FioReadByte();
-		uint width  = FioReadWord();
-		Sprite *sprite;
-		byte *dest;
-
-		num = width * height;
-		sprite = (Sprite *)allocator(sizeof(*sprite) + num);
-		sprite->height = height;
-		sprite->width  = width;
-		sprite->x_offs = FioReadWord();
-		sprite->y_offs = FioReadWord();
-
-		dest = sprite->data;
-		while (num > 0) {
-			int8 i = FioReadByte();
-			if (i >= 0) {
-				num -= i;
-				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++;
-			}
-		}
-
-		return sprite;
-	}
-
-	assert(sprite_type == ST_NORMAL || sprite_type == ST_FONT);
-
 	SpriteLoaderGrf sprite_loader;
 	SpriteLoader::Sprite sprite;
 	sprite.type = sprite_type;
 
 	if (!sprite_loader.LoadSprite(&sprite, file_slot, file_pos, sprite_type)) {
+		if (sprite_type == ST_MAPGEN) return NULL;
 		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, allocator);
 	}
+
+	if (sprite_type == ST_MAPGEN) {
+		/* Ugly hack to work around the problem that the old landscape
+		 *  generator assumes that those sprites are stored uncompressed in
+		 *  the memory, and they are only read directly by the code, never
+		 *  send to the blitter. So do not send it to the blitter (which will
+		 *  result in a data array in the format the blitter likes most), but
+		 *  extract the data directly and store that as sprite.
+		 * Ugly: yes. Other solution: no. Blame the original author or
+		 *  something ;) The image should really have been a data-stream
+		 *  (so type = 0xFF basicly). */
+		uint num = sprite.width * sprite.height;
+
+		Sprite *s = (Sprite *)allocator(sizeof(*s) + num);
+		s->width  = sprite.width;
+		s->height = sprite.height;
+		s->x_offs = sprite.x_offs;
+		s->y_offs = sprite.y_offs;
+
+		SpriteLoader::CommonPixel *src = sprite.data;
+		byte *dest = s->data;
+		while (num-- > 0) {
+			*dest++ = src->m;
+			src++;
+		}
+
+		return s;
+	}
+
 	return BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, allocator);
 }