OpenTTD Source 20250529-master-g10c159a79f
spriteloader.hpp
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 SPRITELOADER_HPP
11#define SPRITELOADER_HPP
12
13#include "../core/alloc_type.hpp"
14#include "../core/enum_type.hpp"
15#include "../gfx_type.h"
16#include "sprite_file_type.hpp"
17
18struct Sprite;
19
21enum class SpriteComponent : uint8_t {
22 RGB = 0,
23 Alpha = 1,
24 Palette = 2,
25 End,
26};
28
32template <class T>
34 std::array<T, to_underlying(ZoomLevel::End)> data{};
35public:
36 inline constexpr T &operator[](const ZoomLevel &zoom) { return this->data[to_underlying(zoom)]; }
37 inline constexpr const T &operator[](const ZoomLevel &zoom) const { return this->data[to_underlying(zoom)]; }
38
39 T &Root() { return this->data[to_underlying(ZoomLevel::Min)]; }
40 const T &Root() const { return this->data[to_underlying(ZoomLevel::Min)]; }
41};
42
45public:
47 struct CommonPixel {
48 uint8_t r = 0;
49 uint8_t g = 0;
50 uint8_t b = 0;
51 uint8_t a = 0;
52 uint8_t m = 0;
53 };
54
61 struct Sprite {
62 uint16_t height;
63 uint16_t width;
64 int16_t x_offs;
65 int16_t y_offs;
68
74 void AllocateData(ZoomLevel zoom, size_t size) { this->data = Sprite::buffer[zoom].ZeroAllocate(size); }
75 private:
78 };
79
84
97 virtual ZoomLevels LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, uint8_t control_flags, ZoomLevels &avail_8bpp, ZoomLevels &avail_32bpp) = 0;
98
99 virtual ~SpriteLoader() = default;
100};
101
104public:
105 virtual ~SpriteAllocator() = default;
106
113 template <typename T>
114 T *Allocate(size_t size)
115 {
116 return static_cast<T *>(this->AllocatePtr(size));
117 }
118
119protected:
125 virtual void *AllocatePtr(size_t size) = 0;
126};
127
130public:
131
132 virtual ~SpriteEncoder() = default;
133
137 virtual bool Is32BppSupported() = 0;
138
142 virtual Sprite *Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) = 0;
143
148 virtual uint GetSpriteAlignment()
149 {
150 return 0;
151 }
152};
153#endif /* SPRITELOADER_HPP */
Interface for something that can allocate memory for a sprite.
virtual void * AllocatePtr(size_t size)=0
Allocate memory for a sprite.
T * Allocate(size_t size)
Allocate memory for a sprite.
Map zoom level to data.
Interface for something that can encode a sprite.
virtual bool Is32BppSupported()=0
Can the sprite encoder make use of RGBA sprites?
virtual uint GetSpriteAlignment()
Get the value which the height and width on a sprite have to be aligned by.
virtual Sprite * Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator)=0
Convert a sprite from the loader to our own format.
RandomAccessFile with some extra information specific for sprite files.
Interface for the loader of our sprites.
virtual ZoomLevels LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, uint8_t control_flags, ZoomLevels &avail_8bpp, ZoomLevels &avail_32bpp)=0
Load a sprite from the disk and return a sprite struct which is the same for all loaders.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23)
Definition enum_type.hpp:17
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:352
Random Access File specialised for accessing sprites.
SpriteComponent
The different colour components a sprite can have.
@ Alpha
Sprite has alpha.
@ RGB
Sprite has RGB.
Information about the currently used palette.
Definition gfx_type.h:368
Definition of a common pixel in OpenTTD's realm.
uint8_t m
Remap-channel.
uint8_t b
Blue-channel.
uint8_t r
Red-channel.
uint8_t g
Green-channel.
uint8_t a
Alpha-channel.
Structure for passing information from the sprite loader to the blitter.
static SpriteCollMap< ReusableBuffer< SpriteLoader::CommonPixel > > buffer
Allocated memory to pass sprite data around.
SpriteComponents colours
The colour components of the sprite with useful information.
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
uint16_t width
Width of the sprite.
int16_t x_offs
The x-offset of where the sprite will be drawn.
SpriteLoader::CommonPixel * data
The sprite itself.
uint16_t height
Height of the sprite.
int16_t y_offs
The y-offset of where the sprite will be drawn.
Data structure describing a sprite.
Definition spritecache.h:17
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:16
@ Min
Minimum zoom level.
@ End
End for iteration.