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 // 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);
124 video::IImage *baseimage = driver->createImage(original, pos_base, dim);
127 video::IImage *crackimage = driver->createImageFromFile(porting::getDataPath("crack.png").c_str());
131 video::ITexture *other = driver->getTexture(porting::getDataPath("crack.png").c_str());
133 dstream<<__FUNCTION_NAME<<": crack texture size is "
134 <<other->getSize().Width<<"x"
135 <<other->getSize().Height<<std::endl;
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());
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.
151 video::IImage *crackimage = driver->createImage(
152 baseimage->getColorFormat(), dim_crack);
156 otherimage->copyToScaling(crackimage);
160 // Then copy the right part of crackimage to baseimage
162 crackimage->copyToWithAlpha(baseimage, v2s32(0,0),
163 core::rect<s32>(pos_other, dim),
164 video::SColor(255,255,255,255),
169 // Create texture from resulting image
171 video::ITexture *newtexture = driver->addTexture(newname, baseimage);
178 video::ITexture * ProgressBarTextureMod::make(video::ITexture *original,
179 const char *newname, video::IVideoDriver* driver)
181 core::position2d<s32> pos_base(0, 0);
182 core::dimension2d<u32> dim = original->getOriginalSize();
184 video::IImage *baseimage = driver->createImage(original, pos_base, dim);
187 core::dimension2d<u32> size = baseimage->getDimension();
192 u32 barwidth = size.Width - barpad_x*2;
193 v2u32 barpos(barpad_x, size.Height - barheight - barpad_y);
195 u32 barvalue_i = (u32)(((float)barwidth * value) + 0.5);
197 video::SColor active(255,255,0,0);
198 video::SColor inactive(255,0,0,0);
199 for(u32 x0=0; x0<barwidth; x0++)
206 u32 x = x0 + barpos.X;
207 for(u32 y=barpos.Y; y<barpos.Y+barheight; y++)
209 baseimage->setPixel(x,y, *c);
213 video::ITexture *newtexture = driver->addTexture(newname, baseimage);