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

OpenTTD Source 15.0-beta1

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
21 enum SpriteColourComponent {
22 SCC_RGB = 1 << 0,
23 SCC_ALPHA = 1 << 1,
24 SCC_PAL = 1 << 2,
25 SCC_MASK = SCC_RGB | SCC_ALPHA | SCC_PAL,
26};
27 DECLARE_ENUM_AS_BIT_SET(SpriteColourComponent)
28
29
30class SpriteLoader {
31public:
33 struct CommonPixel {
34 uint8_t r;
35 uint8_t g;
36 uint8_t b;
37 uint8_t a;
38 uint8_t m;
39 };
40
47 struct Sprite {
48 uint16_t height;
49 uint16_t width;
50 int16_t x_offs;
51 int16_t y_offs;
52 SpriteType type;
53 SpriteColourComponent colours;
54 SpriteLoader::CommonPixel *data;
55
61 void AllocateData(ZoomLevel zoom, size_t size) { this->data = Sprite::buffer[zoom].ZeroAllocate(size); }
62 private:
64 static ReusableBuffer<SpriteLoader::CommonPixel> buffer[ZOOM_LVL_END];
65 };
66
70 using SpriteCollection = std::array<Sprite, ZOOM_LVL_END>;
71
82 virtual uint8_t LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, uint8_t control_flags) = 0;
83
84 virtual ~SpriteLoader() = default;
85};
86
88 class SpriteAllocator {
89public:
90 virtual ~SpriteAllocator() = default;
91
98 template <typename T>
99 T *Allocate(size_t size)
100 {
101 return static_cast<T *>(this->AllocatePtr(size));
102 }
103
104protected:
110 virtual void *AllocatePtr(size_t size) = 0;
111};
112
114 class SpriteEncoder {
115public:
116
117 virtual ~SpriteEncoder() = default;
118
122 virtual bool Is32BppSupported() = 0;
123
127 virtual Sprite *Encode(const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) = 0;
128
133 virtual uint GetSpriteAlignment()
134 {
135 return 0;
136 }
137};
138#endif /* SPRITELOADER_HPP */
ReusableBuffer
A reusable buffer that can be used for places that temporary allocate a bit of memory and do that ver...
Definition alloc_type.hpp:24
SpriteAllocator
Interface for something that can allocate memory for a sprite.
Definition spriteloader.hpp:88
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:99
SpriteEncoder
Interface for something that can encode a sprite.
Definition spriteloader.hpp:114
SpriteEncoder::Encode
virtual Sprite * Encode(const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator)=0
Convert a sprite from the loader to our own format.
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:133
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:30
SpriteLoader::SpriteCollection
std::array< Sprite, ZOOM_LVL_END > SpriteCollection
Type defining a collection of sprites, one for each zoom level.
Definition spriteloader.hpp:70
SpriteLoader::LoadSprite
virtual uint8_t LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, uint8_t control_flags)=0
Load a sprite from the disk and return a sprite struct which is the same for all loaders.
DECLARE_ENUM_AS_BIT_SET
#define DECLARE_ENUM_AS_BIT_SET(enum_type)
Operators to allow to work with enum as with type safe bit set in C++.
Definition enum_type.hpp:35
SpriteType
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:312
sprite_file_type.hpp
Random Access File specialised for accessing sprites.
SpriteColourComponent
SpriteColourComponent
The different colour components a sprite can have.
Definition spriteloader.hpp:21
SCC_MASK
@ SCC_MASK
Mask of valid colour bits.
Definition spriteloader.hpp:25
SCC_ALPHA
@ SCC_ALPHA
Sprite has alpha.
Definition spriteloader.hpp:23
SCC_PAL
@ SCC_PAL
Sprite has palette data.
Definition spriteloader.hpp:24
SCC_RGB
@ SCC_RGB
Sprite has RGB.
Definition spriteloader.hpp:22
SpriteLoader::CommonPixel
Definition of a common pixel in OpenTTD's realm.
Definition spriteloader.hpp:33
SpriteLoader::CommonPixel::m
uint8_t m
Remap-channel.
Definition spriteloader.hpp:38
SpriteLoader::CommonPixel::b
uint8_t b
Blue-channel.
Definition spriteloader.hpp:36
SpriteLoader::CommonPixel::r
uint8_t r
Red-channel.
Definition spriteloader.hpp:34
SpriteLoader::CommonPixel::g
uint8_t g
Green-channel.
Definition spriteloader.hpp:35
SpriteLoader::CommonPixel::a
uint8_t a
Alpha-channel.
Definition spriteloader.hpp:37
SpriteLoader::Sprite
Structure for passing information from the sprite loader to the blitter.
Definition spriteloader.hpp:47
SpriteLoader::Sprite::AllocateData
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
Definition spriteloader.hpp:61
SpriteLoader::Sprite::colours
SpriteColourComponent colours
The colour components of the sprite with useful information.
Definition spriteloader.hpp:53
SpriteLoader::Sprite::width
uint16_t width
Width of the sprite.
Definition spriteloader.hpp:49
SpriteLoader::Sprite::x_offs
int16_t x_offs
The x-offset of where the sprite will be drawn.
Definition spriteloader.hpp:50
SpriteLoader::Sprite::data
SpriteLoader::CommonPixel * data
The sprite itself.
Definition spriteloader.hpp:54
SpriteLoader::Sprite::height
uint16_t height
Height of the sprite.
Definition spriteloader.hpp:48
SpriteLoader::Sprite::type
SpriteType type
The sprite type.
Definition spriteloader.hpp:52
SpriteLoader::Sprite::y_offs
int16_t y_offs
The y-offset of where the sprite will be drawn.
Definition spriteloader.hpp:51
Sprite
Data structure describing a sprite.
Definition spritecache.h:17
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:16
ZOOM_LVL_END
@ ZOOM_LVL_END
End for iteration.
Definition zoom_type.h:25