]> git.lizzy.rs Git - minetest.git/blob - src/tile.h
Add redis database backend
[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 void clearTextureNameCache();
61
62 /*
63         ITextureSource::generateTextureFromMesh parameters
64 */
65 namespace irr {namespace scene {class IMesh;}}
66 struct TextureFromMeshParams
67 {
68         scene::IMesh *mesh;
69         core::dimension2d<u32> dim;
70         std::string rtt_texture_name;
71         bool delete_texture_on_shutdown;
72         v3f camera_position;
73         v3f camera_lookat;
74         core::CMatrix4<f32> camera_projection_matrix;
75         video::SColorf ambient_light;
76         v3f light_position;
77         video::SColorf light_color;
78         f32 light_radius;
79 };
80
81 /*
82         TextureSource creates and caches textures.
83 */
84
85 class ISimpleTextureSource
86 {
87 public:
88         ISimpleTextureSource(){}
89         virtual ~ISimpleTextureSource(){}
90         virtual video::ITexture* getTexture(
91                         const std::string &name, u32 *id = NULL) = 0;
92 };
93
94 class ITextureSource : public ISimpleTextureSource
95 {
96 public:
97         ITextureSource(){}
98         virtual ~ITextureSource(){}
99         virtual u32 getTextureId(const std::string &name)=0;
100         virtual u32 getTextureIdDirect(const std::string &name)=0;
101         virtual std::string getTextureName(u32 id)=0;
102         virtual video::ITexture* getTexture(u32 id)=0;
103         virtual video::ITexture* getTexture(
104                         const std::string &name, u32 *id = NULL)=0;
105         virtual IrrlichtDevice* getDevice()=0;
106         virtual bool isKnownSourceImage(const std::string &name)=0;
107         virtual video::ITexture* generateTextureFromMesh(
108                         const TextureFromMeshParams &params)=0;
109 };
110
111 class IWritableTextureSource : public ITextureSource
112 {
113 public:
114         IWritableTextureSource(){}
115         virtual ~IWritableTextureSource(){}
116         virtual u32 getTextureId(const std::string &name)=0;
117         virtual u32 getTextureIdDirect(const std::string &name)=0;
118         virtual std::string getTextureName(u32 id)=0;
119         virtual video::ITexture* getTexture(u32 id)=0;
120         virtual video::ITexture* getTexture(
121                         const std::string &name, u32 *id = NULL)=0;
122         virtual IrrlichtDevice* getDevice()=0;
123         virtual bool isKnownSourceImage(const std::string &name)=0;
124         virtual video::ITexture* generateTextureFromMesh(
125                         const TextureFromMeshParams &params)=0;
126
127         virtual void processQueue()=0;
128         virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
129         virtual void rebuildImagesAndTextures()=0;
130 };
131
132 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
133
134 enum MaterialType{
135         TILE_MATERIAL_BASIC,
136         TILE_MATERIAL_ALPHA,
137         TILE_MATERIAL_LIQUID_TRANSPARENT,
138         TILE_MATERIAL_LIQUID_OPAQUE,
139         TILE_MATERIAL_LEAVES,
140         TILE_MATERIAL_PLANTS
141 };
142
143 // Material flags
144 // Should backface culling be enabled?
145 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
146 // Should a crack be drawn?
147 #define MATERIAL_FLAG_CRACK 0x02
148 // Should the crack be drawn on transparent pixels (unset) or not (set)?
149 // Ignored if MATERIAL_FLAG_CRACK is not set.
150 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
151 // Animation made up by splitting the texture to vertical frames, as
152 // defined by extra parameters
153 #define MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES 0x08
154
155 /*
156         This fully defines the looks of a tile.
157         The SMaterial of a tile is constructed according to this.
158 */
159 struct TileSpec
160 {
161         TileSpec():
162                 texture_id(0),
163                 texture(NULL),
164                 alpha(255),
165                 material_type(TILE_MATERIAL_BASIC),
166                 material_flags(
167                         //0 // <- DEBUG, Use the one below
168                         MATERIAL_FLAG_BACKFACE_CULLING
169                 ),
170                 animation_frame_count(1),
171                 animation_frame_length_ms(0),
172                 rotation(0)
173         {
174         }
175
176         bool operator==(const TileSpec &other) const
177         {
178                 return (
179                         texture_id == other.texture_id &&
180                         /* texture == other.texture && */
181                         alpha == other.alpha &&
182                         material_type == other.material_type &&
183                         material_flags == other.material_flags &&
184                         rotation == other.rotation
185                 );
186         }
187
188         bool operator!=(const TileSpec &other) const
189         {
190                 return !(*this == other);
191         }
192         
193         // Sets everything else except the texture in the material
194         void applyMaterialOptions(video::SMaterial &material) const
195         {
196                 switch(material_type){
197                 case TILE_MATERIAL_BASIC:
198                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
199                         break;
200                 case TILE_MATERIAL_ALPHA:
201                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
202                         break;
203                 case TILE_MATERIAL_LIQUID_TRANSPARENT:
204                         material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
205                         break;
206                 case TILE_MATERIAL_LIQUID_OPAQUE:
207                         material.MaterialType = video::EMT_SOLID;
208                         break;
209                 case TILE_MATERIAL_LEAVES:
210                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
211                         break;
212                 case TILE_MATERIAL_PLANTS:
213                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
214                 break;
215                 }
216                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
217         }
218         void applyMaterialOptionsWithShaders(video::SMaterial &material,
219                         const video::E_MATERIAL_TYPE &basic,
220                         const video::E_MATERIAL_TYPE &liquid,
221                         const video::E_MATERIAL_TYPE &alpha,
222                         const video::E_MATERIAL_TYPE &leaves,
223                         const video::E_MATERIAL_TYPE &plants) const
224         {
225                 switch(material_type){
226                 case TILE_MATERIAL_BASIC:
227                         material.MaterialType = basic;
228                         break;
229                 case TILE_MATERIAL_ALPHA:
230                         material.MaterialType = alpha;
231                         break;
232                 case TILE_MATERIAL_LIQUID_TRANSPARENT:
233                         material.MaterialType = liquid;
234                         break;
235                 case TILE_MATERIAL_LIQUID_OPAQUE:
236                         material.MaterialType = liquid;
237                         break;
238                 case TILE_MATERIAL_LEAVES:
239                         material.MaterialType = leaves;
240                         break;
241                 case TILE_MATERIAL_PLANTS:
242                         material.MaterialType = plants;
243                         break;
244                 }
245                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
246         }
247         
248         u32 texture_id;
249         video::ITexture *texture;
250         // Vertex alpha (when MATERIAL_ALPHA_VERTEX is used)
251         u8 alpha;
252         // Material parameters
253         u8 material_type;
254         u8 material_flags;
255         // Animation parameters
256         u8 animation_frame_count;
257         u16 animation_frame_length_ms;
258         u8 rotation;
259 };
260
261 #endif