1 #include "irrlichtwrapper.h"
4 IrrlichtWrapper::IrrlichtWrapper(IrrlichtDevice *device)
6 m_main_thread = get_current_thread_id();
11 void IrrlichtWrapper::Run()
16 if(m_get_texture_queue.size() > 0)
18 GetRequest<TextureSpec, video::ITexture*, u8, u8>
19 request = m_get_texture_queue.pop();
21 dstream<<"got texture request with key.name="
22 <<request.key.name<<std::endl;
24 GetResult<TextureSpec, video::ITexture*, u8, u8>
26 result.key = request.key;
27 result.callers = request.callers;
28 result.item = getTextureDirect(request.key);
30 request.dest->push_back(result);
34 video::ITexture* IrrlichtWrapper::getTexture(TextureSpec spec)
36 video::ITexture *t = m_texturecache.get(spec.name);
40 if(get_current_thread_id() == m_main_thread)
42 dstream<<"Getting texture directly: name="
43 <<spec.name<<std::endl;
45 t = getTextureDirect(spec);
49 // We're gonna ask the result to be put into here
50 ResultQueue<TextureSpec, video::ITexture*, u8, u8> result_queue;
53 m_get_texture_queue.add(spec, 0, 0, &result_queue);
55 dstream<<"Waiting for texture from main thread: "
56 <<spec.name<<std::endl;
60 // Wait result for a second
61 GetResult<TextureSpec, video::ITexture*, u8, u8>
62 result = result_queue.pop_front(1000);
64 // Check that at least something worked OK
65 assert(result.key.name == spec.name);
69 catch(ItemNotFoundException &e)
71 dstream<<"Waiting for texture timed out."<<std::endl;
76 // Add to cache and return
77 m_texturecache.set(spec.name, t);
81 video::ITexture* IrrlichtWrapper::getTexture(const std::string &path)
83 return getTexture(TextureSpec(path, path, NULL));
87 Non-thread-safe functions
90 video::ITexture* IrrlichtWrapper::getTextureDirect(TextureSpec spec)
92 video::IVideoDriver* driver = m_device->getVideoDriver();
96 dstream<<"IrrlichtWrapper::getTextureDirect: Loading texture "
97 <<spec.path<<std::endl;
98 return driver->getTexture(spec.path.c_str());
101 dstream<<"IrrlichtWrapper::getTextureDirect: Loading and modifying "
102 "texture "<<spec.path<<" to make "<<spec.name<<std::endl;
104 video::ITexture *base = driver->getTexture(spec.path.c_str());
105 video::ITexture *result = spec.mod->make(base, spec.name.c_str(), driver);
112 video::ITexture * CrackTextureMod::make(video::ITexture *original,
113 const char *newname, video::IVideoDriver* driver)
115 core::dimension2d<u32> dim(16, 16);
116 core::position2d<s32> pos_base(0, 0);
117 core::position2d<s32> pos_other(0, 16 * progression);
119 video::IImage *baseimage = driver->createImage(original, pos_base, dim);
122 video::ITexture *other = driver->getTexture("../data/crack.png");
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));
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),
143 otherimage->copyToWithAlpha(baseimage, v2s32(0,0),
144 core::rect<s32>(pos_other, dim),
145 video::SColor(255,255,255,255),
150 video::ITexture *newtexture = driver->addTexture(newname, baseimage);
157 video::ITexture * ProgressBarTextureMod::make(video::ITexture *original,
158 const char *newname, video::IVideoDriver* driver)
160 core::position2d<s32> pos_base(0, 0);
161 core::dimension2d<u32> dim = original->getOriginalSize();
163 video::IImage *baseimage = driver->createImage(original, pos_base, dim);
166 core::dimension2d<u32> size = baseimage->getDimension();
171 u32 barwidth = size.Width - barpad_x*2;
172 v2u32 barpos(barpad_x, size.Height - barheight - barpad_y);
174 u32 barvalue_i = (u32)(((float)barwidth * value) + 0.5);
176 video::SColor active(255,255,0,0);
177 video::SColor inactive(255,0,0,0);
178 for(u32 x0=0; x0<barwidth; x0++)
185 u32 x = x0 + barpos.X;
186 for(u32 y=barpos.Y; y<barpos.Y+barheight; y++)
188 baseimage->setPixel(x,y, *c);
192 video::ITexture *newtexture = driver->addTexture(newname, baseimage);