OpenTTD Source 20250704-master-gf23845805a
spritecache.cpp File Reference

Caching of sprites. More...

#include "stdafx.h"
#include "spriteloader/grf.hpp"
#include "spriteloader/makeindexed.h"
#include "error_func.h"
#include "strings_func.h"
#include "zoom_func.h"
#include "settings_type.h"
#include "blitter/factory.hpp"
#include "core/math_func.hpp"
#include "video/video_driver.hpp"
#include "spritecache.h"
#include "spritecache_internal.h"
#include "table/sprites.h"
#include "table/palette_convert.h"
#include "safeguards.h"

Go to the source code of this file.

Data Structures

struct  GrfSpriteOffset
 

Functions

static SpriteCacheGetSpriteCache (uint index)
 
SpriteCacheAllocateSpriteCache (uint index)
 
static SpriteFileGetCachedSpriteFileByName (const std::string &filename)
 Get the cached SpriteFile given the name of the file.
 
std::span< const std::unique_ptr< SpriteFile > > GetCachedSpriteFiles ()
 Get the list of cached SpriteFiles.
 
SpriteFileOpenCachedSpriteFile (const std::string &filename, Subdirectory subdir, bool palette_remap)
 Open/get the SpriteFile that is cached for use in the sprite cache.
 
bool SkipSpriteData (SpriteFile &file, uint8_t type, uint16_t 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.
 
SpriteFileGetOriginFile (SpriteID sprite)
 Get the SpriteFile of a given sprite.
 
uint32_t GetSpriteLocalID (SpriteID sprite)
 Get the GRF-local sprite id of a given sprite.
 
uint GetSpriteCountForFile (const std::string &filename, SpriteID begin, SpriteID end)
 Count the sprites which originate from a specific file in a range of SpriteIDs.
 
SpriteID 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::SpriteCollection &sprite, ZoomLevel src, ZoomLevel tgt)
 
static void ResizeSpriteOut (SpriteLoader::SpriteCollection &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::SpriteCollection &sprite, ZoomLevels sprite_avail, SpriteEncoder *encoder)
 
static bool ResizeSprites (SpriteLoader::SpriteCollection &sprite, ZoomLevels sprite_avail, SpriteEncoder *encoder)
 
static void * ReadRecolourSprite (SpriteFile &file, size_t file_pos, uint num, SpriteAllocator &allocator)
 Load a recolour sprite into memory.
 
static void * ReadSprite (const SpriteCache *sc, SpriteID id, SpriteType sprite_type, SpriteAllocator &allocator, SpriteEncoder *encoder)
 Read a sprite from disk.
 
size_t GetGRFSpriteOffset (uint32_t id)
 Get the file offset for a specific sprite in the sprite section of a GRF.
 
void ReadGRFSpriteOffsets (SpriteFile &file)
 Parse the sprite section of GRFs.
 
bool LoadNextSprite (SpriteID load_index, SpriteFile &file, uint file_sprite_id)
 Load a real or recolour sprite.
 
void DupSprite (SpriteID old_spr, SpriteID new_spr)
 
static void DeleteEntriesFromSpriteCache (size_t to_remove)
 Delete entries from the sprite cache to remove the requested number of bytes.
 
void IncreaseSpriteLRU ()
 
static void * HandleInvalidSpriteRequest (SpriteID sprite, SpriteType requested, SpriteCache *sc, SpriteAllocator *allocator)
 Handles the case when a sprite of different type is requested than is present in the SpriteCache.
 
void * GetRawSprite (SpriteID sprite, SpriteType type, SpriteAllocator *allocator, SpriteEncoder *encoder)
 Reads a sprite (from disk or sprite cache).
 
void GfxInitSpriteMem ()
 
void GfxClearSpriteCache ()
 Remove all encoded sprites from the sprite cache without discarding sprite location information.
 
void GfxClearFontSpriteCache ()
 Remove all encoded font sprites from the sprite cache without discarding sprite location information.
 

Variables

uint _sprite_cache_size = 4
 
static std::vector< SpriteCache_spritecache
 
static size_t _spritecache_bytes_used = 0
 
static uint32_t _sprite_lru_counter
 
static std::vector< std::unique_ptr< SpriteFile > > _sprite_files
 
static std::map< uint32_t, GrfSpriteOffset_grf_sprite_offsets
 Map from sprite numbers to position in the GRF file.
 

Detailed Description

Caching of sprites.

Definition in file spritecache.cpp.

Function Documentation

◆ AllocateSpriteCache()

SpriteCache * AllocateSpriteCache ( uint  index)

Definition at line 42 of file spritecache.cpp.

◆ DeleteEntriesFromSpriteCache()

static void DeleteEntriesFromSpriteCache ( size_t  to_remove)
static

Delete entries from the sprite cache to remove the requested number of bytes.

Sprite data is removed in order of LRU values. The total number of bytes removed may be larger than the number requested.

Parameters
to_removeRequested number of bytes to remove.

Definition at line 710 of file spritecache.cpp.

References Debug, and SpriteCache::length.

◆ DupSprite()

void DupSprite ( SpriteID  old_spr,
SpriteID  new_spr 
)

Definition at line 690 of file spritecache.cpp.

◆ GetCachedSpriteFileByName()

static SpriteFile * GetCachedSpriteFileByName ( const std::string &  filename)
static

Get the cached SpriteFile given the name of the file.

Parameters
filenameThe name of the file at the disk.
Returns
The SpriteFile or null.

Definition at line 61 of file spritecache.cpp.

Referenced by GetSpriteCountForFile(), and OpenCachedSpriteFile().

◆ GetCachedSpriteFiles()

std::span< const std::unique_ptr< SpriteFile > > GetCachedSpriteFiles ( )

Get the list of cached SpriteFiles.

Returns
Read-only list of cache SpriteFiles.

Definition at line 75 of file spritecache.cpp.

Referenced by SpriteAlignerWindow::UpdateWidgetSize().

◆ GetGRFSpriteOffset()

size_t GetGRFSpriteOffset ( uint32_t  id)

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

Parameters
idID 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 555 of file spritecache.cpp.

References _grf_sprite_offsets.

◆ GetMaxSpriteID()

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

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

◆ GetOriginFile()

SpriteFile * GetOriginFile ( SpriteID  sprite)

Get the SpriteFile of a given sprite.

Parameters
spriteThe sprite to look at.
Returns
The SpriteFile.

Definition at line 152 of file spritecache.cpp.

References SpriteCache::file.

Referenced by SpriteAlignerWindow::DrawWidget(), and SpriteAlignerWindow::GetWidgetString().

◆ GetRawSprite()

void * GetRawSprite ( SpriteID  sprite,
SpriteType  type,
SpriteAllocator allocator,
SpriteEncoder encoder 
)

Reads a sprite (from disk or sprite cache).

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

Parameters
spriteSprite to read.
typeExpected sprite type.
allocatorAllocator function to use. Set to nullptr to use the usual sprite cache.
encoderSprite encoder to use. Set to nullptr to use the currently active blitter.
Returns
Sprite raw data

Definition at line 859 of file spritecache.cpp.

References Debug, SpriteCache::file, HandleInvalidSpriteRequest(), Invalid, SpriteCache::length, MapGen, ReadRecolourSprite(), ReadSprite(), Recolour, and SpriteCache::type.

Referenced by HandleInvalidSpriteRequest(), and ReadSprite().

◆ GetSpriteCache()

static SpriteCache * GetSpriteCache ( uint  index)
inlinestatic

Definition at line 37 of file spritecache.cpp.

◆ GetSpriteCountForFile()

uint GetSpriteCountForFile ( const std::string &  filename,
SpriteID  begin,
SpriteID  end 
)

Count the sprites which originate from a specific file in a range of SpriteIDs.

Parameters
fileThe loaded SpriteFile.
beginFirst sprite in range.
endFirst sprite not in range.
Returns
Number of sprites.

Definition at line 176 of file spritecache.cpp.

References Debug, SpriteCache::file, and GetCachedSpriteFileByName().

Referenced by LoadSpriteTables().

◆ GetSpriteLocalID()

uint32_t GetSpriteLocalID ( SpriteID  sprite)

Get the GRF-local sprite id of a given sprite.

Parameters
spriteThe sprite to look at.
Returns
The GRF-local sprite id.

Definition at line 163 of file spritecache.cpp.

Referenced by SpriteAlignerWindow::DrawWidget(), NewGRFProfiler::EndResolve(), and SpriteAlignerWindow::GetWidgetString().

◆ GetSpriteType()

SpriteType GetSpriteType ( SpriteID  sprite)

Get the sprite type of a given sprite.

Parameters
spriteThe sprite to look at.
Returns
the type of sprite.

Definition at line 141 of file spritecache.cpp.

References Invalid, and SpriteCache::type.

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

◆ GfxClearFontSpriteCache()

void GfxClearFontSpriteCache ( )

Remove all encoded font sprites from the sprite cache without discarding sprite location information.

Definition at line 929 of file spritecache.cpp.

References Font.

Referenced by AdjustGUIZoom().

◆ GfxClearSpriteCache()

void GfxClearSpriteCache ( )

Remove all encoded sprites from the sprite cache without discarding sprite location information.

Definition at line 915 of file spritecache.cpp.

References VideoDriver::ClearSystemSprites(), and VideoDriver::GetInstance().

Referenced by CheckBlitter().

◆ GfxInitSpriteMem()

void GfxInitSpriteMem ( )

Definition at line 901 of file spritecache.cpp.

◆ HandleInvalidSpriteRequest()

static void * HandleInvalidSpriteRequest ( SpriteID  sprite,
SpriteType  requested,
SpriteCache sc,
SpriteAllocator allocator 
)
static

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

For SpriteType::Font sprites, it is normal. In other cases, default sprite is loaded instead.

Parameters
spriteID of loaded sprite
requestedrequested sprite type
scthe 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 814 of file spritecache.cpp.

References Debug, Font, GetRawSprite(), MapGen, Normal, Recolour, SpriteCache::type, and SpriteCache::warned.

Referenced by GetRawSprite().

◆ IncreaseSpriteLRU()

void IncreaseSpriteLRU ( )

Definition at line 768 of file spritecache.cpp.

◆ LoadNextSprite()

bool LoadNextSprite ( SpriteID  load_index,
SpriteFile file,
uint  file_sprite_id 
)

Load a real or recolour sprite.

Parameters
load_indexGlobal sprite index.
fileGRF to load from.
file_sprite_idSprite number in the GRF.
container_versionContainer version of the GRF.
Returns
True if a valid sprite was loaded, false on any error.

Definition at line 620 of file spritecache.cpp.

References _grf_sprite_offsets, SpriteCache::control_flags, SpriteCache::file, SpriteFile::GetContainerVersion(), RandomAccessFile::GetPos(), Invalid, SpriteCache::length, MapGen, MAX_SPRITES, Normal, RandomAccessFile::ReadByte(), RandomAccessFile::ReadDword(), RandomAccessFile::ReadWord(), Recolour, RandomAccessFile::SkipBytes(), SkipSpriteData(), SpriteCache::type, and SpriteCache::warned.

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

◆ OpenCachedSpriteFile()

SpriteFile & OpenCachedSpriteFile ( const std::string &  filename,
Subdirectory  subdir,
bool  palette_remap 
)

Open/get the SpriteFile that is cached for use in the sprite cache.

Parameters
filenameName of the file at the disk.
subdirThe sub directory to search this file in.
palette_remapWhether a palette remap needs to be performed for this file.
Returns
The reference to the SpriteCache.

Definition at line 87 of file spritecache.cpp.

References GetCachedSpriteFileByName(), and SpriteFile::SeekToBegin().

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

◆ PadSingleSprite()

static bool PadSingleSprite ( SpriteLoader::Sprite sprite,
ZoomLevel  zoom,
uint  pad_left,
uint  pad_top,
uint  pad_right,
uint  pad_bottom 
)
static

Definition at line 272 of file spritecache.cpp.

◆ PadSprites()

static bool PadSprites ( SpriteLoader::SpriteCollection sprite,
ZoomLevels  sprite_avail,
SpriteEncoder encoder 
)
static

Definition at line 321 of file spritecache.cpp.

◆ ReadGRFSpriteOffsets()

◆ ReadRecolourSprite()

static void * ReadRecolourSprite ( SpriteFile file,
size_t  file_pos,
uint  num,
SpriteAllocator allocator 
)
static

Load a recolour sprite into memory.

Parameters
fileGRF we're reading from.
file_posPosition within file.
numSize of the sprite in the GRF.
allocatorSprite allocator to use.
Returns
Sprite data.

Definition at line 423 of file spritecache.cpp.

References _palmap_d2w, SpriteAllocator::Allocate(), SpriteFile::NeedsPaletteRemap(), RandomAccessFile::ReadBlock(), and RandomAccessFile::SeekTo().

Referenced by GetRawSprite().

◆ ReadSprite()

◆ ResizeSpriteIn()

static bool ResizeSpriteIn ( SpriteLoader::SpriteCollection sprite,
ZoomLevel  src,
ZoomLevel  tgt 
)
static

Definition at line 207 of file spritecache.cpp.

◆ ResizeSpriteOut()

static void ResizeSpriteOut ( SpriteLoader::SpriteCollection sprite,
ZoomLevel  zoom 
)
static

Definition at line 236 of file spritecache.cpp.

◆ ResizeSprites()

static bool ResizeSprites ( SpriteLoader::SpriteCollection sprite,
ZoomLevels  sprite_avail,
SpriteEncoder encoder 
)
static

Definition at line 370 of file spritecache.cpp.

◆ SkipSpriteData()

bool SkipSpriteData ( SpriteFile file,
uint8_t  type,
uint16_t  num 
)

Skip the given amount of sprite graphics data.

Parameters
typethe type of sprite (compressed etc)
numthe amount of sprites to skip
Returns
true if the data could be correctly skipped.

Definition at line 104 of file spritecache.cpp.

References RandomAccessFile::ReadByte(), and RandomAccessFile::SkipBytes().

Referenced by LoadNewGRFFileFromFile(), and LoadNextSprite().

◆ SpriteExists()

bool SpriteExists ( SpriteID  id)

Definition at line 127 of file spritecache.cpp.

Variable Documentation

◆ _grf_sprite_offsets

std::map<uint32_t, GrfSpriteOffset> _grf_sprite_offsets
static

Map from sprite numbers to position in the GRF file.

Definition at line 548 of file spritecache.cpp.

Referenced by GetGRFSpriteOffset(), LoadNextSprite(), and ReadGRFSpriteOffsets().

◆ _sprite_cache_size

uint _sprite_cache_size = 4

Definition at line 29 of file spritecache.cpp.

◆ _sprite_files

std::vector<std::unique_ptr<SpriteFile> > _sprite_files
static

Definition at line 35 of file spritecache.cpp.

◆ _sprite_lru_counter

uint32_t _sprite_lru_counter
static

Definition at line 34 of file spritecache.cpp.

◆ _spritecache

std::vector<SpriteCache> _spritecache
static

Definition at line 32 of file spritecache.cpp.

◆ _spritecache_bytes_used

size_t _spritecache_bytes_used = 0
static

Definition at line 33 of file spritecache.cpp.