]> git.lizzy.rs Git - dragonfireclient.git/blob - src/tile.h
Tune gravel and picks
[dragonfireclient.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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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)
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         float x0(){ return pos.X; }
91         float x1(){ return pos.X + size.X; }
92         float y0(){ return pos.Y; }
93         float y1(){ return pos.Y + size.Y; }
94 };
95
96 /*
97         An internal variant of the former with more data.
98 */
99 struct SourceAtlasPointer
100 {
101         std::string name;
102         AtlasPointer a;
103         video::IImage *atlas_img; // The source image of the atlas
104         // Integer variants of position and size
105         v2s32 intpos;
106         v2u32 intsize;
107
108         SourceAtlasPointer(
109                         const std::string &name_,
110                         AtlasPointer a_=AtlasPointer(0, NULL),
111                         video::IImage *atlas_img_=NULL,
112                         v2s32 intpos_=v2s32(0,0),
113                         v2u32 intsize_=v2u32(0,0)
114                 ):
115                 name(name_),
116                 a(a_),
117                 atlas_img(atlas_img_),
118                 intpos(intpos_),
119                 intsize(intsize_)
120         {
121         }
122 };
123
124 /*
125         TextureSource creates and caches textures.
126 */
127
128 class ITextureSource
129 {
130 public:
131         ITextureSource(){}
132         virtual ~ITextureSource(){}
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 void updateAP(AtlasPointer &ap){};
142 };
143
144 class IWritableTextureSource : public ITextureSource
145 {
146 public:
147         IWritableTextureSource(){}
148         virtual ~IWritableTextureSource(){}
149         virtual u32 getTextureId(const std::string &name){return 0;}
150         virtual u32 getTextureIdDirect(const std::string &name){return 0;}
151         virtual std::string getTextureName(u32 id){return "";}
152         virtual AtlasPointer getTexture(u32 id){return AtlasPointer(0);}
153         virtual AtlasPointer getTexture(const std::string &name)
154                 {return AtlasPointer(0);}
155         virtual video::ITexture* getTextureRaw(const std::string &name)
156                 {return NULL;}
157         virtual void updateAP(AtlasPointer &ap){};
158
159         virtual void buildMainAtlas(class IGameDef *gamedef)=0;
160         virtual void processQueue()=0;
161 };
162
163 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
164
165 enum MaterialType{
166         MATERIAL_ALPHA_NONE,
167         MATERIAL_ALPHA_VERTEX,
168         MATERIAL_ALPHA_SIMPLE, // >127 = opaque
169         MATERIAL_ALPHA_BLEND,
170 };
171
172 // Material flags
173 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
174
175 /*
176         This fully defines the looks of a tile.
177         The SMaterial of a tile is constructed according to this.
178 */
179 struct TileSpec
180 {
181         TileSpec():
182                 texture(0),
183                 alpha(255),
184                 //material_type(MATERIAL_ALPHA_NONE),
185                 // Use this so that leaves don't need a separate material
186                 material_type(MATERIAL_ALPHA_SIMPLE),
187                 material_flags(
188                         //0 // <- DEBUG, Use the one below
189                         MATERIAL_FLAG_BACKFACE_CULLING
190                 )
191         {
192         }
193
194         bool operator==(TileSpec &other)
195         {
196                 return (
197                         texture == other.texture &&
198                         alpha == other.alpha &&
199                         material_type == other.material_type &&
200                         material_flags == other.material_flags
201                 );
202         }
203         
204         // Sets everything else except the texture in the material
205         void applyMaterialOptions(video::SMaterial &material) const
206         {
207                 if(alpha != 255 && material_type != MATERIAL_ALPHA_VERTEX)
208                         dstream<<"WARNING: TileSpec: alpha != 255 "
209                                         "but not MATERIAL_ALPHA_VERTEX"
210                                         <<std::endl;
211
212                 if(material_type == MATERIAL_ALPHA_NONE)
213                         material.MaterialType = video::EMT_SOLID;
214                 else if(material_type == MATERIAL_ALPHA_VERTEX)
215                         material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
216                 else if(material_type == MATERIAL_ALPHA_SIMPLE)
217                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
218                 else if(material_type == MATERIAL_ALPHA_BLEND)
219                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
220
221                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
222         }
223         
224         // NOTE: Deprecated, i guess?
225         void setTexturePos(u8 tx_, u8 ty_, u8 tw_, u8 th_)
226         {
227                 texture.pos = v2f((float)tx_/256.0, (float)ty_/256.0);
228                 texture.size = v2f(((float)tw_ + 1.0)/256.0, ((float)th_ + 1.0)/256.0);
229         }
230         
231         AtlasPointer texture;
232         // Vertex alpha
233         u8 alpha;
234         // Material type
235         u8 material_type;
236         // Material flags
237         u8 material_flags;
238 };
239
240 #endif