OpenTTD Source  20241108-master-g80f628063a
spritecache.h File Reference

Functions to cache sprites in memory. More...

Go to the source code of this file.

Data Structures

struct  Sprite
 Data structure describing a sprite. More...
 
class  SimpleSpriteAllocator
 SpriteAllocate that uses malloc to allocate memory. More...
 
class  UniquePtrSpriteAllocator
 SpriteAllocator that allocates memory via a unique_ptr array. More...
 

Enumerations

enum  SpriteCacheCtrlFlags { SCCF_ALLOW_ZOOM_MIN_1X_PAL = 0 , SCCF_ALLOW_ZOOM_MIN_1X_32BPP = 1 , SCCF_ALLOW_ZOOM_MIN_2X_PAL = 2 , SCCF_ALLOW_ZOOM_MIN_2X_32BPP = 3 }
 

Functions

void * GetRawSprite (SpriteID sprite, SpriteType type, SpriteAllocator *allocator=nullptr, SpriteEncoder *encoder=nullptr)
 Reads a sprite (from disk or sprite cache). More...
 
bool SpriteExists (SpriteID sprite)
 
SpriteType GetSpriteType (SpriteID sprite)
 Get the sprite type of a given sprite. More...
 
SpriteFileGetOriginFile (SpriteID sprite)
 Get the SpriteFile of a given sprite. More...
 
uint32_t GetSpriteLocalID (SpriteID sprite)
 Get the GRF-local sprite id of a given sprite. More...
 
uint GetSpriteCountForFile (const std::string &filename, SpriteID begin, SpriteID end)
 Count the sprites which originate from a specific file in a range of SpriteIDs. More...
 
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. More...
 
const SpriteGetSprite (SpriteID sprite, SpriteType type)
 
const uint8_t * GetNonSprite (SpriteID sprite, SpriteType type)
 
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.
 
void IncreaseSpriteLRU ()
 
SpriteFileOpenCachedSpriteFile (const std::string &filename, Subdirectory subdir, bool palette_remap)
 Open/get the SpriteFile that is cached for use in the sprite cache. More...
 
std::span< const std::unique_ptr< SpriteFile > > GetCachedSpriteFiles ()
 Get the list of cached SpriteFiles. More...
 
void ReadGRFSpriteOffsets (SpriteFile &file)
 Parse the sprite section of GRFs. More...
 
size_t GetGRFSpriteOffset (uint32_t id)
 Get the file offset for a specific sprite in the sprite section of a GRF. More...
 
bool LoadNextSprite (SpriteID load_index, SpriteFile &file, uint file_sprite_id)
 Load a real or recolour sprite. More...
 
bool SkipSpriteData (SpriteFile &file, uint8_t type, uint16_t num)
 Skip the given amount of sprite graphics data. More...
 
void DupSprite (SpriteID old_spr, SpriteID new_spr)
 

Variables

uint _sprite_cache_size
 

Detailed Description

Functions to cache sprites in memory.

Definition in file spritecache.h.

Enumeration Type Documentation

◆ SpriteCacheCtrlFlags

Enumerator
SCCF_ALLOW_ZOOM_MIN_1X_PAL 

Allow use of sprite min zoom setting at 1x in palette mode.

SCCF_ALLOW_ZOOM_MIN_1X_32BPP 

Allow use of sprite min zoom setting at 1x in 32bpp mode.

SCCF_ALLOW_ZOOM_MIN_2X_PAL 

Allow use of sprite min zoom setting at 2x in palette mode.

SCCF_ALLOW_ZOOM_MIN_2X_32BPP 

Allow use of sprite min zoom setting at 2x in 32bpp mode.

Definition at line 25 of file spritecache.h.

Function Documentation

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

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

References _grf_sprite_offsets.

◆ GetMaxSpriteID()

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

Referenced by 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 170 of file spritecache.cpp.

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

References MapGen.

Referenced by HandleInvalidSpriteRequest().

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

References GetCachedSpriteFileByName().

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

◆ 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 159 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 618 of file spritecache.cpp.

References _grf_sprite_offsets, SpriteFile::GetContainerVersion(), RandomAccessFile::GetPos(), Invalid, MAX_SPRITES, Normal, RandomAccessFile::ReadByte(), RandomAccessFile::ReadDword(), ReadRecolourSprite(), RandomAccessFile::ReadWord(), Recolour, RandomAccessFile::SkipBytes(), and SkipSpriteData().

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

References GetCachedSpriteFileByName().

Referenced by LoadGrfFile(), and LoadGrfFileIndexed().

◆ ReadGRFSpriteOffsets()

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

Referenced by LoadNextSprite().