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