# spritecache.cpp File Reference

Caching of sprites. More...

`#include "stdafx.h"`
`#include "gfx_type.h"`
`#include "fileio_func.h"`
`#include "spriteloader/grf.hpp"`
`#include "core/alloc_func.hpp"`
`#include "gfx_func.h"`
`#include "blitter/factory.hpp"`
`#include "core/math_func.hpp"`
`#include "table/sprites.h"`

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)
void * | AllocSprite (size_t)
static void * | ReadSprite (SpriteCache *sc, SpriteID id, SpriteType sprite_type)
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 const void * | HandleInvalidSpriteRequest (SpriteID sprite, SpriteType requested, SpriteCache *sc)
| Handles the case when a sprite of different type is requested than is present in the SpriteCache.
const void * | GetRawSprite (SpriteID sprite, SpriteType type)
void | GfxInitSpriteMem ()
## Variables
uint | _sprite_cache_size = 4
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 370 of file spritecache.cpp.

static const void* HandleInvalidSpriteRequest | ( | SpriteID | sprite,
|  | SpriteType | requested,
|  | SpriteCache * | sc |
| ) |  |  | `[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
  | available | available sprite type

Returns:
  fallback sprite

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

Definition at line 492 of file spritecache.cpp.

References ST_FONT, ST_MAPGEN, ST_NORMAL, ST_RECOLOUR, and usererror().

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 111 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 85 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 315 of file spritecache.cpp.