Optimize headers
[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 "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                         u16 id_,
66                         video::ITexture *atlas_=NULL,
67                         v2f pos_=v2f(0,0),
68                         v2f size_=v2f(1,1),
69                         u16 tiled_=1
70                 ):
71                 id(id_),
72                 atlas(atlas_),
73                 pos(pos_),
74                 size(size_),
75                 tiled(tiled_)
76         {
77         }
78
79         bool operator==(const AtlasPointer &other) const
80         {
81                 return (
82                         id == other.id
83                 );
84                 /*return (
85                         id == other.id &&
86                         atlas == other.atlas &&
87                         pos == other.pos &&
88                         size == other.size &&
89                         tiled == other.tiled
90                 );*/
91         }
92
93         bool operator!=(const AtlasPointer &other) const
94         {
95                 return !(*this == other);
96         }
97
98         float x0(){ return pos.X; }
99         float x1(){ return pos.X + size.X; }
100         float y0(){ return pos.Y; }
101         float y1(){ return pos.Y + size.Y; }
102 };
103
104 /*
105         TextureSource creates and caches textures.
106 */
107
108 class ITextureSource
109 {
110 public:
111         ITextureSource(){}
112         virtual ~ITextureSource(){}
113         virtual u32 getTextureId(const std::string &name){return 0;}
114         virtual u32 getTextureIdDirect(const std::string &name){return 0;}
115         virtual std::string getTextureName(u32 id){return "";}
116         virtual AtlasPointer getTexture(u32 id){return AtlasPointer(0);}
117         virtual AtlasPointer getTexture(const std::string &name)
118                 {return AtlasPointer(0);}
119         virtual video::ITexture* getTextureRaw(const std::string &name)
120                 {return NULL;}
121         virtual AtlasPointer getTextureRawAP(const AtlasPointer &ap)
122                 {return AtlasPointer(0);}
123         virtual IrrlichtDevice* getDevice()
124                 {return NULL;}
125         virtual void updateAP(AtlasPointer &ap){};
126 };
127
128 class IWritableTextureSource : public ITextureSource
129 {
130 public:
131         IWritableTextureSource(){}
132         virtual ~IWritableTextureSource(){}
133         virtual u32 getTextureId(const std::string &name){return 0;}
134         virtual u32 getTextureIdDirect(const std::string &name){return 0;}
135         virtual std::string getTextureName(u32 id){return "";}
136         virtual AtlasPointer getTexture(u32 id){return AtlasPointer(0);}
137         virtual AtlasPointer getTexture(const std::string &name)
138                 {return AtlasPointer(0);}
139         virtual video::ITexture* getTextureRaw(const std::string &name)
140                 {return NULL;}
141         virtual IrrlichtDevice* getDevice()
142                 {return NULL;}
143         virtual void updateAP(AtlasPointer &ap){};
144
145         virtual void processQueue()=0;
146         virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
147         virtual void rebuildImagesAndTextures()=0;
148         virtual void buildMainAtlas(class IGameDef *gamedef)=0;
149 };
150
151 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
152
153 enum MaterialType{
154         MATERIAL_ALPHA_NONE,
155         MATERIAL_ALPHA_VERTEX,
156         MATERIAL_ALPHA_SIMPLE, // >127 = opaque
157         MATERIAL_ALPHA_BLEND,
158 };
159
160 // Material flags
161 // Should backface culling be enabled?
162 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
163 // Should a crack be drawn?
164 #define MATERIAL_FLAG_CRACK 0x02
165 // Should the crack be drawn on transparent pixels (unset) or not (set)?
166 // Ignored if MATERIAL_FLAG_CRACK is not set.
167 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
168 // Animation made up by splitting the texture to vertical frames, as
169 // defined by extra parameters
170 #define MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES 0x08
171
172 /*
173         This fully defines the looks of a tile.
174         The SMaterial of a tile is constructed according to this.
175 */
176 struct TileSpec
177 {
178         TileSpec():
179                 texture(0),
180                 alpha(255),
181                 //material_type(MATERIAL_ALPHA_NONE),
182                 // Use this so that leaves don't need a separate material
183                 material_type(MATERIAL_ALPHA_SIMPLE),
184                 material_flags(
185                         //0 // <- DEBUG, Use the one below
186                         MATERIAL_FLAG_BACKFACE_CULLING
187                 ),
188                 animation_frame_count(1),
189                 animation_frame_length_ms(0)
190         {
191         }
192
193         bool operator==(const TileSpec &other) const
194         {
195                 return (
196                         texture == other.texture &&
197                         alpha == other.alpha &&
198                         material_type == other.material_type &&
199                         material_flags == other.material_flags
200                 );
201         }
202
203         bool operator!=(const TileSpec &other) const
204         {
205                 return !(*this == other);
206         }
207         
208         // Sets everything else except the texture in the material
209         void applyMaterialOptions(video::SMaterial &material) const
210         {
211                 if(material_type == MATERIAL_ALPHA_NONE)
212                         material.MaterialType = video::EMT_SOLID;
213                 else if(material_type == MATERIAL_ALPHA_VERTEX)
214                         material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
215                 else if(material_type == MATERIAL_ALPHA_SIMPLE)
216                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
217                 else if(material_type == MATERIAL_ALPHA_BLEND)
218                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
219
220                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
221         }
222         
223         // NOTE: Deprecated, i guess?
224         void setTexturePos(u8 tx_, u8 ty_, u8 tw_, u8 th_)
225         {
226                 texture.pos = v2f((float)tx_/256.0, (float)ty_/256.0);
227                 texture.size = v2f(((float)tw_ + 1.0)/256.0, ((float)th_ + 1.0)/256.0);
228         }
229         
230         AtlasPointer texture;
231         // Vertex alpha (when MATERIAL_ALPHA_VERTEX is used)
232         u8 alpha;
233         // Material parameters
234         u8 material_type;
235         u8 material_flags;
236         // Animation parameters
237         u8 animation_frame_count;
238         u16 animation_frame_length_ms;
239 };
240
241 #endif