]> git.lizzy.rs Git - dragonfireclient.git/blob - src/irrlichtwrapper.h
end-of-day.
[dragonfireclient.git] / src / irrlichtwrapper.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef IRRLICHTWRAPPER_HEADER
21 #define IRRLICHTWRAPPER_HEADER
22
23 #include "threads.h"
24 #include "common_irrlicht.h"
25 #include "debug.h"
26 #include "utility.h"
27 #include "texture.h"
28 #include "iirrlichtwrapper.h"
29
30 #include <jmutex.h>
31 #include <jmutexautolock.h>
32 #include <string>
33
34 /*
35         A thread-safe texture pointer cache.
36         
37         This is used so that irrlicht doesn't get called from many
38         threads, because texture pointers have to be handled in
39         background threads.
40 */
41 #if 0
42 class TextureCache
43 {
44 public:
45         TextureCache()
46         {
47                 m_mutex.Init();
48                 assert(m_mutex.IsInitialized());
49         }
50         
51         void set(std::string name, video::ITexture *texture)
52         {
53                 if(texture == NULL)
54                         return;
55                 
56                 JMutexAutoLock lock(m_mutex);
57
58                 m_textures[name] = texture;
59         }
60         
61         video::ITexture* get(const std::string &name)
62         {
63                 JMutexAutoLock lock(m_mutex);
64
65                 core::map<std::string, video::ITexture*>::Node *n;
66                 n = m_textures.find(name);
67
68                 if(n != NULL)
69                         return n->getValue();
70
71                 return NULL;
72         }
73
74 private:
75         core::map<std::string, video::ITexture*> m_textures;
76         JMutex m_mutex;
77 };
78 #endif
79
80 /*
81         A thread-safe texture pointer cache
82 */
83 class TextureCache
84 {
85 public:
86         TextureCache()
87         {
88                 m_mutex.Init();
89                 assert(m_mutex.IsInitialized());
90         }
91         
92         void set(const TextureSpec &spec, video::ITexture *texture)
93         {
94                 if(texture == NULL)
95                         return;
96                 
97                 JMutexAutoLock lock(m_mutex);
98
99                 m_textures[spec] = texture;
100         }
101         
102         video::ITexture* get(const TextureSpec &spec)
103         {
104                 JMutexAutoLock lock(m_mutex);
105
106                 core::map<TextureSpec, video::ITexture*>::Node *n;
107                 n = m_textures.find(spec);
108
109                 if(n != NULL)
110                         return n->getValue();
111
112                 return NULL;
113         }
114
115 private:
116         core::map<TextureSpec, video::ITexture*> m_textures;
117         JMutex m_mutex;
118 };
119
120 /*
121         A thread-safe wrapper for irrlicht, to be accessed from
122         background worker threads.
123
124         Queues tasks to be done in the main thread.
125
126         Also caches texture specification strings to ids and textures.
127 */
128
129 class IrrlichtWrapper : public IIrrlichtWrapper
130 {
131 public:
132         /*
133                 These are called from the main thread
134         */
135
136         IrrlichtWrapper(IrrlichtDevice *device);
137         
138         // Run queued tasks
139         void Run();
140
141         // Shutdown wrapper; this disables queued texture fetching
142         void Shutdown(bool shutdown);
143
144         /*
145                 These are called from other threads
146         */
147
148         // Not exactly thread-safe but this needs to be fast.
149         // getTimer()->getRealTime() only reads one variable anyway.
150         u32 getTime()
151         {
152                 return m_device->getTimer()->getRealTime();
153         }
154         
155         /*
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)
160         */
161         /*
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.
165         */
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);
173         
174 private:
175         /*
176                 Non-thread-safe variants of stuff, for internal use
177         */
178
179         // DEPRECATED NO-OP
180         //video::ITexture* getTextureDirect(const std::string &spec);
181         
182         // Constructs a texture according to spec
183         video::ITexture* getTextureDirect(const TextureSpec &spec);
184         
185         /*
186                 Members
187         */
188
189         bool m_running;
190         
191         // The id of the thread that can (and has to) use irrlicht directly
192         threadid_t m_main_thread;
193         
194         // The irrlicht device
195         JMutex m_device_mutex;
196         IrrlichtDevice *m_device;
197         
198         // Queued texture fetches (to be processed by the main thread)
199         RequestQueue<TextureSpec, video::ITexture*, u8, u8> m_get_texture_queue;
200
201         // Cache of textures by spec
202         TextureCache m_texturecache;
203
204         // A mapping from texture id to string spec
205         MutexedIdGenerator<std::string> m_namecache;
206 };
207
208 #endif
209