3 Copyright (C) 2010-2011 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 "common_irrlicht.h"
31 tile.{h,cpp}: Texture handling stuff.
35 Gets the path to a texture by first checking if the texture exists
36 in texture_path and if not, using the data path.
38 Checks all supported extensions by replacing the original extension.
40 If not found, returns "".
42 Utilizes a thread-safe cache.
44 std::string getTexturePath(const std::string &filename);
47 Specifies a texture in an atlas.
49 This is used to specify single textures also.
51 This has been designed to be small enough to be thrown around a lot.
56 video::ITexture *atlas; // Atlas in where the texture is
57 v2f pos; // Position in atlas
58 v2f size; // Size in atlas
59 u16 tiled; // X-wise tiling count. If 0, width of atlas is width of image.
63 video::ITexture *atlas_=NULL,
76 bool operator==(const AtlasPointer &other) const
83 atlas == other.atlas &&
90 bool operator!=(const AtlasPointer &other) const
92 return !(*this == other);
95 float x0(){ return pos.X; }
96 float x1(){ return pos.X + size.X; }
97 float y0(){ return pos.Y; }
98 float y1(){ return pos.Y + size.Y; }
102 TextureSource creates and caches textures.
109 virtual ~ITextureSource(){}
110 virtual u32 getTextureId(const std::string &name){return 0;}
111 virtual u32 getTextureIdDirect(const std::string &name){return 0;}
112 virtual std::string getTextureName(u32 id){return "";}
113 virtual AtlasPointer getTexture(u32 id){return AtlasPointer(0);}
114 virtual AtlasPointer getTexture(const std::string &name)
115 {return AtlasPointer(0);}
116 virtual video::ITexture* getTextureRaw(const std::string &name)
118 virtual AtlasPointer getTextureRawAP(const AtlasPointer &ap)
119 {return AtlasPointer(0);}
120 virtual IrrlichtDevice* getDevice()
122 virtual void updateAP(AtlasPointer &ap){};
125 class IWritableTextureSource : public ITextureSource
128 IWritableTextureSource(){}
129 virtual ~IWritableTextureSource(){}
130 virtual u32 getTextureId(const std::string &name){return 0;}
131 virtual u32 getTextureIdDirect(const std::string &name){return 0;}
132 virtual std::string getTextureName(u32 id){return "";}
133 virtual AtlasPointer getTexture(u32 id){return AtlasPointer(0);}
134 virtual AtlasPointer getTexture(const std::string &name)
135 {return AtlasPointer(0);}
136 virtual video::ITexture* getTextureRaw(const std::string &name)
138 virtual IrrlichtDevice* getDevice()
140 virtual void updateAP(AtlasPointer &ap){};
142 virtual void processQueue()=0;
143 virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
144 virtual void rebuildImagesAndTextures()=0;
145 virtual void buildMainAtlas(class IGameDef *gamedef)=0;
148 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
152 MATERIAL_ALPHA_VERTEX,
153 MATERIAL_ALPHA_SIMPLE, // >127 = opaque
154 MATERIAL_ALPHA_BLEND,
158 // Should backface culling be enabled?
159 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
160 // Should a crack be drawn?
161 #define MATERIAL_FLAG_CRACK 0x02
162 // Should the crack be drawn on transparent pixels (unset) or not (set)?
163 // Ignored if MATERIAL_FLAG_CRACK is not set.
164 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
165 // Animation made up by splitting the texture to vertical frames, as
166 // defined by extra parameters
167 #define MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES 0x08
170 This fully defines the looks of a tile.
171 The SMaterial of a tile is constructed according to this.
178 //material_type(MATERIAL_ALPHA_NONE),
179 // Use this so that leaves don't need a separate material
180 material_type(MATERIAL_ALPHA_SIMPLE),
182 //0 // <- DEBUG, Use the one below
183 MATERIAL_FLAG_BACKFACE_CULLING
185 animation_frame_count(1),
186 animation_frame_length_ms(0)
190 bool operator==(const TileSpec &other) const
193 texture == other.texture &&
194 alpha == other.alpha &&
195 material_type == other.material_type &&
196 material_flags == other.material_flags
200 bool operator!=(const TileSpec &other) const
202 return !(*this == other);
205 // Sets everything else except the texture in the material
206 void applyMaterialOptions(video::SMaterial &material) const
208 if(alpha != 255 && material_type != MATERIAL_ALPHA_VERTEX)
209 dstream<<"WARNING: TileSpec: alpha != 255 "
210 "but not MATERIAL_ALPHA_VERTEX"
213 if(material_type == MATERIAL_ALPHA_NONE)
214 material.MaterialType = video::EMT_SOLID;
215 else if(material_type == MATERIAL_ALPHA_VERTEX)
216 material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
217 else if(material_type == MATERIAL_ALPHA_SIMPLE)
218 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
219 else if(material_type == MATERIAL_ALPHA_BLEND)
220 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
222 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
225 // NOTE: Deprecated, i guess?
226 void setTexturePos(u8 tx_, u8 ty_, u8 tw_, u8 th_)
228 texture.pos = v2f((float)tx_/256.0, (float)ty_/256.0);
229 texture.size = v2f(((float)tw_ + 1.0)/256.0, ((float)th_ + 1.0)/256.0);
232 AtlasPointer texture;
235 // Material parameters
238 // Animation parameters
239 u8 animation_frame_count;
240 u16 animation_frame_length_ms;