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 uint _allocated_sprite_cache_size = 0;
00083 static int _compact_cache_counter;
00084 
00085 static void CompactSpriteCache();
00086 static void *AllocSprite(size_t mem_req);
00087 
00094 bool SkipSpriteData(byte type, uint16 num)
00095 {
00096   if (type & 2) {
00097     FioSkipBytes(num);
00098   } else {
00099     while (num > 0) {
00100       int8 i = FioReadByte();
00101       if (i >= 0) {
00102         int size = (i == 0) ? 0x80 : i;
00103         if (size > num) return false;
00104         num -= size;
00105         FioSkipBytes(size);
00106       } else {
00107         i = -(i >> 3);
00108         num -= i;
00109         FioReadByte();
00110       }
00111     }
00112   }
00113   return true;
00114 }
00115 
00116 /* Check if the given Sprite ID exists */
00117 bool SpriteExists(SpriteID id)
00118 {
00119   /* Special case for Sprite ID zero -- its position is also 0... */
00120   if (id == 0) return true;
00121   if (id >= _spritecache_items) return false;
00122   return !(GetSpriteCache(id)->file_pos == 0 && GetSpriteCache(id)->file_slot == 0);
00123 }
00124 
00130 SpriteType GetSpriteType(SpriteID sprite)
00131 {
00132   if (!SpriteExists(sprite)) return ST_INVALID;
00133   return GetSpriteCache(sprite)->type;
00134 }
00135 
00141 uint GetOriginFileSlot(SpriteID sprite)
00142 {
00143   if (!SpriteExists(sprite)) return 0;
00144   return GetSpriteCache(sprite)->file_slot;
00145 }
00146 
00155 uint GetMaxSpriteID()
00156 {
00157   return _spritecache_items;
00158 }
00159 
00160 static bool ResizeSpriteIn(SpriteLoader::Sprite *sprite, ZoomLevel src, ZoomLevel tgt)
00161 {
00162   uint8 scaled_1 = UnScaleByZoom(1, (ZoomLevel)(tgt - src));
00163 
00164   /* Check for possible memory overflow. */
00165   if (sprite[src].width * scaled_1 > UINT16_MAX || sprite[src].height * scaled_1 > UINT16_MAX) return false;
00166 
00167   sprite[tgt].width  = sprite[src].width  * scaled_1;
00168   sprite[tgt].height = sprite[src].height * scaled_1;
00169   sprite[tgt].x_offs = sprite[src].x_offs * scaled_1;
00170   sprite[tgt].y_offs = sprite[src].y_offs * scaled_1;
00171 
00172   sprite[tgt].AllocateData(tgt, sprite[tgt].width * sprite[tgt].height);
00173 
00174   SpriteLoader::CommonPixel *dst = sprite[tgt].data;
00175   for (int y = 0; y < sprite[tgt].height; y++) {
00176     const SpriteLoader::CommonPixel *src_ln = &sprite[src].data[y / scaled_1 * sprite[src].width];
00177     for (int x = 0; x < sprite[tgt].width; x++) {
00178       *dst = src_ln[x / scaled_1];
00179       dst++;
00180     }
00181   }
00182 
00183   return true;
00184 }
00185 
00186 static void ResizeSpriteOut(SpriteLoader::Sprite *sprite, ZoomLevel zoom)
00187 {
00188   /* Algorithm based on 32bpp_Optimized::ResizeSprite() */
00189   sprite[zoom].width  = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].width,  zoom);
00190   sprite[zoom].height = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].height, zoom);
00191   sprite[zoom].x_offs = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].x_offs, zoom);
00192   sprite[zoom].y_offs = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].y_offs, zoom);
00193 
00194   sprite[zoom].AllocateData(zoom, sprite[zoom].height * sprite[zoom].width);
00195 
00196   SpriteLoader::CommonPixel *dst = sprite[zoom].data;
00197   const SpriteLoader::CommonPixel *src = sprite[zoom - 1].data;
00198   const SpriteLoader::CommonPixel *src_end = src + sprite[zoom - 1].height * sprite[zoom - 1].width;
00199 
00200   for (uint y = 0; y < sprite[zoom].height; y++) {
00201     const SpriteLoader::CommonPixel *src_ln = src + sprite[zoom - 1].width;
00202     assert(src_ln <= src_end);
00203     for (uint x = 0; x < sprite[zoom].width; x++) {
00204       assert(src < src_ln);
00205       if (src + 1 != src_ln && (src + 1)->a != 0) {
00206         *dst = *(src + 1);
00207       } else {
00208         *dst = *src;
00209       }
00210       dst++;
00211       src += 2;
00212     }
00213     src = src_ln + sprite[zoom - 1].width;
00214   }
00215 }
00216 
00217 static bool PadSingleSprite(SpriteLoader::Sprite *sprite, ZoomLevel zoom, uint pad_left, uint pad_top, uint pad_right, uint pad_bottom)
00218 {
00219   uint width  = sprite->width + pad_left + pad_right;
00220   uint height = sprite->height + pad_top + pad_bottom;
00221 
00222   if (width > UINT16_MAX || height > UINT16_MAX) return false;
00223 
00224   /* Copy source data and reallocate sprite memory. */
00225   SpriteLoader::CommonPixel *src_data = MallocT<SpriteLoader::CommonPixel>(sprite->width * sprite->height);
00226   MemCpyT(src_data, sprite->data, sprite->width * sprite->height);
00227   sprite->AllocateData(zoom, width * height);
00228 
00229   /* Copy with padding to destination. */
00230   SpriteLoader::CommonPixel *src = src_data;
00231   SpriteLoader::CommonPixel *data = sprite->data;
00232   for (uint y = 0; y < height; y++) {
00233     if (y < pad_top || pad_bottom + y >= height) {
00234       /* Top/bottom padding. */
00235       MemSetT(data, 0, width);
00236       data += width;
00237     } else {
00238       if (pad_left > 0) {
00239         /* Pad left. */
00240         MemSetT(data, 0, pad_left);
00241         data += pad_left;
00242       }
00243 
00244       /* Copy pixels. */
00245       MemCpyT(data, src, sprite->width);
00246       src += sprite->width;
00247       data += sprite->width;
00248 
00249       if (pad_right > 0) {
00250         /* Pad right. */
00251         MemSetT(data, 0, pad_right);
00252         data += pad_right;
00253       }
00254     }
00255   }
00256   free(src_data);
00257 
00258   /* Update sprite size. */
00259   sprite->width   = width;
00260   sprite->height  = height;
00261   sprite->x_offs -= pad_left;
00262   sprite->y_offs -= pad_top;
00263 
00264   return true;
00265 }
00266 
00267 static bool PadSprites(SpriteLoader::Sprite *sprite, uint8 sprite_avail)
00268 {
00269   /* Get minimum top left corner coordinates. */
00270   int min_xoffs = INT32_MAX;
00271   int min_yoffs = INT32_MAX;
00272   for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
00273     if (HasBit(sprite_avail, zoom)) {
00274       min_xoffs = min(min_xoffs, ScaleByZoom(sprite[zoom].x_offs, zoom));
00275       min_yoffs = min(min_yoffs, ScaleByZoom(sprite[zoom].y_offs, zoom));
00276     }
00277   }
00278 
00279   /* Get maximum dimensions taking necessary padding at the top left into account. */
00280   int max_width  = INT32_MIN;
00281   int max_height = INT32_MIN;
00282   for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
00283     if (HasBit(sprite_avail, zoom)) {
00284       max_width  = max(max_width, ScaleByZoom(sprite[zoom].width + sprite[zoom].x_offs - UnScaleByZoom(min_xoffs, zoom), zoom));
00285       max_height = max(max_height, ScaleByZoom(sprite[zoom].height + sprite[zoom].y_offs - UnScaleByZoom(min_yoffs, zoom), zoom));
00286     }
00287   }
00288 
00289   /* Pad sprites where needed. */
00290   for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
00291     if (HasBit(sprite_avail, zoom)) {
00292       /* Scaling the sprite dimensions in the blitter is done with rounding up,
00293        * so a negative padding here is not an error. */
00294       int pad_left   = max(0, sprite[zoom].x_offs - UnScaleByZoom(min_xoffs, zoom));
00295       int pad_top    = max(0, sprite[zoom].y_offs - UnScaleByZoom(min_yoffs, zoom));
00296       int pad_right  = max(0, UnScaleByZoom(max_width, zoom) - sprite[zoom].width - pad_left);
00297       int pad_bottom = max(0, UnScaleByZoom(max_height, zoom) - sprite[zoom].height - pad_top);
00298 
00299       if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0) {
00300         if (!PadSingleSprite(&sprite[zoom], zoom, pad_left, pad_top, pad_right, pad_bottom)) return false;
00301       }
00302     }
00303   }
00304 
00305   return true;
00306 }
00307 
00308 static bool ResizeSprites(SpriteLoader::Sprite *sprite, uint8 sprite_avail, uint32 file_slot, uint32 file_pos)
00309 {
00310   /* Create a fully zoomed image if it does not exist */
00311   ZoomLevel first_avail = static_cast<ZoomLevel>(FIND_FIRST_BIT(sprite_avail));
00312   if (first_avail != ZOOM_LVL_NORMAL) {
00313     if (!ResizeSpriteIn(sprite, first_avail, ZOOM_LVL_NORMAL)) return false;
00314     SetBit(sprite_avail, ZOOM_LVL_NORMAL);
00315   }
00316 
00317   /* Pad sprites to make sizes match. */
00318   if (!PadSprites(sprite, sprite_avail)) return false;
00319 
00320   /* Create other missing zoom levels */
00321   for (ZoomLevel zoom = ZOOM_LVL_OUT_2X; zoom != ZOOM_LVL_END; zoom++) {
00322     if (HasBit(sprite_avail, zoom)) {
00323       /* Check that size and offsets match the fully zoomed image. */
00324       assert(sprite[zoom].width  == UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].width,  zoom));
00325       assert(sprite[zoom].height == UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].height, zoom));
00326       assert(sprite[zoom].x_offs == UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].x_offs, zoom));
00327       assert(sprite[zoom].y_offs == UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].y_offs, zoom));
00328     }
00329 
00330     /* Zoom level is not available, or unusable, so create it */
00331     if (!HasBit(sprite_avail, zoom)) ResizeSpriteOut(sprite, zoom);
00332   }
00333 
00334   return  true;
00335 }
00336 
00343 static void *ReadRecolourSprite(uint16 file_slot, uint num)
00344 {
00345   /* "Normal" recolour sprites are ALWAYS 257 bytes. Then there is a small
00346    * number of recolour sprites that are 17 bytes that only exist in DOS
00347    * GRFs which are the same as 257 byte recolour sprites, but with the last
00348    * 240 bytes zeroed.  */
00349   static const uint RECOLOUR_SPRITE_SIZE = 257;
00350   byte *dest = (byte *)AllocSprite(max(RECOLOUR_SPRITE_SIZE, num));
00351 
00352   if (_palette_remap_grf[file_slot]) {
00353     byte *dest_tmp = AllocaM(byte, max(RECOLOUR_SPRITE_SIZE, num));
00354 
00355     /* Only a few recolour sprites are less than 257 bytes */
00356     if (num < RECOLOUR_SPRITE_SIZE) memset(dest_tmp, 0, RECOLOUR_SPRITE_SIZE);
00357     FioReadBlock(dest_tmp, num);
00358 
00359     /* The data of index 0 is never used; "literal 00" according to the (New)GRF specs. */
00360     for (uint i = 1; i < RECOLOUR_SPRITE_SIZE; i++) {
00361       dest[i] = _palmap_w2d[dest_tmp[_palmap_d2w[i - 1] + 1]];
00362     }
00363   } else {
00364     FioReadBlock(dest, num);
00365   }
00366 
00367   return dest;
00368 }
00369 
00378 static void *ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_type, AllocatorProc *allocator)
00379 {
00380   uint8 file_slot = sc->file_slot;
00381   size_t file_pos = sc->file_pos;
00382 
00383   assert(sprite_type != ST_RECOLOUR);
00384   assert(IsMapgenSpriteID(id) == (sprite_type == ST_MAPGEN));
00385   assert(sc->type == sprite_type);
00386 
00387   DEBUG(sprite, 9, "Load sprite %d", id);
00388 
00389   SpriteLoader::Sprite sprite[ZOOM_LVL_COUNT];
00390   uint8 sprite_avail = 0;
00391   sprite[ZOOM_LVL_NORMAL].type = sprite_type;
00392 
00393   SpriteLoaderGrf sprite_loader(sc->container_ver);
00394   if (sprite_type != ST_MAPGEN && BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 32) {
00395     /* Try for 32bpp sprites first. */
00396     sprite_avail = sprite_loader.LoadSprite(sprite, file_slot, file_pos, sprite_type, true);
00397   }
00398   if (sprite_avail == 0) {
00399     sprite_avail = sprite_loader.LoadSprite(sprite, file_slot, file_pos, sprite_type, false);
00400   }
00401 
00402   if (sprite_avail == 0) {
00403     if (sprite_type == ST_MAPGEN) return NULL;
00404     if (id == SPR_IMG_QUERY) usererror("Okay... something went horribly wrong. I couldn't load the fallback sprite. What should I do?");
00405     return (void*)GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
00406   }
00407 
00408   if (sprite_type == ST_MAPGEN) {
00409     /* Ugly hack to work around the problem that the old landscape
00410      *  generator assumes that those sprites are stored uncompressed in
00411      *  the memory, and they are only read directly by the code, never
00412      *  send to the blitter. So do not send it to the blitter (which will
00413      *  result in a data array in the format the blitter likes most), but
00414      *  extract the data directly and store that as sprite.
00415      * Ugly: yes. Other solution: no. Blame the original author or
00416      *  something ;) The image should really have been a data-stream
00417      *  (so type = 0xFF basically). */
00418     uint num = sprite[ZOOM_LVL_NORMAL].width * sprite[ZOOM_LVL_NORMAL].height;
00419 
00420     Sprite *s = (Sprite *)allocator(sizeof(*s) + num);
00421     s->width  = sprite[ZOOM_LVL_NORMAL].width;
00422     s->height = sprite[ZOOM_LVL_NORMAL].height;
00423     s->x_offs = sprite[ZOOM_LVL_NORMAL].x_offs;
00424     s->y_offs = sprite[ZOOM_LVL_NORMAL].y_offs;
00425 
00426     SpriteLoader::CommonPixel *src = sprite[ZOOM_LVL_NORMAL].data;
00427     byte *dest = s->data;
00428     while (num-- > 0) {
00429       *dest++ = src->m;
00430       src++;
00431     }
00432 
00433     return s;
00434   }
00435 
00436   if (sprite_type == ST_NORMAL) {
00437     if (!ResizeSprites(sprite, sprite_avail, file_slot, sc->id)) {
00438       if (id == SPR_IMG_QUERY) usererror("Okay... something went horribly wrong. I couldn't resize the fallback sprite. What should I do?");
00439       return (void*)GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
00440     }
00441   }
00442   return BlitterFactoryBase::GetCurrentBlitter()->Encode(sprite, allocator);
00443 }
00444 
00445 
00447 static std::map<uint32, size_t> _grf_sprite_offsets;
00448 
00454 size_t GetGRFSpriteOffset(uint32 id)
00455 {
00456   return _grf_sprite_offsets.find(id) != _grf_sprite_offsets.end() ? _grf_sprite_offsets[id] : SIZE_MAX;
00457 }
00458 
00463 void ReadGRFSpriteOffsets(byte container_version)
00464 {
00465   _grf_sprite_offsets.clear();
00466 
00467   if (container_version >= 2) {
00468     /* Seek to sprite section of the GRF. */
00469     size_t data_offset = FioReadDword();
00470     size_t old_pos = FioGetPos();
00471     FioSeekTo(data_offset, SEEK_CUR);
00472 
00473     /* Loop over all sprite section entries and store the file
00474      * offset for each newly encountered ID. */
00475     uint32 id, prev_id = 0;
00476     while ((id = FioReadDword()) != 0) {
00477       if (id != prev_id) _grf_sprite_offsets[id] = FioGetPos() - 4;
00478       prev_id = id;
00479       FioSkipBytes(FioReadDword());
00480     }
00481 
00482     /* Continue processing the data section. */
00483     FioSeekTo(old_pos, SEEK_SET);
00484   }
00485 }
00486 
00487 
00496 bool LoadNextSprite(int load_index, byte file_slot, uint file_sprite_id, byte container_version)
00497 {
00498   size_t file_pos = FioGetPos();
00499 
00500   /* Read sprite header. */
00501   uint32 num = container_version >= 2 ? FioReadDword() : FioReadWord();
00502   if (num == 0) return false;
00503   byte grf_type = FioReadByte();
00504 
00505   SpriteType type;
00506   void *data = NULL;
00507   if (grf_type == 0xFF) {
00508     /* Some NewGRF files have "empty" pseudo-sprites which are 1
00509      * byte long. Catch these so the sprites won't be displayed. */
00510     if (num == 1) {
00511       FioReadByte();
00512       return false;
00513     }
00514     type = ST_RECOLOUR;
00515     data = ReadRecolourSprite(file_slot, num);
00516   } else if (container_version >= 2 && grf_type == 0xFD) {
00517     if (num != 4) {
00518       /* Invalid sprite section include, ignore. */
00519       FioSkipBytes(num);
00520       return false;
00521     }
00522     /* It is not an error if no sprite with the provided ID is found in the sprite section. */
00523     file_pos = GetGRFSpriteOffset(FioReadDword());
00524     type = ST_NORMAL;
00525   } else {
00526     FioSkipBytes(7);
00527     type = SkipSpriteData(grf_type, num - 8) ? ST_NORMAL : ST_INVALID;
00528     /* Inline sprites are not supported for container version >= 2. */
00529     if (container_version >= 2) return false;
00530   }
00531 
00532   if (type == ST_INVALID) return false;
00533 
00534   if (load_index >= MAX_SPRITES) {
00535     usererror("Tried to load too many sprites (#%d; max %d)", load_index, MAX_SPRITES);
00536   }
00537 
00538   bool is_mapgen = IsMapgenSpriteID(load_index);
00539 
00540   if (is_mapgen) {
00541     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?");
00542     type = ST_MAPGEN;
00543   }
00544 
00545   SpriteCache *sc = AllocateSpriteCache(load_index);
00546   sc->file_slot = file_slot;
00547   sc->file_pos = file_pos;
00548   sc->ptr = data;
00549   sc->lru = 0;
00550   sc->id = file_sprite_id;
00551   sc->type = type;
00552   sc->warned = false;
00553   sc->container_ver = container_version;
00554 
00555   return true;
00556 }
00557 
00558 
00559 void DupSprite(SpriteID old_spr, SpriteID new_spr)
00560 {
00561   SpriteCache *scnew = AllocateSpriteCache(new_spr); // may reallocate: so put it first
00562   SpriteCache *scold = GetSpriteCache(old_spr);
00563 
00564   scnew->file_slot = scold->file_slot;
00565   scnew->file_pos = scold->file_pos;
00566   scnew->ptr = NULL;
00567   scnew->id = scold->id;
00568   scnew->type = scold->type;
00569   scnew->warned = false;
00570   scnew->container_ver = scold->container_ver;
00571 }
00572 
00579 static const size_t S_FREE_MASK = sizeof(size_t) - 1;
00580 
00581 /* to make sure nobody adds things to MemBlock without checking S_FREE_MASK first */
00582 assert_compile(sizeof(MemBlock) == sizeof(size_t));
00583 /* make sure it's a power of two */
00584 assert_compile((sizeof(size_t) & (sizeof(size_t) - 1)) == 0);
00585 
00586 static inline MemBlock *NextBlock(MemBlock *block)
00587 {
00588   return (MemBlock*)((byte*)block + (block->size & ~S_FREE_MASK));
00589 }
00590 
00591 static size_t GetSpriteCacheUsage()
00592 {
00593   size_t tot_size = 0;
00594   MemBlock *s;
00595 
00596   for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
00597     if (!(s->size & S_FREE_MASK)) tot_size += s->size;
00598   }
00599 
00600   return tot_size;
00601 }
00602 
00603 
00604 void IncreaseSpriteLRU()
00605 {
00606   /* Increase all LRU values */
00607   if (_sprite_lru_counter > 16384) {
00608     SpriteID i;
00609 
00610     DEBUG(sprite, 3, "Fixing lru %u, inuse=" PRINTF_SIZE, _sprite_lru_counter, GetSpriteCacheUsage());
00611 
00612     for (i = 0; i != _spritecache_items; i++) {
00613       SpriteCache *sc = GetSpriteCache(i);
00614       if (sc->ptr != NULL) {
00615         if (sc->lru >= 0) {
00616           sc->lru = -1;
00617         } else if (sc->lru != -32768) {
00618           sc->lru--;
00619         }
00620       }
00621     }
00622     _sprite_lru_counter = 0;
00623   }
00624 
00625   /* Compact sprite cache every now and then. */
00626   if (++_compact_cache_counter >= 740) {
00627     CompactSpriteCache();
00628     _compact_cache_counter = 0;
00629   }
00630 }
00631 
00636 static void CompactSpriteCache()
00637 {
00638   MemBlock *s;
00639 
00640   DEBUG(sprite, 3, "Compacting sprite cache, inuse=" PRINTF_SIZE, GetSpriteCacheUsage());
00641 
00642   for (s = _spritecache_ptr; s->size != 0;) {
00643     if (s->size & S_FREE_MASK) {
00644       MemBlock *next = NextBlock(s);
00645       MemBlock temp;
00646       SpriteID i;
00647 
00648       /* Since free blocks are automatically coalesced, this should hold true. */
00649       assert(!(next->size & S_FREE_MASK));
00650 
00651       /* If the next block is the sentinel block, we can safely return */
00652       if (next->size == 0) break;
00653 
00654       /* Locate the sprite belonging to the next pointer. */
00655       for (i = 0; GetSpriteCache(i)->ptr != next->data; i++) {
00656         assert(i != _spritecache_items);
00657       }
00658 
00659       GetSpriteCache(i)->ptr = s->data; // Adjust sprite array entry
00660       /* Swap this and the next block */
00661       temp = *s;
00662       memmove(s, next, next->size);
00663       s = NextBlock(s);
00664       *s = temp;
00665 
00666       /* Coalesce free blocks */
00667       while (NextBlock(s)->size & S_FREE_MASK) {
00668         s->size += NextBlock(s)->size & ~S_FREE_MASK;
00669       }
00670     } else {
00671       s = NextBlock(s);
00672     }
00673   }
00674 }
00675 
00680 static void DeleteEntryFromSpriteCache(uint item)
00681 {
00682   /* Mark the block as free (the block must be in use) */
00683   MemBlock *s = (MemBlock*)GetSpriteCache(item)->ptr - 1;
00684   assert(!(s->size & S_FREE_MASK));
00685   s->size |= S_FREE_MASK;
00686   GetSpriteCache(item)->ptr = NULL;
00687 
00688   /* And coalesce adjacent free blocks */
00689   for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
00690     if (s->size & S_FREE_MASK) {
00691       while (NextBlock(s)->size & S_FREE_MASK) {
00692         s->size += NextBlock(s)->size & ~S_FREE_MASK;
00693       }
00694     }
00695   }
00696 }
00697 
00698 static void DeleteEntryFromSpriteCache()
00699 {
00700   uint best = UINT_MAX;
00701   int cur_lru;
00702 
00703   DEBUG(sprite, 3, "DeleteEntryFromSpriteCache, inuse=" PRINTF_SIZE, GetSpriteCacheUsage());
00704 
00705   cur_lru = 0xffff;
00706   for (SpriteID i = 0; i != _spritecache_items; i++) {
00707     SpriteCache *sc = GetSpriteCache(i);
00708     if (sc->type != ST_RECOLOUR && sc->ptr != NULL && sc->lru < cur_lru) {
00709       cur_lru = sc->lru;
00710       best = i;
00711     }
00712   }
00713 
00714   /* Display an error message and die, in case we found no sprite at all.
00715    * This shouldn't really happen, unless all sprites are locked. */
00716   if (best == UINT_MAX) error("Out of sprite memory");
00717 
00718   DeleteEntryFromSpriteCache(best);
00719 }
00720 
00721 static void *AllocSprite(size_t mem_req)
00722 {
00723   mem_req += sizeof(MemBlock);
00724 
00725   /* Align this to correct boundary. This also makes sure at least one
00726    * bit is not used, so we can use it for other things. */
00727   mem_req = Align(mem_req, S_FREE_MASK + 1);
00728 
00729   for (;;) {
00730     MemBlock *s;
00731 
00732     for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
00733       if (s->size & S_FREE_MASK) {
00734         size_t cur_size = s->size & ~S_FREE_MASK;
00735 
00736         /* Is the block exactly the size we need or
00737          * big enough for an additional free block? */
00738         if (cur_size == mem_req ||
00739             cur_size >= mem_req + sizeof(MemBlock)) {
00740           /* Set size and in use */
00741           s->size = mem_req;
00742 
00743           /* Do we need to inject a free block too? */
00744           if (cur_size != mem_req) {
00745             NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK;
00746           }
00747 
00748           return s->data;
00749         }
00750       }
00751     }
00752 
00753     /* Reached sentinel, but no block found yet. Delete some old entry. */
00754     DeleteEntryFromSpriteCache();
00755   }
00756 }
00757 
00767 static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc, AllocatorProc *allocator)
00768 {
00769   static const char * const sprite_types[] = {
00770     "normal",        // ST_NORMAL
00771     "map generator", // ST_MAPGEN
00772     "character",     // ST_FONT
00773     "recolour",      // ST_RECOLOUR
00774   };
00775 
00776   SpriteType available = sc->type;
00777   if (requested == ST_FONT && available == ST_NORMAL) {
00778     if (sc->ptr == NULL) sc->type = ST_FONT;
00779     return GetRawSprite(sprite, sc->type, allocator);
00780   }
00781 
00782   byte warning_level = sc->warned ? 6 : 0;
00783   sc->warned = true;
00784   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]);
00785 
00786   switch (requested) {
00787     case ST_NORMAL:
00788       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?");
00789       /* FALL THROUGH */
00790     case ST_FONT:
00791       return GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
00792     case ST_RECOLOUR:
00793       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?");
00794       return GetRawSprite(PALETTE_TO_DARK_BLUE, ST_RECOLOUR, allocator);
00795     case ST_MAPGEN:
00796       /* this shouldn't happen, overriding of ST_MAPGEN sprites is checked in LoadNextSprite()
00797        * (the only case the check fails is when these sprites weren't even loaded...) */
00798     default:
00799       NOT_REACHED();
00800   }
00801 }
00802 
00811 void *GetRawSprite(SpriteID sprite, SpriteType type, AllocatorProc *allocator)
00812 {
00813   assert(type != ST_MAPGEN || IsMapgenSpriteID(sprite));
00814   assert(type < ST_INVALID);
00815 
00816   if (!SpriteExists(sprite)) {
00817     DEBUG(sprite, 1, "Tried to load non-existing sprite #%d. Probable cause: Wrong/missing NewGRFs", sprite);
00818 
00819     /* SPR_IMG_QUERY is a BIG FAT RED ? */
00820     sprite = SPR_IMG_QUERY;
00821   }
00822 
00823   SpriteCache *sc = GetSpriteCache(sprite);
00824 
00825   if (sc->type != type) return HandleInvalidSpriteRequest(sprite, type, sc, allocator);
00826 
00827   if (allocator == NULL) {
00828     /* Load sprite into/from spritecache */
00829 
00830     /* Update LRU */
00831     sc->lru = ++_sprite_lru_counter;
00832 
00833     /* Load the sprite, if it is not loaded, yet */
00834     if (sc->ptr == NULL) sc->ptr = ReadSprite(sc, sprite, type, AllocSprite);
00835 
00836     return sc->ptr;
00837   } else {
00838     /* Do not use the spritecache, but a different allocator. */
00839     return ReadSprite(sc, sprite, type, allocator);
00840   }
00841 }
00842 
00843 
00844 static void GfxInitSpriteCache()
00845 {
00846   /* initialize sprite cache heap */
00847   int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth();
00848   uint target_size = (bpp > 0 ? _sprite_cache_size * bpp / 8 : 1) * 1024 * 1024;
00849 
00850   if (_spritecache_ptr == NULL || _allocated_sprite_cache_size != target_size) {
00851     free(_spritecache_ptr);
00852     _allocated_sprite_cache_size = target_size;
00853     _spritecache_ptr = (MemBlock*)MallocT<byte>(_allocated_sprite_cache_size);
00854   }
00855 
00856   /* A big free block */
00857   _spritecache_ptr->size = (_allocated_sprite_cache_size - sizeof(MemBlock)) | S_FREE_MASK;
00858   /* Sentinel block (identified by size == 0) */
00859   NextBlock(_spritecache_ptr)->size = 0;
00860 }
00861 
00862 void GfxInitSpriteMem()
00863 {
00864   GfxInitSpriteCache();
00865 
00866   /* Reset the spritecache 'pool' */
00867   free(_spritecache);
00868   _spritecache_items = 0;
00869   _spritecache = NULL;
00870 
00871   _compact_cache_counter = 0;
00872 }
00873 
00878 void GfxClearSpriteCache()
00879 {
00880   /* Clear sprite ptr for all cached items */
00881   for (uint i = 0; i != _spritecache_items; i++) {
00882     SpriteCache *sc = GetSpriteCache(i);
00883     if (sc->type != ST_RECOLOUR && sc->ptr != NULL) DeleteEntryFromSpriteCache(i);
00884   }
00885 }
00886 
00887 /* static */ ReusableBuffer<SpriteLoader::CommonPixel> SpriteLoader::Sprite::buffer[ZOOM_LVL_COUNT];