OpenTTD Source: src/spritecache.h File Reference

OpenTTD Source 15.0-RC2

Data Structures | Functions | Variables
spritecache.h File Reference

Functions to cache sprites in memory. More...

`#include "gfx_type.h"`
`#include "spritecache_type.h"`
`#include "spriteloader/spriteloader.hpp"`

Go to the source code of this file.

## Data Structures
class | UniquePtrSpriteAllocator
| SpriteAllocator that allocates memory via a unique_ptr array. More...

## Functions
void * | GetRawSprite (SpriteID sprite, SpriteType type, SpriteAllocator *allocator=nullptr, SpriteEncoder *encoder=nullptr)
| Reads a sprite (from disk or sprite cache).
bool | SpriteExists (SpriteID sprite)
SpriteType | GetSpriteType (SpriteID sprite)
| Get the sprite type of a given sprite.
SpriteFile * | GetOriginFile (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.
const Sprite * | GetSprite (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 ()
SpriteFile & | OpenCachedSpriteFile (const std::string &filename, Subdirectory subdir, bool palette_remap)
| Open/get the SpriteFile that is cached for use in the sprite cache.
std::span< const std::unique_ptr< SpriteFile > > | GetCachedSpriteFiles ()
| Get the list of cached SpriteFiles.
void | ReadGRFSpriteOffsets (SpriteFile &file)
| Parse the sprite section of GRFs.
size_t | GetGRFSpriteOffset (uint32_t id)
| Get the file offset for a specific sprite in the sprite section of a GRF.
bool | LoadNextSprite (SpriteID load_index, SpriteFile &file, uint file_sprite_id)
| Load a real or recolour sprite.
bool | SkipSpriteData (SpriteFile &file, uint8_t type, uint16_t num)
| Skip the given amount of sprite graphics data.
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.

## Function Documentation

## ◆ DupSprite()

void DupSprite | ( | SpriteID | old_spr,
|  | SpriteID | new_spr
| ) |  |

Definition at line 684 of file spritecache.cpp.

## ◆ 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

  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 549 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().

## ◆ GetNonSprite()

const uint8_t * GetNonSprite | ( | SpriteID | sprite,
|  | SpriteType | type
| ) |  |
| inline

Definition at line 44 of file spritecache.h.

## ◆ GetOriginFile()

SpriteFile * GetOriginFile | ( | SpriteID | sprite | ) |

Get the SpriteFile of a given sprite.

Parameters

  sprite | The 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

  sprite | Sprite to read.
  type | Expected sprite type.
  allocator | Allocator function to use. Set to nullptr to use the usual sprite cache.
  encoder | Sprite encoder to use. Set to nullptr to use the currently active blitter.

Returns
  Sprite raw data

Definition at line 853 of file spritecache.cpp.

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

Referenced by HandleInvalidSpriteRequest(), and ReadSprite().

## ◆ GetSprite()

const Sprite * GetSprite | ( | SpriteID | sprite,
|  | SpriteType | type
| ) |  |
| inline

Definition at line 38 of file spritecache.h.

## ◆ 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

  file | The loaded SpriteFile.
  begin | First sprite in range.
  end | First 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

  sprite | The 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

  sprite | The 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 923 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 909 of file spritecache.cpp.

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

Referenced by CheckBlitter().

## ◆ GfxInitSpriteMem()

void GfxInitSpriteMem | ( |  | ) |

Definition at line 895 of file spritecache.cpp.

## ◆ IncreaseSpriteLRU()

void IncreaseSpriteLRU | ( |  | ) |

Definition at line 762 of file spritecache.cpp.

## ◆ LoadNextSprite()

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

Load a real or recolour sprite.

Parameters

  load_index | Global sprite index.
  file | 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 614 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

  filename | Name of the file at the disk.
  subdir | The sub directory to search this file in.
  palette_remap | Whether 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().

## ◆ ReadGRFSpriteOffsets()

void ReadGRFSpriteOffsets | ( | SpriteFile & | file | ) |

Parse the sprite section of GRFs.

Parameters

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

Definition at line 558 of file spritecache.cpp.

References _grf_sprite_offsets, AllowZoomMin1x32bpp, AllowZoomMin1xPal, AllowZoomMin2x32bpp, AllowZoomMin2xPal, SpriteFile::GetContainerVersion(), RandomAccessFile::GetPos(), Palette, RandomAccessFile::ReadByte(), RandomAccessFile::ReadDword(), RandomAccessFile::SeekTo(), and RandomAccessFile::SkipBytes().

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

## ◆ SkipSpriteData()

bool SkipSpriteData | ( | SpriteFile & | file,
|  | uint8_t | type,
|  | uint16_t | 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 104 of file spritecache.cpp.

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

Referenced by LoadNewGRFFileFromFile(), and LoadNextSprite().

## ◆ SpriteExists()

bool SpriteExists | ( | SpriteID | sprite | ) |

Definition at line 127 of file spritecache.cpp.

## Variable Documentation

## ◆ _sprite_cache_size

uint _sprite_cache_size
| extern

Definition at line 29 of file spritecache.cpp.