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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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)
83 atlas == other.atlas &&
90 float x0(){ return pos.X; }
91 float x1(){ return pos.X + size.X; }
92 float y0(){ return pos.Y; }
93 float y1(){ return pos.Y + size.Y; }
97 An internal variant of the former with more data.
99 struct SourceAtlasPointer
103 video::IImage *atlas_img; // The source image of the atlas
104 // Integer variants of position and size
109 const std::string &name_,
110 AtlasPointer a_=AtlasPointer(0, NULL),
111 video::IImage *atlas_img_=NULL,
112 v2s32 intpos_=v2s32(0,0),
113 v2u32 intsize_=v2u32(0,0)
117 atlas_img(atlas_img_),
125 TextureSource creates and caches textures.
132 virtual ~ITextureSource(){}
133 virtual u32 getTextureId(const std::string &name){return 0;}
134 virtual u32 getTextureIdDirect(const std::string &name){return 0;}
135 virtual std::string getTextureName(u32 id){return "";}
136 virtual AtlasPointer getTexture(u32 id){return AtlasPointer(0);}
137 virtual AtlasPointer getTexture(const std::string &name)
138 {return AtlasPointer(0);}
139 virtual video::ITexture* getTextureRaw(const std::string &name)
141 virtual void updateAP(AtlasPointer &ap){};
144 class IWritableTextureSource : public ITextureSource
147 IWritableTextureSource(){}
148 virtual ~IWritableTextureSource(){}
149 virtual u32 getTextureId(const std::string &name){return 0;}
150 virtual u32 getTextureIdDirect(const std::string &name){return 0;}
151 virtual std::string getTextureName(u32 id){return "";}
152 virtual AtlasPointer getTexture(u32 id){return AtlasPointer(0);}
153 virtual AtlasPointer getTexture(const std::string &name)
154 {return AtlasPointer(0);}
155 virtual video::ITexture* getTextureRaw(const std::string &name)
157 virtual void updateAP(AtlasPointer &ap){};
159 virtual void processQueue()=0;
160 virtual void buildMainAtlas(class IGameDef *gamedef)=0;
161 // img is eaten, do not drop it
162 virtual void insertImage(const std::string &name, video::IImage *img)=0;
165 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
169 MATERIAL_ALPHA_VERTEX,
170 MATERIAL_ALPHA_SIMPLE, // >127 = opaque
171 MATERIAL_ALPHA_BLEND,
175 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
178 This fully defines the looks of a tile.
179 The SMaterial of a tile is constructed according to this.
186 //material_type(MATERIAL_ALPHA_NONE),
187 // Use this so that leaves don't need a separate material
188 material_type(MATERIAL_ALPHA_SIMPLE),
190 //0 // <- DEBUG, Use the one below
191 MATERIAL_FLAG_BACKFACE_CULLING
196 bool operator==(TileSpec &other)
199 texture == other.texture &&
200 alpha == other.alpha &&
201 material_type == other.material_type &&
202 material_flags == other.material_flags
206 // Sets everything else except the texture in the material
207 void applyMaterialOptions(video::SMaterial &material) const
209 if(alpha != 255 && material_type != MATERIAL_ALPHA_VERTEX)
210 dstream<<"WARNING: TileSpec: alpha != 255 "
211 "but not MATERIAL_ALPHA_VERTEX"
214 if(material_type == MATERIAL_ALPHA_NONE)
215 material.MaterialType = video::EMT_SOLID;
216 else if(material_type == MATERIAL_ALPHA_VERTEX)
217 material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
218 else if(material_type == MATERIAL_ALPHA_SIMPLE)
219 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
220 else if(material_type == MATERIAL_ALPHA_BLEND)
221 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
223 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
226 // NOTE: Deprecated, i guess?
227 void setTexturePos(u8 tx_, u8 ty_, u8 tw_, u8 th_)
229 texture.pos = v2f((float)tx_/256.0, (float)ty_/256.0);
230 texture.size = v2f(((float)tw_ + 1.0)/256.0, ((float)th_ + 1.0)/256.0);
233 AtlasPointer texture;