]> git.lizzy.rs Git - minetest.git/blob - src/tile.h
Remove no virtual dtor warnings, make MapgenParams contain actual NoiseParams
[minetest.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
31 class IGameDef;
32
33 /*
34         tile.{h,cpp}: Texture handling stuff.
35 */
36
37 /*
38         Find out the full path of an image by trying different filename
39         extensions.
40
41         If failed, return "".
42
43         TODO: Should probably be moved out from here, because things needing
44               this function do not need anything else from this header
45 */
46 std::string getImagePath(std::string path);
47
48 /*
49         Gets the path to a texture by first checking if the texture exists
50         in texture_path and if not, using the data path.
51
52         Checks all supported extensions by replacing the original extension.
53
54         If not found, returns "".
55
56         Utilizes a thread-safe cache.
57 */
58 std::string getTexturePath(const std::string &filename);
59
60 /*
61         Specifies a texture in an atlas.
62
63         This is used to specify single textures also.
64
65         This has been designed to be small enough to be thrown around a lot.
66 */
67 struct AtlasPointer
68 {
69         u32 id; // Texture id
70         video::ITexture *atlas; // Atlas in where the texture is
71         v2f pos; // Position in atlas
72         v2f size; // Size in atlas
73         u16 tiled; // X-wise tiling count. If 0, width of atlas is width of image.
74
75         AtlasPointer():
76                 id(0),
77                 atlas(NULL),
78                 pos(0,0),
79                 size(1,1),
80                 tiled(1)
81         {}
82
83         AtlasPointer(
84                         u16 id_,
85                         video::ITexture *atlas_=NULL,
86                         v2f pos_=v2f(0,0),
87                         v2f size_=v2f(1,1),
88                         u16 tiled_=1
89                 ):
90                 id(id_),
91                 atlas(atlas_),
92                 pos(pos_),
93                 size(size_),
94                 tiled(tiled_)
95         {
96         }
97
98         bool operator==(const AtlasPointer &other) const
99         {
100                 return (
101                         id == other.id
102                 );
103                 /*return (
104                         id == other.id &&
105                         atlas == other.atlas &&
106                         pos == other.pos &&
107                         size == other.size &&
108                         tiled == other.tiled
109                 );*/
110         }
111
112         bool operator!=(const AtlasPointer &other) const
113         {
114                 return !(*this == other);
115         }
116
117         float x0(){ return pos.X; }
118         float x1(){ return pos.X + size.X; }
119         float y0(){ return pos.Y; }
120         float y1(){ return pos.Y + size.Y; }
121 };
122
123 /*
124         TextureSource creates and caches textures.
125 */
126
127 class ITextureSource
128 {
129 public:
130         ITextureSource(){}
131         virtual ~ITextureSource(){}
132         virtual u32 getTextureId(const std::string &name){return 0;}
133         virtual u32 getTextureIdDirect(const std::string &name){return 0;}
134         virtual std::string getTextureName(u32 id){return "";}
135         virtual AtlasPointer getTexture(u32 id){return AtlasPointer(0);}
136         virtual AtlasPointer getTexture(const std::string &name)
137                 {return AtlasPointer(0);}
138         virtual video::ITexture* getTextureRaw(const std::string &name)
139                 {return NULL;}
140         virtual AtlasPointer getTextureRawAP(const AtlasPointer &ap)
141                 {return AtlasPointer(0);}
142         virtual IrrlichtDevice* getDevice()
143                 {return NULL;}
144         virtual void updateAP(AtlasPointer &ap){};
145         virtual bool isKnownSourceImage(const std::string &name)=0;
146 };
147
148 class IWritableTextureSource : public ITextureSource
149 {
150 public:
151         IWritableTextureSource(){}
152         virtual ~IWritableTextureSource(){}
153         virtual u32 getTextureId(const std::string &name){return 0;}
154         virtual u32 getTextureIdDirect(const std::string &name){return 0;}
155         virtual std::string getTextureName(u32 id){return "";}
156         virtual AtlasPointer getTexture(u32 id){return AtlasPointer(0);}
157         virtual AtlasPointer getTexture(const std::string &name)
158                 {return AtlasPointer(0);}
159         virtual video::ITexture* getTextureRaw(const std::string &name)
160                 {return NULL;}
161         virtual IrrlichtDevice* getDevice()
162                 {return NULL;}
163         virtual void updateAP(AtlasPointer &ap){};
164         virtual bool isKnownSourceImage(const std::string &name)=0;
165
166         virtual void processQueue()=0;
167         virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
168         virtual void rebuildImagesAndTextures()=0;
169         virtual void buildMainAtlas(class IGameDef *gamedef)=0;
170 };
171
172 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
173
174 enum MaterialType{
175         TILE_MATERIAL_BASIC,
176         TILE_MATERIAL_ALPHA,
177         TILE_MATERIAL_LIQUID_TRANSPARENT,
178         TILE_MATERIAL_LIQUID_OPAQUE,
179 };
180
181 // Material flags
182 // Should backface culling be enabled?
183 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
184 // Should a crack be drawn?
185 #define MATERIAL_FLAG_CRACK 0x02
186 // Should the crack be drawn on transparent pixels (unset) or not (set)?
187 // Ignored if MATERIAL_FLAG_CRACK is not set.
188 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
189 // Animation made up by splitting the texture to vertical frames, as
190 // defined by extra parameters
191 #define MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES 0x08
192 // Whether liquid shader should be used
193 #define MATERIAL_FLAG_
194
195 /*
196         This fully defines the looks of a tile.
197         The SMaterial of a tile is constructed according to this.
198 */
199 struct TileSpec
200 {
201         TileSpec():
202                 texture(0),
203                 alpha(255),
204                 material_type(TILE_MATERIAL_BASIC),
205                 material_flags(
206                         //0 // <- DEBUG, Use the one below
207                         MATERIAL_FLAG_BACKFACE_CULLING
208                 ),
209                 animation_frame_count(1),
210                 animation_frame_length_ms(0)
211         {
212         }
213
214         bool operator==(const TileSpec &other) const
215         {
216                 return (
217                         texture == other.texture &&
218                         alpha == other.alpha &&
219                         material_type == other.material_type &&
220                         material_flags == other.material_flags &&
221                         rotation == other.rotation
222                 );
223         }
224
225         bool operator!=(const TileSpec &other) const
226         {
227                 return !(*this == other);
228         }
229         
230         // Sets everything else except the texture in the material
231         void applyMaterialOptions(video::SMaterial &material) const
232         {
233                 switch(material_type){
234                 case TILE_MATERIAL_BASIC:
235                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
236                         break;
237                 case TILE_MATERIAL_ALPHA:
238                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
239                         break;
240                 case TILE_MATERIAL_LIQUID_TRANSPARENT:
241                         material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
242                         break;
243                 case TILE_MATERIAL_LIQUID_OPAQUE:
244                         material.MaterialType = video::EMT_SOLID;
245                         break;
246                 }
247                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
248         }
249         void applyMaterialOptionsWithShaders(video::SMaterial &material,
250                         const video::E_MATERIAL_TYPE &basic,
251                         const video::E_MATERIAL_TYPE &liquid,
252                         const video::E_MATERIAL_TYPE &alpha) const
253         {
254                 switch(material_type){
255                 case TILE_MATERIAL_BASIC:
256                         material.MaterialType = basic;
257                         break;
258                 case TILE_MATERIAL_ALPHA:
259                         material.MaterialType = alpha;
260                         break;
261                 case TILE_MATERIAL_LIQUID_TRANSPARENT:
262                         material.MaterialType = liquid;
263                         break;
264                 case TILE_MATERIAL_LIQUID_OPAQUE:
265                         material.MaterialType = liquid;
266                         break;
267                 }
268                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
269         }
270         
271         // NOTE: Deprecated, i guess?
272         void setTexturePos(u8 tx_, u8 ty_, u8 tw_, u8 th_)
273         {
274                 texture.pos = v2f((float)tx_/256.0, (float)ty_/256.0);
275                 texture.size = v2f(((float)tw_ + 1.0)/256.0, ((float)th_ + 1.0)/256.0);
276         }
277         
278         AtlasPointer texture;
279         // Vertex alpha (when MATERIAL_ALPHA_VERTEX is used)
280         u8 alpha;
281         // Material parameters
282         u8 material_type;
283         u8 material_flags;
284         // Animation parameters
285         u8 animation_frame_count;
286         u16 animation_frame_length_ms;
287         u8 rotation;
288 };
289
290 #endif