OpenTTD Source  20241108-master-g80f628063a
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 
18 struct Sprite;
19 
22  SCC_RGB = 1 << 0,
23  SCC_ALPHA = 1 << 1,
24  SCC_PAL = 1 << 2,
26 };
28 
29 
30 class SpriteLoader {
31 public:
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;
55 
61  void AllocateData(ZoomLevel zoom, size_t size) { this->data = Sprite::buffer[zoom].ZeroAllocate(size); }
62  private:
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 
89 public:
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 
104 protected:
110  virtual void *AllocatePtr(size_t size) = 0;
111 };
112 
115 public:
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 */
Interface for something that can allocate memory for a sprite.
T * Allocate(size_t size)
Allocate memory for a sprite.
virtual void * AllocatePtr(size_t size)=0
Allocate memory for a sprite.
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(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.
std::array< Sprite, ZOOM_LVL_END > SpriteCollection
Type defining a collection of sprites, one for each zoom level.
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(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
SpriteType
Types of sprites that might be loaded.
Definition: gfx_type.h:312
Random Access File specialised for accessing sprites.
SpriteColourComponent
The different colour components a sprite can have.
@ SCC_MASK
Mask of valid colour bits.
@ SCC_ALPHA
Sprite has alpha.
@ SCC_PAL
Sprite has palette data.
@ SCC_RGB
Sprite has RGB.
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.
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
SpriteColourComponent colours
The colour components of the sprite with useful information.
int16_t x_offs
The x-offset of where the sprite will be drawn.
SpriteLoader::CommonPixel * data
The sprite itself.
SpriteType type
The sprite type.
int16_t y_offs
The y-offset of where the sprite will be drawn.
Data structure describing a sprite.
Definition: spritecache.h:17
uint16_t width
Width of the sprite.
Definition: spritecache.h:19
uint16_t height
Height of the sprite.
Definition: spritecache.h:18
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:16
@ ZOOM_LVL_END
End for iteration.
Definition: zoom_type.h:25