Switch the license to be LGPLv2/later, with small parts still remaining as GPLv2...
[oweals/minetest.git] / src / tile.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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 "common_irrlicht.h"
24 #include "threads.h"
25 #include "utility.h"
26 #include <string>
27
28 class IGameDef;
29
30 /*
31         tile.{h,cpp}: Texture handling stuff.
32 */
33
34 /*
35         Gets the path to a texture by first checking if the texture exists
36         in texture_path and if not, using the data path.
37
38         Checks all supported extensions by replacing the original extension.
39
40         If not found, returns "".
41
42         Utilizes a thread-safe cache.
43 */
44 std::string getTexturePath(const std::string &filename);
45
46 /*
47         Specifies a texture in an atlas.
48
49         This is used to specify single textures also.
50
51         This has been designed to be small enough to be thrown around a lot.
52 */
53 struct AtlasPointer
54 {
55         u32 id; // Texture id
56         video::ITexture *atlas; // Atlas in where the texture is
57         v2f pos; // Position in atlas
58         v2f size; // Size in atlas
59         u16 tiled; // X-wise tiling count. If 0, width of atlas is width of image.
60
61         AtlasPointer(
62                         u16 id_,
63                         video::ITexture *atlas_=NULL,
64                         v2f pos_=v2f(0,0),
65                         v2f size_=v2f(1,1),
66                         u16 tiled_=1
67                 ):
68                 id(id_),
69                 atlas(atlas_),
70                 pos(pos_),
71                 size(size_),
72                 tiled(tiled_)
73         {
74         }
75
76         bool operator==(const AtlasPointer &other) const
77         {
78                 return (
79                         id == other.id
80                 );
81                 /*return (
82                         id == other.id &&
83                         atlas == other.atlas &&
84                         pos == other.pos &&
85                         size == other.size &&
86                         tiled == other.tiled
87                 );*/
88         }
89
90         bool operator!=(const AtlasPointer &other) const
91         {
92                 return !(*this == other);
93         }
94
95         float x0(){ return pos.X; }
96         float x1(){ return pos.X + size.X; }
97         float y0(){ return pos.Y; }
98         float y1(){ return pos.Y + size.Y; }
99 };
100
101 /*
102         TextureSource creates and caches textures.
103 */
104
105 class ITextureSource
106 {
107 public:
108         ITextureSource(){}
109         virtual ~ITextureSource(){}
110         virtual u32 getTextureId(const std::string &name){return 0;}
111         virtual u32 getTextureIdDirect(const std::string &name){return 0;}
112         virtual std::string getTextureName(u32 id){return "";}
113         virtual AtlasPointer getTexture(u32 id){return AtlasPointer(0);}
114         virtual AtlasPointer getTexture(const std::string &name)
115                 {return AtlasPointer(0);}
116         virtual video::ITexture* getTextureRaw(const std::string &name)
117                 {return NULL;}
118         virtual AtlasPointer getTextureRawAP(const AtlasPointer &ap)
119                 {return AtlasPointer(0);}
120         virtual IrrlichtDevice* getDevice()
121                 {return NULL;}
122         virtual void updateAP(AtlasPointer &ap){};
123 };
124
125 class IWritableTextureSource : public ITextureSource
126 {
127 public:
128         IWritableTextureSource(){}
129         virtual ~IWritableTextureSource(){}
130         virtual u32 getTextureId(const std::string &name){return 0;}
131         virtual u32 getTextureIdDirect(const std::string &name){return 0;}
132         virtual std::string getTextureName(u32 id){return "";}
133         virtual AtlasPointer getTexture(u32 id){return AtlasPointer(0);}
134         virtual AtlasPointer getTexture(const std::string &name)
135                 {return AtlasPointer(0);}
136         virtual video::ITexture* getTextureRaw(const std::string &name)
137                 {return NULL;}
138         virtual IrrlichtDevice* getDevice()
139                 {return NULL;}
140         virtual void updateAP(AtlasPointer &ap){};
141
142         virtual void processQueue()=0;
143         virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
144         virtual void rebuildImagesAndTextures()=0;
145         virtual void buildMainAtlas(class IGameDef *gamedef)=0;
146 };
147
148 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
149
150 enum MaterialType{
151         MATERIAL_ALPHA_NONE,
152         MATERIAL_ALPHA_VERTEX,
153         MATERIAL_ALPHA_SIMPLE, // >127 = opaque
154         MATERIAL_ALPHA_BLEND,
155 };
156
157 // Material flags
158 // Should backface culling be enabled?
159 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
160 // Should a crack be drawn?
161 #define MATERIAL_FLAG_CRACK 0x02
162 // Should the crack be drawn on transparent pixels (unset) or not (set)?
163 // Ignored if MATERIAL_FLAG_CRACK is not set.
164 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
165
166 /*
167         This fully defines the looks of a tile.
168         The SMaterial of a tile is constructed according to this.
169 */
170 struct TileSpec
171 {
172         TileSpec():
173                 texture(0),
174                 alpha(255),
175                 //material_type(MATERIAL_ALPHA_NONE),
176                 // Use this so that leaves don't need a separate material
177                 material_type(MATERIAL_ALPHA_SIMPLE),
178                 material_flags(
179                         //0 // <- DEBUG, Use the one below
180                         MATERIAL_FLAG_BACKFACE_CULLING
181                 )
182         {
183         }
184
185         bool operator==(const TileSpec &other) const
186         {
187                 return (
188                         texture == other.texture &&
189                         alpha == other.alpha &&
190                         material_type == other.material_type &&
191                         material_flags == other.material_flags
192                 );
193         }
194
195         bool operator!=(const TileSpec &other) const
196         {
197                 return !(*this == other);
198         }
199         
200         // Sets everything else except the texture in the material
201         void applyMaterialOptions(video::SMaterial &material) const
202         {
203                 if(alpha != 255 && material_type != MATERIAL_ALPHA_VERTEX)
204                         dstream<<"WARNING: TileSpec: alpha != 255 "
205                                         "but not MATERIAL_ALPHA_VERTEX"
206                                         <<std::endl;
207
208                 if(material_type == MATERIAL_ALPHA_NONE)
209                         material.MaterialType = video::EMT_SOLID;
210                 else if(material_type == MATERIAL_ALPHA_VERTEX)
211                         material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
212                 else if(material_type == MATERIAL_ALPHA_SIMPLE)
213                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
214                 else if(material_type == MATERIAL_ALPHA_BLEND)
215                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
216
217                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
218         }
219         
220         // NOTE: Deprecated, i guess?
221         void setTexturePos(u8 tx_, u8 ty_, u8 tw_, u8 th_)
222         {
223                 texture.pos = v2f((float)tx_/256.0, (float)ty_/256.0);
224                 texture.size = v2f(((float)tw_ + 1.0)/256.0, ((float)th_ + 1.0)/256.0);
225         }
226         
227         AtlasPointer texture;
228         // Vertex alpha
229         u8 alpha;
230         // Material type
231         u8 material_type;
232         // Material flags
233         u8 material_flags;
234 };
235
236 #endif