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/>.
8 /** @file spritecache.h Functions to cache sprites in memory. */
14 #include "spriteloader/spriteloader.hpp"
16 /** Data structure describing a sprite. */
18 uint16_t height
; ///< Height of the sprite.
19 uint16_t width
; ///< Width of the sprite.
20 int16_t x_offs
; ///< Number of pixels to shift the sprite to the right.
21 int16_t y_offs
; ///< Number of pixels to shift the sprite downwards.
22 uint8_t data
[]; ///< Sprite data.
25 enum SpriteCacheCtrlFlags
{
26 SCCF_ALLOW_ZOOM_MIN_1X_PAL
= 0, ///< Allow use of sprite min zoom setting at 1x in palette mode.
27 SCCF_ALLOW_ZOOM_MIN_1X_32BPP
= 1, ///< Allow use of sprite min zoom setting at 1x in 32bpp mode.
28 SCCF_ALLOW_ZOOM_MIN_2X_PAL
= 2, ///< Allow use of sprite min zoom setting at 2x in palette mode.
29 SCCF_ALLOW_ZOOM_MIN_2X_32BPP
= 3, ///< Allow use of sprite min zoom setting at 2x in 32bpp mode.
32 extern uint _sprite_cache_size
;
34 /** SpriteAllocate that uses malloc to allocate memory. */
35 class SimpleSpriteAllocator
: public SpriteAllocator
{
37 void *AllocatePtr(size_t size
) override
;
40 /** SpriteAllocator that allocates memory via a unique_ptr array. */
41 class UniquePtrSpriteAllocator
: public SpriteAllocator
{
43 std::unique_ptr
<uint8_t[]> data
;
45 void *AllocatePtr(size_t size
) override
;
48 void *GetRawSprite(SpriteID sprite
, SpriteType type
, SpriteAllocator
*allocator
= nullptr, SpriteEncoder
*encoder
= nullptr);
49 bool SpriteExists(SpriteID sprite
);
51 SpriteType
GetSpriteType(SpriteID sprite
);
52 SpriteFile
*GetOriginFile(SpriteID sprite
);
53 uint32_t GetSpriteLocalID(SpriteID sprite
);
54 uint
GetSpriteCountForFile(const std::string
&filename
, SpriteID begin
, SpriteID end
);
55 uint
GetMaxSpriteID();
58 inline const Sprite
*GetSprite(SpriteID sprite
, SpriteType type
)
60 assert(type
!= SpriteType::Recolour
);
61 return (Sprite
*)GetRawSprite(sprite
, type
);
64 inline const uint8_t *GetNonSprite(SpriteID sprite
, SpriteType type
)
66 assert(type
== SpriteType::Recolour
);
67 return (uint8_t*)GetRawSprite(sprite
, type
);
70 void GfxInitSpriteMem();
71 void GfxClearSpriteCache();
72 void GfxClearFontSpriteCache();
73 void IncreaseSpriteLRU();
75 SpriteFile
&OpenCachedSpriteFile(const std::string
&filename
, Subdirectory subdir
, bool palette_remap
);
76 std::span
<const std::unique_ptr
<SpriteFile
>> GetCachedSpriteFiles();
78 void ReadGRFSpriteOffsets(SpriteFile
&file
);
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
);
84 #endif /* SPRITECACHE_H */