better caves
[oweals/minetest.git] / src / irrlichtwrapper.cpp
1 #include "irrlichtwrapper.h"
2 #include "constants.h"
3
4 IrrlichtWrapper::IrrlichtWrapper(IrrlichtDevice *device)
5 {
6         m_main_thread = get_current_thread_id();
7         m_device_mutex.Init();
8         m_device = device;
9 }
10
11 void IrrlichtWrapper::Run()
12 {
13         /*
14                 Fetch textures
15         */
16         if(m_get_texture_queue.size() > 0)
17         {
18                 GetRequest<TextureSpec, video::ITexture*, u8, u8>
19                                 request = m_get_texture_queue.pop();
20
21                 dstream<<"got texture request with key.name="
22                                 <<request.key.name<<std::endl;
23
24                 GetResult<TextureSpec, video::ITexture*, u8, u8>
25                                 result;
26                 result.key = request.key;
27                 result.callers = request.callers;
28                 result.item = getTextureDirect(request.key);
29
30                 request.dest->push_back(result);
31         }
32 }
33
34 video::ITexture* IrrlichtWrapper::getTexture(TextureSpec spec)
35 {
36         video::ITexture *t = m_texturecache.get(spec.name);
37         if(t != NULL)
38                 return t;
39         
40         if(get_current_thread_id() == m_main_thread)
41         {
42                 dstream<<"Getting texture directly: name="
43                                 <<spec.name<<std::endl;
44                                 
45                 t = getTextureDirect(spec);
46         }
47         else
48         {
49                 // We're gonna ask the result to be put into here
50                 ResultQueue<TextureSpec, video::ITexture*, u8, u8> result_queue;
51                 
52                 // Throw a request in
53                 m_get_texture_queue.add(spec, 0, 0, &result_queue);
54                 
55                 dstream<<"Waiting for texture from main thread: "
56                                 <<spec.name<<std::endl;
57                 
58                 try
59                 {
60                         // Wait result for a second
61                         GetResult<TextureSpec, video::ITexture*, u8, u8>
62                                         result = result_queue.pop_front(1000);
63                 
64                         // Check that at least something worked OK
65                         assert(result.key.name == spec.name);
66
67                         t = result.item;
68                 }
69                 catch(ItemNotFoundException &e)
70                 {
71                         dstream<<"Waiting for texture timed out."<<std::endl;
72                         t = NULL;
73                 }
74         }
75
76         // Add to cache and return
77         m_texturecache.set(spec.name, t);
78         return t;
79 }
80
81 video::ITexture* IrrlichtWrapper::getTexture(const std::string &path)
82 {
83         return getTexture(TextureSpec(path, path, NULL));
84 }
85
86 /*
87         Non-thread-safe functions
88 */
89
90 video::ITexture* IrrlichtWrapper::getTextureDirect(TextureSpec spec)
91 {
92         video::IVideoDriver* driver = m_device->getVideoDriver();
93         
94         if(spec.mod == NULL)
95         {
96                 dstream<<"IrrlichtWrapper::getTextureDirect: Loading texture "
97                                 <<spec.path<<std::endl;
98                 return driver->getTexture(spec.path.c_str());
99         }
100
101         dstream<<"IrrlichtWrapper::getTextureDirect: Loading and modifying "
102                         "texture "<<spec.path<<" to make "<<spec.name<<std::endl;
103
104         video::ITexture *base = driver->getTexture(spec.path.c_str());
105         video::ITexture *result = spec.mod->make(base, spec.name.c_str(), driver);
106
107         delete spec.mod;
108         
109         return result;
110 }
111
112 video::ITexture * CrackTextureMod::make(video::ITexture *original,
113                 const char *newname, video::IVideoDriver* driver)
114 {
115         core::dimension2d<u32> dim(16, 16);
116         core::position2d<s32> pos_base(0, 0);
117         core::position2d<s32> pos_other(0, 16 * progression);
118
119         video::IImage *baseimage = driver->createImage(original, pos_base, dim);
120         assert(baseimage);
121         
122         video::ITexture *other = driver->getTexture("../data/crack.png");
123
124         // We have to get the whole texture because getting a smaller area
125         // messes the whole thing. It is probably a bug in Irrlicht.
126         // NOTE: This doesn't work probably because some systems scale
127         //       the image to fit a texture or something...
128         /*video::IImage *otherimage = driver->createImage(
129                         other, core::position2d<s32>(0,0), other->getSize());*/
130         // This should work on more systems
131         video::IImage *otherimage = driver->createImage(
132                         other, core::position2d<s32>(0,0),
133                         v2u32(16, CRACK_ANIMATION_LENGTH * 16));
134
135         assert(otherimage);
136         
137         /*core::rect<s32> clip_rect(v2s32(0,0), dim);
138         otherimage->copyToWithAlpha(baseimage, v2s32(0,0),
139                         core::rect<s32>(pos_other, dim),
140                         video::SColor(255,255,255,255),
141                         &clip_rect);*/
142         
143         otherimage->copyToWithAlpha(baseimage, v2s32(0,0),
144                         core::rect<s32>(pos_other, dim),
145                         video::SColor(255,255,255,255),
146                         NULL);
147         
148         otherimage->drop();
149
150         video::ITexture *newtexture = driver->addTexture(newname, baseimage);
151
152         baseimage->drop();
153
154         return newtexture;
155 }
156
157 video::ITexture * ProgressBarTextureMod::make(video::ITexture *original,
158                 const char *newname, video::IVideoDriver* driver)
159 {
160         core::position2d<s32> pos_base(0, 0);
161         core::dimension2d<u32> dim = original->getOriginalSize();
162
163         video::IImage *baseimage = driver->createImage(original, pos_base, dim);
164         assert(baseimage);
165         
166         core::dimension2d<u32> size = baseimage->getDimension();
167
168         u32 barheight = 1;
169         u32 barpad_x = 1;
170         u32 barpad_y = 1;
171         u32 barwidth = size.Width - barpad_x*2;
172         v2u32 barpos(barpad_x, size.Height - barheight - barpad_y);
173
174         u32 barvalue_i = (u32)(((float)barwidth * value) + 0.5);
175
176         video::SColor active(255,255,0,0);
177         video::SColor inactive(255,0,0,0);
178         for(u32 x0=0; x0<barwidth; x0++)
179         {
180                 video::SColor *c;
181                 if(x0 < barvalue_i)
182                         c = &active;
183                 else
184                         c = &inactive;
185                 u32 x = x0 + barpos.X;
186                 for(u32 y=barpos.Y; y<barpos.Y+barheight; y++)
187                 {
188                         baseimage->setPixel(x,y, *c);
189                 }
190         }
191         
192         video::ITexture *newtexture = driver->addTexture(newname, baseimage);
193
194         baseimage->drop();
195
196         return newtexture;
197 }
198