]> git.lizzy.rs Git - minetest.git/blob - src/tile.h
Remove texture atlas / AtlasPointer, rename getTextureRaw to getTexture
[minetest.git] / src / tile.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 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         Find out the full path of an image by trying different filename
39         extensions.
40
41         If failed, return "".
42
43         TODO: Should probably be moved out from here, because things needing
44               this function do not need anything else from this header
45 */
46 std::string getImagePath(std::string path);
47
48 /*
49         Gets the path to a texture by first checking if the texture exists
50         in texture_path and if not, using the data path.
51
52         Checks all supported extensions by replacing the original extension.
53
54         If not found, returns "".
55
56         Utilizes a thread-safe cache.
57 */
58 std::string getTexturePath(const std::string &filename);
59
60 /*
61         TextureSource creates and caches textures.
62 */
63
64 class ITextureSource
65 {
66 public:
67         ITextureSource(){}
68         virtual ~ITextureSource(){}
69         virtual u32 getTextureId(const std::string &name){return 0;}
70         virtual u32 getTextureIdDirect(const std::string &name){return 0;}
71         virtual std::string getTextureName(u32 id){return "";}
72         virtual video::ITexture* getTexture(u32 id){return NULL;}
73         virtual video::ITexture* getTexture(
74                         const std::string &name, u32 *id = NULL){
75                 if(id) *id = 0;
76                 return NULL;
77         }
78         virtual IrrlichtDevice* getDevice()
79                 {return NULL;}
80         virtual bool isKnownSourceImage(const std::string &name)=0;
81 };
82
83 class IWritableTextureSource : public ITextureSource
84 {
85 public:
86         IWritableTextureSource(){}
87         virtual ~IWritableTextureSource(){}
88         virtual u32 getTextureId(const std::string &name){return 0;}
89         virtual u32 getTextureIdDirect(const std::string &name){return 0;}
90         virtual std::string getTextureName(u32 id){return "";}
91         virtual video::ITexture* getTexture(u32 id){return NULL;}
92         virtual video::ITexture* getTexture(
93                         const std::string &name, u32 *id = NULL){
94                 if(id) *id = 0;
95                 return NULL;
96         }
97         virtual IrrlichtDevice* getDevice(){return NULL;}
98         virtual bool isKnownSourceImage(const std::string &name)=0;
99
100         virtual void processQueue()=0;
101         virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
102         virtual void rebuildImagesAndTextures()=0;
103 };
104
105 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
106
107 enum MaterialType{
108         TILE_MATERIAL_BASIC,
109         TILE_MATERIAL_ALPHA,
110         TILE_MATERIAL_LIQUID_TRANSPARENT,
111         TILE_MATERIAL_LIQUID_OPAQUE,
112 };
113
114 // Material flags
115 // Should backface culling be enabled?
116 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
117 // Should a crack be drawn?
118 #define MATERIAL_FLAG_CRACK 0x02
119 // Should the crack be drawn on transparent pixels (unset) or not (set)?
120 // Ignored if MATERIAL_FLAG_CRACK is not set.
121 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
122 // Animation made up by splitting the texture to vertical frames, as
123 // defined by extra parameters
124 #define MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES 0x08
125
126 /*
127         This fully defines the looks of a tile.
128         The SMaterial of a tile is constructed according to this.
129 */
130 struct TileSpec
131 {
132         TileSpec():
133                 texture_id(0),
134                 texture(NULL),
135                 alpha(255),
136                 material_type(TILE_MATERIAL_BASIC),
137                 material_flags(
138                         //0 // <- DEBUG, Use the one below
139                         MATERIAL_FLAG_BACKFACE_CULLING
140                 ),
141                 animation_frame_count(1),
142                 animation_frame_length_ms(0),
143                 rotation(0)
144         {
145         }
146
147         bool operator==(const TileSpec &other) const
148         {
149                 return (
150                         texture_id == other.texture_id &&
151                         /* texture == other.texture && */
152                         alpha == other.alpha &&
153                         material_type == other.material_type &&
154                         material_flags == other.material_flags &&
155                         rotation == other.rotation
156                 );
157         }
158
159         bool operator!=(const TileSpec &other) const
160         {
161                 return !(*this == other);
162         }
163         
164         // Sets everything else except the texture in the material
165         void applyMaterialOptions(video::SMaterial &material) const
166         {
167                 switch(material_type){
168                 case TILE_MATERIAL_BASIC:
169                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
170                         break;
171                 case TILE_MATERIAL_ALPHA:
172                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
173                         break;
174                 case TILE_MATERIAL_LIQUID_TRANSPARENT:
175                         material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
176                         break;
177                 case TILE_MATERIAL_LIQUID_OPAQUE:
178                         material.MaterialType = video::EMT_SOLID;
179                         break;
180                 }
181                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
182         }
183         void applyMaterialOptionsWithShaders(video::SMaterial &material,
184                         const video::E_MATERIAL_TYPE &basic,
185                         const video::E_MATERIAL_TYPE &liquid,
186                         const video::E_MATERIAL_TYPE &alpha) const
187         {
188                 switch(material_type){
189                 case TILE_MATERIAL_BASIC:
190                         material.MaterialType = basic;
191                         break;
192                 case TILE_MATERIAL_ALPHA:
193                         material.MaterialType = alpha;
194                         break;
195                 case TILE_MATERIAL_LIQUID_TRANSPARENT:
196                         material.MaterialType = liquid;
197                         break;
198                 case TILE_MATERIAL_LIQUID_OPAQUE:
199                         material.MaterialType = liquid;
200                         break;
201                 }
202                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
203         }
204         
205         u32 texture_id;
206         video::ITexture *texture;
207         // Vertex alpha (when MATERIAL_ALPHA_VERTEX is used)
208         u8 alpha;
209         // Material parameters
210         u8 material_type;
211         u8 material_flags;
212         // Animation parameters
213         u8 animation_frame_count;
214         u16 animation_frame_length_ms;
215         u8 rotation;
216 };
217
218 #endif