3 Copyright (C) 2010 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"
29 Specifies a texture in an atlas.
31 This is used to specify single textures also.
33 This has been designed to be small enough to be thrown around a lot.
38 video::ITexture *atlas; // Atlas in where the texture is
39 v2f pos; // Position in atlas
40 v2f size; // Size in atlas
41 u16 tiled; // X-wise tiling count. If 0, width of atlas is width of image.
45 video::ITexture *atlas_=NULL,
58 bool operator==(const AtlasPointer &other)
65 atlas == other.atlas &&
72 float x0(){ return pos.X; }
73 float x1(){ return pos.X + size.X; }
74 float y0(){ return pos.Y; }
75 float y1(){ return pos.Y + size.Y; }
79 An internal variant of the former with more data.
81 struct SourceAtlasPointer
85 video::IImage *atlas_img; // The source image of the atlas
86 // Integer variants of position and size
91 const std::string &name_,
92 AtlasPointer a_=AtlasPointer(0, NULL),
93 video::IImage *atlas_img_=NULL,
94 v2s32 intpos_=v2s32(0,0),
95 v2u32 intsize_=v2u32(0,0)
99 atlas_img(atlas_img_),
107 Creates and caches textures.
112 TextureSource(IrrlichtDevice *device);
116 Processes queued texture requests from other threads.
118 Shall be called from the main thread.
124 Now, assume a texture with the id 1 exists, and has the name
125 "stone.png^mineral1".
126 Then a random thread calls getTextureId for a texture called
127 "stone.png^mineral1^crack0".
128 ...Now, WTF should happen? Well:
129 - getTextureId strips off stuff recursively from the end until
130 the remaining part is found, or nothing is left when
131 something is stripped out
133 But it is slow to search for textures by names and modify them
135 - ContentFeatures is made to contain ids for the basic plain
137 - Crack textures can be slow by themselves, but the framework
141 - Assume a texture with the id 1 exists, and has the name
142 "stone.png^mineral1" and is specified as a part of some atlas.
143 - Now MapBlock::getNodeTile() stumbles upon a node which uses
144 texture id 1, and finds out that NODEMOD_CRACK must be applied
146 - It finds out the name of the texture with getTextureName(1),
147 appends "^crack0" to it and gets a new texture id with
148 getTextureId("stone.png^mineral1^crack0")
153 Gets a texture id from cache or
154 - if main thread, from getTextureIdDirect
155 - if other thread, adds to request queue and waits for main thread
157 u32 getTextureId(const std::string &name);
163 "stone.png^blit:mineral_coal.png"
164 "stone.png^blit:mineral_coal.png^crack1"
166 - If texture specified by name is found from cache, return the
168 - Otherwise generate the texture, add to cache and return id.
169 Recursion is used to find out the largest found part of the
170 texture and continue based on it.
172 The id 0 points to a NULL texture. It is returned in case of error.
174 u32 getTextureIdDirect(const std::string &name);
177 Finds out the name of a cached texture.
179 std::string getTextureName(u32 id);
182 If texture specified by the name pointed by the id doesn't
183 exist, create it, then return the cached texture.
185 Can be called from any thread. If called from some other thread
186 and not found in cache, the call is queued to the main thread
189 AtlasPointer getTexture(u32 id);
191 AtlasPointer getTexture(const std::string &name)
193 return getTexture(getTextureId(name));
196 // Gets a separate texture
197 video::ITexture* getTextureRaw(const std::string &name)
199 AtlasPointer ap = getTexture(name);
205 Build the main texture atlas which contains most of the
208 This is called by the constructor.
210 void buildMainAtlas();
212 // The id of the thread that is allowed to use irrlicht directly
213 threadid_t m_main_thread;
214 // The irrlicht device
215 IrrlichtDevice *m_device;
217 // A texture id is index in this array.
218 // The first position contains a NULL texture.
219 core::array<SourceAtlasPointer> m_atlaspointer_cache;
220 // Maps a texture name to an index in the former.
221 core::map<std::string, u32> m_name_to_id;
222 // The two former containers are behind this mutex
223 JMutex m_atlaspointer_cache_mutex;
225 // Main texture atlas. This is filled at startup and is then not touched.
226 video::IImage *m_main_atlas_image;
227 video::ITexture *m_main_atlas_texture;
229 // Queued texture fetches (to be processed by the main thread)
230 RequestQueue<std::string, u32, u8, u8> m_get_texture_queue;
235 MATERIAL_ALPHA_VERTEX,
236 MATERIAL_ALPHA_SIMPLE, // >127 = opaque
237 MATERIAL_ALPHA_BLEND,
241 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
244 This fully defines the looks of a tile.
245 The SMaterial of a tile is constructed according to this.
247 TODO: Change this to use an AtlasPointer
254 material_type(MATERIAL_ALPHA_NONE),
255 // Use this so that leaves don't need a separate material
256 //material_type(MATERIAL_ALPHA_SIMPLE),
258 MATERIAL_FLAG_BACKFACE_CULLING
263 bool operator==(TileSpec &other)
266 texture == other.texture &&
267 alpha == other.alpha &&
268 material_type == other.material_type &&
269 material_flags == other.material_flags
273 // Sets everything else except the texture in the material
274 void applyMaterialOptions(video::SMaterial &material)
276 if(alpha != 255 && material_type != MATERIAL_ALPHA_VERTEX)
277 dstream<<"WARNING: TileSpec: alpha != 255 "
278 "but not MATERIAL_ALPHA_VERTEX"
281 if(material_type == MATERIAL_ALPHA_NONE)
282 material.MaterialType = video::EMT_SOLID;
283 else if(material_type == MATERIAL_ALPHA_VERTEX)
284 material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
285 else if(material_type == MATERIAL_ALPHA_SIMPLE)
286 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
287 else if(material_type == MATERIAL_ALPHA_BLEND)
288 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
290 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
293 // NOTE: Deprecated, i guess?
294 void setTexturePos(u8 tx_, u8 ty_, u8 tw_, u8 th_)
296 texture.pos = v2f((float)tx_/256.0, (float)ty_/256.0);
297 texture.size = v2f(((float)tw_ + 1.0)/256.0, ((float)th_ + 1.0)/256.0);
300 AtlasPointer texture;