]> git.lizzy.rs Git - dragonfireclient.git/blob - src/irrlichtwrapper.h
Mainly some texture tweaking
[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
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                 if(texture == NULL)
52                         return;
53                 
54                 JMutexAutoLock lock(m_mutex);
55
56                 m_textures[name] = texture;
57         }
58         
59         video::ITexture* get(std::string name)
60         {
61                 JMutexAutoLock lock(m_mutex);
62
63                 core::map<std::string, video::ITexture*>::Node *n;
64                 n = m_textures.find(name);
65
66                 if(n != NULL)
67                         return n->getValue();
68
69                 return NULL;
70         }
71
72 private:
73         core::map<std::string, video::ITexture*> m_textures;
74         JMutex m_mutex;
75 };
76
77 struct TextureMod
78 {
79         /*
80                 Returns a new texture which can be based on the original.
81                 Shall not modify or delete the original texture.
82         */
83         virtual video::ITexture * make(video::ITexture *original,
84                         const char *newname, video::IVideoDriver* driver) = 0;
85 };
86
87 struct CrackTextureMod: public TextureMod
88 {
89         CrackTextureMod(u16 a_progression)
90         {
91                 progression = a_progression;
92         }
93         
94         virtual video::ITexture * make(video::ITexture *original,
95                         const char *newname, video::IVideoDriver* driver);
96         
97         u16 progression;
98 };
99
100 struct SideGrassTextureMod: public TextureMod
101 {
102         SideGrassTextureMod()
103         {
104         }
105         
106         virtual video::ITexture * make(video::ITexture *original,
107                         const char *newname, video::IVideoDriver* driver);
108 };
109
110 struct ProgressBarTextureMod: public TextureMod
111 {
112         // value is from 0.0 to 1.0
113         ProgressBarTextureMod(float a_value)
114         {
115                 value = a_value;
116         }
117         
118         virtual video::ITexture * make(video::ITexture *original,
119                         const char *newname, video::IVideoDriver* driver);
120         
121         float value;
122 };
123
124 /*
125         A class for specifying a requested texture
126 */
127 struct TextureSpec
128 {
129         TextureSpec()
130         {
131                 mod = NULL;
132         }
133         TextureSpec(const std::string &a_name, const std::string &a_path,
134                         TextureMod *a_mod)
135         {
136                 name = a_name;
137                 path = a_path;
138                 mod = a_mod;;
139         }
140         ~TextureSpec()
141         {
142         }
143         bool operator==(const TextureSpec &other)
144         {
145                 return name == other.name;
146         }
147         // An unique name for the texture. Usually the same as the path.
148         // Note that names and paths reside the same namespace.
149         std::string name;
150         // This is the path of the base texture
151         std::string path;
152         // Modification to do to the base texture
153         // NOTE: This is deleted by the one who processes the request
154         TextureMod *mod;
155 };
156
157 /*
158         A thread-safe wrapper for irrlicht, to be accessed from
159         background worker threads.
160
161         Queues tasks to be done in the main thread.
162 */
163
164 class IrrlichtWrapper
165 {
166 public:
167         /*
168                 These are called from the main thread
169         */
170         IrrlichtWrapper(IrrlichtDevice *device);
171         
172         // Run queued tasks
173         void Run();
174
175         /*
176                 These are called from other threads
177         */
178
179         // Not exactly thread-safe but this needs to be fast.
180         // getTimer()->getRealTime() only reads one variable anyway.
181         u32 getTime()
182         {
183                 return m_device->getTimer()->getRealTime();
184         }
185         
186         video::ITexture* getTexture(TextureSpec spec);
187         video::ITexture* getTexture(const std::string &path);
188
189 private:
190         /*
191                 Non-thread-safe variants of stuff, for internal use
192         */
193         video::ITexture* getTextureDirect(TextureSpec spec);
194         
195         /*
196                 Members
197         */
198         
199         threadid_t m_main_thread;
200
201         JMutex m_device_mutex;
202         IrrlichtDevice *m_device;
203
204         TextureCache m_texturecache;
205         
206         RequestQueue<TextureSpec, video::ITexture*, u8, u8> m_get_texture_queue;
207 };
208
209 #endif
210