1 #include "irrlichtwrapper.h"
6 IrrlichtWrapper::IrrlichtWrapper(IrrlichtDevice *device)
8 m_main_thread = get_current_thread_id();
13 void IrrlichtWrapper::Run()
18 if(m_get_texture_queue.size() > 0)
20 GetRequest<std::string, video::ITexture*, u8, u8>
21 request = m_get_texture_queue.pop();
23 dstream<<"got texture request with key="
24 <<request.key<<std::endl;
26 GetResult<std::string, video::ITexture*, u8, u8>
28 result.key = request.key;
29 result.callers = request.callers;
30 result.item = getTextureDirect(request.key);
32 request.dest->push_back(result);
36 video::ITexture* IrrlichtWrapper::getTexture(const std::string &spec)
41 video::ITexture *t = m_texturecache.get(spec);
45 if(get_current_thread_id() == m_main_thread)
47 dstream<<"Getting texture directly: spec="
50 t = getTextureDirect(spec);
54 // We're gonna ask the result to be put into here
55 ResultQueue<std::string, video::ITexture*, u8, u8> result_queue;
58 m_get_texture_queue.add(spec, 0, 0, &result_queue);
60 dstream<<"Waiting for texture from main thread: "
65 // Wait result for a second
66 GetResult<std::string, video::ITexture*, u8, u8>
67 result = result_queue.pop_front(1000);
69 // Check that at least something worked OK
70 assert(result.key == spec);
74 catch(ItemNotFoundException &e)
76 dstream<<"Waiting for texture timed out."<<std::endl;
81 // Add to cache and return
82 m_texturecache.set(spec, t);
87 Non-thread-safe functions
91 Texture modifier functions
94 // blitted_name = eg. "mineral_coal.png"
95 video::ITexture * make_blitname(const std::string &blitted_name,
96 video::ITexture *original,
97 const char *newname, video::IVideoDriver* driver)
102 // Size of the base image
103 core::dimension2d<u32> dim(16, 16);
104 // Position to copy the blitted to in the base image
105 core::position2d<s32> pos_base(0, 0);
106 // Position to copy the blitted from in the blitted image
107 core::position2d<s32> pos_other(0, 0);
109 video::IImage *baseimage = driver->createImage(original, pos_base, dim);
112 video::IImage *blittedimage = driver->createImageFromFile(porting::getDataPath(blitted_name.c_str()).c_str());
113 assert(blittedimage);
115 // Then copy the right part of blittedimage to baseimage
117 blittedimage->copyToWithAlpha(baseimage, v2s32(0,0),
118 core::rect<s32>(pos_other, dim),
119 video::SColor(255,255,255,255),
122 blittedimage->drop();
124 // Create texture from resulting image
126 video::ITexture *newtexture = driver->addTexture(newname, baseimage);
133 video::ITexture * make_crack(u16 progression, video::ITexture *original,
134 const char *newname, video::IVideoDriver* driver)
139 // Size of the base image
140 core::dimension2d<u32> dim(16, 16);
141 // Size of the crack image
142 //core::dimension2d<u32> dim_crack(16, 16 * CRACK_ANIMATION_LENGTH);
143 // Position to copy the crack to in the base image
144 core::position2d<s32> pos_base(0, 0);
145 // Position to copy the crack from in the crack image
146 core::position2d<s32> pos_other(0, 16 * progression);
148 video::IImage *baseimage = driver->createImage(original, pos_base, dim);
151 video::IImage *crackimage = driver->createImageFromFile(porting::getDataPath("crack.png").c_str());
154 // Then copy the right part of crackimage to baseimage
156 crackimage->copyToWithAlpha(baseimage, v2s32(0,0),
157 core::rect<s32>(pos_other, dim),
158 video::SColor(255,255,255,255),
163 // Create texture from resulting image
165 video::ITexture *newtexture = driver->addTexture(newname, baseimage);
173 video::ITexture * make_sidegrass(video::ITexture *original,
174 const char *newname, video::IVideoDriver* driver)
179 // Size of the base image
180 core::dimension2d<u32> dim(16, 16);
181 // Position to copy the grass to in the base image
182 core::position2d<s32> pos_base(0, 0);
183 // Position to copy the grass from in the grass image
184 core::position2d<s32> pos_other(0, 0);
186 video::IImage *baseimage = driver->createImage(original, pos_base, dim);
189 video::IImage *grassimage = driver->createImageFromFile(porting::getDataPath("grass_side.png").c_str());
192 // Then copy the right part of grassimage to baseimage
194 grassimage->copyToWithAlpha(baseimage, v2s32(0,0),
195 core::rect<s32>(pos_other, dim),
196 video::SColor(255,255,255,255),
201 // Create texture from resulting image
203 video::ITexture *newtexture = driver->addTexture(newname, baseimage);
211 video::ITexture * make_progressbar(float value, video::ITexture *original,
212 const char *newname, video::IVideoDriver* driver)
217 core::position2d<s32> pos_base(0, 0);
218 core::dimension2d<u32> dim = original->getOriginalSize();
220 video::IImage *baseimage = driver->createImage(original, pos_base, dim);
223 core::dimension2d<u32> size = baseimage->getDimension();
228 u32 barwidth = size.Width - barpad_x*2;
229 v2u32 barpos(barpad_x, size.Height - barheight - barpad_y);
231 u32 barvalue_i = (u32)(((float)barwidth * value) + 0.5);
233 video::SColor active(255,255,0,0);
234 video::SColor inactive(255,0,0,0);
235 for(u32 x0=0; x0<barwidth; x0++)
242 u32 x = x0 + barpos.X;
243 for(u32 y=barpos.Y; y<barpos.Y+barheight; y++)
245 baseimage->setPixel(x,y, *c);
249 video::ITexture *newtexture = driver->addTexture(newname, baseimage);
257 Texture fetcher/maker function, called always from the main thread
260 video::ITexture* IrrlichtWrapper::getTextureDirect(const std::string &spec)
265 video::IVideoDriver* driver = m_device->getVideoDriver();
268 Input (spec) is something like this:
269 "/usr/share/minetest/stone.png[[mod:mineral0[[mod:crack3"
272 video::ITexture* t = NULL;
273 std::string modmagic = "[[mod:";
275 std::string path = f.next(modmagic);
276 t = driver->getTexture(path.c_str());
277 std::string texture_name = path;
278 while(f.atend() == false)
280 std::string mod = f.next(modmagic);
281 texture_name += modmagic + mod;
282 dstream<<"Making texture \""<<texture_name<<"\""<<std::endl;
283 /*if(mod == "sidegrass")
285 t = make_sidegrass(t, texture_name.c_str(), driver);
288 if(mod.substr(0, 9) == "blitname:")
290 //t = make_sidegrass(t, texture_name.c_str(), driver);
291 t = make_blitname(mod.substr(9), t, texture_name.c_str(), driver);
293 else if(mod.substr(0,5) == "crack")
295 u16 prog = stoi(mod.substr(5));
296 t = make_crack(prog, t, texture_name.c_str(), driver);
298 else if(mod.substr(0,11) == "progressbar")
300 float value = stof(mod.substr(11));
301 t = make_progressbar(value, t, texture_name.c_str(), driver);
305 dstream<<"Invalid texture mod: \""<<mod<<"\""<<std::endl;
311 video::ITexture* t = NULL;
312 const char *modmagic = "[[mod:";
313 const s32 modmagic_len = 6;
317 } readmode = READMODE_PATH;
318 s32 specsize = spec.size()+1;
319 char *strcache = (char*)malloc(specsize);
323 // Next index of modmagic to be found
328 strcache[length++] = spec[i];
330 bool got_modmagic = false;
335 if(spec[i] == modmagic[modmagic_i])
338 if(modmagic_i == modmagic_len)
342 length -= modmagic_len;
348 // Set i to be the length of read string
351 if(got_modmagic || i >= spec.size())
353 strcache[length] = '\0';
354 // Now our string is in strcache, ending in \0
356 if(readmode == READMODE_PATH)
358 // Get initial texture (strcache is path)
360 t = driver->getTexture(strcache);
361 readmode = READMODE_MOD;
363 strcache = (char*)malloc(specsize);
368 dstream<<"Parsing mod \""<<strcache<<"\""<<std::endl;
369 // The name of the result of adding this mod.
370 // This doesn't have to be fast so std::string is used.
371 std::string name(path);
374 dstream<<"Name of modded texture is \""<<name<<"\""
377 if(strcmp(strcache, "sidegrass") == 0)
379 t = make_sidegrass(t, name.c_str(), driver);
383 dstream<<"Invalid texture mod"<<std::endl;
394 /*if(spec.mod == NULL)
396 dstream<<"IrrlichtWrapper::getTextureDirect: Loading texture "
397 <<spec.path<<std::endl;
398 return driver->getTexture(spec.path.c_str());
401 dstream<<"IrrlichtWrapper::getTextureDirect: Loading and modifying "
402 "texture "<<spec.path<<" to make "<<spec.name<<std::endl;
404 video::ITexture *base = driver->getTexture(spec.path.c_str());
405 video::ITexture *result = spec.mod->make(base, spec.name.c_str(), driver);