# spritecache.cpp File Reference

Caching of sprites. More...

`#include "stdafx.h"`
`#include "gfx_func.h"`
`#include "table/sprites.h"`
`#include "fileio_func.h"`
`#include "spriteloader/grf.hpp"`
`#include "spriteloader/png.hpp"`
`#include "blitter/factory.hpp"`
`#include "blitter/32bpp_optimized.hpp"`
`#include "core/math_func.hpp"`

Go to the source code of this file.

## Data Structures
struct | SpriteCache
struct | MemBlock
## Typedefs
typedef SimpleTinyEnumT < SpriteType, byte > | SpriteTypeByte
## Functions
static SpriteCache * | GetSpriteCache (uint index)
static bool | IsMapgenSpriteID (SpriteID sprite)
static SpriteCache * | AllocateSpriteCache (uint index)
static void | CompactSpriteCache ()
| Called when holes in the sprite cache should be removed.
bool | SkipSpriteData (byte type, uint16 num)
| Skip the given amount of sprite graphics data.
static SpriteType | ReadSpriteHeaderSkipData ()
| Read the sprite header data and then skip the real payload.
bool | SpriteExists (SpriteID id)
SpriteType | GetSpriteType (SpriteID sprite)
| Get the sprite type of a given sprite.
uint | GetOriginFileSlot (SpriteID sprite)
| Get the (FIOS) file slot of a given sprite.
uint | GetMaxSpriteID ()
| Get a reasonable (upper bound) estimate of the maximum SpriteID used in OpenTTD; there will be no sprites with a higher SpriteID, although there might be up to roughly a thousand unused SpriteIDs below this number.
static void * | ReadSprite (const SpriteCache *sc, SpriteID id, SpriteType sprite_type, AllocatorProc *allocator)
| Read a sprite from disk.
bool | LoadNextSprite (int load_index, byte file_slot, uint file_sprite_id)
void | DupSprite (SpriteID old_spr, SpriteID new_spr)
| assert_compile (sizeof(MemBlock)==sizeof(size_t))
| assert_compile ((sizeof(size_t)&(sizeof(size_t)-1))==0)
static MemBlock * | NextBlock (MemBlock *block)
static size_t | GetSpriteCacheUsage ()
void | IncreaseSpriteLRU ()
static void | DeleteEntryFromSpriteCache ()
static void * | AllocSprite (size_t mem_req)
static void * | HandleInvalidSpriteRequest (SpriteID sprite, SpriteType requested, SpriteCache *sc, AllocatorProc *allocator)
| Handles the case when a sprite of different type is requested than is present in the SpriteCache.
void * | GetRawSprite (SpriteID sprite, SpriteType type, AllocatorProc *allocator)
| Reads a sprite (from disk or sprite cache).
void | GfxInitSpriteMem ()
## Variables
uint | _sprite_cache_size = 64
static uint | _spritecache_items = 0
static SpriteCache * | _spritecache = NULL
static uint | _sprite_lru_counter
static MemBlock * | _spritecache_ptr
static int | _compact_cache_counter
static const size_t | S_FREE_MASK = sizeof(size_t) - 1
| S_FREE_MASK is used to mask-out lower bits of MemBlock::size If they are non-zero, the block is free.

## Detailed Description

Caching of sprites.

Definition in file spritecache.cpp.

## Function Documentation

static void CompactSpriteCache | ( |  | ) | `[static]`

Called when holes in the sprite cache should be removed.

That is accomplished by moving the cached data.

Definition at line 475 of file spritecache.cpp.

References DEBUG.

uint GetMaxSpriteID | ( |  | ) |

Get a reasonable (upper bound) estimate of the maximum SpriteID used in OpenTTD; there will be no sprites with a higher SpriteID, although there might be up to roughly a thousand unused SpriteIDs below this number.

Note:
  It's actually the number of spritecache items.

Returns:
  maximum SpriteID

Definition at line 181 of file spritecache.cpp.

Referenced by SpriteAlignerWindow::OnClick(), and SpriteAlignerWindow::OnQueryTextFinished().

uint GetOriginFileSlot | ( | SpriteID | sprite | ) |

Get the (FIOS) file slot of a given sprite.

Parameters:

  | sprite | The sprite to look at.

Returns:
  the FIOS file slot

Definition at line 167 of file spritecache.cpp.

Referenced by SpriteAlignerWindow::SetStringParameters().

void* GetRawSprite | ( | SpriteID | sprite,
|  | SpriteType | type,
|  | AllocatorProc * | allocator |
| ) |  |  |

Reads a sprite (from disk or sprite cache).

If the sprite is not available or of wrong type, a fallback sprite is returned.

Parameters:

  | sprite | Sprite to read.
  | type | Expected sprite type.
  | allocator | Allocator function to use. Set to NULL to use the usual sprite cache.

Returns:
  Sprite raw data

Definition at line 659 of file spritecache.cpp.

References DEBUG, HandleInvalidSpriteRequest(), ReadSprite(), ST_INVALID, ST_MAPGEN, and SpriteCache::type.

Referenced by HandleInvalidSpriteRequest().

SpriteType GetSpriteType | ( | SpriteID | sprite | ) |

Get the sprite type of a given sprite.

Parameters:

  | sprite | The sprite to look at.

Returns:
  the type of sprite.

Definition at line 156 of file spritecache.cpp.

References ST_INVALID, and SpriteCache::type.

Referenced by SpriteAlignerWindow::OnClick(), and SpriteAlignerWindow::OnQueryTextFinished().

static void* HandleInvalidSpriteRequest | ( | SpriteID | sprite,
|  | SpriteType | requested,
|  | SpriteCache * | sc,
|  | AllocatorProc * | allocator |
| ) |  |  | `[static]`

Handles the case when a sprite of different type is requested than is present in the SpriteCache.

For ST_FONT sprites, it is normal. In other cases, default sprite is loaded instead.

Parameters:

  | sprite | ID of loaded sprite
  | requested | requested sprite type
  | sc | the currently known sprite cache for the requested sprite

Returns:
  fallback sprite

Note:
  this function will do usererror() in the case the fallback sprite isn't available

Definition at line 615 of file spritecache.cpp.

References DEBUG, GetRawSprite(), ST_FONT, ST_MAPGEN, ST_NORMAL, ST_RECOLOUR, SpriteCache::type, usererror(), and SpriteCache::warned.

Referenced by GetRawSprite().

static void* ReadSprite | ( | const SpriteCache * | sc,
|  | SpriteID | id,
|  | SpriteType | sprite_type,
|  | AllocatorProc * | allocator |
| ) |  |  | `[static]`

Read a sprite from disk.

Parameters:

  | sc | Location of sprite.
  | id | Sprite number.
  | sprite_type | Type of sprite.
  | allocator | Allocator function to use.

Returns:
  Read sprite data.

Definition at line 194 of file spritecache.cpp.

References _palette_remap, _palette_remap_grf, _palette_reverse_remap, AllocaM, Sprite::data, DEBUG, Blitter::Encode(), BlitterFactoryBase::GetCurrentBlitter(), Blitter::GetScreenDepth(), Sprite::height, SpriteLoaderGrf::LoadSprite(), SpriteLoaderPNG::LoadSprite(), max(), ST_FONT, ST_MAPGEN, ST_NORMAL, ST_RECOLOUR, SpriteCache::type, Sprite::width, Sprite::x_offs, and Sprite::y_offs.

Referenced by GetRawSprite().

static SpriteType ReadSpriteHeaderSkipData | ( |  | ) | `[static]`

Read the sprite header data and then skip the real payload.

Returns:
  type of sprite; ST_INVALID if the sprite is a pseudo- or unusable sprite

Definition at line 124 of file spritecache.cpp.

References SkipSpriteData(), ST_INVALID, ST_NORMAL, and ST_RECOLOUR.

bool SkipSpriteData | ( | byte | type,
|  | uint16 | num |
| ) |  |  |

Skip the given amount of sprite graphics data.

Parameters:

  | type | the type of sprite (compressed etc)
  | num | the amount of sprites to skip

Returns:
  true if the data could be correctly skipped.

Definition at line 98 of file spritecache.cpp.

Referenced by ReadSpriteHeaderSkipData().

## Variable Documentation

const size_t S_FREE_MASK = sizeof(size_t) - 1`[static]`

S_FREE_MASK is used to mask-out lower bits of MemBlock::size If they are non-zero, the block is free.

S_FREE_MASK has to ensure MemBlock is correctly aligned - it means 8B (S_FREE_MASK == 7) on 64bit systems!

Definition at line 416 of file spritecache.cpp.