]> git.lizzy.rs Git - dragonfireclient.git/blob - src/irrlichtwrapper.cpp
fixed crack animation timing in client
[dragonfireclient.git] / src / irrlichtwrapper.cpp
1 #include "irrlichtwrapper.h"
2
3 IrrlichtWrapper::IrrlichtWrapper(IrrlichtDevice *device)
4 {
5         m_main_thread = get_current_thread_id();
6         m_device_mutex.Init();
7         m_device = device;
8 }
9
10 void IrrlichtWrapper::Run()
11 {
12         /*
13                 Fetch textures
14         */
15         if(m_get_texture_queue.size() > 0)
16         {
17                 GetRequest<TextureSpec, video::ITexture*, u8, u8>
18                                 request = m_get_texture_queue.pop();
19
20                 dstream<<"got texture request with key.name="
21                                 <<request.key.name<<std::endl;
22
23                 GetResult<TextureSpec, video::ITexture*, u8, u8>
24                                 result;
25                 result.key = request.key;
26                 result.callers = request.callers;
27                 result.item = getTextureDirect(request.key);
28
29                 request.dest->push_back(result);
30         }
31 }
32
33 video::ITexture* IrrlichtWrapper::getTexture(TextureSpec spec)
34 {
35         video::ITexture *t = m_texturecache.get(spec.name);
36         if(t != NULL)
37                 return t;
38         
39         if(get_current_thread_id() == m_main_thread)
40         {
41                 dstream<<"Getting texture directly: name="
42                                 <<spec.name<<std::endl;
43                                 
44                 t = getTextureDirect(spec);
45         }
46         else
47         {
48                 // We're gonna ask the result to be put into here
49                 ResultQueue<TextureSpec, video::ITexture*, u8, u8> result_queue;
50                 
51                 // Throw a request in
52                 m_get_texture_queue.add(spec, 0, 0, &result_queue);
53                 
54                 dstream<<"Waiting for texture "<<spec.name<<std::endl;
55
56                 // Wait result
57                 GetResult<TextureSpec, video::ITexture*, u8, u8>
58                                 result = result_queue.pop_front(1000);
59                 
60                 // Check that at least something worked OK
61                 assert(result.key.name == spec.name);
62
63                 t = result.item;
64         }
65
66         // Add to cache and return
67         m_texturecache.set(spec.name, t);
68         return t;
69 }
70
71 video::ITexture* IrrlichtWrapper::getTexture(const std::string &path)
72 {
73         return getTexture(TextureSpec(path, path, NULL));
74 }
75
76 /*
77         Non-thread-safe functions
78 */
79
80 video::ITexture* IrrlichtWrapper::getTextureDirect(TextureSpec spec)
81 {
82         video::IVideoDriver* driver = m_device->getVideoDriver();
83         
84         if(spec.mod == NULL)
85         {
86                 dstream<<"IrrlichtWrapper::getTextureDirect: Loading texture "
87                                 <<spec.path<<std::endl;
88                 return driver->getTexture(spec.path.c_str());
89         }
90
91         dstream<<"IrrlichtWrapper::getTextureDirect: Loading and modifying "
92                         "texture "<<spec.path<<" to make "<<spec.name<<std::endl;
93
94         video::ITexture *base = driver->getTexture(spec.path.c_str());
95         video::ITexture *result = spec.mod->make(base, spec.name.c_str(), driver);
96
97         delete spec.mod;
98         
99         return result;
100 }
101
102 video::ITexture * CrackTextureMod::make(video::ITexture *original,
103                 const char *newname, video::IVideoDriver* driver)
104 {
105         core::dimension2d<u32> dim(16, 16);
106         core::position2d<s32> pos_base(0, 0);
107         core::position2d<s32> pos_other(0, 16 * progression);
108
109         video::IImage *baseimage = driver->createImage(original, pos_base, dim);
110         assert(baseimage);
111         
112         video::ITexture *other = driver->getTexture("../data/crack.png");
113         // We have to get the whole texture because getting a smaller area
114         // messes the whole thing. It is probably a bug in Irrlicht.
115         video::IImage *otherimage = driver->createImage(
116                         other, core::position2d<s32>(0,0), other->getSize());
117
118         assert(otherimage);
119         
120         /*core::rect<s32> clip_rect(v2s32(0,0), dim);
121         otherimage->copyToWithAlpha(baseimage, v2s32(0,0),
122                         core::rect<s32>(pos_other, dim),
123                         video::SColor(255,255,255,255),
124                         &clip_rect);*/
125         
126         otherimage->copyToWithAlpha(baseimage, v2s32(0,0),
127                         core::rect<s32>(pos_other, dim),
128                         video::SColor(255,255,255,255),
129                         NULL);
130         
131         otherimage->drop();
132
133         video::ITexture *newtexture = driver->addTexture(newname, baseimage);
134
135         baseimage->drop();
136
137         return newtexture;
138 }
139
140 #if 0
141 video::ITexture * createAlphaBlitTexture(const char *name, video::ITexture *base,
142                 video::ITexture *other, v2u32 size, v2s32 pos_base, v2s32 pos_other)
143 {
144         if(g_device == NULL)
145                 return NULL;
146         video::IVideoDriver* driver = g_device->getVideoDriver();
147
148         core::dimension2d<u32> dim(size.X, size.Y);
149
150         video::IImage *baseimage = driver->createImage(
151                         base,
152                         core::position2d<s32>(pos_base.X, pos_base.Y),
153                         dim);
154         assert(baseimage);
155
156         video::IImage *otherimage = driver->createImage(
157                         other,
158                         core::position2d<s32>(pos_other.X, pos_other.Y),
159                         dim);
160         assert(sourceimage);
161         
162         otherimage->copyToWithAlpha(baseimage, v2s32(0,0),
163                         core::rect<s32>(v2s32(0,0), dim),
164                         video::SColor(255,255,255,255),
165                         core::rect<s32>(v2s32(0,0), dim));
166         otherimage->drop();
167
168         video::ITexture *newtexture = driver->addTexture(name, baseimage);
169
170         baseimage->drop();
171
172         return newtexture;
173 }
174 #endif
175