removed duplicate "bmp"
[oweals/minetest.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         NOTE: This is deprecated and should be removed completely
36 */
37
38 /*
39         A thread-safe texture pointer cache.
40         
41         This is used so that irrlicht doesn't get called from many
42         threads, because texture pointers have to be handled in
43         background threads.
44 */
45
46 #if 0
47 /*
48         A thread-safe texture pointer cache
49 */
50 class TextureCache
51 {
52 public:
53         TextureCache()
54         {
55                 m_mutex.Init();
56                 assert(m_mutex.IsInitialized());
57         }
58         
59         void set(const TextureSpec &spec, video::ITexture *texture)
60         {
61                 if(texture == NULL)
62                         return;
63                 
64                 JMutexAutoLock lock(m_mutex);
65
66                 m_textures[spec] = texture;
67         }
68         
69         video::ITexture* get(const TextureSpec &spec)
70         {
71                 JMutexAutoLock lock(m_mutex);
72
73                 core::map<TextureSpec, video::ITexture*>::Node *n;
74                 n = m_textures.find(spec);
75
76                 if(n != NULL)
77                         return n->getValue();
78
79                 return NULL;
80         }
81
82 private:
83         core::map<TextureSpec, video::ITexture*> m_textures;
84         JMutex m_mutex;
85 };
86 #endif
87
88 /*
89         A thread-safe wrapper for irrlicht, to be accessed from
90         background worker threads.
91
92         Queues tasks to be done in the main thread.
93
94         Also caches texture specification strings to ids and textures.
95
96         TODO: Remove this and move all texture functionality to TextureSource
97 */
98
99 class IrrlichtWrapper : public IIrrlichtWrapper
100 {
101 public:
102         /*
103                 These are called from the main thread
104         */
105
106         IrrlichtWrapper(IrrlichtDevice *device);
107
108         ~IrrlichtWrapper();
109         
110         // Run queued tasks
111         void Run();
112
113         // Shutdown wrapper; this disables queued texture fetching
114         void Shutdown(bool shutdown);
115
116         IrrlichtDevice* getDevice();
117         
118         /*
119                 These are called from other threads
120         */
121
122         // Not exactly thread-safe but this needs to be fast.
123         // getTimer()->getRealTime() only reads one variable anyway.
124         u32 getTime()
125         {
126                 return m_device->getTimer()->getRealTime();
127         }
128
129 #if 0
130         /*
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)
135         */
136         /*
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.
140         */
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);
148 #endif
149         
150 private:
151         /*
152                 Non-thread-safe variants of stuff, for internal use
153         */
154
155         // Constructs a texture according to spec
156         //video::ITexture* getTextureDirect(const TextureSpec &spec);
157         
158         /*
159                 Members
160         */
161
162         bool m_running;
163         
164         // The id of the thread that can (and has to) use irrlicht directly
165         threadid_t m_main_thread;
166         
167         // The irrlicht device
168         JMutex m_device_mutex;
169         IrrlichtDevice *m_device;
170         
171 #if 0
172         // Queued texture fetches (to be processed by the main thread)
173         RequestQueue<TextureSpec, video::ITexture*, u8, u8> m_get_texture_queue;
174
175         // Cache of textures by spec
176         TextureCache m_texturecache;
177
178         // Cached or generated source images by texture name
179         core::map<std::string, video::IImage*> m_imagecache;
180
181         // A mapping from texture id to string spec
182         MutexedIdGenerator<std::string> m_namecache;
183 #endif
184 };
185
186 #endif
187