3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
23 #include "irrlichttypes.h"
27 #include <IrrlichtDevice.h>
34 tile.{h,cpp}: Texture handling stuff.
38 Find out the full path of an image by trying different filename
43 TODO: Should probably be moved out from here, because things needing
44 this function do not need anything else from this header
46 std::string getImagePath(std::string path);
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.
52 Checks all supported extensions by replacing the original extension.
54 If not found, returns "".
56 Utilizes a thread-safe cache.
58 std::string getTexturePath(const std::string &filename);
61 Specifies a texture in an atlas.
63 This is used to specify single textures also.
65 This has been designed to be small enough to be thrown around a lot.
70 video::ITexture *atlas; // Atlas in where the texture is
71 v2f pos; // Position in atlas
72 v2f size; // Size in atlas
73 u16 tiled; // X-wise tiling count. If 0, width of atlas is width of image.
85 video::ITexture *atlas_=NULL,
98 bool operator==(const AtlasPointer &other) const
105 atlas == other.atlas &&
107 size == other.size &&
112 bool operator!=(const AtlasPointer &other) const
114 return !(*this == other);
117 float x0(){ return pos.X; }
118 float x1(){ return pos.X + size.X; }
119 float y0(){ return pos.Y; }
120 float y1(){ return pos.Y + size.Y; }
124 TextureSource creates and caches textures.
131 virtual ~ITextureSource(){}
132 virtual u32 getTextureId(const std::string &name){return 0;}
133 virtual u32 getTextureIdDirect(const std::string &name){return 0;}
134 virtual std::string getTextureName(u32 id){return "";}
135 virtual AtlasPointer getTexture(u32 id){return AtlasPointer(0);}
136 virtual AtlasPointer getTexture(const std::string &name)
137 {return AtlasPointer(0);}
138 virtual video::ITexture* getTextureRaw(const std::string &name)
140 virtual AtlasPointer getTextureRawAP(const AtlasPointer &ap)
141 {return AtlasPointer(0);}
142 virtual IrrlichtDevice* getDevice()
144 virtual void updateAP(AtlasPointer &ap){};
145 virtual bool isKnownSourceImage(const std::string &name)=0;
148 class IWritableTextureSource : public ITextureSource
151 IWritableTextureSource(){}
152 virtual ~IWritableTextureSource(){}
153 virtual u32 getTextureId(const std::string &name){return 0;}
154 virtual u32 getTextureIdDirect(const std::string &name){return 0;}
155 virtual std::string getTextureName(u32 id){return "";}
156 virtual AtlasPointer getTexture(u32 id){return AtlasPointer(0);}
157 virtual AtlasPointer getTexture(const std::string &name)
158 {return AtlasPointer(0);}
159 virtual video::ITexture* getTextureRaw(const std::string &name)
161 virtual IrrlichtDevice* getDevice()
163 virtual void updateAP(AtlasPointer &ap){};
164 virtual bool isKnownSourceImage(const std::string &name)=0;
166 virtual void processQueue()=0;
167 virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
168 virtual void rebuildImagesAndTextures()=0;
169 virtual void buildMainAtlas(class IGameDef *gamedef)=0;
172 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
177 TILE_MATERIAL_LIQUID_TRANSPARENT,
178 TILE_MATERIAL_LIQUID_OPAQUE,
182 // Should backface culling be enabled?
183 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
184 // Should a crack be drawn?
185 #define MATERIAL_FLAG_CRACK 0x02
186 // Should the crack be drawn on transparent pixels (unset) or not (set)?
187 // Ignored if MATERIAL_FLAG_CRACK is not set.
188 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
189 // Animation made up by splitting the texture to vertical frames, as
190 // defined by extra parameters
191 #define MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES 0x08
192 // Whether liquid shader should be used
193 #define MATERIAL_FLAG_
196 This fully defines the looks of a tile.
197 The SMaterial of a tile is constructed according to this.
204 material_type(TILE_MATERIAL_BASIC),
206 //0 // <- DEBUG, Use the one below
207 MATERIAL_FLAG_BACKFACE_CULLING
209 animation_frame_count(1),
210 animation_frame_length_ms(0)
214 bool operator==(const TileSpec &other) const
217 texture == other.texture &&
218 alpha == other.alpha &&
219 material_type == other.material_type &&
220 material_flags == other.material_flags &&
221 rotation == other.rotation
225 bool operator!=(const TileSpec &other) const
227 return !(*this == other);
230 // Sets everything else except the texture in the material
231 void applyMaterialOptions(video::SMaterial &material) const
233 switch(material_type){
234 case TILE_MATERIAL_BASIC:
235 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
237 case TILE_MATERIAL_ALPHA:
238 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
240 case TILE_MATERIAL_LIQUID_TRANSPARENT:
241 material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
243 case TILE_MATERIAL_LIQUID_OPAQUE:
244 material.MaterialType = video::EMT_SOLID;
247 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
249 void applyMaterialOptionsWithShaders(video::SMaterial &material,
250 const video::E_MATERIAL_TYPE &basic,
251 const video::E_MATERIAL_TYPE &liquid,
252 const video::E_MATERIAL_TYPE &alpha) const
254 switch(material_type){
255 case TILE_MATERIAL_BASIC:
256 material.MaterialType = basic;
258 case TILE_MATERIAL_ALPHA:
259 material.MaterialType = alpha;
261 case TILE_MATERIAL_LIQUID_TRANSPARENT:
262 material.MaterialType = liquid;
264 case TILE_MATERIAL_LIQUID_OPAQUE:
265 material.MaterialType = liquid;
268 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
271 // NOTE: Deprecated, i guess?
272 void setTexturePos(u8 tx_, u8 ty_, u8 tw_, u8 th_)
274 texture.pos = v2f((float)tx_/256.0, (float)ty_/256.0);
275 texture.size = v2f(((float)tw_ + 1.0)/256.0, ((float)th_ + 1.0)/256.0);
278 AtlasPointer texture;
279 // Vertex alpha (when MATERIAL_ALPHA_VERTEX is used)
281 // Material parameters
284 // Animation parameters
285 u8 animation_frame_count;
286 u16 animation_frame_length_ms;