]> git.lizzy.rs Git - minetest.git/blob - src/irrlichtwrapper.cpp
added some missing files
[minetest.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 request with key.name="<<request.key.name<<std::endl;
21
22                 GetResult<TextureSpec, video::ITexture*, u8, u8>
23                                 result;
24                 result.key = request.key;
25                 result.callers = request.callers;
26                 result.item = getTextureDirect(request.key);
27
28                 request.dest->push_back(result);
29         }
30 }
31
32 video::ITexture* IrrlichtWrapper::getTexture(TextureSpec spec)
33 {
34         video::ITexture *t = m_texturecache.get(spec.name);
35         if(t != NULL)
36                 return t;
37         
38         if(get_current_thread_id() == m_main_thread)
39         {
40                 dstream<<"Loading texture directly: "<<spec.name<<std::endl;
41                 t = getTextureDirect(spec);
42         }
43         else
44         {
45                 // We're gonna ask the result to be put into here
46                 ResultQueue<TextureSpec, video::ITexture*, u8, u8> result_queue;
47                 
48                 // Throw a request in
49                 m_get_texture_queue.add(spec, 0, 0, &result_queue);
50                 
51                 dstream<<"Waiting for texture "<<spec.name<<std::endl;
52
53                 // Wait result
54                 GetResult<TextureSpec, video::ITexture*, u8, u8>
55                                 result = result_queue.pop_front(true);
56                 
57                 // Check that at least something worked OK
58                 assert(result.key.name == spec.name);
59
60                 t = result.item;
61         }
62
63         // Add to cache and return
64         m_texturecache.set(spec.name, t);
65         return t;
66 }
67
68 video::ITexture* IrrlichtWrapper::getTexture(const std::string &path)
69 {
70         /*TextureSpec spec;
71         spec.name = path;
72         spec.path = path;
73         return getTexture(spec);*/
74         return getTexture(TextureSpec(path, path, NULL));
75 }
76
77 /*
78         Non-thread-safe functions
79 */
80
81 video::ITexture* IrrlichtWrapper::getTextureDirect(TextureSpec spec)
82 {
83         video::IVideoDriver* driver = m_device->getVideoDriver();
84         //TODO
85         if(spec.mod != NULL)
86         {
87                 dstream<<"IrrlichtWrapper::getTextureDirect: Modified textures"
88                                 " not supported"<<std::endl;
89         }
90         return driver->getTexture(spec.path.c_str());
91 }
92
93 video::ITexture * CrackTextureMod::make(video::ITexture *original,
94                 video::IVideoDriver* driver)
95 {
96         //TODO
97         dstream<<__FUNCTION_NAME<<std::endl;
98         return NULL;
99 }
100
101 #if 0
102 video::ITexture * createAlphaBlitTexture(const char *name, video::ITexture *base,
103                 video::ITexture *other, v2u32 size, v2s32 pos_base, v2s32 pos_other)
104 {
105         if(g_device == NULL)
106                 return NULL;
107         video::IVideoDriver* driver = g_device->getVideoDriver();
108
109         core::dimension2d<u32> dim(size.X, size.Y);
110
111         video::IImage *baseimage = driver->createImage(
112                         base,
113                         core::position2d<s32>(pos_base.X, pos_base.Y),
114                         dim);
115         assert(baseimage);
116
117         video::IImage *otherimage = driver->createImage(
118                         other,
119                         core::position2d<s32>(pos_other.X, pos_other.Y),
120                         dim);
121         assert(sourceimage);
122         
123         otherimage->copyToWithAlpha(baseimage, v2s32(0,0),
124                         core::rect<s32>(v2s32(0,0), dim),
125                         video::SColor(255,255,255,255),
126                         core::rect<s32>(v2s32(0,0), dim));
127         otherimage->drop();
128
129         video::ITexture *newtexture = driver->addTexture(name, baseimage);
130
131         baseimage->drop();
132
133         return newtexture;
134 }
135 #endif
136