]> git.lizzy.rs Git - minetest.git/blob - src/client/tile.h
Add colored text (not only colored chat).
[minetest.git] / src / client / tile.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef TILE_HEADER
21 #define TILE_HEADER
22
23 #include "irrlichttypes.h"
24 #include "irr_v3d.h"
25 #include <ITexture.h>
26 #include <IrrlichtDevice.h>
27 #include "threads.h"
28 #include <string>
29 #include <vector>
30 #include "util/numeric.h"
31
32 class IGameDef;
33 struct TileSpec;
34 struct TileDef;
35
36 /*
37         tile.{h,cpp}: Texture handling stuff.
38 */
39
40 /*
41         Find out the full path of an image by trying different filename
42         extensions.
43
44         If failed, return "".
45
46         TODO: Should probably be moved out from here, because things needing
47               this function do not need anything else from this header
48 */
49 std::string getImagePath(std::string path);
50
51 /*
52         Gets the path to a texture by first checking if the texture exists
53         in texture_path and if not, using the data path.
54
55         Checks all supported extensions by replacing the original extension.
56
57         If not found, returns "".
58
59         Utilizes a thread-safe cache.
60 */
61 std::string getTexturePath(const std::string &filename);
62
63 void clearTextureNameCache();
64
65 /*
66         ITextureSource::generateTextureFromMesh parameters
67 */
68 namespace irr {namespace scene {class IMesh;}}
69 struct TextureFromMeshParams
70 {
71         scene::IMesh *mesh;
72         core::dimension2d<u32> dim;
73         std::string rtt_texture_name;
74         bool delete_texture_on_shutdown;
75         v3f camera_position;
76         v3f camera_lookat;
77         core::CMatrix4<f32> camera_projection_matrix;
78         video::SColorf ambient_light;
79         v3f light_position;
80         video::SColorf light_color;
81         f32 light_radius;
82 };
83
84 /*
85         TextureSource creates and caches textures.
86 */
87
88 class ISimpleTextureSource
89 {
90 public:
91         ISimpleTextureSource(){}
92         virtual ~ISimpleTextureSource(){}
93         virtual video::ITexture* getTexture(
94                         const std::string &name, u32 *id = NULL) = 0;
95 };
96
97 class ITextureSource : public ISimpleTextureSource
98 {
99 public:
100         ITextureSource(){}
101         virtual ~ITextureSource(){}
102         virtual u32 getTextureId(const std::string &name)=0;
103         virtual std::string getTextureName(u32 id)=0;
104         virtual video::ITexture* getTexture(u32 id)=0;
105         virtual video::ITexture* getTexture(
106                         const std::string &name, u32 *id = NULL)=0;
107         virtual video::ITexture* getTextureForMesh(
108                         const std::string &name, u32 *id = NULL) = 0;
109         virtual IrrlichtDevice* getDevice()=0;
110         virtual bool isKnownSourceImage(const std::string &name)=0;
111         virtual video::ITexture* generateTextureFromMesh(
112                         const TextureFromMeshParams &params)=0;
113         virtual video::ITexture* getNormalTexture(const std::string &name)=0;
114         virtual video::SColor getTextureAverageColor(const std::string &name)=0;
115         virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
116 };
117
118 class IWritableTextureSource : public ITextureSource
119 {
120 public:
121         IWritableTextureSource(){}
122         virtual ~IWritableTextureSource(){}
123         virtual u32 getTextureId(const std::string &name)=0;
124         virtual std::string getTextureName(u32 id)=0;
125         virtual video::ITexture* getTexture(u32 id)=0;
126         virtual video::ITexture* getTexture(
127                         const std::string &name, u32 *id = NULL)=0;
128         virtual IrrlichtDevice* getDevice()=0;
129         virtual bool isKnownSourceImage(const std::string &name)=0;
130         virtual video::ITexture* generateTextureFromMesh(
131                         const TextureFromMeshParams &params)=0;
132
133         virtual void processQueue()=0;
134         virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
135         virtual void rebuildImagesAndTextures()=0;
136         virtual video::ITexture* getNormalTexture(const std::string &name)=0;
137         virtual video::SColor getTextureAverageColor(const std::string &name)=0;
138         virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
139 };
140
141 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
142
143 #ifdef __ANDROID__
144 video::IImage * Align2Npot2(video::IImage * image, video::IVideoDriver* driver);
145 #endif
146
147 enum MaterialType{
148         TILE_MATERIAL_BASIC,
149         TILE_MATERIAL_ALPHA,
150         TILE_MATERIAL_LIQUID_TRANSPARENT,
151         TILE_MATERIAL_LIQUID_OPAQUE,
152         TILE_MATERIAL_WAVING_LEAVES,
153         TILE_MATERIAL_WAVING_PLANTS
154 };
155
156 // Material flags
157 // Should backface culling be enabled?
158 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
159 // Should a crack be drawn?
160 #define MATERIAL_FLAG_CRACK 0x02
161 // Should the crack be drawn on transparent pixels (unset) or not (set)?
162 // Ignored if MATERIAL_FLAG_CRACK is not set.
163 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
164 // Animation made up by splitting the texture to vertical frames, as
165 // defined by extra parameters
166 #define MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES 0x08
167 #define MATERIAL_FLAG_HIGHLIGHTED 0x10
168 #define MATERIAL_FLAG_TILEABLE_HORIZONTAL 0x20
169 #define MATERIAL_FLAG_TILEABLE_VERTICAL 0x40
170
171 /*
172         This fully defines the looks of a tile.
173         The SMaterial of a tile is constructed according to this.
174 */
175 struct FrameSpec
176 {
177         FrameSpec():
178                 texture_id(0),
179                 texture(NULL),
180                 normal_texture(NULL),
181                 flags_texture(NULL)
182         {
183         }
184         u32 texture_id;
185         video::ITexture *texture;
186         video::ITexture *normal_texture;
187         video::ITexture *flags_texture;
188 };
189
190 struct TileSpec
191 {
192         TileSpec():
193                 texture_id(0),
194                 texture(NULL),
195                 normal_texture(NULL),
196                 flags_texture(NULL),
197                 alpha(255),
198                 material_type(TILE_MATERIAL_BASIC),
199                 material_flags(
200                         //0 // <- DEBUG, Use the one below
201                         MATERIAL_FLAG_BACKFACE_CULLING
202                 ),
203                 shader_id(0),
204                 animation_frame_count(1),
205                 animation_frame_length_ms(0),
206                 rotation(0)
207         {
208         }
209
210         bool operator==(const TileSpec &other) const
211         {
212                 return (
213                         texture_id == other.texture_id &&
214                         /* texture == other.texture && */
215                         alpha == other.alpha &&
216                         material_type == other.material_type &&
217                         material_flags == other.material_flags &&
218                         rotation == other.rotation
219                 );
220         }
221
222         bool operator!=(const TileSpec &other) const
223         {
224                 return !(*this == other);
225         }
226         
227         // Sets everything else except the texture in the material
228         void applyMaterialOptions(video::SMaterial &material) const
229         {
230                 switch (material_type) {
231                 case TILE_MATERIAL_BASIC:
232                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
233                         break;
234                 case TILE_MATERIAL_ALPHA:
235                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
236                         break;
237                 case TILE_MATERIAL_LIQUID_TRANSPARENT:
238                         material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
239                         break;
240                 case TILE_MATERIAL_LIQUID_OPAQUE:
241                         material.MaterialType = video::EMT_SOLID;
242                         break;
243                 case TILE_MATERIAL_WAVING_LEAVES:
244                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
245                         break;
246                 case TILE_MATERIAL_WAVING_PLANTS:
247                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
248                         break;
249                 }
250                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
251                         ? true : false;
252                 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
253                         material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
254                 }
255                 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
256                         material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
257                 }
258         }
259
260         void applyMaterialOptionsWithShaders(video::SMaterial &material) const
261         {
262                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
263                         ? true : false;
264                 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
265                         material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
266                         material.TextureLayer[1].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
267                 }
268                 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
269                         material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
270                         material.TextureLayer[1].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
271                 }
272         }
273         
274         u32 texture_id;
275         video::ITexture *texture;
276         video::ITexture *normal_texture;
277         video::ITexture *flags_texture;
278         
279         // Vertex alpha (when MATERIAL_ALPHA_VERTEX is used)
280         u8 alpha;
281         // Material parameters
282         u8 material_type;
283         u8 material_flags;
284         u32 shader_id;
285         // Animation parameters
286         u8 animation_frame_count;
287         u16 animation_frame_length_ms;
288         std::vector<FrameSpec> frames;
289
290         u8 rotation;
291 };
292 #endif