OpenTTD Source  20241120-master-g6d3adc6169
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"
15 
17 struct Sprite {
18  uint16_t height;
19  uint16_t width;
20  int16_t x_offs;
21  int16_t y_offs;
22  uint8_t data[];
23 };
24 
30 };
31 
32 extern uint _sprite_cache_size;
33 
36 protected:
37  void *AllocatePtr(size_t size) override;
38 };
39 
42 public:
43  std::unique_ptr<uint8_t[]> data;
44 protected:
45  void *AllocatePtr(size_t size) override;
46 };
47 
48 void *GetRawSprite(SpriteID sprite, SpriteType type, SpriteAllocator *allocator = nullptr, SpriteEncoder *encoder = nullptr);
49 bool SpriteExists(SpriteID sprite);
50 
53 uint32_t GetSpriteLocalID(SpriteID sprite);
54 uint GetSpriteCountForFile(const std::string &filename, SpriteID begin, SpriteID end);
55 uint GetMaxSpriteID();
56 
57 
58 inline const Sprite *GetSprite(SpriteID sprite, SpriteType type)
59 {
60  assert(type != SpriteType::Recolour);
61  return (Sprite*)GetRawSprite(sprite, type);
62 }
63 
64 inline const uint8_t *GetNonSprite(SpriteID sprite, SpriteType type)
65 {
66  assert(type == SpriteType::Recolour);
67  return (uint8_t*)GetRawSprite(sprite, type);
68 }
69 
70 void GfxInitSpriteMem();
71 void GfxClearSpriteCache();
73 void IncreaseSpriteLRU();
74 
75 SpriteFile &OpenCachedSpriteFile(const std::string &filename, Subdirectory subdir, bool palette_remap);
76 std::span<const std::unique_ptr<SpriteFile>> GetCachedSpriteFiles();
77 
79 size_t GetGRFSpriteOffset(uint32_t id);
80 bool LoadNextSprite(SpriteID load_index, SpriteFile &file, uint file_sprite_id);
81 bool SkipSpriteData(SpriteFile &file, uint8_t type, uint16_t num);
82 void DupSprite(SpriteID old_spr, SpriteID new_spr);
83 
84 #endif /* SPRITECACHE_H */
SpriteAllocate that uses malloc to allocate memory.
Definition: spritecache.h:35
void * AllocatePtr(size_t size) override
Sprite allocator simply using malloc.
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:41
void * AllocatePtr(size_t size) override
Allocate memory for a sprite.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:115
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:18
SpriteType
Types of sprites that might be loaded.
Definition: gfx_type.h:312
@ Recolour
Recolour sprite.
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.
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:93
std::span< const std::unique_ptr< SpriteFile > > GetCachedSpriteFiles()
Get the list of cached SpriteFiles.
Definition: spritecache.cpp:81
bool LoadNextSprite(SpriteID load_index, SpriteFile &file, uint file_sprite_id)
Load a real or recolour sprite.
SpriteCacheCtrlFlags
Definition: spritecache.h:25
@ SCCF_ALLOW_ZOOM_MIN_2X_32BPP
Allow use of sprite min zoom setting at 2x in 32bpp mode.
Definition: spritecache.h:29
@ SCCF_ALLOW_ZOOM_MIN_1X_32BPP
Allow use of sprite min zoom setting at 1x in 32bpp mode.
Definition: spritecache.h:27
@ SCCF_ALLOW_ZOOM_MIN_1X_PAL
Allow use of sprite min zoom setting at 1x in palette mode.
Definition: spritecache.h:26
@ SCCF_ALLOW_ZOOM_MIN_2X_PAL
Allow use of sprite min zoom setting at 2x in palette mode.
Definition: spritecache.h:28
SpriteFile * GetOriginFile(SpriteID sprite)
Get the SpriteFile of a given sprite.
uint GetMaxSpriteID()
Get a reasonable (upper bound) estimate of the maximum SpriteID used in OpenTTD; there will be no spr...
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.
void * GetRawSprite(SpriteID sprite, SpriteType type, SpriteAllocator *allocator=nullptr, SpriteEncoder *encoder=nullptr)
Reads a sprite (from disk or sprite cache).
Base for loading sprites.
Data structure describing a sprite.
Definition: spritecache.h:17
uint16_t width
Width of the sprite.
Definition: spritecache.h:19
uint16_t height
Height of the sprite.
Definition: spritecache.h:18
int16_t y_offs
Number of pixels to shift the sprite downwards.
Definition: spritecache.h:21
uint8_t data[]
Sprite data.
Definition: spritecache.h:22
int16_t x_offs
Number of pixels to shift the sprite to the right.
Definition: spritecache.h:20