src/spritecache.cpp
branchnoai
changeset 9627 6a7c8ead2328
parent 9626 79f2b5a0cdd7
child 9628 b5c2449616b5
equal deleted inserted replaced
9626:79f2b5a0cdd7 9627:6a7c8ead2328
   116 	return GetSpriteCache(id)->file_pos != 0;
   116 	return GetSpriteCache(id)->file_pos != 0;
   117 }
   117 }
   118 
   118 
   119 void* AllocSprite(size_t);
   119 void* AllocSprite(size_t);
   120 
   120 
   121 static void* ReadSprite(SpriteCache *sc, SpriteID id)
   121 static void* ReadSprite(SpriteCache *sc, SpriteID id, bool real_sprite)
   122 {
   122 {
   123 	uint32 file_pos = sc->file_pos;
   123 	uint32 file_pos = sc->file_pos;
   124 
   124 
   125 	DEBUG(sprite, 9, "Load sprite %d", id);
   125 	DEBUG(sprite, 9, "Load sprite %d", id);
   126 
   126 
   137 	/* Read the size and type */
   137 	/* Read the size and type */
   138 	int num  = FioReadWord();
   138 	int num  = FioReadWord();
   139 	byte type = FioReadByte();
   139 	byte type = FioReadByte();
   140 	/* Type 0xFF indicates either a colormap or some other non-sprite info */
   140 	/* Type 0xFF indicates either a colormap or some other non-sprite info */
   141 	if (type == 0xFF) {
   141 	if (type == 0xFF) {
       
   142 		if (real_sprite) {
       
   143 			static byte warning_level = 0;
       
   144 			DEBUG(sprite, warning_level, "Tried to load non sprite #%d as a real sprite. Probable cause: NewGRF interference", id);
       
   145 			warning_level = 6;
       
   146 			if (id == SPR_IMG_QUERY) error("Uhm, would you be so kind not to load a NewGRF that makes the 'query' sprite a non- sprite?");
       
   147 			return (void*)GetSprite(SPR_IMG_QUERY);
       
   148 		}
       
   149 
   142 		byte *dest = (byte *)AllocSprite(num);
   150 		byte *dest = (byte *)AllocSprite(num);
   143 
   151 
   144 		sc->ptr = dest;
   152 		sc->ptr = dest;
   145 		FioReadBlock(dest, num);
   153 		FioReadBlock(dest, num);
   146 
   154 
   147 		return dest;
   155 		return sc->ptr;
       
   156 	}
       
   157 	/* Ugly hack to work around the problem that the old landscape
       
   158 	 *  generator assumes that those sprites are stored uncompressed in
       
   159 	 *  the memory, and they are only read directly by the code, never
       
   160 	 *  send to the blitter. So do not send it to the blitter (which will
       
   161 	 *  result in a data array in the format the blitter likes most), but
       
   162 	 *  read the data directly from disk and store that as sprite.
       
   163 	 * Ugly: yes. Other solution: no. Blame the original author or
       
   164 	 *  something ;) The image should really have been a data-stream
       
   165 	 *  (so type = 0xFF basicly). */
       
   166 	if (id >= 4845 && id <= 4881) {
       
   167 		uint height = FioReadByte();
       
   168 		uint width  = FioReadWord();
       
   169 		Sprite *sprite;
       
   170 		byte *dest;
       
   171 
       
   172 		num = width * height;
       
   173 		sprite = (Sprite *)AllocSprite(sizeof(*sprite) + num);
       
   174 		sc->ptr = sprite;
       
   175 		sprite->height = height;
       
   176 		sprite->width  = width;
       
   177 		sprite->x_offs = FioReadWord();
       
   178 		sprite->y_offs = FioReadWord();
       
   179 
       
   180 		dest = sprite->data;
       
   181 		while (num > 0) {
       
   182 			int8 i = FioReadByte();
       
   183 			if (i >= 0) {
       
   184 				num -= i;
       
   185 				for (; i > 0; --i) *dest++ = FioReadByte();
       
   186 			} else {
       
   187 				const byte* rel = dest - (((i & 7) << 8) | FioReadByte());
       
   188 				i = -(i >> 3);
       
   189 				num -= i;
       
   190 				for (; i > 0; --i) *dest++ = *rel++;
       
   191 			}
       
   192 		}
       
   193 
       
   194 		return sc->ptr;
       
   195 	}
       
   196 
       
   197 	if (!real_sprite) {
       
   198 		static byte warning_level = 0;
       
   199 		DEBUG(sprite, warning_level, "Tried to load real sprite #%d as a non sprite. Probable cause: NewGRF interference", id);
       
   200 		warning_level = 6;
   148 	}
   201 	}
   149 
   202 
   150 	SpriteLoaderGrf sprite_loader;
   203 	SpriteLoaderGrf sprite_loader;
   151 	SpriteLoader::Sprite sprite;
   204 	SpriteLoader::Sprite sprite;
   152 
   205 
   362 		DeleteEntryFromSpriteCache();
   415 		DeleteEntryFromSpriteCache();
   363 	}
   416 	}
   364 }
   417 }
   365 
   418 
   366 
   419 
   367 const void *GetRawSprite(SpriteID sprite)
   420 const void *GetRawSprite(SpriteID sprite, bool real_sprite)
   368 {
   421 {
   369 	SpriteCache *sc;
   422 	SpriteCache *sc;
   370 	void* p;
   423 	void* p;
   371 
   424 
   372 	assert(sprite < _spritecache_items);
   425 	assert(sprite < _spritecache_items);
   377 	sc->lru = ++_sprite_lru_counter;
   430 	sc->lru = ++_sprite_lru_counter;
   378 
   431 
   379 	p = sc->ptr;
   432 	p = sc->ptr;
   380 
   433 
   381 	/* Load the sprite, if it is not loaded, yet */
   434 	/* Load the sprite, if it is not loaded, yet */
   382 	if (p == NULL) p = ReadSprite(sc, sprite);
   435 	if (p == NULL) p = ReadSprite(sc, sprite, real_sprite);
   383 	return p;
   436 	return p;
   384 }
   437 }
   385 
   438 
   386 
   439 
   387 void GfxInitSpriteMem()
   440 void GfxInitSpriteMem()