1 #include "irrlichtwrapper.h"
3 IrrlichtWrapper::IrrlichtWrapper(IrrlichtDevice *device)
5 m_main_thread = get_current_thread_id();
10 void IrrlichtWrapper::Run()
15 if(m_get_texture_queue.size() > 0)
17 GetRequest<TextureSpec, video::ITexture*, u8, u8>
18 request = m_get_texture_queue.pop();
20 dstream<<"got texture request with key.name="
21 <<request.key.name<<std::endl;
23 GetResult<TextureSpec, video::ITexture*, u8, u8>
25 result.key = request.key;
26 result.callers = request.callers;
27 result.item = getTextureDirect(request.key);
29 request.dest->push_back(result);
33 video::ITexture* IrrlichtWrapper::getTexture(TextureSpec spec)
35 video::ITexture *t = m_texturecache.get(spec.name);
39 if(get_current_thread_id() == m_main_thread)
41 dstream<<"Getting texture directly: name="
42 <<spec.name<<std::endl;
44 t = getTextureDirect(spec);
48 // We're gonna ask the result to be put into here
49 ResultQueue<TextureSpec, video::ITexture*, u8, u8> result_queue;
52 m_get_texture_queue.add(spec, 0, 0, &result_queue);
54 dstream<<"Waiting for texture from main thread: "
55 <<spec.name<<std::endl;
59 // Wait result for a second
60 GetResult<TextureSpec, video::ITexture*, u8, u8>
61 result = result_queue.pop_front(1000);
63 // Check that at least something worked OK
64 assert(result.key.name == spec.name);
68 catch(ItemNotFoundException &e)
70 dstream<<"Waiting for texture timed out."<<std::endl;
75 // Add to cache and return
76 m_texturecache.set(spec.name, t);
80 video::ITexture* IrrlichtWrapper::getTexture(const std::string &path)
82 return getTexture(TextureSpec(path, path, NULL));
86 Non-thread-safe functions
89 video::ITexture* IrrlichtWrapper::getTextureDirect(TextureSpec spec)
91 video::IVideoDriver* driver = m_device->getVideoDriver();
95 dstream<<"IrrlichtWrapper::getTextureDirect: Loading texture "
96 <<spec.path<<std::endl;
97 return driver->getTexture(spec.path.c_str());
100 dstream<<"IrrlichtWrapper::getTextureDirect: Loading and modifying "
101 "texture "<<spec.path<<" to make "<<spec.name<<std::endl;
103 video::ITexture *base = driver->getTexture(spec.path.c_str());
104 video::ITexture *result = spec.mod->make(base, spec.name.c_str(), driver);
111 video::ITexture * CrackTextureMod::make(video::ITexture *original,
112 const char *newname, video::IVideoDriver* driver)
114 core::dimension2d<u32> dim(16, 16);
115 core::position2d<s32> pos_base(0, 0);
116 core::position2d<s32> pos_other(0, 16 * progression);
118 video::IImage *baseimage = driver->createImage(original, pos_base, dim);
121 video::ITexture *other = driver->getTexture("../data/crack.png");
122 // We have to get the whole texture because getting a smaller area
123 // messes the whole thing. It is probably a bug in Irrlicht.
124 video::IImage *otherimage = driver->createImage(
125 other, core::position2d<s32>(0,0), other->getSize());
129 /*core::rect<s32> clip_rect(v2s32(0,0), dim);
130 otherimage->copyToWithAlpha(baseimage, v2s32(0,0),
131 core::rect<s32>(pos_other, dim),
132 video::SColor(255,255,255,255),
135 otherimage->copyToWithAlpha(baseimage, v2s32(0,0),
136 core::rect<s32>(pos_other, dim),
137 video::SColor(255,255,255,255),
142 video::ITexture *newtexture = driver->addTexture(newname, baseimage);
149 video::ITexture * ProgressBarTextureMod::make(video::ITexture *original,
150 const char *newname, video::IVideoDriver* driver)
152 core::position2d<s32> pos_base(0, 0);
153 core::dimension2d<u32> dim = original->getOriginalSize();
155 video::IImage *baseimage = driver->createImage(original, pos_base, dim);
158 core::dimension2d<u32> size = baseimage->getDimension();
163 u32 barwidth = size.Width - barpad_x*2;
164 v2u32 barpos(barpad_x, size.Height - barheight - barpad_y);
166 u32 barvalue_i = round((float)barwidth * value);
168 video::SColor active(255,255,0,0);
169 video::SColor inactive(255,0,0,0);
170 for(u32 x0=0; x0<barwidth; x0++)
177 u32 x = x0 + barpos.X;
178 for(u32 y=barpos.Y; y<barpos.Y+barheight; y++)
180 baseimage->setPixel(x,y, *c);
184 video::ITexture *newtexture = driver->addTexture(newname, baseimage);