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 Gets the path to a texture by first checking if the texture exists
39 in texture_path and if not, using the data path.
41 Checks all supported extensions by replacing the original extension.
43 If not found, returns "".
45 Utilizes a thread-safe cache.
47 std::string getTexturePath(const std::string &filename);
50 Specifies a texture in an atlas.
52 This is used to specify single textures also.
54 This has been designed to be small enough to be thrown around a lot.
59 video::ITexture *atlas; // Atlas in where the texture is
60 v2f pos; // Position in atlas
61 v2f size; // Size in atlas
62 u16 tiled; // X-wise tiling count. If 0, width of atlas is width of image.
74 video::ITexture *atlas_=NULL,
87 bool operator==(const AtlasPointer &other) const
94 atlas == other.atlas &&
101 bool operator!=(const AtlasPointer &other) const
103 return !(*this == other);
106 float x0(){ return pos.X; }
107 float x1(){ return pos.X + size.X; }
108 float y0(){ return pos.Y; }
109 float y1(){ return pos.Y + size.Y; }
113 TextureSource creates and caches textures.
120 virtual ~ITextureSource(){}
121 virtual u32 getTextureId(const std::string &name){return 0;}
122 virtual u32 getTextureIdDirect(const std::string &name){return 0;}
123 virtual std::string getTextureName(u32 id){return "";}
124 virtual AtlasPointer getTexture(u32 id){return AtlasPointer(0);}
125 virtual AtlasPointer getTexture(const std::string &name)
126 {return AtlasPointer(0);}
127 virtual video::ITexture* getTextureRaw(const std::string &name)
129 virtual AtlasPointer getTextureRawAP(const AtlasPointer &ap)
130 {return AtlasPointer(0);}
131 virtual IrrlichtDevice* getDevice()
133 virtual void updateAP(AtlasPointer &ap){};
134 virtual bool isKnownSourceImage(const std::string &name)=0;
137 class IWritableTextureSource : public ITextureSource
140 IWritableTextureSource(){}
141 virtual ~IWritableTextureSource(){}
142 virtual u32 getTextureId(const std::string &name){return 0;}
143 virtual u32 getTextureIdDirect(const std::string &name){return 0;}
144 virtual std::string getTextureName(u32 id){return "";}
145 virtual AtlasPointer getTexture(u32 id){return AtlasPointer(0);}
146 virtual AtlasPointer getTexture(const std::string &name)
147 {return AtlasPointer(0);}
148 virtual video::ITexture* getTextureRaw(const std::string &name)
150 virtual IrrlichtDevice* getDevice()
152 virtual void updateAP(AtlasPointer &ap){};
153 virtual bool isKnownSourceImage(const std::string &name)=0;
155 virtual void processQueue()=0;
156 virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
157 virtual void rebuildImagesAndTextures()=0;
158 virtual void buildMainAtlas(class IGameDef *gamedef)=0;
161 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
165 TILE_MATERIAL_LIQUID_TRANSPARENT,
166 TILE_MATERIAL_LIQUID_OPAQUE,
170 // Should backface culling be enabled?
171 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
172 // Should a crack be drawn?
173 #define MATERIAL_FLAG_CRACK 0x02
174 // Should the crack be drawn on transparent pixels (unset) or not (set)?
175 // Ignored if MATERIAL_FLAG_CRACK is not set.
176 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
177 // Animation made up by splitting the texture to vertical frames, as
178 // defined by extra parameters
179 #define MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES 0x08
180 // Whether liquid shader should be used
181 #define MATERIAL_FLAG_
184 This fully defines the looks of a tile.
185 The SMaterial of a tile is constructed according to this.
192 material_type(TILE_MATERIAL_BASIC),
194 //0 // <- DEBUG, Use the one below
195 MATERIAL_FLAG_BACKFACE_CULLING
197 animation_frame_count(1),
198 animation_frame_length_ms(0)
202 bool operator==(const TileSpec &other) const
205 texture == other.texture &&
206 alpha == other.alpha &&
207 material_type == other.material_type &&
208 material_flags == other.material_flags &&
209 rotation == other.rotation
213 bool operator!=(const TileSpec &other) const
215 return !(*this == other);
218 // Sets everything else except the texture in the material
219 void applyMaterialOptions(video::SMaterial &material) const
221 switch(material_type){
222 case TILE_MATERIAL_BASIC:
223 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
225 case TILE_MATERIAL_LIQUID_TRANSPARENT:
226 material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
228 case TILE_MATERIAL_LIQUID_OPAQUE:
229 material.MaterialType = video::EMT_SOLID;
232 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
234 void applyMaterialOptionsWithShaders(video::SMaterial &material,
235 const video::E_MATERIAL_TYPE &basic,
236 const video::E_MATERIAL_TYPE &liquid) const
238 switch(material_type){
239 case TILE_MATERIAL_BASIC:
240 material.MaterialType = basic;
242 case TILE_MATERIAL_LIQUID_TRANSPARENT:
243 material.MaterialType = liquid;
245 case TILE_MATERIAL_LIQUID_OPAQUE:
246 material.MaterialType = liquid;
249 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
252 // NOTE: Deprecated, i guess?
253 void setTexturePos(u8 tx_, u8 ty_, u8 tw_, u8 th_)
255 texture.pos = v2f((float)tx_/256.0, (float)ty_/256.0);
256 texture.size = v2f(((float)tw_ + 1.0)/256.0, ((float)th_ + 1.0)/256.0);
259 AtlasPointer texture;
260 // Vertex alpha (when MATERIAL_ALPHA_VERTEX is used)
262 // Material parameters
265 // Animation parameters
266 u8 animation_frame_count;
267 u16 animation_frame_length_ms;