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