]> git.lizzy.rs Git - dragonfireclient.git/blob - src/irrlichtwrapper.cpp
Mainly some texture tweaking
[dragonfireclient.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         // Size of the base image
116         core::dimension2d<u32> dim(16, 16);
117         // Size of the crack image
118         //core::dimension2d<u32> dim_crack(16, 16 * CRACK_ANIMATION_LENGTH);
119         // Position to copy the crack to in the base image
120         core::position2d<s32> pos_base(0, 0);
121         // Position to copy the crack from in the crack image
122         core::position2d<s32> pos_other(0, 16 * progression);
123
124         video::IImage *baseimage = driver->createImage(original, pos_base, dim);
125         assert(baseimage);
126
127         video::IImage *crackimage = driver->createImageFromFile(porting::getDataPath("crack.png").c_str());
128         assert(crackimage);
129         
130 #if 0
131         video::ITexture *other = driver->getTexture(porting::getDataPath("crack.png").c_str());
132         
133         dstream<<__FUNCTION_NAME<<": crack texture size is "
134                         <<other->getSize().Width<<"x"
135                         <<other->getSize().Height<<std::endl;
136
137         // We have to get the whole texture because getting a smaller area
138         // messes the whole thing. It is probably a bug in Irrlicht.
139         // NOTE: This doesn't work probably because some systems scale
140         //       the image to fit a texture or something...
141         video::IImage *otherimage = driver->createImage(
142                         other, core::position2d<s32>(0,0), other->getSize());
143
144         assert(otherimage);
145
146         // Now, the image might be 80 or 128 high depending on the computer
147         // Let's make an image of the right size and copy the possibly
148         // wrong sized one with scaling
149         // NOTE: This is an ugly hack.
150
151         video::IImage *crackimage = driver->createImage(
152                         baseimage->getColorFormat(), dim_crack);
153         
154         assert(crackimage);
155         
156         otherimage->copyToScaling(crackimage);
157         otherimage->drop();
158 #endif
159
160         // Then copy the right part of crackimage to baseimage
161         
162         crackimage->copyToWithAlpha(baseimage, v2s32(0,0),
163                         core::rect<s32>(pos_other, dim),
164                         video::SColor(255,255,255,255),
165                         NULL);
166         
167         crackimage->drop();
168
169         // Create texture from resulting image
170
171         video::ITexture *newtexture = driver->addTexture(newname, baseimage);
172
173         baseimage->drop();
174
175         return newtexture;
176 }
177
178 video::ITexture * SideGrassTextureMod::make(video::ITexture *original,
179                 const char *newname, video::IVideoDriver* driver)
180 {
181         // Size of the base image
182         core::dimension2d<u32> dim(16, 16);
183         // Position to copy the grass to in the base image
184         core::position2d<s32> pos_base(0, 0);
185         // Position to copy the grass from in the grass image
186         core::position2d<s32> pos_other(0, 0);
187
188         video::IImage *baseimage = driver->createImage(original, pos_base, dim);
189         assert(baseimage);
190
191         video::IImage *grassimage = driver->createImageFromFile(porting::getDataPath("grass_side.png").c_str());
192         assert(grassimage);
193         
194         // Then copy the right part of grassimage to baseimage
195         
196         grassimage->copyToWithAlpha(baseimage, v2s32(0,0),
197                         core::rect<s32>(pos_other, dim),
198                         video::SColor(255,255,255,255),
199                         NULL);
200         
201         grassimage->drop();
202
203         // Create texture from resulting image
204
205         video::ITexture *newtexture = driver->addTexture(newname, baseimage);
206
207         baseimage->drop();
208
209         return newtexture;
210 }
211
212 video::ITexture * ProgressBarTextureMod::make(video::ITexture *original,
213                 const char *newname, video::IVideoDriver* driver)
214 {
215         core::position2d<s32> pos_base(0, 0);
216         core::dimension2d<u32> dim = original->getOriginalSize();
217
218         video::IImage *baseimage = driver->createImage(original, pos_base, dim);
219         assert(baseimage);
220         
221         core::dimension2d<u32> size = baseimage->getDimension();
222
223         u32 barheight = 1;
224         u32 barpad_x = 1;
225         u32 barpad_y = 1;
226         u32 barwidth = size.Width - barpad_x*2;
227         v2u32 barpos(barpad_x, size.Height - barheight - barpad_y);
228
229         u32 barvalue_i = (u32)(((float)barwidth * value) + 0.5);
230
231         video::SColor active(255,255,0,0);
232         video::SColor inactive(255,0,0,0);
233         for(u32 x0=0; x0<barwidth; x0++)
234         {
235                 video::SColor *c;
236                 if(x0 < barvalue_i)
237                         c = &active;
238                 else
239                         c = &inactive;
240                 u32 x = x0 + barpos.X;
241                 for(u32 y=barpos.Y; y<barpos.Y+barheight; y++)
242                 {
243                         baseimage->setPixel(x,y, *c);
244                 }
245         }
246         
247         video::ITexture *newtexture = driver->addTexture(newname, baseimage);
248
249         baseimage->drop();
250
251         return newtexture;
252 }
253