]> git.lizzy.rs Git - dragonfireclient.git/blob - src/tile.h
Check minetest.hud_change() parameters on conversion (Fix #1714)
[dragonfireclient.git] / src / 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_v2d.h"
25 #include "irr_v3d.h"
26 #include <ITexture.h>
27 #include <IrrlichtDevice.h>
28 #include "threads.h"
29 #include <string>
30 #include <map>
31
32 class IGameDef;
33
34 /*
35         tile.{h,cpp}: Texture handling stuff.
36 */
37
38 /*
39         Find out the full path of an image by trying different filename
40         extensions.
41
42         If failed, return "".
43
44         TODO: Should probably be moved out from here, because things needing
45               this function do not need anything else from this header
46 */
47 std::string getImagePath(std::string path);
48
49 /*
50         Gets the path to a texture by first checking if the texture exists
51         in texture_path and if not, using the data path.
52
53         Checks all supported extensions by replacing the original extension.
54
55         If not found, returns "".
56
57         Utilizes a thread-safe cache.
58 */
59 std::string getTexturePath(const std::string &filename);
60
61 void clearTextureNameCache();
62
63 /*
64         ITextureSource::generateTextureFromMesh parameters
65 */
66 namespace irr {namespace scene {class IMesh;}}
67 struct TextureFromMeshParams
68 {
69         scene::IMesh *mesh;
70         core::dimension2d<u32> dim;
71         std::string rtt_texture_name;
72         bool delete_texture_on_shutdown;
73         v3f camera_position;
74         v3f camera_lookat;
75         core::CMatrix4<f32> camera_projection_matrix;
76         video::SColorf ambient_light;
77         v3f light_position;
78         video::SColorf light_color;
79         f32 light_radius;
80 };
81
82 /*
83         TextureSource creates and caches textures.
84 */
85
86 class ISimpleTextureSource
87 {
88 public:
89         ISimpleTextureSource(){}
90         virtual ~ISimpleTextureSource(){}
91         virtual video::ITexture* getTexture(
92                         const std::string &name, u32 *id = NULL) = 0;
93 };
94
95 class ITextureSource : public ISimpleTextureSource
96 {
97 public:
98         ITextureSource(){}
99         virtual ~ITextureSource(){}
100         virtual u32 getTextureId(const std::string &name)=0;
101         virtual std::string getTextureName(u32 id)=0;
102         virtual video::ITexture* getTexture(u32 id)=0;
103         virtual video::ITexture* getTexture(
104                         const std::string &name, u32 *id = NULL)=0;
105         virtual IrrlichtDevice* getDevice()=0;
106         virtual bool isKnownSourceImage(const std::string &name)=0;
107         virtual video::ITexture* generateTextureFromMesh(
108                         const TextureFromMeshParams &params)=0;
109         virtual video::ITexture* getNormalTexture(const std::string &name)=0;
110 };
111
112 class IWritableTextureSource : public ITextureSource
113 {
114 public:
115         IWritableTextureSource(){}
116         virtual ~IWritableTextureSource(){}
117         virtual u32 getTextureId(const std::string &name)=0;
118         virtual std::string getTextureName(u32 id)=0;
119         virtual video::ITexture* getTexture(u32 id)=0;
120         virtual video::ITexture* getTexture(
121                         const std::string &name, u32 *id = NULL)=0;
122         virtual IrrlichtDevice* getDevice()=0;
123         virtual bool isKnownSourceImage(const std::string &name)=0;
124         virtual video::ITexture* generateTextureFromMesh(
125                         const TextureFromMeshParams &params)=0;
126
127         virtual void processQueue()=0;
128         virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
129         virtual void rebuildImagesAndTextures()=0;
130         virtual video::ITexture* getNormalTexture(const std::string &name)=0;
131 };
132
133 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
134
135 #ifdef __ANDROID__
136 /**
137  * @param size get next npot2 value
138  * @return npot2 value
139  */
140 inline unsigned int npot2(unsigned int size)
141 {
142         if (size == 0) return 0;
143         unsigned int npot = 1;
144
145         while ((size >>= 1) > 0) {
146                 npot <<= 1;
147         }
148         return npot;
149 }
150
151 video::IImage * Align2Npot2(video::IImage * image, video::IVideoDriver* driver);
152 #endif
153
154 enum MaterialType{
155         TILE_MATERIAL_BASIC,
156         TILE_MATERIAL_ALPHA,
157         TILE_MATERIAL_LIQUID_TRANSPARENT,
158         TILE_MATERIAL_LIQUID_OPAQUE,
159         TILE_MATERIAL_WAVING_LEAVES,
160         TILE_MATERIAL_WAVING_PLANTS
161 };
162
163 // Material flags
164 // Should backface culling be enabled?
165 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
166 // Should a crack be drawn?
167 #define MATERIAL_FLAG_CRACK 0x02
168 // Should the crack be drawn on transparent pixels (unset) or not (set)?
169 // Ignored if MATERIAL_FLAG_CRACK is not set.
170 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
171 // Animation made up by splitting the texture to vertical frames, as
172 // defined by extra parameters
173 #define MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES 0x08
174 #define MATERIAL_FLAG_HIGHLIGHTED 0x10
175
176 /*
177         This fully defines the looks of a tile.
178         The SMaterial of a tile is constructed according to this.
179 */
180 struct FrameSpec
181 {
182         FrameSpec():
183                 texture_id(0),
184                 texture(NULL),
185                 normal_texture(NULL)
186         {
187         }
188         u32 texture_id;
189         video::ITexture *texture;
190         video::ITexture *normal_texture;
191 };
192
193 struct TileSpec
194 {
195         TileSpec():
196                 texture_id(0),
197                 texture(NULL),
198                 normal_texture(NULL),
199                 alpha(255),
200                 material_type(TILE_MATERIAL_BASIC),
201                 material_flags(
202                         //0 // <- DEBUG, Use the one below
203                         MATERIAL_FLAG_BACKFACE_CULLING
204                 ),
205                 shader_id(0),
206                 animation_frame_count(1),
207                 animation_frame_length_ms(0),
208                 rotation(0)
209         {
210         }
211
212         bool operator==(const TileSpec &other) const
213         {
214                 return (
215                         texture_id == other.texture_id &&
216                         /* texture == other.texture && */
217                         alpha == other.alpha &&
218                         material_type == other.material_type &&
219                         material_flags == other.material_flags &&
220                         rotation == other.rotation
221                 );
222         }
223
224         bool operator!=(const TileSpec &other) const
225         {
226                 return !(*this == other);
227         }
228         
229         // Sets everything else except the texture in the material
230         void applyMaterialOptions(video::SMaterial &material) const
231         {
232                 switch (material_type) {
233                 case TILE_MATERIAL_BASIC:
234                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
235                         break;
236                 case TILE_MATERIAL_ALPHA:
237                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
238                         break;
239                 case TILE_MATERIAL_LIQUID_TRANSPARENT:
240                         material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
241                         break;
242                 case TILE_MATERIAL_LIQUID_OPAQUE:
243                         material.MaterialType = video::EMT_SOLID;
244                         break;
245                 case TILE_MATERIAL_WAVING_LEAVES:
246                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
247                         break;
248                 case TILE_MATERIAL_WAVING_PLANTS:
249                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
250                         break;
251                 }
252                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
253                         ? true : false;
254         }
255
256         void applyMaterialOptionsWithShaders(video::SMaterial &material) const
257         {
258                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
259                         ? true : false;
260         }
261         
262         u32 texture_id;
263         video::ITexture *texture;
264         video::ITexture *normal_texture;
265         
266         // Vertex alpha (when MATERIAL_ALPHA_VERTEX is used)
267         u8 alpha;
268         // Material parameters
269         u8 material_type;
270         u8 material_flags;
271         u32 shader_id;
272         // Animation parameters
273         u8 animation_frame_count;
274         u16 animation_frame_length_ms;
275         std::map<u32, FrameSpec> frames;
276
277         u8 rotation;
278 };
279
280 #endif