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.
20 #ifndef IRRLICHTWRAPPER_HEADER
21 #define IRRLICHTWRAPPER_HEADER
24 #include "common_irrlicht.h"
28 #include "iirrlichtwrapper.h"
31 #include <jmutexautolock.h>
35 A thread-safe texture pointer cache.
37 This is used so that irrlicht doesn't get called from many
38 threads, because texture pointers have to be handled in
48 assert(m_mutex.IsInitialized());
51 void set(std::string name, video::ITexture *texture)
56 JMutexAutoLock lock(m_mutex);
58 m_textures[name] = texture;
61 video::ITexture* get(const std::string &name)
63 JMutexAutoLock lock(m_mutex);
65 core::map<std::string, video::ITexture*>::Node *n;
66 n = m_textures.find(name);
75 core::map<std::string, video::ITexture*> m_textures;
81 A thread-safe texture pointer cache
89 assert(m_mutex.IsInitialized());
92 void set(const TextureSpec &spec, video::ITexture *texture)
97 JMutexAutoLock lock(m_mutex);
99 m_textures[spec] = texture;
102 video::ITexture* get(const TextureSpec &spec)
104 JMutexAutoLock lock(m_mutex);
106 core::map<TextureSpec, video::ITexture*>::Node *n;
107 n = m_textures.find(spec);
110 return n->getValue();
116 core::map<TextureSpec, video::ITexture*> m_textures;
121 A thread-safe wrapper for irrlicht, to be accessed from
122 background worker threads.
124 Queues tasks to be done in the main thread.
126 Also caches texture specification strings to ids and textures.
129 class IrrlichtWrapper : public IIrrlichtWrapper
133 These are called from the main thread
136 IrrlichtWrapper(IrrlichtDevice *device);
141 // Shutdown wrapper; this disables queued texture fetching
142 void Shutdown(bool shutdown);
145 These are called from other threads
148 // Not exactly thread-safe but this needs to be fast.
149 // getTimer()->getRealTime() only reads one variable anyway.
152 return m_device->getTimer()->getRealTime();
156 Format of a texture name:
157 "stone.png" (filename in image data directory)
158 "[crack1" (a name starting with "[" is a special feature)
159 "[progress1.0" (a name starting with "[" is a special feature)
162 Loads texture defined by "name" and assigns a texture id to it.
163 If texture has to be generated, generates it.
164 If the texture has already been loaded, returns existing id.
166 textureid_t getTextureId(const std::string &name);
167 // The reverse of the above
168 std::string getTextureName(textureid_t id);
169 // Gets a texture based on a filename
170 video::ITexture* getTexture(const std::string &name);
171 // Gets a texture based on a TextureSpec (a textureid_t is fine too)
172 video::ITexture* getTexture(const TextureSpec &spec);
176 Non-thread-safe variants of stuff, for internal use
180 //video::ITexture* getTextureDirect(const std::string &spec);
182 // Constructs a texture according to spec
183 video::ITexture* getTextureDirect(const TextureSpec &spec);
191 // The id of the thread that can (and has to) use irrlicht directly
192 threadid_t m_main_thread;
194 // The irrlicht device
195 JMutex m_device_mutex;
196 IrrlichtDevice *m_device;
198 // Queued texture fetches (to be processed by the main thread)
199 RequestQueue<TextureSpec, video::ITexture*, u8, u8> m_get_texture_queue;
201 // Cache of textures by spec
202 TextureCache m_texturecache;
204 // A mapping from texture id to string spec
205 MutexedIdGenerator<std::string> m_namecache;