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