]> git.lizzy.rs Git - minetest.git/blob - src/irrlichtwrapper.h
added some missing files
[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
28 #include <jmutex.h>
29 #include <jmutexautolock.h>
30 #include <string>
31
32 /*
33         A thread-safe texture pointer cache.
34         
35         This is used so that irrlicht doesn't get called from many
36         threads, because texture pointers have to be handled in
37         background threads.
38 */
39
40 class TextureCache
41 {
42 public:
43         TextureCache()
44         {
45                 m_mutex.Init();
46                 assert(m_mutex.IsInitialized());
47         }
48         
49         void set(std::string name, video::ITexture *texture)
50         {
51                 JMutexAutoLock lock(m_mutex);
52
53                 m_textures[name] = texture;
54         }
55         
56         video::ITexture* get(std::string name)
57         {
58                 JMutexAutoLock lock(m_mutex);
59
60                 core::map<std::string, video::ITexture*>::Node *n;
61                 n = m_textures.find(name);
62
63                 if(n != NULL)
64                         return n->getValue();
65
66                 return NULL;
67         }
68
69 private:
70         core::map<std::string, video::ITexture*> m_textures;
71         JMutex m_mutex;
72 };
73
74 struct TextureMod
75 {
76         /*
77                 Returns a new texture which can be based on the original.
78                 Shall not modify or delete the original texture.
79         */
80         virtual video::ITexture * make(video::ITexture *original,
81                         video::IVideoDriver* driver) = 0;
82 };
83
84 struct CrackTextureMod: public TextureMod
85 {
86         CrackTextureMod(u16 a_progression)
87         {
88                 progression = a_progression;
89         }
90         
91         virtual video::ITexture * make(video::ITexture *original,
92                         video::IVideoDriver* driver);
93         
94         u16 progression;
95 };
96
97 /*
98         A class for specifying a requested texture
99 */
100 struct TextureSpec
101 {
102         TextureSpec()
103         {
104                 mod = NULL;
105         }
106         TextureSpec(const std::string &a_name, const std::string &a_path,
107                         TextureMod *a_mod)
108         {
109                 name = a_name;
110                 path = a_path;
111                 mod = a_mod;;
112         }
113         ~TextureSpec()
114         {
115         }
116         bool operator==(const TextureSpec &other)
117         {
118                 return name == other.name;
119         }
120         // An unique name for the texture. Usually the same as the path.
121         // Note that names and paths reside the same namespace.
122         std::string name;
123         // This is the path of the base texture
124         std::string path;
125         // Modification to do to the base texture
126         // NOTE: This is deleted by the one who processes the request
127         TextureMod *mod;
128 };
129
130 /*
131         A thread-safe wrapper for irrlicht, to be accessed from
132         background worker threads.
133
134         Queues tasks to be done in the main thread.
135 */
136
137 class IrrlichtWrapper
138 {
139 public:
140         /*
141                 These are called from the main thread
142         */
143         IrrlichtWrapper(IrrlichtDevice *device);
144         
145         // Run queued tasks
146         void Run();
147
148         /*
149                 These are called from other threads
150         */
151
152         // Not exactly thread-safe but this needs to be fast
153         u32 getTime()
154         {
155                 return m_device->getTimer()->getTime();
156         }
157         
158         video::ITexture* getTexture(TextureSpec spec);
159         video::ITexture* getTexture(const std::string &path);
160
161 private:
162         /*
163                 Non-thread-safe variants of stuff, for internal use
164         */
165         video::ITexture* getTextureDirect(TextureSpec spec);
166         
167         /*
168                 Members
169         */
170         
171         threadid_t m_main_thread;
172
173         JMutex m_device_mutex;
174         IrrlichtDevice *m_device;
175
176         TextureCache m_texturecache;
177         
178         RequestQueue<TextureSpec, video::ITexture*, u8, u8> m_get_texture_queue;
179 };
180
181 #endif
182