spritecache.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "fileio_func.h"
00014 #include "spriteloader/grf.hpp"
00015 #include "gfx_func.h"
00016 #include "zoom_func.h"
00017 #include "settings_type.h"
00018 #include "blitter/factory.hpp"
00019 #include "core/math_func.hpp"
00020 #include "core/mem_func.hpp"
00021 
00022 #include "table/sprites.h"
00023 #include "table/palette_convert.h"
00024 
00025 /* Default of 4MB spritecache */
00026 uint _sprite_cache_size = 4;
00027 
00028 typedef SimpleTinyEnumT<SpriteType, byte> SpriteTypeByte;
00029 
00030 struct SpriteCache {
00031   void *ptr;
00032   size_t file_pos;
00033   uint32 id;
00034   uint16 file_slot;
00035   int16 lru;
00036   SpriteTypeByte type; 
00037   bool warned;         
00038   byte container_ver;  
00039 };
00040 
00041 
00042 static uint _spritecache_items = 0;
00043 static SpriteCache *_spritecache = NULL;
00044 
00045 
00046 static inline SpriteCache *GetSpriteCache(uint index)
00047 {
00048   return &_spritecache[index];
00049 }
00050 
00051 static inline bool IsMapgenSpriteID(SpriteID sprite)
00052 {
00053   return IsInsideMM(sprite, 4845, 4882);
00054 }
00055 
00056 static SpriteCache *AllocateSpriteCache(uint index)
00057 {
00058   if (index >= _spritecache_items) {
00059     /* Add another 1024 items to the 'pool' */
00060     uint items = Align(index + 1, 1024);
00061 
00062     DEBUG(sprite, 4, "Increasing sprite cache to %u items (" PRINTF_SIZE " bytes)", items, items * sizeof(*_spritecache));
00063 
00064     _spritecache = ReallocT(_spritecache, items);
00065 
00066     /* Reset the new items and update the count */
00067     memset(_spritecache + _spritecache_items, 0, (items - _spritecache_items) * sizeof(*_spritecache));
00068     _spritecache_items = items;
00069   }
00070 
00071   return GetSpriteCache(index);
00072 }
00073 
00074 
00075 struct MemBlock {
00076   size_t size;
00077   byte data[];
00078 };
00079 
00080 static uint _sprite_lru_counter;
00081 static MemBlock *_spritecache_ptr;
00082 static int _compact_cache_counter;
00083 
00084 static void CompactSpriteCache();
00085 static void *AllocSprite(size_t mem_req);
00086 
00093 bool SkipSpriteData(byte type, uint16 num)
00094 {
00095   if (type & 2) {
00096     FioSkipBytes(num);
00097   } else {
00098     while (num > 0) {
00099       int8 i = FioReadByte();
00100       if (i >= 0) {
00101         int size = (i == 0) ? 0x80 : i;
00102         if (size > num) return false;
00103         num -= size;
00104         FioSkipBytes(size);
00105       } else {
00106         i = -(i >> 3);
00107         num -= i;
00108         FioReadByte();
00109       }
00110     }
00111   }
00112   return true;
00113 }
00114 
00115 /* Check if the given Sprite ID exists */
00116 bool SpriteExists(SpriteID id)
00117 {
00118   /* Special case for Sprite ID zero -- its position is also 0... */
00119   if (id == 0) return true;
00120   if (id >= _spritecache_items) return false;
00121   return !(GetSpriteCache(id)->file_pos == 0 && GetSpriteCache(id)->file_slot == 0);
00122 }
00123 
00129 SpriteType GetSpriteType(SpriteID sprite)
00130 {
00131   if (!SpriteExists(sprite)) return ST_INVALID;
00132   return GetSpriteCache(sprite)->type;
00133 }
00134 
00140 uint GetOriginFileSlot(SpriteID sprite)
00141 {
00142   if (!SpriteExists(sprite)) return 0;
00143   return GetSpriteCache(sprite)->file_slot;
00144 }
00145 
00154 uint GetMaxSpriteID()
00155 {
00156   return _spritecache_items;
00157 }
00158 
00159 static bool ResizeSpriteIn(SpriteLoader::Sprite *sprite, ZoomLevel src, ZoomLevel tgt)
00160 {
00161   uint8 scaled_1 = UnScaleByZoom(1, (ZoomLevel)(tgt - src));
00162 
00163   /* Check for possible memory overflow. */
00164   if (sprite[src].width * scaled_1 > UINT16_MAX || sprite[src].height * scaled_1 > UINT16_MAX) return false;
00165 
00166   sprite[tgt].width  = sprite[src].width  * scaled_1;
00167   sprite[tgt].height = sprite[src].height * scaled_1;
00168   sprite[tgt].x_offs = sprite[src].x_offs * scaled_1;
00169   sprite[tgt].y_offs = sprite[src].y_offs * scaled_1;
00170 
00171   sprite[tgt].AllocateData(tgt, sprite[tgt].width * sprite[tgt].height);
00172 
00173   SpriteLoader::CommonPixel *dst = sprite[tgt].data;
00174   for (int y = 0; y < sprite[tgt].height; y++) {
00175     const SpriteLoader::CommonPixel *src_ln = &sprite[src].data[y / scaled_1 * sprite[src].width];
00176     for (int x = 0; x < sprite[tgt].width; x++) {
00177       *dst = src_ln[x / scaled_1];
00178       dst++;
00179     }
00180   }
00181 
00182   return true;
00183 }
00184 
00185 static void ResizeSpriteOut(SpriteLoader::Sprite *sprite, ZoomLevel zoom)
00186 {
00187   /* Algorithm based on 32bpp_Optimized::ResizeSprite() */
00188   sprite[zoom].width  = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].width,  zoom);
00189   sprite[zoom].height = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].height, zoom);
00190   sprite[zoom].x_offs = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].x_offs, zoom);
00191   sprite[zoom].y_offs = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].y_offs, zoom);
00192 
00193   sprite[zoom].AllocateData(zoom, sprite[zoom].height * sprite[zoom].width);
00194 
00195   SpriteLoader::CommonPixel *dst = sprite[zoom].data;
00196   const SpriteLoader::CommonPixel *src = sprite[zoom - 1].data;
00197   const SpriteLoader::CommonPixel *src_end = src + sprite[zoom - 1].height * sprite[zoom - 1].width;
00198 
00199   for (uint y = 0; y < sprite[zoom].height; y++) {
00200     if (src >= src_end) src = src_end - sprite[zoom - 1].width;
00201 
00202     const SpriteLoader::CommonPixel *src_ln = src + sprite[zoom - 1].width * 2;
00203     for (uint x = 0; x < sprite[zoom].width; x++) {
00204       if (src >= src_ln) src = src_ln - 1;
00205       if ((src + 1)->a != 0) { *dst = *(src + 1); }
00206       else { *dst = *src; }
00207       dst++;
00208       src += 2;
00209     }
00210 
00211     src = src_ln;
00212   }
00213 }
00214 
00215 static bool PadSingleSprite(SpriteLoader::Sprite *sprite, ZoomLevel zoom, uint pad_left, uint pad_top, uint pad_right, uint pad_bottom)
00216 {
00217   uint width  = sprite->width + pad_left + pad_right;
00218   uint height = sprite->height + pad_top + pad_bottom;
00219 
00220   if (width > UINT16_MAX || height > UINT16_MAX) return false;
00221 
00222   /* Copy source data and reallocate sprite memory. */
00223   SpriteLoader::CommonPixel *src_data = MallocT<SpriteLoader::CommonPixel>(sprite->width * sprite->height);
00224   MemCpyT(src_data, sprite->data, sprite->width * sprite->height);
00225   sprite->AllocateData(zoom, width * height);
00226 
00227   /* Copy with padding to destination. */
00228   SpriteLoader::CommonPixel *src = src_data;
00229   SpriteLoader::CommonPixel *data = sprite->data;
00230   for (uint y = 0; y < height; y++) {
00231     if (y < pad_top || pad_bottom + y >= height) {
00232       /* Top/bottom padding. */
00233       MemSetT(data, 0, width);
00234       data += width;
00235     } else {
00236       if (pad_left > 0) {
00237         /* Pad left. */
00238         MemSetT(data, 0, pad_left);
00239         data += pad_left;
00240       }
00241 
00242       /* Copy pixels. */
00243       MemCpyT(data, src, sprite->width);
00244       src += sprite->width;
00245       data += sprite->width;
00246 
00247       if (pad_right > 0) {
00248         /* Pad right. */
00249         MemSetT(data, 0, pad_right);
00250         data += pad_right;
00251       }
00252     }
00253   }
00254   free(src_data);
00255 
00256   /* Update sprite size. */
00257   sprite->width   = width;
00258   sprite->height  = height;
00259   sprite->x_offs -= pad_left;
00260   sprite->y_offs -= pad_top;
00261 
00262   return true;
00263 }
00264 
00265 static bool PadSprites(SpriteLoader::Sprite *sprite, uint8 sprite_avail)
00266 {
00267   int left   = sprite[ZOOM_LVL_NORMAL].x_offs;
00268   int top    = sprite[ZOOM_LVL_NORMAL].y_offs;
00269   int right  = sprite[ZOOM_LVL_NORMAL].x_offs + sprite[ZOOM_LVL_NORMAL].width;
00270   int bottom = sprite[ZOOM_LVL_NORMAL].y_offs + sprite[ZOOM_LVL_NORMAL].height;
00271 
00272   /* Find combined bounds of all zoom levels.*/
00273   for (ZoomLevel zoom = ZOOM_LVL_OUT_2X; zoom != ZOOM_LVL_END; zoom++) {
00274     if (HasBit(sprite_avail, zoom)) {
00275       uint8 scaled_1 = ScaleByZoom(1, zoom);
00276 
00277       left   = min(left, sprite[zoom].x_offs * scaled_1);
00278       top    = min(top, sprite[zoom].y_offs * scaled_1);
00279       right  = max(right, (sprite[zoom].x_offs + sprite[zoom].width - 1) * scaled_1);
00280       bottom = max(bottom, (sprite[zoom].y_offs + sprite[zoom].height - 1) * scaled_1);
00281     }
00282   }
00283 
00284   /* Pad too small sprites. */
00285   SetBit(sprite_avail, ZOOM_LVL_NORMAL);
00286   for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
00287     if (HasBit(sprite_avail, zoom)) {
00288       int pad_left   = sprite[zoom].x_offs - UnScaleByZoom(left, zoom);
00289       int pad_top    = sprite[zoom].y_offs - UnScaleByZoom(top, zoom);
00290       int pad_right  = UnScaleByZoom(right, zoom) - (sprite[zoom].x_offs + sprite[zoom].width);
00291       int pad_bottom = UnScaleByZoom(bottom, zoom) - (sprite[zoom].y_offs + sprite[zoom].height);
00292 
00293       if (pad_left != 0 || pad_right != 0 || pad_top != 0 || pad_bottom != 0) {
00294         if (!PadSingleSprite(&sprite[zoom], zoom, pad_left, pad_top, pad_right, pad_bottom)) return false;
00295       }
00296     }
00297   }
00298 
00299   return true;
00300 }
00301 
00302 static bool ResizeSprites(SpriteLoader::Sprite *sprite, uint8 sprite_avail, uint32 file_slot, uint32 file_pos)
00303 {
00304   /* Create a fully zoomed image if it does not exist */
00305   ZoomLevel first_avail = static_cast<ZoomLevel>(FIND_FIRST_BIT(sprite_avail));
00306   if (first_avail != ZOOM_LVL_NORMAL) {
00307     if (!ResizeSpriteIn(sprite, first_avail, ZOOM_LVL_NORMAL)) return false;
00308   }
00309 
00310   /* Pad sprites to make sizes match. */
00311   if (!PadSprites(sprite, sprite_avail)) return false;
00312 
00313   /* Create other missing zoom levels */
00314   for (ZoomLevel zoom = ZOOM_LVL_OUT_2X; zoom != ZOOM_LVL_END; zoom++) {
00315     if (HasBit(sprite_avail, zoom)) {
00316       /* Check that size and offsets match the fully zoomed image. */
00317       assert(sprite[zoom].width  == UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].width,  zoom));
00318       assert(sprite[zoom].height == UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].height, zoom));
00319       assert(sprite[zoom].x_offs == UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].x_offs, zoom));
00320       assert(sprite[zoom].y_offs == UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].y_offs, zoom));
00321     }
00322 
00323     /* Zoom level is not available, or unusable, so create it */
00324     if (!HasBit(sprite_avail, zoom)) ResizeSpriteOut(sprite, zoom);
00325   }
00326 
00327   return  true;
00328 }
00329 
00336 static void *ReadRecolourSprite(uint16 file_slot, uint num)
00337 {
00338   /* "Normal" recolour sprites are ALWAYS 257 bytes. Then there is a small
00339    * number of recolour sprites that are 17 bytes that only exist in DOS
00340    * GRFs which are the same as 257 byte recolour sprites, but with the last
00341    * 240 bytes zeroed.  */
00342   static const uint RECOLOUR_SPRITE_SIZE = 257;
00343   byte *dest = (byte *)AllocSprite(max(RECOLOUR_SPRITE_SIZE, num));
00344 
00345   if (_palette_remap_grf[file_slot]) {
00346     byte *dest_tmp = AllocaM(byte, max(RECOLOUR_SPRITE_SIZE, num));
00347 
00348     /* Only a few recolour sprites are less than 257 bytes */
00349     if (num < RECOLOUR_SPRITE_SIZE) memset(dest_tmp, 0, RECOLOUR_SPRITE_SIZE);
00350     FioReadBlock(dest_tmp, num);
00351 
00352     /* The data of index 0 is never used; "literal 00" according to the (New)GRF specs. */
00353     for (uint i = 1; i < RECOLOUR_SPRITE_SIZE; i++) {
00354       dest[i] = _palmap_w2d[dest_tmp[_palmap_d2w[i - 1] + 1]];
00355     }
00356   } else {
00357     FioReadBlock(dest, num);
00358   }
00359 
00360   return dest;
00361 }
00362 
00371 static void *ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_type, AllocatorProc *allocator)
00372 {
00373   uint8 file_slot = sc->file_slot;
00374   size_t file_pos = sc->file_pos;
00375 
00376   assert(sprite_type != ST_RECOLOUR);
00377   assert(IsMapgenSpriteID(id) == (sprite_type == ST_MAPGEN));
00378   assert(sc->type == sprite_type);
00379 
00380   DEBUG(sprite, 9, "Load sprite %d", id);
00381 
00382   SpriteLoader::Sprite sprite[ZOOM_LVL_COUNT];
00383   uint8 sprite_avail = 0;
00384   sprite[ZOOM_LVL_NORMAL].type = sprite_type;
00385 
00386   SpriteLoaderGrf sprite_loader(sc->container_ver);
00387   if (sprite_type != ST_MAPGEN && BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 32) {
00388     /* Try for 32bpp sprites first. */
00389     sprite_avail = sprite_loader.LoadSprite(sprite, file_slot, file_pos, sprite_type, true);
00390   }
00391   if (sprite_avail == 0) {
00392     sprite_avail = sprite_loader.LoadSprite(sprite, file_slot, file_pos, sprite_type, false);
00393   }
00394 
00395   if (sprite_avail == 0) {
00396     if (sprite_type == ST_MAPGEN) return NULL;
00397     if (id == SPR_IMG_QUERY) usererror("Okay... something went horribly wrong. I couldn't load the fallback sprite. What should I do?");
00398     return (void*)GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
00399   }
00400 
00401   if (sprite_type == ST_MAPGEN) {
00402     /* Ugly hack to work around the problem that the old landscape
00403      *  generator assumes that those sprites are stored uncompressed in
00404      *  the memory, and they are only read directly by the code, never
00405      *  send to the blitter. So do not send it to the blitter (which will
00406      *  result in a data array in the format the blitter likes most), but
00407      *  extract the data directly and store that as sprite.
00408      * Ugly: yes. Other solution: no. Blame the original author or
00409      *  something ;) The image should really have been a data-stream
00410      *  (so type = 0xFF basicly). */
00411     uint num = sprite[ZOOM_LVL_NORMAL].width * sprite[ZOOM_LVL_NORMAL].height;
00412 
00413     Sprite *s = (Sprite *)allocator(sizeof(*s) + num);
00414     s->width  = sprite[ZOOM_LVL_NORMAL].width;
00415     s->height = sprite[ZOOM_LVL_NORMAL].height;
00416     s->x_offs = sprite[ZOOM_LVL_NORMAL].x_offs;
00417     s->y_offs = sprite[ZOOM_LVL_NORMAL].y_offs;
00418 
00419     SpriteLoader::CommonPixel *src = sprite[ZOOM_LVL_NORMAL].data;
00420     byte *dest = s->data;
00421     while (num-- > 0) {
00422       *dest++ = src->m;
00423       src++;
00424     }
00425 
00426     return s;
00427   }
00428 
00429   if (sprite_type == ST_NORMAL) {
00430     if (!ResizeSprites(sprite, sprite_avail, file_slot, sc->id)) {
00431       if (id == SPR_IMG_QUERY) usererror("Okay... something went horribly wrong. I couldn't resize the fallback sprite. What should I do?");
00432       return (void*)GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
00433     }
00434   }
00435   return BlitterFactoryBase::GetCurrentBlitter()->Encode(sprite, allocator);
00436 }
00437 
00438 
00440 static std::map<uint32, size_t> _grf_sprite_offsets;
00441 
00447 size_t GetGRFSpriteOffset(uint32 id)
00448 {
00449   return _grf_sprite_offsets.find(id) != _grf_sprite_offsets.end() ? _grf_sprite_offsets[id] : SIZE_MAX;
00450 }
00451 
00456 void ReadGRFSpriteOffsets(byte container_version)
00457 {
00458   _grf_sprite_offsets.clear();
00459 
00460   if (container_version >= 2) {
00461     /* Seek to sprite section of the GRF. */
00462     size_t data_offset = FioReadDword();
00463     size_t old_pos = FioGetPos();
00464     FioSeekTo(data_offset, SEEK_CUR);
00465 
00466     /* Loop over all sprite section entries and store the file
00467      * offset for each newly encountered ID. */
00468     uint32 id, prev_id = 0;
00469     while ((id = FioReadDword()) != 0) {
00470       if (id != prev_id) _grf_sprite_offsets[id] = FioGetPos() - 4;
00471       prev_id = id;
00472       FioSkipBytes(FioReadDword());
00473     }
00474 
00475     /* Continue processing the data section. */
00476     FioSeekTo(old_pos, SEEK_SET);
00477   }
00478 }
00479 
00480 
00489 bool LoadNextSprite(int load_index, byte file_slot, uint file_sprite_id, byte container_version)
00490 {
00491   size_t file_pos = FioGetPos();
00492 
00493   /* Read sprite header. */
00494   uint32 num = container_version >= 2 ? FioReadDword() : FioReadWord();
00495   if (num == 0) return false;
00496   byte grf_type = FioReadByte();
00497 
00498   SpriteType type;
00499   void *data = NULL;
00500   if (grf_type == 0xFF) {
00501     /* Some NewGRF files have "empty" pseudo-sprites which are 1
00502      * byte long. Catch these so the sprites won't be displayed. */
00503     if (num == 1) {
00504       FioReadByte();
00505       return false;
00506     }
00507     type = ST_RECOLOUR;
00508     data = ReadRecolourSprite(file_slot, num);
00509   } else if (container_version >= 2 && grf_type == 0xFD) {
00510     if (num != 4) {
00511       /* Invalid sprite section include, ignore. */
00512       FioSkipBytes(num);
00513       return false;
00514     }
00515     /* It is not an error if no sprite with the provided ID is found in the sprite section. */
00516     file_pos = GetGRFSpriteOffset(FioReadDword());
00517     type = ST_NORMAL;
00518   } else {
00519     FioSkipBytes(7);
00520     type = SkipSpriteData(grf_type, num - 8) ? ST_NORMAL : ST_INVALID;
00521     /* Inline sprites are not supported for container version >= 2. */
00522     if (container_version >= 2) return false;
00523   }
00524 
00525   if (type == ST_INVALID) return false;
00526 
00527   if (load_index >= MAX_SPRITES) {
00528     usererror("Tried to load too many sprites (#%d; max %d)", load_index, MAX_SPRITES);
00529   }
00530 
00531   bool is_mapgen = IsMapgenSpriteID(load_index);
00532 
00533   if (is_mapgen) {
00534     if (type != ST_NORMAL) usererror("Uhm, would you be so kind not to load a NewGRF that changes the type of the map generator sprites?");
00535     type = ST_MAPGEN;
00536   }
00537 
00538   SpriteCache *sc = AllocateSpriteCache(load_index);
00539   sc->file_slot = file_slot;
00540   sc->file_pos = file_pos;
00541   sc->ptr = data;
00542   sc->lru = 0;
00543   sc->id = file_sprite_id;
00544   sc->type = type;
00545   sc->warned = false;
00546   sc->container_ver = container_version;
00547 
00548   return true;
00549 }
00550 
00551 
00552 void DupSprite(SpriteID old_spr, SpriteID new_spr)
00553 {
00554   SpriteCache *scnew = AllocateSpriteCache(new_spr); // may reallocate: so put it first
00555   SpriteCache *scold = GetSpriteCache(old_spr);
00556 
00557   scnew->file_slot = scold->file_slot;
00558   scnew->file_pos = scold->file_pos;
00559   scnew->ptr = NULL;
00560   scnew->id = scold->id;
00561   scnew->type = scold->type;
00562   scnew->warned = false;
00563   scnew->container_ver = scold->container_ver;
00564 }
00565 
00572 static const size_t S_FREE_MASK = sizeof(size_t) - 1;
00573 
00574 /* to make sure nobody adds things to MemBlock without checking S_FREE_MASK first */
00575 assert_compile(sizeof(MemBlock) == sizeof(size_t));
00576 /* make sure it's a power of two */
00577 assert_compile((sizeof(size_t) & (sizeof(size_t) - 1)) == 0);
00578 
00579 static inline MemBlock *NextBlock(MemBlock *block)
00580 {
00581   return (MemBlock*)((byte*)block + (block->size & ~S_FREE_MASK));
00582 }
00583 
00584 static size_t GetSpriteCacheUsage()
00585 {
00586   size_t tot_size = 0;
00587   MemBlock *s;
00588 
00589   for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
00590     if (!(s->size & S_FREE_MASK)) tot_size += s->size;
00591   }
00592 
00593   return tot_size;
00594 }
00595 
00596 
00597 void IncreaseSpriteLRU()
00598 {
00599   /* Increase all LRU values */
00600   if (_sprite_lru_counter > 16384) {
00601     SpriteID i;
00602 
00603     DEBUG(sprite, 3, "Fixing lru %u, inuse=" PRINTF_SIZE, _sprite_lru_counter, GetSpriteCacheUsage());
00604 
00605     for (i = 0; i != _spritecache_items; i++) {
00606       SpriteCache *sc = GetSpriteCache(i);
00607       if (sc->ptr != NULL) {
00608         if (sc->lru >= 0) {
00609           sc->lru = -1;
00610         } else if (sc->lru != -32768) {
00611           sc->lru--;
00612         }
00613       }
00614     }
00615     _sprite_lru_counter = 0;
00616   }
00617 
00618   /* Compact sprite cache every now and then. */
00619   if (++_compact_cache_counter >= 740) {
00620     CompactSpriteCache();
00621     _compact_cache_counter = 0;
00622   }
00623 }
00624 
00629 static void CompactSpriteCache()
00630 {
00631   MemBlock *s;
00632 
00633   DEBUG(sprite, 3, "Compacting sprite cache, inuse=" PRINTF_SIZE, GetSpriteCacheUsage());
00634 
00635   for (s = _spritecache_ptr; s->size != 0;) {
00636     if (s->size & S_FREE_MASK) {
00637       MemBlock *next = NextBlock(s);
00638       MemBlock temp;
00639       SpriteID i;
00640 
00641       /* Since free blocks are automatically coalesced, this should hold true. */
00642       assert(!(next->size & S_FREE_MASK));
00643 
00644       /* If the next block is the sentinel block, we can safely return */
00645       if (next->size == 0) break;
00646 
00647       /* Locate the sprite belonging to the next pointer. */
00648       for (i = 0; GetSpriteCache(i)->ptr != next->data; i++) {
00649         assert(i != _spritecache_items);
00650       }
00651 
00652       GetSpriteCache(i)->ptr = s->data; // Adjust sprite array entry
00653       /* Swap this and the next block */
00654       temp = *s;
00655       memmove(s, next, next->size);
00656       s = NextBlock(s);
00657       *s = temp;
00658 
00659       /* Coalesce free blocks */
00660       while (NextBlock(s)->size & S_FREE_MASK) {
00661         s->size += NextBlock(s)->size & ~S_FREE_MASK;
00662       }
00663     } else {
00664       s = NextBlock(s);
00665     }
00666   }
00667 }
00668 
00673 static void DeleteEntryFromSpriteCache(uint item)
00674 {
00675   /* Mark the block as free (the block must be in use) */
00676   MemBlock *s = (MemBlock*)GetSpriteCache(item)->ptr - 1;
00677   assert(!(s->size & S_FREE_MASK));
00678   s->size |= S_FREE_MASK;
00679   GetSpriteCache(item)->ptr = NULL;
00680 
00681   /* And coalesce adjacent free blocks */
00682   for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
00683     if (s->size & S_FREE_MASK) {
00684       while (NextBlock(s)->size & S_FREE_MASK) {
00685         s->size += NextBlock(s)->size & ~S_FREE_MASK;
00686       }
00687     }
00688   }
00689 }
00690 
00691 static void DeleteEntryFromSpriteCache()
00692 {
00693   uint best = UINT_MAX;
00694   int cur_lru;
00695 
00696   DEBUG(sprite, 3, "DeleteEntryFromSpriteCache, inuse=" PRINTF_SIZE, GetSpriteCacheUsage());
00697 
00698   cur_lru = 0xffff;
00699   for (SpriteID i = 0; i != _spritecache_items; i++) {
00700     SpriteCache *sc = GetSpriteCache(i);
00701     if (sc->type != ST_RECOLOUR && sc->ptr != NULL && sc->lru < cur_lru) {
00702       cur_lru = sc->lru;
00703       best = i;
00704     }
00705   }
00706 
00707   /* Display an error message and die, in case we found no sprite at all.
00708    * This shouldn't really happen, unless all sprites are locked. */
00709   if (best == UINT_MAX) error("Out of sprite memory");
00710 
00711   DeleteEntryFromSpriteCache(best);
00712 }
00713 
00714 static void *AllocSprite(size_t mem_req)
00715 {
00716   mem_req += sizeof(MemBlock);
00717 
00718   /* Align this to correct boundary. This also makes sure at least one
00719    * bit is not used, so we can use it for other things. */
00720   mem_req = Align(mem_req, S_FREE_MASK + 1);
00721 
00722   for (;;) {
00723     MemBlock *s;
00724 
00725     for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
00726       if (s->size & S_FREE_MASK) {
00727         size_t cur_size = s->size & ~S_FREE_MASK;
00728 
00729         /* Is the block exactly the size we need or
00730          * big enough for an additional free block? */
00731         if (cur_size == mem_req ||
00732             cur_size >= mem_req + sizeof(MemBlock)) {
00733           /* Set size and in use */
00734           s->size = mem_req;
00735 
00736           /* Do we need to inject a free block too? */
00737           if (cur_size != mem_req) {
00738             NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK;
00739           }
00740 
00741           return s->data;
00742         }
00743       }
00744     }
00745 
00746     /* Reached sentinel, but no block found yet. Delete some old entry. */
00747     DeleteEntryFromSpriteCache();
00748   }
00749 }
00750 
00760 static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc, AllocatorProc *allocator)
00761 {
00762   static const char * const sprite_types[] = {
00763     "normal",        // ST_NORMAL
00764     "map generator", // ST_MAPGEN
00765     "character",     // ST_FONT
00766     "recolour",      // ST_RECOLOUR
00767   };
00768 
00769   SpriteType available = sc->type;
00770   if (requested == ST_FONT && available == ST_NORMAL) {
00771     if (sc->ptr == NULL) sc->type = ST_FONT;
00772     return GetRawSprite(sprite, sc->type, allocator);
00773   }
00774 
00775   byte warning_level = sc->warned ? 6 : 0;
00776   sc->warned = true;
00777   DEBUG(sprite, warning_level, "Tried to load %s sprite #%d as a %s sprite. Probable cause: NewGRF interference", sprite_types[available], sprite, sprite_types[requested]);
00778 
00779   switch (requested) {
00780     case ST_NORMAL:
00781       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?");
00782       /* FALL THROUGH */
00783     case ST_FONT:
00784       return GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
00785     case ST_RECOLOUR:
00786       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?");
00787       return GetRawSprite(PALETTE_TO_DARK_BLUE, ST_RECOLOUR, allocator);
00788     case ST_MAPGEN:
00789       /* this shouldn't happen, overriding of ST_MAPGEN sprites is checked in LoadNextSprite()
00790        * (the only case the check fails is when these sprites weren't even loaded...) */
00791     default:
00792       NOT_REACHED();
00793   }
00794 }
00795 
00804 void *GetRawSprite(SpriteID sprite, SpriteType type, AllocatorProc *allocator)
00805 {
00806   assert(IsMapgenSpriteID(sprite) == (type == ST_MAPGEN));
00807   assert(type < ST_INVALID);
00808 
00809   if (!SpriteExists(sprite)) {
00810     DEBUG(sprite, 1, "Tried to load non-existing sprite #%d. Probable cause: Wrong/missing NewGRFs", sprite);
00811 
00812     /* SPR_IMG_QUERY is a BIG FAT RED ? */
00813     sprite = SPR_IMG_QUERY;
00814   }
00815 
00816   SpriteCache *sc = GetSpriteCache(sprite);
00817 
00818   if (sc->type != type) return HandleInvalidSpriteRequest(sprite, type, sc, allocator);
00819 
00820   if (allocator == NULL) {
00821     /* Load sprite into/from spritecache */
00822 
00823     /* Update LRU */
00824     sc->lru = ++_sprite_lru_counter;
00825 
00826     /* Load the sprite, if it is not loaded, yet */
00827     if (sc->ptr == NULL) sc->ptr = ReadSprite(sc, sprite, type, AllocSprite);
00828 
00829     return sc->ptr;
00830   } else {
00831     /* Do not use the spritecache, but a different allocator. */
00832     return ReadSprite(sc, sprite, type, allocator);
00833   }
00834 }
00835 
00836 
00837 static void GfxInitSpriteCache()
00838 {
00839   /* initialize sprite cache heap */
00840   if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)MallocT<byte>(_sprite_cache_size * 1024 * 1024);
00841 
00842   /* A big free block */
00843   _spritecache_ptr->size = ((_sprite_cache_size * 1024 * 1024) - sizeof(MemBlock)) | S_FREE_MASK;
00844   /* Sentinel block (identified by size == 0) */
00845   NextBlock(_spritecache_ptr)->size = 0;
00846 }
00847 
00848 void GfxInitSpriteMem()
00849 {
00850   GfxInitSpriteCache();
00851 
00852   /* Reset the spritecache 'pool' */
00853   free(_spritecache);
00854   _spritecache_items = 0;
00855   _spritecache = NULL;
00856 
00857   _compact_cache_counter = 0;
00858 }
00859 
00864 void GfxClearSpriteCache()
00865 {
00866   /* Clear sprite ptr for all cached items */
00867   for (uint i = 0; i != _spritecache_items; i++) {
00868     SpriteCache *sc = GetSpriteCache(i);
00869     if (sc->type != ST_RECOLOUR && sc->ptr != NULL) DeleteEntryFromSpriteCache(i);
00870   }
00871 }
00872 
00873 /* static */ ReusableBuffer<SpriteLoader::CommonPixel> SpriteLoader::Sprite::buffer[ZOOM_LVL_COUNT];