Luanti 5.15.0-dev
 
Loading...
Searching...
No Matches
tile.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "irrlichttypes.h"
8#include <ITexture.h>
9#include <vector>
10#include <SMaterial.h>
11
27
32{
33 switch (type) {
38 default:
39 return type;
40 }
41}
42
43// Material flags
44// Should backface culling be enabled?
45#define MATERIAL_FLAG_BACKFACE_CULLING 0x01
46// Should a crack be drawn?
47#define MATERIAL_FLAG_CRACK 0x02
48// Does this layer have texture animation?
49#define MATERIAL_FLAG_ANIMATION 0x08
50#define MATERIAL_FLAG_TILEABLE_HORIZONTAL 0x20
51#define MATERIAL_FLAG_TILEABLE_VERTICAL 0x40
52
53/*
54 This fully defines the looks of a tile.
55 The SMaterial of a tile is constructed according to this.
56*/
57
59{
60 FrameSpec() = default;
61
62 u32 texture_id = 0;
63 video::ITexture *texture = nullptr;
64};
65
71#define MAX_TILE_LAYERS 2
72
75{
76 TileLayer() = default;
77
81 bool operator==(const TileLayer &other) const
82 {
83 return
84 texture_id == other.texture_id &&
85 shader_id == other.shader_id &&
87 has_color == other.has_color &&
88 color == other.color &&
90 // texture_layer_idx and scale are notably part of the vertex data
91 }
92
96 bool operator!=(const TileLayer &other) const
97 {
98 return !(*this == other);
99 }
100
107 void applyMaterialOptions(video::SMaterial &material, int layer) const;
108
110 bool empty() const
111 {
112 return !shader_id && !texture_id;
113 }
114
116 bool isTransparent() const
117 {
118 // see also: the mapping in ShaderSource::generateShader()
119 switch (material_type) {
124 return true;
125 default:
126 return false;
127 }
128 }
129
130 // Ordered for size, please do not reorder
131
132 video::ITexture *texture = nullptr;
133
134 u32 shader_id = 0;
135
136 u32 texture_id = 0;
137
140
143
149
151 u8 scale = 1;
152
156
158 std::vector<FrameSpec> *frames = nullptr;
159
164 video::SColor color;
165
167 bool has_color = false;
168};
169
170template<>
171struct std::hash<TileLayer>
172{
173 // All layers equal according to TileLayer::operator== will have the same
174 // hash value according to this function.
175 std::size_t operator()(const TileLayer &l) const noexcept
176 {
177 std::size_t ret = 0;
178 for (auto h : { l.texture_id, l.shader_id, (u32)l.material_flags }) {
179 ret += h;
180 ret ^= (ret << 6) + (ret >> 2); // distribute bits
181 }
182 return ret;
183 }
184};
185
186
187// Stores information for drawing an animated tile
189
190 AnimationInfo() = default;
191
193 m_frame_length_ms(tile.animation_frame_length_ms),
194 m_frame_count(tile.animation_frame_count),
195 m_frames(tile.frames)
196 {}
197
198 AnimationInfo(std::vector<FrameSpec> *frames, u16 frame_length_ms) :
199 m_frame_length_ms(frame_length_ms),
200 m_frame_count(frames->size()),
201 m_frames(frames)
202 {}
203
204 size_t getFrameCount() const
205 {
206 return m_frames ? m_frame_count : 0;
207 }
208
209 void updateTexture(video::SMaterial &material, float animation_time);
210
211 // Returns nullptr if texture did not change since last time
212 video::ITexture *getTexture(float animation_time) const;
213
214private:
217
219 std::vector<FrameSpec> *m_frames = nullptr;
220};
221
222enum class TileRotation: u8 {
223 None,
224 R90,
225 R180,
226 R270,
227};
228
Definition tile.h:188
size_t getFrameCount() const
Definition tile.h:204
AnimationInfo(const TileLayer &tile)
Definition tile.h:192
AnimationInfo()=default
u16 m_frame_length_ms
Definition tile.h:215
std::vector< FrameSpec > * m_frames
Definition tile.h:219
u16 m_frame_count
Definition tile.h:216
AnimationInfo(std::vector< FrameSpec > *frames, u16 frame_length_ms)
Definition tile.h:198
void updateTexture(video::SMaterial &material, float animation_time)
Definition tile.cpp:21
video::ITexture * getTexture(float animation_time) const
Definition tile.cpp:8
Definition tile.h:59
video::ITexture * texture
Definition tile.h:63
u32 texture_id
Definition tile.h:62
FrameSpec()=default
Defines a layer of a tile.
Definition tile.h:75
video::SColor color
Definition tile.h:164
bool operator==(const TileLayer &other) const
Definition tile.h:81
std::vector< FrameSpec > * frames
Definition tile.h:158
bool has_color
If true, the tile has its own color.
Definition tile.h:167
u8 scale
Texture scale in both directions (used for world-align)
Definition tile.h:151
MaterialType material_type
Definition tile.h:144
TileLayer()=default
u16 animation_frame_count
Definition tile.h:139
u8 material_flags
Definition tile.h:145
u16 texture_layer_idx
Layer index to use, if the texture is an array texture.
Definition tile.h:142
bool empty() const
Definition tile.h:110
bool need_polygon_offset
does this tile need to have a positive polygon offset set?
Definition tile.h:155
video::ITexture * texture
Definition tile.h:132
void applyMaterialOptions(video::SMaterial &material, int layer) const
Set some material parameters accordingly.
Definition tile.cpp:29
u32 shader_id
Definition tile.h:134
u16 animation_frame_length_ms
Definition tile.h:138
bool isTransparent() const
Definition tile.h:116
u32 texture_id
Definition tile.h:136
bool operator!=(const TileLayer &other) const
Definition tile.h:96
Definition tile.h:233
TileSpec()=default
TileRotation rotation
Tile rotation.
Definition tile.h:239
bool world_aligned
If true, the tile rotation is ignored.
Definition tile.h:237
TileLayer layers[MAX_TILE_LAYERS]
The first is base texture, the second is overlay.
Definition tile.h:241
std::size_t operator()(const TileLayer &l) const noexcept
Definition tile.h:175
#define MATERIAL_FLAG_BACKFACE_CULLING
Definition tile.h:45
MaterialType
Definition tile.h:12
@ TILE_MATERIAL_PLAIN_ALPHA
Definition tile.h:25
@ TILE_MATERIAL_OPAQUE
Definition tile.h:19
@ TILE_MATERIAL_LIQUID_TRANSPARENT
Definition tile.h:15
@ TILE_MATERIAL_WAVING_PLANTS
Definition tile.h:18
@ TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT
Definition tile.h:21
@ TILE_MATERIAL_BASIC
Definition tile.h:13
@ TILE_MATERIAL_LIQUID_OPAQUE
Definition tile.h:16
@ TILE_MATERIAL_WAVING_LIQUID_OPAQUE
Definition tile.h:22
@ TILE_MATERIAL_WAVING_LIQUID_BASIC
Definition tile.h:20
@ TILE_MATERIAL_WAVING_LEAVES
Definition tile.h:17
@ TILE_MATERIAL_ALPHA
Definition tile.h:14
@ TILE_MATERIAL_PLAIN
Definition tile.h:24
#define MATERIAL_FLAG_TILEABLE_VERTICAL
Definition tile.h:51
static MaterialType material_type_with_alpha(MaterialType type)
change type so it has at least simple transparency
Definition tile.h:31
TileRotation
Definition tile.h:222
#define MATERIAL_FLAG_TILEABLE_HORIZONTAL
Definition tile.h:50
#define MAX_TILE_LAYERS
We have two tile layers: layer 0 = base layer 1 = overlay.
Definition tile.h:71