# 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 "gfx_func.h"
    00014 #include "table/sprites.h"
    00015 #include "fileio_func.h"
    00016 #include "spriteloader/grf.hpp"
    00017 #include "gfx_func.h"
    00018 #ifdef WITH_PNG
    00019 #include "spriteloader/png.hpp"
    00020 #endif /* WITH_PNG */
    00021 #include "blitter/factory.hpp"
    00022 #include "blitter/32bpp_optimized.hpp"
    00023
    00024 #include "core/math_func.hpp"
    00025
    00026 #include "table/sprites.h"
    00027
    00028 /* Default of 4MB spritecache */
    00029 uint _sprite_cache_size = 64;
    00030
    00031 typedef SimpleTinyEnumT<SpriteType, byte> SpriteTypeByte;
    00032
    00033 struct SpriteCache {
    00034   void *ptr[ZOOM_LVL_END];
    00035   size_t file_pos;
    00036   uint32 id;
    00037   uint16 file_slot;
    00038   int16 lru;
    00039   SpriteTypeByte type;
    00040   bool warned;
    00041 };
    00042
    00043
    00044 static uint _spritecache_items = 0;
    00045 static SpriteCache *_spritecache = NULL;
    00046
    00047
    00048 static inline SpriteCache *GetSpriteCache(uint index)
    00049 {
    00050   return &_spritecache[index];
    00051 }
    00052
    00053 static inline bool IsMapgenSpriteID(SpriteID sprite)
    00054 {
    00055   return IsInsideMM(sprite, 4845, 4882);
    00056 }
    00057
    00058 static SpriteCache *AllocateSpriteCache(uint index)
    00059 {
    00060   if (index >= _spritecache_items) {
    00061     /* Add another 1024 items to the 'pool' */
    00062     uint items = Align(index + 1, 1024);
    00063
    00064     DEBUG(sprite, 4, "Increasing sprite cache to %u items (" PRINTF_SIZE " bytes)", items, items * sizeof(*_spritecache));
    00065
    00066     _spritecache = ReallocT(_spritecache, items);
    00067
    00068     if (_spritecache == NULL) {
    00069       error("Unable to allocate sprite cache of %u items (" PRINTF_SIZE " bytes)", items, items * sizeof(*_spritecache));
    00070     }
    00071
    00072     /* Reset the new items and update the count */
    00073     memset(_spritecache + _spritecache_items, 0, (items - _spritecache_items) * sizeof(*_spritecache));
    00074     _spritecache_items = items;
    00075   }
    00076
    00077   return GetSpriteCache(index);
    00078 }
    00079
    00080
    00081 struct MemBlock {
    00082   size_t size;
    00083   byte data[];
    00084 };
    00085
    00086 static uint _sprite_lru_counter;
    00087 static MemBlock *_spritecache_ptr;
    00088 static int _compact_cache_counter;
    00089
    00090 static void CompactSpriteCache();
    00091
    00098 bool SkipSpriteData(byte type, uint16 num)
    00099 {
    00100   if (type & 2) {
    00101     FioSkipBytes(num);
    00102   } else {
    00103     while (num > 0) {
    00104       int8 i = FioReadByte();
    00105       if (i >= 0) {
    00106         int size = (i == 0) ? 0x80 : i;
    00107         if (size > num) return false;
    00108         num -= size;
    00109         FioSkipBytes(size);
    00110       } else {
    00111         i = -(i >> 3);
    00112         num -= i;
    00113         FioReadByte();
    00114       }
    00115     }
    00116   }
    00117   return true;
    00118 }
    00119
    00124 static SpriteType ReadSpriteHeaderSkipData()
    00125 {
    00126   uint16 num = FioReadWord();
    00127
    00128   if (num == 0) return ST_INVALID;
    00129
    00130   byte type = FioReadByte();
    00131   if (type == 0xFF) {
    00132     FioSkipBytes(num);
    00133     /* Some NewGRF files have "empty" pseudo-sprites which are 1
    00134      * byte long. Catch these so the sprites won't be displayed. */
    00135     return (num == 1) ? ST_INVALID : ST_RECOLOUR;
    00136   }
    00137
    00138   FioSkipBytes(7);
    00139   return SkipSpriteData(type, num - 8) ? ST_NORMAL : ST_INVALID;
    00140 }
    00141
    00142 /* Check if the given Sprite ID exists */
    00143 bool SpriteExists(SpriteID id)
    00144 {
    00145   /* Special case for Sprite ID zero -- its position is also 0... */
    00146   if (id == 0) return true;
    00147   if (id >= _spritecache_items) return false;
    00148   return !(GetSpriteCache(id)->file_pos == 0 && GetSpriteCache(id)->file_slot == 0);
    00149 }
    00150
    00156 SpriteType GetSpriteType(SpriteID sprite)
    00157 {
    00158   if (!SpriteExists(sprite)) return ST_INVALID;
    00159   return GetSpriteCache(sprite)->type;
    00160 }
    00161
    00167 uint GetOriginFileSlot(SpriteID sprite)
    00168 {
    00169   if (!SpriteExists(sprite)) return 0;
    00170   return GetSpriteCache(sprite)->file_slot;
    00171 }
    00172
    00181 uint GetMaxSpriteID()
    00182 {
    00183   return _spritecache_items;
    00184 }
    00185
    00194 static void *ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_type, AllocatorProc *allocator)
    00195 {
    00196   uint8 file_slot = sc->file_slot;
    00197   size_t file_pos = sc->file_pos;
    00198
    00199   ZoomLevel zoom = ZOOM_LVL_NORMAL;
    00200
    00201   if (_cur_dpi) {
    00202     zoom = ZoomLevel(_cur_dpi->zoom);
    00203   }
    00204
    00205
    00206   assert(IsMapgenSpriteID(id) == (sprite_type == ST_MAPGEN));
    00207   assert(sc->type == sprite_type);
    00208
    00209   DEBUG(sprite, 9, "Load sprite %d", id);
    00210
    00211   if (sprite_type == ST_NORMAL && BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 32) {
    00212 #ifdef WITH_PNG
    00213     /* Try loading 32bpp graphics in case we are 32bpp output */
    00214     SpriteLoaderPNG sprite_loader;
    00215     SpriteLoader::Sprite sprite, dst_sprite;
    00216
    00217     ZoomLevel zoom_idx = (zoom > ZOOM_LVL_NORMAL ? ZOOM_LVL_NORMAL : zoom);
    00218     bool found = false;
    00219     do {
    00220       found = sprite_loader.LoadSprite(&sprite, sc->file_slot,
    00221                              sc->id, sprite_type, zoom_idx);
    00222       zoom_idx--;
    00223     } while ( !found && (zoom_idx >= ZOOM_LVL_MIN ) );
    00224     if (found) {
    00225       zoom_idx++;
    00226       if (zoom > zoom_idx) {
    00227         Blitter_32bppOptimized *blitter = (Blitter_32bppOptimized *)BlitterFactoryBase::GetCurrentBlitter();
    00228         do {
    00229           blitter->RescaleSpriteHalfSize(&sprite, &dst_sprite, true);
    00230           sprite = dst_sprite;
    00231           zoom_idx++;
    00232         } while (zoom_idx < zoom);
    00233       }
    00234       return BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, allocator);
    00235
    00236     }
    00237     /* If the PNG couldn't be loaded, fall back to 8bpp grfs */
    00238 #else
    00239     static bool show_once = true;
    00240     if (show_once) {
    00241       DEBUG(misc, 0, "You are running a 32bpp blitter, but this build is without libpng support; falling back to 8bpp graphics");
    00242       show_once = false;
    00243     }
    00244 #endif /* WITH_PNG */
    00245   }
    00246
    00247   FioSeekToFile(file_slot, file_pos);
    00248
    00249   /* Read the size and type */
    00250   int num  = FioReadWord();
    00251   byte type = FioReadByte();
    00252
    00253   /* Type 0xFF indicates either a colourmap or some other non-sprite info */
    00254   assert((type == 0xFF) == (sprite_type == ST_RECOLOUR));
    00255   if (type == 0xFF) {
    00256     /* "Normal" recolour sprites are ALWAYS 257 bytes. Then there is a small
    00257      * number of recolour sprites that are 17 bytes that only exist in DOS
    00258      * GRFs which are the same as 257 byte recolour sprites, but with the last
    00259      * 240 bytes zeroed.  */
    00260     static const int RECOLOUR_SPRITE_SIZE = 257;
    00261     byte *dest = (byte *)allocator(max(RECOLOUR_SPRITE_SIZE, num) + sizeof(byte *));
    00262
    00263     if (_palette_remap_grf[sc->file_slot]) {
    00264       byte *dest_tmp = AllocaM(byte, max(RECOLOUR_SPRITE_SIZE, num));
    00265
    00266       /* Only a few recolour sprites are less than 257 bytes */
    00267       if (num < RECOLOUR_SPRITE_SIZE) memset(dest_tmp, 0, RECOLOUR_SPRITE_SIZE);
    00268       FioReadBlock(dest_tmp, num);
    00269
    00270       /* The data of index 0 is never used; "literal 00" according to the (New)GRF specs. */
    00271       for (int i = 1; i < RECOLOUR_SPRITE_SIZE; i++) {
    00272         dest[i] = _palette_remap[dest_tmp[_palette_reverse_remap[i - 1] + 1]];
    00273       }
    00274     } else {
    00275       FioReadBlock(dest, num);
    00276     }
    00277
    00278     if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 32){
    00279       Blitter_32bppOptimized *blitter = (Blitter_32bppOptimized *)BlitterFactoryBase::GetCurrentBlitter();
    00280       byte *tmp_ptr = blitter->FillRGBPalette(id, dest);
    00281       memcpy(&dest[257], &tmp_ptr, sizeof (tmp_ptr));
    00282     }
    00283     return dest;
    00284   }
    00285
    00286   /* Ugly hack to work around the problem that the old landscape
    00287    *  generator assumes that those sprites are stored uncompressed in
    00288    *  the memory, and they are only read directly by the code, never
    00289    *  send to the blitter. So do not send it to the blitter (which will
    00290    *  result in a data array in the format the blitter likes most), but
    00291    *  read the data directly from disk and store that as sprite.
    00292    * Ugly: yes. Other solution: no. Blame the original author or
    00293    *  something ;) The image should really have been a data-stream
    00294    *  (so type = 0xFF basicly). */
    00295   if (sprite_type == ST_MAPGEN) {
    00296     uint height = FioReadByte();
    00297     uint width  = FioReadWord();
    00298     Sprite *sprite;
    00299     byte *dest;
    00300
    00301     num = width * height;
    00302     sprite = (Sprite *)allocator(sizeof(*sprite) + num);
    00303     sprite->height = height;
    00304     sprite->width  = width;
    00305     sprite->x_offs = FioReadWord();
    00306     sprite->y_offs = FioReadWord();
    00307
    00308     dest = sprite->data;
    00309     while (num > 0) {
    00310       int8 i = FioReadByte();
    00311       if (i >= 0) {
    00312         num -= i;
    00313         for (; i > 0; --i) *dest++ = FioReadByte();
    00314       } else {
    00315         const byte *rel = dest - (((i & 7) << 8) | FioReadByte());
    00316         i = -(i >> 3);
    00317         num -= i;
    00318         for (; i > 0; --i) *dest++ = *rel++;
    00319       }
    00320     }
    00321
    00322     return sprite;
    00323   }
    00324
    00325   assert(sprite_type == ST_NORMAL || sprite_type == ST_FONT);
    00326
    00327   SpriteLoaderGrf sprite_loader;
    00328   SpriteLoader::Sprite sprite, dst_sprite;
    00329
    00330   if (!sprite_loader.LoadSprite(&sprite, file_slot, file_pos, sprite_type, ZOOM_LVL_NORMAL)) {
    00331     return NULL;
    00332   }
    00333   Blitter_32bppOptimized *blitter = (Blitter_32bppOptimized *)BlitterFactoryBase::GetCurrentBlitter();
    00334   if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 32) {
    00335     blitter->FillRGBFromPalette(&sprite);
    00336   }
    00337   if (zoom < ZOOM_LVL_NORMAL) {
    00338     ZoomLevel zoom_idx = zoom;
    00339     do {
    00340       blitter->RescaleSpriteDoubleSize(&sprite, &dst_sprite);
    00341       sprite = dst_sprite;
    00342       zoom_idx++;
    00343     } while (zoom_idx < ZOOM_LVL_NORMAL);
    00344   }
    00345   if (zoom > ZOOM_LVL_NORMAL) {
    00346     ZoomLevel zoom_idx = ZOOM_LVL_NORMAL;
    00347     do {
    00348       blitter->RescaleSpriteHalfSize(&sprite, &dst_sprite, true);
    00349       sprite = dst_sprite;
    00350       zoom_idx++;
    00351     } while (zoom_idx < zoom);
    00352   }
    00353
    00354   return BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, allocator);
    00355 }
    00356
    00357
    00358 bool LoadNextSprite(int load_index, byte file_slot, uint file_sprite_id)
    00359 {
    00360   size_t file_pos = FioGetPos();
    00361
    00362   SpriteType type = ReadSpriteHeaderSkipData();
    00363
    00364   if (type == ST_INVALID) return false;
    00365
    00366   if (load_index >= MAX_SPRITES) {
    00367     usererror("Tried to load too many sprites (#%d; max %d)", load_index, MAX_SPRITES);
    00368   }
    00369
    00370   bool is_mapgen = IsMapgenSpriteID(load_index);
    00371
    00372   if (is_mapgen) {
    00373     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?");
    00374     type = ST_MAPGEN;
    00375   }
    00376
    00377   SpriteCache *sc = AllocateSpriteCache(load_index);
    00378   sc->file_slot = file_slot;
    00379   sc->file_pos = file_pos;
    00380   sc->ptr[ZOOM_LVL_IN_4X] = NULL;
    00381   sc->ptr[ZOOM_LVL_IN_2X] = NULL;
    00382   sc->ptr[ZOOM_LVL_NORMAL] = NULL;
    00383   sc->ptr[ZOOM_LVL_OUT_2X] = NULL;
    00384   sc->ptr[ZOOM_LVL_OUT_4X] = NULL;
    00385   sc->ptr[ZOOM_LVL_OUT_8X] = NULL;
    00386   sc->lru = 0;
    00387   sc->id = file_sprite_id;
    00388   sc->type = type;
    00389   sc->warned = false;
    00390
    00391   return true;
    00392 }
    00393
    00394
    00395 void DupSprite(SpriteID old_spr, SpriteID new_spr)
    00396 {
    00397   SpriteCache *scnew = AllocateSpriteCache(new_spr); // may reallocate: so put it first
    00398   SpriteCache *scold = GetSpriteCache(old_spr);
    00399   ZoomLevel zoom;
    00400
    00401   scnew->file_slot = scold->file_slot;
    00402   scnew->file_pos = scold->file_pos;
    00403   zoom = ZoomLevel(_cur_dpi->zoom);
    00404   scnew->ptr[zoom] = NULL;
    00405   scnew->id = scold->id;
    00406   scnew->type = scold->type;
    00407   scnew->warned = false;
    00408 }
    00409
    00416 static const size_t S_FREE_MASK = sizeof(size_t) - 1;
    00417
    00418 /* to make sure nobody adds things to MemBlock without checking S_FREE_MASK first */
    00419 assert_compile(sizeof(MemBlock) == sizeof(size_t));
    00420 /* make sure it's a power of two */
    00421 assert_compile((sizeof(size_t) & (sizeof(size_t) - 1)) == 0);
    00422
    00423 static inline MemBlock *NextBlock(MemBlock *block)
    00424 {
    00425   return (MemBlock*)((byte*)block + (block->size & ~S_FREE_MASK));
    00426 }
    00427
    00428 static size_t GetSpriteCacheUsage()
    00429 {
    00430   size_t tot_size = 0;
    00431   MemBlock *s;
    00432
    00433   for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
    00434     if (!(s->size & S_FREE_MASK)) tot_size += s->size;
    00435   }
    00436
    00437   return tot_size;
    00438 }
    00439
    00440
    00441 void IncreaseSpriteLRU()
    00442 {
    00443   ZoomLevel zoom;
    00444   /* Increase all LRU values */
    00445   if (_sprite_lru_counter > 16384) {
    00446     SpriteID i;
    00447
    00448     DEBUG(sprite, 3, "Fixing lru %u, inuse=" PRINTF_SIZE, _sprite_lru_counter, GetSpriteCacheUsage());
    00449
    00450     for (i = 0; i != _spritecache_items; i++) {
    00451       SpriteCache *sc = GetSpriteCache(i);
    00452       zoom = ZoomLevel(_cur_dpi->zoom);
    00453       if (sc->ptr[zoom] != NULL) {
    00454         if (sc->lru >= 0) {
    00455           sc->lru = -1;
    00456         } else if (sc->lru != -32768) {
    00457           sc->lru--;
    00458         }
    00459       }
    00460     }
    00461     _sprite_lru_counter = 0;
    00462   }
    00463
    00464   /* Compact sprite cache every now and then. */
    00465   if (++_compact_cache_counter >= 740) {
    00466     CompactSpriteCache();
    00467     _compact_cache_counter = 0;
    00468   }
    00469 }
    00470
    00475 static void CompactSpriteCache()
    00476 {
    00477   MemBlock *s;
    00478   ZoomLevel zoom;
    00479
    00480   DEBUG(sprite, 3, "Compacting sprite cache, inuse=" PRINTF_SIZE, GetSpriteCacheUsage());
    00481
    00482   for (s = _spritecache_ptr; s->size != 0;) {
    00483     if (s->size & S_FREE_MASK) {
    00484       MemBlock *next = NextBlock(s);
    00485       MemBlock temp;
    00486       SpriteID i;
    00487
    00488       /* Since free blocks are automatically coalesced, this should hold true. */
    00489       assert(!(next->size & S_FREE_MASK));
    00490
    00491       /* If the next block is the sentinel block, we can safely return */
    00492       if (next->size == 0) break;
    00493
    00494       /* Locate the sprite belonging to the next pointer. */
    00495       bool found = false;
    00496       for (zoom = ZOOM_LVL_MIN;
    00497           (zoom < ZOOM_LVL_END) && (!found);
    00498           zoom = ZoomLevel(zoom + 1)) {
    00499         for (i = 0; i < _spritecache_items && !found; i++) {
    00500           found = (GetSpriteCache(i)->ptr[zoom] == next->data);
    00501         }
    00502       }
    00503       assert(found);
    00504       GetSpriteCache(i - 1)->ptr[zoom - 1] = s->data; // Adjust sprite array entry
    00505       /* Swap this and the next block */
    00506       temp = *s;
    00507       memmove(s, next, next->size);
    00508       s = NextBlock(s);
    00509       *s = temp;
    00510
    00511       /* Coalesce free blocks */
    00512       while (NextBlock(s)->size & S_FREE_MASK) {
    00513         s->size += NextBlock(s)->size & ~S_FREE_MASK;
    00514       }
    00515     } else {
    00516       s = NextBlock(s);
    00517     }
    00518   }
    00519 }
    00520
    00521 static void DeleteEntryFromSpriteCache()
    00522 {
    00523   SpriteID i;
    00524   uint best = UINT_MAX;
    00525   MemBlock *s;
    00526   int16 cur_lru;
    00527   ZoomLevel zoom;
    00528
    00529   DEBUG(sprite, 3, "DeleteEntryFromSpriteCache, inuse=" PRINTF_SIZE, GetSpriteCacheUsage());
    00530
    00531   cur_lru = -1;
    00532   for (i = 0; i != _spritecache_items; i++) {
    00533     SpriteCache *sc = GetSpriteCache(i);
    00534     if (sc->lru < cur_lru) {
    00535       for (zoom = ZOOM_LVL_MIN; zoom < ZOOM_LVL_END; zoom ++) {
    00536         if (sc->ptr[zoom]) {
    00537           cur_lru = sc->lru;
    00538           best = i;
    00539         }
    00540       }
    00541     }
    00542   }
    00543
    00544   /* Display an error message and die, in case we found no sprite at all.
    00545    * This shouldn't really happen, unless all sprites are locked. */
    00546   if (best == UINT_MAX) error("Out of sprite memory");
    00547
    00548   SpriteCache *sc = GetSpriteCache(best);
    00549   /* Mark the block as free (the block must be in use) */
    00550   for (zoom = ZOOM_LVL_MIN ; zoom < ZOOM_LVL_END ; zoom = ZoomLevel(zoom + 1)) {
    00551     if (sc->ptr[zoom]) {
    00552       s = (MemBlock*)sc->ptr[zoom] - 1;
    00553       assert(!(s->size & S_FREE_MASK));
    00554       s->size |= S_FREE_MASK;
    00555       sc->ptr[zoom] = NULL;
    00556
    00557       /* And coalesce adjacent free blocks */
    00558       for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
    00559         if (s->size & S_FREE_MASK) {
    00560           while (NextBlock(s)->size & S_FREE_MASK) {
    00561             s->size += NextBlock(s)->size & ~S_FREE_MASK;
    00562           }
    00563         }
    00564       }
    00565     }
    00566   }
    00567 }
    00568
    00569 static void *AllocSprite(size_t mem_req)
    00570 {
    00571   mem_req += sizeof(MemBlock);
    00572
    00573   /* Align this to correct boundary. This also makes sure at least one
    00574    * bit is not used, so we can use it for other things. */
    00575   mem_req = Align(mem_req, S_FREE_MASK + 1);
    00576   DEBUG(sprite, 3, "AllocSprite, memreq=" PRINTF_SIZE, mem_req);
    00577   for (;;) {
    00578     MemBlock *s;
    00579
    00580     for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
    00581       if (s->size & S_FREE_MASK) {
    00582         size_t cur_size = s->size & ~S_FREE_MASK;
    00583
    00584         /* Is the block exactly the size we need or
    00585          * big enough for an additional free block? */
    00586         if (cur_size == mem_req ||
    00587             cur_size >= mem_req + sizeof(MemBlock)) {
    00588           /* Set size and in use */
    00589           s->size = mem_req;
    00590
    00591           /* Do we need to inject a free block too? */
    00592           if (cur_size != mem_req) {
    00593             NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK;
    00594           }
    00595
    00596           return s->data;
    00597         }
    00598       }
    00599     }
    00600
    00601     /* Reached sentinel, but no block found yet. Delete some old entry. */
    00602     DeleteEntryFromSpriteCache();
    00603   }
    00604 }
    00605
    00615 static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc, AllocatorProc *allocator)
    00616 {
    00617   static const char * const sprite_types[] = {
    00618     "normal",        // ST_NORMAL
    00619     "map generator", // ST_MAPGEN
    00620     "character",     // ST_FONT
    00621     "recolour",      // ST_RECOLOUR
    00622   };
    00623
    00624   SpriteType available = sc->type;
    00625   if (requested == ST_FONT && available == ST_NORMAL) {
    00626     if (sc->ptr == NULL) sc->type = ST_FONT;
    00627     return GetRawSprite(sprite, sc->type, allocator);
    00628   }
    00629
    00630   byte warning_level = sc->warned ? 6 : 0;
    00631   sc->warned = true;
    00632   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]);
    00633
    00634   switch (requested) {
    00635     case ST_NORMAL:
    00636       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?");
    00637       /* FALL THROUGH */
    00638     case ST_FONT:
    00639       return GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
    00640     case ST_RECOLOUR:
    00641       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?");
    00642       return GetRawSprite(PALETTE_TO_DARK_BLUE, ST_RECOLOUR, allocator);
    00643     case ST_MAPGEN:
    00644       /* this shouldn't happen, overriding of ST_MAPGEN sprites is checked in LoadNextSprite()
    00645        * (the only case the check fails is when these sprites weren't even loaded...) */
    00646     default:
    00647       NOT_REACHED();
    00648   }
    00649 }
    00650
    00659 void *GetRawSprite(SpriteID sprite, SpriteType type, AllocatorProc *allocator)
    00660 {
    00661   assert(IsMapgenSpriteID(sprite) == (type == ST_MAPGEN));
    00662   assert(type < ST_INVALID);
    00663   ZoomLevel zoom;
    00664
    00665   if (!SpriteExists(sprite)) {
    00666     DEBUG(sprite, 1, "Tried to load non-existing sprite #%d. Probable cause: Wrong/missing NewGRFs", sprite);
    00667
    00668     /* SPR_IMG_QUERY is a BIG FAT RED ? */
    00669     sprite = SPR_IMG_QUERY;
    00670   }
    00671
    00672   SpriteCache *sc = GetSpriteCache(sprite);
    00673
    00674   if (sc->type != type) return HandleInvalidSpriteRequest(sprite, type, sc, allocator);
    00675
    00676   if (allocator == NULL) {
    00677     /* Load sprite into/from spritecache */
    00678
    00679     /* Update LRU */
    00680     sc->lru = ++_sprite_lru_counter;
    00681
    00682     zoom = ZoomLevel(_cur_dpi->zoom);
    00683
    00684     /* Load the sprite, if it is not loaded, yet */
    00685     if (sc->ptr[zoom] == NULL) sc->ptr[zoom] = ReadSprite(sc, sprite, type, AllocSprite);
    00686
    00687     return sc->ptr[zoom];
    00688   } else {
    00689     /* Do not use the spritecache, but a different allocator. */
    00690     return ReadSprite(sc, sprite, type, allocator);
    00691   }
    00692 }
    00693
    00694
    00695 void GfxInitSpriteMem()
    00696 {
    00697   /* initialize sprite cache heap */
    00698   if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)MallocT<byte>(_sprite_cache_size * 1024 * 1024);
    00699
    00700   /* A big free block */
    00701   _spritecache_ptr->size = ((_sprite_cache_size * 1024 * 1024) - sizeof(MemBlock)) | S_FREE_MASK;
    00702   /* Sentinel block (identified by size == 0) */
    00703   NextBlock(_spritecache_ptr)->size = 0;
    00704
    00705   /* Reset the spritecache 'pool' */
    00706   free(_spritecache);
    00707   _spritecache_items = 0;
    00708   _spritecache = NULL;
    00709   _compact_cache_counter = 0;
    00710 }
    00711
    00712 /* static */ ReusableBuffer<SpriteLoader::CommonPixel> SpriteLoader::Sprite::buffer;