OpenTTD Source 20260621-master-g720d10536d
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef SPRITELOADER_HPP
11#define SPRITELOADER_HPP
12
14#include "../core/enum_type.hpp"
15#include "../gfx_type.h"
16#include "../spritecache_type.h"
17#include "sprite_file_type.hpp"
18
19struct Sprite;
20
22enum class SpriteComponent : uint8_t {
23 RGB = 0,
24 Alpha = 1,
25 Palette = 2,
27};
28
31
35template <class T>
38public:
39 inline constexpr T &operator[](const ZoomLevel &zoom) { return this->data[zoom]; }
40 inline constexpr const T &operator[](const ZoomLevel &zoom) const { return this->data[zoom]; }
41
42 T &Root() { return this->data[ZoomLevel::Min]; }
43 const T &Root() const { return this->data[ZoomLevel::Min]; }
44};
45
48public:
50 struct CommonPixel {
51 uint8_t r = 0;
52 uint8_t g = 0;
53 uint8_t b = 0;
54 uint8_t a = 0;
55 uint8_t m = 0;
56 };
57
64 struct Sprite {
65 uint16_t height;
66 uint16_t width;
67 int16_t x_offs;
68 int16_t y_offs;
71
77 void AllocateData(ZoomLevel zoom, size_t size) { this->data = Sprite::buffer[zoom].ZeroAllocate(size); }
78 private:
81 };
82
87
100 virtual ZoomLevels LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, SpriteCacheCtrlFlags control_flags, ZoomLevels &avail_8bpp, ZoomLevels &avail_32bpp) = 0;
101
103 virtual ~SpriteLoader() = default;
104};
105
108public:
110 virtual ~SpriteAllocator() = default;
111
118 template <typename T>
119 T *Allocate(size_t size)
120 {
121 return static_cast<T *>(this->AllocatePtr(size));
122 }
123
124protected:
130 virtual void *AllocatePtr(size_t size) = 0;
131};
132
135public:
136
138 virtual ~SpriteEncoder() = default;
139
144 virtual bool Is32BppSupported() = 0;
145
153 virtual Sprite *Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) = 0;
154
159 virtual uint GetSpriteAlignment()
160 {
161 return 0;
162 }
163};
164#endif /* SPRITELOADER_HPP */
Helper types related to the allocation of memory.
Enum-as-bit-set wrapper.
Interface for something that can allocate memory for a sprite.
virtual void * AllocatePtr(size_t size)=0
Allocate memory for a sprite.
virtual ~SpriteAllocator()=default
Ensure the destructor of the sub classes are called as well.
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.
virtual ~SpriteEncoder()=default
Ensure the destructor of the sub classes are called as well.
RandomAccessFile with some extra information specific for sprite files.
Interface for the loader of our sprites.
virtual ~SpriteLoader()=default
Ensure the destructor of the sub classes are called as well.
SpriteCollMap< Sprite > SpriteCollection
Type defining a collection of sprites, one for each zoom level.
virtual ZoomLevels LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, SpriteCacheCtrlFlags 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.
#define T
Climate temperate.
Definition engines.h:91
Type (helpers) for enums.
EnumClassIndexContainer< std::array< T, to_underlying(N)>, Index > EnumIndexArray
A typedef for EnumClassIndexContainer using std::array as the backing container type.
Types related to the graphics and/or input devices.
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:405
Random Access File specialised for accessing sprites.
Types related to the sprite cache.
EnumBitSet< SpriteCacheCtrlFlag, uint8_t > SpriteCacheCtrlFlags
Bitset of SpriteCacheCtrlFlag elements.
SpriteComponent
The different colour components a sprite can have.
@ Alpha
Sprite has alpha.
@ End
End marker.
@ RGB
Sprite has RGB.
EnumBitSet< SpriteComponent, uint8_t, SpriteComponent::End > SpriteComponents
Bitset of SpriteComponent elements.
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.
EnumBitSet< ZoomLevel, uint8_t > ZoomLevels
Bitset of ZoomLevel elements.
Definition zoom_type.h:51
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:20
@ Min
Minimum zoom level.
Definition zoom_type.h:23