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 NOTE: This is deprecated and should be removed completely
39 A thread-safe texture pointer cache.
41 This is used so that irrlicht doesn't get called from many
42 threads, because texture pointers have to be handled in
48 A thread-safe texture pointer cache
56 assert(m_mutex.IsInitialized());
59 void set(const TextureSpec &spec, video::ITexture *texture)
64 JMutexAutoLock lock(m_mutex);
66 m_textures[spec] = texture;
69 video::ITexture* get(const TextureSpec &spec)
71 JMutexAutoLock lock(m_mutex);
73 core::map<TextureSpec, video::ITexture*>::Node *n;
74 n = m_textures.find(spec);
83 core::map<TextureSpec, video::ITexture*> m_textures;
89 A thread-safe wrapper for irrlicht, to be accessed from
90 background worker threads.
92 Queues tasks to be done in the main thread.
94 Also caches texture specification strings to ids and textures.
96 TODO: Remove this and move all texture functionality to TextureSource
99 class IrrlichtWrapper : public IIrrlichtWrapper
103 These are called from the main thread
106 IrrlichtWrapper(IrrlichtDevice *device);
113 // Shutdown wrapper; this disables queued texture fetching
114 void Shutdown(bool shutdown);
116 IrrlichtDevice* getDevice();
119 These are called from other threads
122 // Not exactly thread-safe but this needs to be fast.
123 // getTimer()->getRealTime() only reads one variable anyway.
126 return m_device->getTimer()->getRealTime();
131 Format of a texture name:
132 "stone.png" (filename in image data directory)
133 "[crack1" (a name starting with "[" is a special feature)
134 "[progress1.0" (a name starting with "[" is a special feature)
137 Loads texture defined by "name" and assigns a texture id to it.
138 If texture has to be generated, generates it.
139 If the texture has already been loaded, returns existing id.
141 textureid_t getTextureId(const std::string &name);
142 // The reverse of the above
143 std::string getTextureName(textureid_t id);
144 // Gets a texture based on a filename
145 video::ITexture* getTexture(const std::string &filename);
146 // Gets a texture based on a TextureSpec (a textureid_t is fine too)
147 video::ITexture* getTexture(const TextureSpec &spec);
152 Non-thread-safe variants of stuff, for internal use
155 // Constructs a texture according to spec
156 //video::ITexture* getTextureDirect(const TextureSpec &spec);
164 // The id of the thread that can (and has to) use irrlicht directly
165 threadid_t m_main_thread;
167 // The irrlicht device
168 JMutex m_device_mutex;
169 IrrlichtDevice *m_device;
172 // Queued texture fetches (to be processed by the main thread)
173 RequestQueue<TextureSpec, video::ITexture*, u8, u8> m_get_texture_queue;
175 // Cache of textures by spec
176 TextureCache m_texturecache;
178 // Cached or generated source images by texture name
179 core::map<std::string, video::IImage*> m_imagecache;
181 // A mapping from texture id to string spec
182 MutexedIdGenerator<std::string> m_namecache;