OpenTTD Source 20250814-master-g3d806d6a65
spritecache.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#ifndef SPRITECACHE_H
11#define SPRITECACHE_H
12
13#include "gfx_type.h"
14#include "spritecache_type.h"
16
17extern uint _sprite_cache_size;
18
21public:
22 std::unique_ptr<std::byte[]> data;
23 size_t size;
24protected:
25 void *AllocatePtr(size_t size) override;
26};
27
28void *GetRawSprite(SpriteID sprite, SpriteType type, SpriteAllocator *allocator = nullptr, SpriteEncoder *encoder = nullptr);
29bool SpriteExists(SpriteID sprite);
30
33uint32_t GetSpriteLocalID(SpriteID sprite);
34uint GetSpriteCountForFile(const std::string &filename, SpriteID begin, SpriteID end);
36
37
38inline const Sprite *GetSprite(SpriteID sprite, SpriteType type)
39{
40 assert(type != SpriteType::Recolour);
41 return (Sprite*)GetRawSprite(sprite, type);
42}
43
44inline const uint8_t *GetNonSprite(SpriteID sprite, SpriteType type)
45{
46 assert(type == SpriteType::Recolour);
47 return (uint8_t*)GetRawSprite(sprite, type);
48}
49
50void GfxInitSpriteMem();
53void IncreaseSpriteLRU();
54
55SpriteFile &OpenCachedSpriteFile(const std::string &filename, Subdirectory subdir, bool palette_remap);
56std::span<const std::unique_ptr<SpriteFile>> GetCachedSpriteFiles();
57
59size_t GetGRFSpriteOffset(uint32_t id);
60bool LoadNextSprite(SpriteID load_index, SpriteFile &file, uint file_sprite_id);
61bool SkipSpriteData(SpriteFile &file, uint8_t type, uint16_t num);
62void DupSprite(SpriteID old_spr, SpriteID new_spr);
63
64#endif /* SPRITECACHE_H */
Interface for something that can allocate memory for a sprite.
Interface for something that can encode a sprite.
RandomAccessFile with some extra information specific for sprite files.
SpriteAllocator that allocates memory via a unique_ptr array.
Definition spritecache.h:20
void * AllocatePtr(size_t size) override
Allocate memory for a sprite.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
Types related to the graphics and/or input devices.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:358
@ Recolour
Recolour sprite.
void * GetRawSprite(SpriteID sprite, SpriteType type, SpriteAllocator *allocator=nullptr, SpriteEncoder *encoder=nullptr)
Reads a sprite (from disk or sprite cache).
SpriteType GetSpriteType(SpriteID sprite)
Get the sprite type of a given sprite.
void GfxClearSpriteCache()
Remove all encoded sprites from the sprite cache without discarding sprite location information.
uint GetSpriteCountForFile(const std::string &filename, SpriteID begin, SpriteID end)
Count the sprites which originate from a specific file in a range of SpriteIDs.
void GfxClearFontSpriteCache()
Remove all encoded font sprites from the sprite cache without discarding sprite location information.
SpriteID GetMaxSpriteID()
Get a reasonable (upper bound) estimate of the maximum SpriteID used in OpenTTD; there will be no spr...
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.
bool LoadNextSprite(SpriteID load_index, SpriteFile &file, uint file_sprite_id)
Load a real or recolour sprite.
SpriteFile * GetOriginFile(SpriteID sprite)
Get the SpriteFile of a given sprite.
size_t GetGRFSpriteOffset(uint32_t id)
Get the file offset for a specific sprite in the sprite section of a GRF.
bool SkipSpriteData(SpriteFile &file, uint8_t type, uint16_t num)
Skip the given amount of sprite graphics data.
uint32_t GetSpriteLocalID(SpriteID sprite)
Get the GRF-local sprite id of a given sprite.
void ReadGRFSpriteOffsets(SpriteFile &file)
Parse the sprite section of GRFs.
Types related to the sprite cache.
Base for loading sprites.
Data structure describing a sprite.