OpenTTD Source: src/spriteloader/spriteloader.hpp Source File

OpenTTD Source 15.3

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
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 "../spritecache_type.h"
17#include "sprite_file_type.hpp"
18
19struct Sprite;
20
22 enum class SpriteComponent : uint8_t {
23 RGB = 0,
24 Alpha = 1,
25 Palette = 2,
26 End,
27};
28using SpriteComponents = EnumBitSet<SpriteComponent, uint8_t, SpriteComponent::End>;
29
33template <class T>
34 class SpriteCollMap {
35 std::array<T, to_underlying(ZoomLevel::End)> data{};
36public:
37 inline constexpr T &operator[](const ZoomLevel &zoom) { return this->data[to_underlying(zoom)]; }
38 inline constexpr const T &operator[](const ZoomLevel &zoom) const { return this->data[to_underlying(zoom)]; }
39
40 T &Root() { return this->data[to_underlying(ZoomLevel::Min)]; }
41 const T &Root() const { return this->data[to_underlying(ZoomLevel::Min)]; }
42};
43
45 class SpriteLoader {
46public:
48 struct CommonPixel {
49 uint8_t r = 0;
50 uint8_t g = 0;
51 uint8_t b = 0;
52 uint8_t a = 0;
53 uint8_t m = 0;
54 };
55
62 struct Sprite {
63 uint16_t height;
64 uint16_t width;
65 int16_t x_offs;
66 int16_t y_offs;
67 SpriteComponents colours;
68 SpriteLoader::CommonPixel *data;
69
75 void AllocateData(ZoomLevel zoom, size_t size) { this->data = Sprite::buffer[zoom].ZeroAllocate(size); }
76 private:
78 static SpriteCollMap<ReusableBuffer<SpriteLoader::CommonPixel>> buffer;
79 };
80
84 using SpriteCollection = SpriteCollMap<Sprite>;
85
98 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;
99
100 virtual ~SpriteLoader() = default;
101};
102
104 class SpriteAllocator {
105public:
106 virtual ~SpriteAllocator() = default;
107
114 template <typename T>
115 T *Allocate(size_t size)
116 {
117 return static_cast<T *>(this->AllocatePtr(size));
118 }
119
120protected:
126 virtual void *AllocatePtr(size_t size) = 0;
127};
128
130 class SpriteEncoder {
131public:
132
133 virtual ~SpriteEncoder() = default;
134
138 virtual bool Is32BppSupported() = 0;
139
143 virtual Sprite *Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) = 0;
144
149 virtual uint GetSpriteAlignment()
150 {
151 return 0;
152 }
153};
154#endif /* SPRITELOADER_HPP */
EnumBitSet< SpriteComponent, uint8_t, SpriteComponent::End >
SpriteAllocator
Interface for something that can allocate memory for a sprite.
Definition spriteloader.hpp:104
SpriteAllocator::AllocatePtr
virtual void * AllocatePtr(size_t size)=0
Allocate memory for a sprite.
SpriteAllocator::Allocate
T * Allocate(size_t size)
Allocate memory for a sprite.
Definition spriteloader.hpp:115
SpriteCollMap
Map zoom level to data.
Definition spriteloader.hpp:34
SpriteEncoder
Interface for something that can encode a sprite.
Definition spriteloader.hpp:130
SpriteEncoder::Is32BppSupported
virtual bool Is32BppSupported()=0
Can the sprite encoder make use of RGBA sprites?
SpriteEncoder::GetSpriteAlignment
virtual uint GetSpriteAlignment()
Get the value which the height and width on a sprite have to be aligned by.
Definition spriteloader.hpp:149
SpriteEncoder::Encode
virtual Sprite * Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator)=0
Convert a sprite from the loader to our own format.
SpriteFile
RandomAccessFile with some extra information specific for sprite files.
Definition sprite_file_type.hpp:19
SpriteLoader
Interface for the loader of our sprites.
Definition spriteloader.hpp:45
SpriteLoader::LoadSprite
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.
CommandType::End
@ End
End marker.
to_underlying
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
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:357
sprite_file_type.hpp
Random Access File specialised for accessing sprites.
SpriteComponent
SpriteComponent
The different colour components a sprite can have.
Definition spriteloader.hpp:22
SpriteComponent::Alpha
@ Alpha
Sprite has alpha.
SpriteComponent::RGB
@ RGB
Sprite has RGB.
Palette
Information about the currently used palette.
Definition gfx_type.h:373
SpriteLoader::CommonPixel
Definition of a common pixel in OpenTTD's realm.
Definition spriteloader.hpp:48
SpriteLoader::CommonPixel::m
uint8_t m
Remap-channel.
Definition spriteloader.hpp:53
SpriteLoader::CommonPixel::b
uint8_t b
Blue-channel.
Definition spriteloader.hpp:51
SpriteLoader::CommonPixel::r
uint8_t r
Red-channel.
Definition spriteloader.hpp:49
SpriteLoader::CommonPixel::g
uint8_t g
Green-channel.
Definition spriteloader.hpp:50
SpriteLoader::CommonPixel::a
uint8_t a
Alpha-channel.
Definition spriteloader.hpp:52
SpriteLoader::Sprite
Structure for passing information from the sprite loader to the blitter.
Definition spriteloader.hpp:62
SpriteLoader::Sprite::buffer
static SpriteCollMap< ReusableBuffer< SpriteLoader::CommonPixel > > buffer
Allocated memory to pass sprite data around.
Definition spriteloader.hpp:78
SpriteLoader::Sprite::colours
SpriteComponents colours
The colour components of the sprite with useful information.
Definition spriteloader.hpp:67
SpriteLoader::Sprite::AllocateData
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
Definition spriteloader.hpp:75
SpriteLoader::Sprite::width
uint16_t width
Width of the sprite.
Definition spriteloader.hpp:64
SpriteLoader::Sprite::x_offs
int16_t x_offs
The x-offset of where the sprite will be drawn.
Definition spriteloader.hpp:65
SpriteLoader::Sprite::data
SpriteLoader::CommonPixel * data
The sprite itself.
Definition spriteloader.hpp:68
SpriteLoader::Sprite::height
uint16_t height
Height of the sprite.
Definition spriteloader.hpp:63
SpriteLoader::Sprite::y_offs
int16_t y_offs
The y-offset of where the sprite will be drawn.
Definition spriteloader.hpp:66
Sprite
Data structure describing a sprite.
Definition spritecache_type.h:16
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:20
ZoomLevel::Min
@ Min
Minimum zoom level.
ZoomLevel::End
@ End
End for iteration.