]> git.lizzy.rs Git - dragonfireclient.git/blob - src/tile.h
Moved the temporary mapgen test files and added a modified map.cpp too... These are...
[dragonfireclient.git] / src / tile.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 /*
29         Specifies a texture in an atlas.
30
31         This is used to specify single textures also.
32
33         This has been designed to be small enough to be thrown around a lot.
34 */
35 struct AtlasPointer
36 {
37         u32 id; // Texture id
38         video::ITexture *atlas; // Atlas in where the texture is
39         v2f pos; // Position in atlas
40         v2f size; // Size in atlas
41         u16 tiled; // X-wise tiling count. If 0, width of atlas is width of image.
42
43         AtlasPointer(
44                         u16 id_,
45                         video::ITexture *atlas_=NULL,
46                         v2f pos_=v2f(0,0),
47                         v2f size_=v2f(1,1),
48                         u16 tiled_=1
49                 ):
50                 id(id_),
51                 atlas(atlas_),
52                 pos(pos_),
53                 size(size_),
54                 tiled(tiled_)
55         {
56         }
57
58         bool operator==(const AtlasPointer &other)
59         {
60                 return (
61                         id == other.id
62                 );
63                 /*return (
64                         id == other.id &&
65                         atlas == other.atlas &&
66                         pos == other.pos &&
67                         size == other.size &&
68                         tiled == other.tiled
69                 );*/
70         }
71
72         float x0(){ return pos.X; }
73         float x1(){ return pos.X + size.X; }
74         float y0(){ return pos.Y; }
75         float y1(){ return pos.Y + size.Y; }
76 };
77
78 /*
79         An internal variant of the former with more data.
80 */
81 struct SourceAtlasPointer
82 {
83         std::string name;
84         AtlasPointer a;
85         video::IImage *atlas_img; // The source image of the atlas
86         // Integer variants of position and size
87         v2s32 intpos;
88         v2u32 intsize;
89
90         SourceAtlasPointer(
91                         const std::string &name_,
92                         AtlasPointer a_=AtlasPointer(0, NULL),
93                         video::IImage *atlas_img_=NULL,
94                         v2s32 intpos_=v2s32(0,0),
95                         v2u32 intsize_=v2u32(0,0)
96                 ):
97                 name(name_),
98                 a(a_),
99                 atlas_img(atlas_img_),
100                 intpos(intpos_),
101                 intsize(intsize_)
102         {
103         }
104 };
105
106 /*
107         Implementation (to be used as a no-op on the server)
108 */
109 class ITextureSource
110 {
111 public:
112         ITextureSource(){}
113         virtual ~ITextureSource(){}
114         virtual u32 getTextureId(const std::string &name){return 0;}
115         virtual u32 getTextureIdDirect(const std::string &name){return 0;}
116         virtual std::string getTextureName(u32 id){return "";}
117         virtual AtlasPointer getTexture(u32 id){return AtlasPointer(0);}
118         virtual AtlasPointer getTexture(const std::string &name)
119                 {return AtlasPointer(0);}
120         virtual video::ITexture* getTextureRaw(const std::string &name)
121                 {return NULL;}
122 };
123
124 /*
125         Creates and caches textures.
126 */
127 class TextureSource : public ITextureSource
128 {
129 public:
130         TextureSource(IrrlichtDevice *device);
131         ~TextureSource();
132
133         /*
134                 Processes queued texture requests from other threads.
135
136                 Shall be called from the main thread.
137         */
138         void processQueue();
139         
140         /*
141                 Example case:
142                 Now, assume a texture with the id 1 exists, and has the name
143                 "stone.png^mineral1".
144                 Then a random thread calls getTextureId for a texture called
145                 "stone.png^mineral1^crack0".
146                 ...Now, WTF should happen? Well:
147                 - getTextureId strips off stuff recursively from the end until
148                   the remaining part is found, or nothing is left when
149                   something is stripped out
150
151                 But it is slow to search for textures by names and modify them
152                 like that?
153                 - ContentFeatures is made to contain ids for the basic plain
154                   textures
155                 - Crack textures can be slow by themselves, but the framework
156                   must be fast.
157
158                 Example case #2:
159                 - Assume a texture with the id 1 exists, and has the name
160                   "stone.png^mineral1" and is specified as a part of some atlas.
161                 - Now MapBlock::getNodeTile() stumbles upon a node which uses
162                   texture id 1, and finds out that NODEMOD_CRACK must be applied
163                   with progression=0
164                 - It finds out the name of the texture with getTextureName(1),
165                   appends "^crack0" to it and gets a new texture id with
166                   getTextureId("stone.png^mineral1^crack0")
167
168         */
169         
170         /*
171                 Gets a texture id from cache or
172                 - if main thread, from getTextureIdDirect
173                 - if other thread, adds to request queue and waits for main thread
174         */
175         u32 getTextureId(const std::string &name);
176         
177         /*
178                 Example names:
179                 "stone.png"
180                 "stone.png^crack2"
181                 "stone.png^blit:mineral_coal.png"
182                 "stone.png^blit:mineral_coal.png^crack1"
183
184                 - If texture specified by name is found from cache, return the
185                   cached id.
186                 - Otherwise generate the texture, add to cache and return id.
187                   Recursion is used to find out the largest found part of the
188                   texture and continue based on it.
189
190                 The id 0 points to a NULL texture. It is returned in case of error.
191         */
192         u32 getTextureIdDirect(const std::string &name);
193
194         /*
195                 Finds out the name of a cached texture.
196         */
197         std::string getTextureName(u32 id);
198
199         /*
200                 If texture specified by the name pointed by the id doesn't
201                 exist, create it, then return the cached texture.
202
203                 Can be called from any thread. If called from some other thread
204                 and not found in cache, the call is queued to the main thread
205                 for processing.
206         */
207         AtlasPointer getTexture(u32 id);
208         
209         AtlasPointer getTexture(const std::string &name)
210         {
211                 return getTexture(getTextureId(name));
212         }
213         
214         // Gets a separate texture
215         video::ITexture* getTextureRaw(const std::string &name)
216         {
217                 AtlasPointer ap = getTexture(name);
218                 return ap.atlas;
219         }
220
221 private:
222         /*
223                 Build the main texture atlas which contains most of the
224                 textures.
225                 
226                 This is called by the constructor.
227         */
228         void buildMainAtlas();
229         
230         // The id of the thread that is allowed to use irrlicht directly
231         threadid_t m_main_thread;
232         // The irrlicht device
233         IrrlichtDevice *m_device;
234         
235         // A texture id is index in this array.
236         // The first position contains a NULL texture.
237         core::array<SourceAtlasPointer> m_atlaspointer_cache;
238         // Maps a texture name to an index in the former.
239         core::map<std::string, u32> m_name_to_id;
240         // The two former containers are behind this mutex
241         JMutex m_atlaspointer_cache_mutex;
242         
243         // Main texture atlas. This is filled at startup and is then not touched.
244         video::IImage *m_main_atlas_image;
245         video::ITexture *m_main_atlas_texture;
246
247         // Queued texture fetches (to be processed by the main thread)
248         RequestQueue<std::string, u32, u8, u8> m_get_texture_queue;
249 };
250
251 enum MaterialType{
252         MATERIAL_ALPHA_NONE,
253         MATERIAL_ALPHA_VERTEX,
254         MATERIAL_ALPHA_SIMPLE, // >127 = opaque
255         MATERIAL_ALPHA_BLEND,
256 };
257
258 // Material flags
259 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
260
261 /*
262         This fully defines the looks of a tile.
263         The SMaterial of a tile is constructed according to this.
264 */
265 struct TileSpec
266 {
267         TileSpec():
268                 texture(0),
269                 alpha(255),
270                 material_type(MATERIAL_ALPHA_NONE),
271                 // Use this so that leaves don't need a separate material
272                 //material_type(MATERIAL_ALPHA_SIMPLE),
273                 material_flags(
274                         //0 // <- DEBUG, Use the one below
275                         MATERIAL_FLAG_BACKFACE_CULLING
276                 )
277         {
278         }
279
280         bool operator==(TileSpec &other)
281         {
282                 return (
283                         texture == other.texture &&
284                         alpha == other.alpha &&
285                         material_type == other.material_type &&
286                         material_flags == other.material_flags
287                 );
288         }
289         
290         // Sets everything else except the texture in the material
291         void applyMaterialOptions(video::SMaterial &material)
292         {
293                 if(alpha != 255 && material_type != MATERIAL_ALPHA_VERTEX)
294                         dstream<<"WARNING: TileSpec: alpha != 255 "
295                                         "but not MATERIAL_ALPHA_VERTEX"
296                                         <<std::endl;
297
298                 if(material_type == MATERIAL_ALPHA_NONE)
299                         material.MaterialType = video::EMT_SOLID;
300                 else if(material_type == MATERIAL_ALPHA_VERTEX)
301                         material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
302                 else if(material_type == MATERIAL_ALPHA_SIMPLE)
303                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
304                 else if(material_type == MATERIAL_ALPHA_BLEND)
305                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
306
307                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
308         }
309         
310         // NOTE: Deprecated, i guess?
311         void setTexturePos(u8 tx_, u8 ty_, u8 tw_, u8 th_)
312         {
313                 texture.pos = v2f((float)tx_/256.0, (float)ty_/256.0);
314                 texture.size = v2f(((float)tw_ + 1.0)/256.0, ((float)th_ + 1.0)/256.0);
315         }
316         
317         AtlasPointer texture;
318         // Vertex alpha
319         u8 alpha;
320         // Material type
321         u8 material_type;
322         // Material flags
323         u8 material_flags;
324 };
325
326 #endif