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