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"
30 #include <jmutexautolock.h>
34 A thread-safe texture pointer cache.
36 This is used so that irrlicht doesn't get called from many
37 threads, because texture pointers have to be handled in
47 assert(m_mutex.IsInitialized());
50 void set(std::string name, video::ITexture *texture)
55 JMutexAutoLock lock(m_mutex);
57 m_textures[name] = texture;
60 video::ITexture* get(const std::string &name)
62 JMutexAutoLock lock(m_mutex);
64 core::map<std::string, video::ITexture*>::Node *n;
65 n = m_textures.find(name);
74 core::map<std::string, video::ITexture*> m_textures;
80 A thread-safe texture pointer cache
88 assert(m_mutex.IsInitialized());
91 void set(const TextureSpec &spec, video::ITexture *texture)
96 JMutexAutoLock lock(m_mutex);
98 m_textures[spec] = texture;
101 video::ITexture* get(const TextureSpec &spec)
103 JMutexAutoLock lock(m_mutex);
105 core::map<TextureSpec, video::ITexture*>::Node *n;
106 n = m_textures.find(spec);
109 return n->getValue();
115 core::map<TextureSpec, video::ITexture*> m_textures;
120 A thread-safe wrapper for irrlicht, to be accessed from
121 background worker threads.
123 Queues tasks to be done in the main thread.
125 Also caches texture specification strings to ids and textures.
128 class IrrlichtWrapper
132 These are called from the main thread
134 IrrlichtWrapper(IrrlichtDevice *device);
140 These are called from other threads
143 // Not exactly thread-safe but this needs to be fast.
144 // getTimer()->getRealTime() only reads one variable anyway.
147 return m_device->getTimer()->getRealTime();
151 Format of a texture name:
152 "stone.png" (filename in image data directory)
153 "[crack1" (a name starting with "[" is a special feature)
154 "[progress1.0" (a name starting with "[" is a special feature)
157 Loads texture defined by "name" and assigns a texture id to it.
158 If texture has to be generated, generates it.
159 If the texture has already been loaded, returns existing id.
161 textureid_t getTextureId(const std::string &name);
162 // The reverse of the above
163 std::string getTextureName(textureid_t id);
164 // Gets a texture based on a filename
165 video::ITexture* getTexture(const std::string &name);
166 // Gets a texture based on a TextureSpec (a textureid_t is fine too)
167 video::ITexture* getTexture(const TextureSpec &spec);
171 Non-thread-safe variants of stuff, for internal use
175 //video::ITexture* getTextureDirect(const std::string &spec);
177 // Constructs a texture according to spec
178 video::ITexture* getTextureDirect(const TextureSpec &spec);
184 // The id of the thread that can (and has to) use irrlicht directly
185 threadid_t m_main_thread;
187 // The irrlicht device
188 JMutex m_device_mutex;
189 IrrlichtDevice *m_device;
191 // Queued texture fetches (to be processed by the main thread)
192 RequestQueue<TextureSpec, video::ITexture*, u8, u8> m_get_texture_queue;
194 // Cache of textures by spec
195 TextureCache m_texturecache;
197 // A mapping from texture id to string spec
198 MutexedIdGenerator<std::string> m_namecache;