Fix various copy instead of const ref reported by cppcheck (part 3) (#5616)
[oweals/minetest.git] / src / client / 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_v3d.h"
25 #include <ITexture.h>
26 #include <IrrlichtDevice.h>
27 #include "threads.h"
28 #include <string>
29 #include <vector>
30 #include "util/numeric.h"
31
32 class IGameDef;
33 struct TileSpec;
34 struct TileDef;
35
36 typedef std::vector<video::SColor> Palette;
37
38 /*
39         tile.{h,cpp}: Texture handling stuff.
40 */
41
42 /*
43         Find out the full path of an image by trying different filename
44         extensions.
45
46         If failed, return "".
47
48         TODO: Should probably be moved out from here, because things needing
49               this function do not need anything else from this header
50 */
51 std::string getImagePath(std::string path);
52
53 /*
54         Gets the path to a texture by first checking if the texture exists
55         in texture_path and if not, using the data path.
56
57         Checks all supported extensions by replacing the original extension.
58
59         If not found, returns "".
60
61         Utilizes a thread-safe cache.
62 */
63 std::string getTexturePath(const std::string &filename);
64
65 void clearTextureNameCache();
66
67 /*
68         ITextureSource::generateTextureFromMesh parameters
69 */
70 namespace irr {namespace scene {class IMesh;}}
71 struct TextureFromMeshParams
72 {
73         scene::IMesh *mesh;
74         core::dimension2d<u32> dim;
75         std::string rtt_texture_name;
76         bool delete_texture_on_shutdown;
77         v3f camera_position;
78         v3f camera_lookat;
79         core::CMatrix4<f32> camera_projection_matrix;
80         video::SColorf ambient_light;
81         v3f light_position;
82         video::SColorf light_color;
83         f32 light_radius;
84 };
85
86 /*
87         TextureSource creates and caches textures.
88 */
89
90 class ISimpleTextureSource
91 {
92 public:
93         ISimpleTextureSource(){}
94         virtual ~ISimpleTextureSource(){}
95         virtual video::ITexture* getTexture(
96                         const std::string &name, u32 *id = NULL) = 0;
97 };
98
99 class ITextureSource : public ISimpleTextureSource
100 {
101 public:
102         ITextureSource(){}
103         virtual ~ITextureSource(){}
104         virtual u32 getTextureId(const std::string &name)=0;
105         virtual std::string getTextureName(u32 id)=0;
106         virtual video::ITexture* getTexture(u32 id)=0;
107         virtual video::ITexture* getTexture(
108                         const std::string &name, u32 *id = NULL)=0;
109         virtual video::ITexture* getTextureForMesh(
110                         const std::string &name, u32 *id = NULL) = 0;
111         /*!
112          * Returns a palette from the given texture name.
113          * The pointer is valid until the texture source is
114          * destructed.
115          * Should be called from the main thread.
116          */
117         virtual Palette* getPalette(const std::string &name) = 0;
118         virtual IrrlichtDevice* getDevice()=0;
119         virtual bool isKnownSourceImage(const std::string &name)=0;
120         virtual video::ITexture* generateTextureFromMesh(
121                         const TextureFromMeshParams &params)=0;
122         virtual video::ITexture* getNormalTexture(const std::string &name)=0;
123         virtual video::SColor getTextureAverageColor(const std::string &name)=0;
124         virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
125 };
126
127 class IWritableTextureSource : public ITextureSource
128 {
129 public:
130         IWritableTextureSource(){}
131         virtual ~IWritableTextureSource(){}
132         virtual u32 getTextureId(const std::string &name)=0;
133         virtual std::string getTextureName(u32 id)=0;
134         virtual video::ITexture* getTexture(u32 id)=0;
135         virtual video::ITexture* getTexture(
136                         const std::string &name, u32 *id = NULL)=0;
137         virtual IrrlichtDevice* getDevice()=0;
138         virtual bool isKnownSourceImage(const std::string &name)=0;
139         virtual video::ITexture* generateTextureFromMesh(
140                         const TextureFromMeshParams &params)=0;
141
142         virtual void processQueue()=0;
143         virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
144         virtual void rebuildImagesAndTextures()=0;
145         virtual video::ITexture* getNormalTexture(const std::string &name)=0;
146         virtual video::SColor getTextureAverageColor(const std::string &name)=0;
147         virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
148 };
149
150 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
151
152 #ifdef __ANDROID__
153 video::IImage * Align2Npot2(video::IImage * image, video::IVideoDriver* driver);
154 #endif
155
156 enum MaterialType{
157         TILE_MATERIAL_BASIC,
158         TILE_MATERIAL_ALPHA,
159         TILE_MATERIAL_LIQUID_TRANSPARENT,
160         TILE_MATERIAL_LIQUID_OPAQUE,
161         TILE_MATERIAL_WAVING_LEAVES,
162         TILE_MATERIAL_WAVING_PLANTS
163 };
164
165 // Material flags
166 // Should backface culling be enabled?
167 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
168 // Should a crack be drawn?
169 #define MATERIAL_FLAG_CRACK 0x02
170 // Should the crack be drawn on transparent pixels (unset) or not (set)?
171 // Ignored if MATERIAL_FLAG_CRACK is not set.
172 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
173 #define MATERIAL_FLAG_ANIMATION 0x08
174 #define MATERIAL_FLAG_HIGHLIGHTED 0x10
175 #define MATERIAL_FLAG_TILEABLE_HORIZONTAL 0x20
176 #define MATERIAL_FLAG_TILEABLE_VERTICAL 0x40
177
178 /*
179         This fully defines the looks of a tile.
180         The SMaterial of a tile is constructed according to this.
181 */
182 struct FrameSpec
183 {
184         FrameSpec():
185                 texture_id(0),
186                 texture(NULL),
187                 normal_texture(NULL),
188                 flags_texture(NULL)
189         {
190         }
191         u32 texture_id;
192         video::ITexture *texture;
193         video::ITexture *normal_texture;
194         video::ITexture *flags_texture;
195 };
196
197 struct TileSpec
198 {
199         TileSpec():
200                 texture(NULL),
201                 texture_id(0),
202                 color(),
203                 material_type(TILE_MATERIAL_BASIC),
204                 material_flags(
205                         //0 // <- DEBUG, Use the one below
206                         MATERIAL_FLAG_BACKFACE_CULLING
207                 ),
208                 rotation(0),
209                 emissive_light(0),
210                 shader_id(0),
211                 normal_texture(NULL),
212                 flags_texture(NULL),
213                 animation_frame_length_ms(0),
214                 animation_frame_count(1),
215                 has_color(false)
216         {
217         }
218
219         /*!
220          * Two tiles are equal if they can be appended to
221          * the same mesh buffer.
222          */
223         bool operator==(const TileSpec &other) const
224         {
225                 return (
226                         texture_id == other.texture_id &&
227                         material_type == other.material_type &&
228                         material_flags == other.material_flags &&
229                         rotation == other.rotation
230                 );
231         }
232
233         /*!
234          * Two tiles are not equal if they must be in different mesh buffers.
235          */
236         bool operator!=(const TileSpec &other) const
237         {
238                 return !(*this == other);
239         }
240         
241         // Sets everything else except the texture in the material
242         void applyMaterialOptions(video::SMaterial &material) const
243         {
244                 switch (material_type) {
245                 case TILE_MATERIAL_BASIC:
246                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
247                         break;
248                 case TILE_MATERIAL_ALPHA:
249                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
250                         break;
251                 case TILE_MATERIAL_LIQUID_TRANSPARENT:
252                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
253                         break;
254                 case TILE_MATERIAL_LIQUID_OPAQUE:
255                         material.MaterialType = video::EMT_SOLID;
256                         break;
257                 case TILE_MATERIAL_WAVING_LEAVES:
258                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
259                         break;
260                 case TILE_MATERIAL_WAVING_PLANTS:
261                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
262                         break;
263                 }
264                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
265                         ? true : false;
266                 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
267                         material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
268                 }
269                 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
270                         material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
271                 }
272         }
273
274         void applyMaterialOptionsWithShaders(video::SMaterial &material) const
275         {
276                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
277                         ? true : false;
278                 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
279                         material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
280                         material.TextureLayer[1].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
281                 }
282                 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
283                         material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
284                         material.TextureLayer[1].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
285                 }
286         }
287
288         // ordered for performance! please do not reorder unless you pahole it first.
289         video::ITexture *texture;
290         u32 texture_id;
291         // The color of the tile, or if the tile does not own
292         // a color then the color of the node owning this tile.
293         video::SColor color;
294         // Material parameters
295         u8 material_type;
296         u8 material_flags;
297
298         u8 rotation;
299         //! This much light does the tile emit.
300         u8 emissive_light;
301
302         u32 shader_id;
303
304         video::ITexture *normal_texture;
305         // cacheline (64)
306
307         video::ITexture *flags_texture;
308         // Animation parameters
309         u16 animation_frame_length_ms;
310         u8 animation_frame_count;
311         //! If true, the tile has its own color.
312         bool has_color;
313
314         std::vector<FrameSpec> frames;
315 };
316 #endif