OpenTTD Source: src/spritecache.h Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef SPRITECACHE_H
11#define SPRITECACHE_H
12
13#include "gfx_type.h"
14#include "spritecache_type.h"
15#include "spriteloader/spriteloader.hpp"
16
17extern uint _sprite_cache_size;
18
20 class UniquePtrSpriteAllocator : public SpriteAllocator {
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
31 SpriteType GetSpriteType(SpriteID sprite);
32 SpriteFile *GetOriginFile(SpriteID sprite);
33uint32_t GetSpriteLocalID(SpriteID sprite);
34uint GetSpriteCountForFile(const std::string &filename, SpriteID begin, SpriteID end);
35 SpriteID GetMaxSpriteID();
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();
51void GfxClearSpriteCache();
52void GfxClearFontSpriteCache();
53void IncreaseSpriteLRU();
54
55 SpriteFile &OpenCachedSpriteFile(const std::string &filename, Subdirectory subdir, bool palette_remap);
56std::span<const std::unique_ptr<SpriteFile>> GetCachedSpriteFiles();
57
58void ReadGRFSpriteOffsets(SpriteFile &file);
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 */
SpriteAllocator
Interface for something that can allocate memory for a sprite.
Definition spriteloader.hpp:104
SpriteEncoder
Interface for something that can encode a sprite.
Definition spriteloader.hpp:130
SpriteFile
RandomAccessFile with some extra information specific for sprite files.
Definition sprite_file_type.hpp:19
UniquePtrSpriteAllocator
SpriteAllocator that allocates memory via a unique_ptr array.
Definition spritecache.h:20
UniquePtrSpriteAllocator::AllocatePtr
void * AllocatePtr(size_t size) override
Allocate memory for a sprite.
Definition spritecache.cpp:792
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
gfx_type.h
Types related to the graphics and/or input devices.
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
SpriteType
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:357
SpriteType::Recolour
@ Recolour
Recolour sprite.
GetRawSprite
void * GetRawSprite(SpriteID sprite, SpriteType type, SpriteAllocator *allocator=nullptr, SpriteEncoder *encoder=nullptr)
Reads a sprite (from disk or sprite cache).
Definition spritecache.cpp:855
GetSpriteType
SpriteType GetSpriteType(SpriteID sprite)
Get the sprite type of a given sprite.
Definition spritecache.cpp:141
GfxClearSpriteCache
void GfxClearSpriteCache()
Remove all encoded sprites from the sprite cache without discarding sprite location information.
Definition spritecache.cpp:911
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.
Definition spritecache.cpp:176
GfxClearFontSpriteCache
void GfxClearFontSpriteCache()
Remove all encoded font sprites from the sprite cache without discarding sprite location information.
Definition spritecache.cpp:925
GetMaxSpriteID
SpriteID GetMaxSpriteID()
Get a reasonable (upper bound) estimate of the maximum SpriteID used in OpenTTD; there will be no spr...
Definition spritecache.cpp:202
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.
Definition spritecache.cpp:87
GetCachedSpriteFiles
std::span< const std::unique_ptr< SpriteFile > > GetCachedSpriteFiles()
Get the list of cached SpriteFiles.
Definition spritecache.cpp:75
LoadNextSprite
bool LoadNextSprite(SpriteID load_index, SpriteFile &file, uint file_sprite_id)
Load a real or recolour sprite.
Definition spritecache.cpp:614
GetOriginFile
SpriteFile * GetOriginFile(SpriteID sprite)
Get the SpriteFile of a given sprite.
Definition spritecache.cpp:152
GetGRFSpriteOffset
size_t GetGRFSpriteOffset(uint32_t id)
Get the file offset for a specific sprite in the sprite section of a GRF.
Definition spritecache.cpp:549
SkipSpriteData
bool SkipSpriteData(SpriteFile &file, uint8_t type, uint16_t num)
Skip the given amount of sprite graphics data.
Definition spritecache.cpp:104
GetSpriteLocalID
uint32_t GetSpriteLocalID(SpriteID sprite)
Get the GRF-local sprite id of a given sprite.
Definition spritecache.cpp:163
ReadGRFSpriteOffsets
void ReadGRFSpriteOffsets(SpriteFile &file)
Parse the sprite section of GRFs.
Definition spritecache.cpp:558
spritecache_type.h
Types related to the sprite cache.
spriteloader.hpp
Base for loading sprites.
Sprite
Data structure describing a sprite.
Definition spritecache_type.h:16