Data Structures | Typedefs | Functions | Variables

# spritecache.cpp File Reference

Caching of sprites. More...

`#include "stdafx.h"`
`#include "fileio_func.h"`
`#include "spriteloader/grf.hpp"`
`#include "gfx_func.h"`
`#include "zoom_func.h"`
`#include "settings_type.h"`
`#include "blitter/factory.hpp"`
`#include "core/math_func.hpp"`
`#include "core/mem_func.hpp"`
`#include "table/sprites.h"`
`#include "table/palette_convert.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.
static void * | AllocSprite (size_t mem_req)
bool | SkipSpriteData (byte type, uint16 num)
| Skip the given amount of sprite graphics data.
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 bool | ResizeSpriteIn (SpriteLoader::Sprite *sprite, ZoomLevel src, ZoomLevel tgt)
static void | ResizeSpriteOut (SpriteLoader::Sprite *sprite, ZoomLevel zoom)
static bool | PadSingleSprite (SpriteLoader::Sprite *sprite, ZoomLevel zoom, uint pad_left, uint pad_top, uint pad_right, uint pad_bottom)
static bool | PadSprites (SpriteLoader::Sprite *sprite, uint8 sprite_avail)
static bool | ResizeSprites (SpriteLoader::Sprite *sprite, uint8 sprite_avail, uint32 file_slot, uint32 file_pos)
static void * | ReadRecolourSprite (uint16 file_slot, uint num)
| Load a recolour sprite into memory.
static void * | ReadSprite (const SpriteCache *sc, SpriteID id, SpriteType sprite_type, AllocatorProc *allocator)
| Read a sprite from disk.
size_t | GetGRFSpriteOffset (uint32 id)
| Get the file offset for a specific sprite in the sprite section of a GRF.
void | ReadGRFSpriteOffsets (byte container_version)
| Parse the sprite section of GRFs.
bool | LoadNextSprite (int load_index, byte file_slot, uint file_sprite_id, byte container_version)
| Load a real or recolour sprite.
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 (uint item)
| Delete a single entry from the sprite cache.
static void | DeleteEntryFromSpriteCache ()
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).
static void | GfxInitSpriteCache ()
void | GfxInitSpriteMem ()
void | GfxClearSpriteCache ()
| Remove all encoded sprites from the sprite cache without discarding sprite location information.
## 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 std::map< uint32, size_t > | _grf_sprite_offsets
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 635 of file spritecache.cpp.

References DEBUG.

static void DeleteEntryFromSpriteCache | ( | uint | item | ) | `[static]`

Delete a single entry from the sprite cache.

Parameters:

  | item | Entry to delete.

Definition at line 679 of file spritecache.cpp.

Referenced by GfxClearSpriteCache().

size_t GetGRFSpriteOffset | ( | uint32 | id | ) |

Get the file offset for a specific sprite in the sprite section of a GRF.

Parameters:

  | id | ID of the sprite to look up.

Returns:
  Position of the sprite in the sprite section or SIZE_MAX if no such sprite is present.

Definition at line 453 of file spritecache.cpp.

Referenced by LoadNextSprite().

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 154 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 140 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 810 of file spritecache.cpp.

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

Referenced by HandleInvalidSpriteRequest(), and ReadSprite().

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 129 of file spritecache.cpp.

References 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 766 of file spritecache.cpp.

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

Referenced by GetRawSprite().

bool LoadNextSprite | ( | int | load_index,
|  | byte | file_slot,
|  | uint | file_sprite_id,
|  | byte | container_version |
| ) |  |  |

Load a real or recolour sprite.

Parameters:

  | load_index | Global sprite index.
  | file_slot | GRF to load from.
  | file_sprite_id | Sprite number in the GRF.
  | container_version | Container version of the GRF.

Returns:
  True if a valid sprite was loaded, false on any error.

Definition at line 495 of file spritecache.cpp.

References SpriteCache::container_ver, FioGetPos(), FioReadByte(), FioReadDword(), FioReadWord(), FioSkipBytes(), GetGRFSpriteOffset(), MAX_SPRITES, ReadRecolourSprite(), SkipSpriteData(), ST_INVALID, ST_NORMAL, SpriteCache::type, usererror(), and SpriteCache::warned.

Referenced by LoadFontGlyph(), LoadGrfFile(), and LoadGrfFileIndexed().

void ReadGRFSpriteOffsets | ( | byte | container_version | ) |

Parse the sprite section of GRFs.

Parameters:

  | container_version | Container version of the GRF we're currently processing.

Definition at line 462 of file spritecache.cpp.

References FioGetPos(), FioReadDword(), FioSeekTo(), and FioSkipBytes().

Referenced by LoadGrfFile(), LoadGrfFileIndexed(), and LoadNewGRFFile().

static void* ReadRecolourSprite | ( | uint16 | file_slot,
|  | uint | num |
| ) |  |  | `[static]`

Load a recolour sprite into memory.

Parameters:

  | file_slot | GRF we're reading from.
  | num | Size of the sprite in the GRF.

Returns:
  Sprite data.

Definition at line 342 of file spritecache.cpp.

References _palette_remap_grf, _palmap_d2w, AllocaM, FioReadBlock(), and max().

Referenced by LoadNextSprite().

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 377 of file spritecache.cpp.

References SpriteCache::container_ver, Sprite::data, SpriteLoader::Sprite::data, DEBUG, Blitter::Encode(), BlitterFactoryBase::GetCurrentBlitter(), GetRawSprite(), Sprite::height, SpriteLoader::Sprite::height, SpriteLoaderGrf::LoadSprite(), SpriteLoader::CommonPixel::m, ST_MAPGEN, ST_NORMAL, ST_RECOLOUR, SpriteLoader::Sprite::type, SpriteCache::type, usererror(), Sprite::width, SpriteLoader::Sprite::width, SpriteLoader::Sprite::x_offs, Sprite::x_offs, SpriteLoader::Sprite::y_offs, and Sprite::y_offs.

Referenced by GetRawSprite().

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 93 of file spritecache.cpp.

References FioReadByte(), and FioSkipBytes().

Referenced by LoadNewGRFFile(), and LoadNextSprite().

## 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 578 of file spritecache.cpp.