]> git.lizzy.rs Git - irrlicht.git/blob - source/Irrlicht/CIrrDeviceStub.cpp
3bad68ae9d17520bd46cf9eb5076d82d19d72479
[irrlicht.git] / source / Irrlicht / CIrrDeviceStub.cpp
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt\r
2 // This file is part of the "Irrlicht Engine".\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h\r
4 \r
5 #include "CIrrDeviceStub.h"\r
6 #include "ISceneManager.h"\r
7 #include "IEventReceiver.h"\r
8 #include "IFileSystem.h"\r
9 #include "IGUIElement.h"\r
10 #include "IGUIEnvironment.h"\r
11 #include "os.h"\r
12 #include "IrrCompileConfig.h"\r
13 #include "CTimer.h"\r
14 #include "CLogger.h"\r
15 #include "irrString.h"\r
16 \r
17 namespace irr\r
18 {\r
19 //! constructor\r
20 CIrrDeviceStub::CIrrDeviceStub(const SIrrlichtCreationParameters& params)\r
21 : IrrlichtDevice(), VideoDriver(0), GUIEnvironment(0), SceneManager(0),\r
22         Timer(0), CursorControl(0), UserReceiver(params.EventReceiver),\r
23         Logger(0), Operator(0), FileSystem(0),\r
24         InputReceivingSceneManager(0), ContextManager(0),\r
25         CreationParams(params), Close(false)\r
26 {\r
27         Timer = new CTimer(params.UsePerformanceTimer);\r
28         if (os::Printer::Logger)\r
29         {\r
30                 os::Printer::Logger->grab();\r
31                 Logger = (CLogger*)os::Printer::Logger;\r
32                 Logger->setReceiver(UserReceiver);\r
33         }\r
34         else\r
35         {\r
36                 Logger = new CLogger(UserReceiver);\r
37                 os::Printer::Logger = Logger;\r
38         }\r
39         Logger->setLogLevel(CreationParams.LoggingLevel);\r
40 \r
41         os::Printer::Logger = Logger;\r
42 \r
43         FileSystem = io::createFileSystem();\r
44 \r
45         core::stringc s = "Irrlicht Engine version ";\r
46         s.append(getVersion());\r
47         os::Printer::log(s.c_str(), ELL_INFORMATION);\r
48 \r
49         checkVersion(params.SDK_version_do_not_use);\r
50 }\r
51 \r
52 \r
53 CIrrDeviceStub::~CIrrDeviceStub()\r
54 {\r
55         if (GUIEnvironment)\r
56                 GUIEnvironment->drop();\r
57 \r
58         if (SceneManager)\r
59                 SceneManager->drop();\r
60 \r
61         if (VideoDriver)\r
62                 VideoDriver->drop();\r
63 \r
64         if (ContextManager)\r
65                 ContextManager->drop();\r
66 \r
67         if ( FileSystem )\r
68                 FileSystem->drop();\r
69 \r
70         if (InputReceivingSceneManager)\r
71                 InputReceivingSceneManager->drop();\r
72 \r
73         if (CursorControl)\r
74                 CursorControl->drop();\r
75 \r
76         if (Operator)\r
77                 Operator->drop();\r
78 \r
79         CursorControl = 0;\r
80 \r
81         if (Timer)\r
82                 Timer->drop();\r
83 \r
84         if (Logger->drop())\r
85                 os::Printer::Logger = 0;\r
86 }\r
87 \r
88 \r
89 void CIrrDeviceStub::createGUIAndScene()\r
90 {\r
91         #ifdef _IRR_COMPILE_WITH_GUI_\r
92         // create gui environment\r
93         GUIEnvironment = gui::createGUIEnvironment(FileSystem, VideoDriver, Operator);\r
94         #endif\r
95 \r
96         // create Scene manager\r
97         SceneManager = scene::createSceneManager(VideoDriver, FileSystem, CursorControl, GUIEnvironment);\r
98 \r
99         setEventReceiver(UserReceiver);\r
100 }\r
101 \r
102 \r
103 //! returns the video driver\r
104 video::IVideoDriver* CIrrDeviceStub::getVideoDriver()\r
105 {\r
106         return VideoDriver;\r
107 }\r
108 \r
109 \r
110 //! return file system\r
111 io::IFileSystem* CIrrDeviceStub::getFileSystem()\r
112 {\r
113         return FileSystem;\r
114 }\r
115 \r
116 \r
117 \r
118 //! returns the gui environment\r
119 gui::IGUIEnvironment* CIrrDeviceStub::getGUIEnvironment()\r
120 {\r
121         return GUIEnvironment;\r
122 }\r
123 \r
124 \r
125 \r
126 //! returns the scene manager\r
127 scene::ISceneManager* CIrrDeviceStub::getSceneManager()\r
128 {\r
129         return SceneManager;\r
130 }\r
131 \r
132 \r
133 //! \return Returns a pointer to the ITimer object. With it the\r
134 //! current Time can be received.\r
135 ITimer* CIrrDeviceStub::getTimer()\r
136 {\r
137         return Timer;\r
138 }\r
139 \r
140 \r
141 //! Returns the version of the engine.\r
142 const char* CIrrDeviceStub::getVersion() const\r
143 {\r
144         return IRRLICHT_SDK_VERSION;\r
145 }\r
146 \r
147 //! \return Returns a pointer to the mouse cursor control interface.\r
148 gui::ICursorControl* CIrrDeviceStub::getCursorControl()\r
149 {\r
150         return CursorControl;\r
151 }\r
152 \r
153 \r
154 //! return the context manager\r
155 video::IContextManager* CIrrDeviceStub::getContextManager()\r
156 {\r
157         return ContextManager;\r
158 }\r
159 \r
160 //! checks version of sdk and prints warning if there might be a problem\r
161 bool CIrrDeviceStub::checkVersion(const char* version)\r
162 {\r
163         if (strcmp(getVersion(), version))\r
164         {\r
165                 core::stringc w;\r
166                 w = "Warning: The library version of the Irrlicht Engine (";\r
167                 w += getVersion();\r
168                 w += ") does not match the version the application was compiled with (";\r
169                 w += version;\r
170                 w += "). This may cause problems.";\r
171                 os::Printer::log(w.c_str(), ELL_WARNING);\r
172 \r
173                 return false;\r
174         }\r
175 \r
176         return true;\r
177 }\r
178 \r
179 \r
180 //! Compares to the last call of this function to return double and triple clicks.\r
181 u32 CIrrDeviceStub::checkSuccessiveClicks(s32 mouseX, s32 mouseY, EMOUSE_INPUT_EVENT inputEvent )\r
182 {\r
183         const s32 MAX_MOUSEMOVE = 3;\r
184 \r
185         irr::u32 clickTime = getTimer()->getRealTime();\r
186 \r
187         if ( (clickTime-MouseMultiClicks.LastClickTime) < MouseMultiClicks.DoubleClickTime\r
188                 && core::abs_(MouseMultiClicks.LastClick.X - mouseX ) <= MAX_MOUSEMOVE\r
189                 && core::abs_(MouseMultiClicks.LastClick.Y - mouseY ) <= MAX_MOUSEMOVE\r
190                 && MouseMultiClicks.CountSuccessiveClicks < 3\r
191                 && MouseMultiClicks.LastMouseInputEvent == inputEvent\r
192            )\r
193         {\r
194                 ++MouseMultiClicks.CountSuccessiveClicks;\r
195         }\r
196         else\r
197         {\r
198                 MouseMultiClicks.CountSuccessiveClicks = 1;\r
199         }\r
200 \r
201         MouseMultiClicks.LastMouseInputEvent = inputEvent;\r
202         MouseMultiClicks.LastClickTime = clickTime;\r
203         MouseMultiClicks.LastClick.X = mouseX;\r
204         MouseMultiClicks.LastClick.Y = mouseY;\r
205 \r
206         return MouseMultiClicks.CountSuccessiveClicks;\r
207 }\r
208 \r
209 \r
210 //! send the event to the right receiver\r
211 bool CIrrDeviceStub::postEventFromUser(const SEvent& event)\r
212 {\r
213         bool absorbed = false;\r
214 \r
215         if (UserReceiver)\r
216                 absorbed = UserReceiver->OnEvent(event);\r
217 \r
218         if (!absorbed && GUIEnvironment)\r
219                 absorbed = GUIEnvironment->postEventFromUser(event);\r
220 \r
221         scene::ISceneManager* inputReceiver = InputReceivingSceneManager;\r
222         if (!inputReceiver)\r
223                 inputReceiver = SceneManager;\r
224 \r
225         if (!absorbed && inputReceiver)\r
226                 absorbed = inputReceiver->postEventFromUser(event);\r
227 \r
228         return absorbed;\r
229 }\r
230 \r
231 \r
232 //! Sets a new event receiver to receive events\r
233 void CIrrDeviceStub::setEventReceiver(IEventReceiver* receiver)\r
234 {\r
235         UserReceiver = receiver;\r
236         Logger->setReceiver(receiver);\r
237         if (GUIEnvironment)\r
238                 GUIEnvironment->setUserEventReceiver(receiver);\r
239 }\r
240 \r
241 \r
242 //! Returns poinhter to the current event receiver. Returns 0 if there is none.\r
243 IEventReceiver* CIrrDeviceStub::getEventReceiver()\r
244 {\r
245         return UserReceiver;\r
246 }\r
247 \r
248 \r
249 //! \return Returns a pointer to the logger.\r
250 ILogger* CIrrDeviceStub::getLogger()\r
251 {\r
252         return Logger;\r
253 }\r
254 \r
255 \r
256 //! Returns the operation system opertator object.\r
257 IOSOperator* CIrrDeviceStub::getOSOperator()\r
258 {\r
259         return Operator;\r
260 }\r
261 \r
262 \r
263 //! Sets the input receiving scene manager.\r
264 void CIrrDeviceStub::setInputReceivingSceneManager(scene::ISceneManager* sceneManager)\r
265 {\r
266         if (sceneManager)\r
267                 sceneManager->grab();\r
268         if (InputReceivingSceneManager)\r
269                 InputReceivingSceneManager->drop();\r
270 \r
271         InputReceivingSceneManager = sceneManager;\r
272 }\r
273 \r
274 \r
275 //! Checks if the window is running in fullscreen mode\r
276 bool CIrrDeviceStub::isFullscreen() const\r
277 {\r
278         return CreationParams.Fullscreen;\r
279 }\r
280 \r
281 \r
282 //! returns color format\r
283 video::ECOLOR_FORMAT CIrrDeviceStub::getColorFormat() const\r
284 {\r
285         return video::ECF_R5G6B5;\r
286 }\r
287 \r
288 //! No-op in this implementation\r
289 bool CIrrDeviceStub::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)\r
290 {\r
291         return false;\r
292 }\r
293 \r
294 //! No-op in this implementation\r
295 bool CIrrDeviceStub::activateAccelerometer(float updateInterval)\r
296 {\r
297     return false;\r
298 }\r
299 \r
300 //! No-op in this implementation\r
301 bool CIrrDeviceStub::deactivateAccelerometer()\r
302 {\r
303     return false;\r
304 }\r
305 \r
306 //! No-op in this implementation\r
307 bool CIrrDeviceStub::isAccelerometerActive()\r
308 {\r
309     return false;\r
310 }\r
311 \r
312 //! No-op in this implementation\r
313 bool CIrrDeviceStub::isAccelerometerAvailable()\r
314 {\r
315     return false;\r
316 }\r
317 \r
318 //! No-op in this implementation\r
319 bool CIrrDeviceStub::activateGyroscope(float updateInterval)\r
320 {\r
321     return false;\r
322 }\r
323 \r
324 //! No-op in this implementation\r
325 bool CIrrDeviceStub::deactivateGyroscope()\r
326 {\r
327     return false;\r
328 }\r
329 \r
330 //! No-op in this implementation\r
331 bool CIrrDeviceStub::isGyroscopeActive()\r
332 {\r
333     return false;\r
334 }\r
335 \r
336 //! No-op in this implementation\r
337 bool CIrrDeviceStub::isGyroscopeAvailable()\r
338 {\r
339     return false;\r
340 }\r
341 \r
342 //! No-op in this implementation\r
343 bool CIrrDeviceStub::activateDeviceMotion(float updateInterval)\r
344 {\r
345     return false;\r
346 }\r
347 \r
348 //! No-op in this implementation\r
349 bool CIrrDeviceStub::deactivateDeviceMotion()\r
350 {\r
351     return false;\r
352 }\r
353 \r
354 //! No-op in this implementation\r
355 bool CIrrDeviceStub::isDeviceMotionActive()\r
356 {\r
357     return false;\r
358 }\r
359 \r
360 //! No-op in this implementation\r
361 bool CIrrDeviceStub::isDeviceMotionAvailable()\r
362 {\r
363     return false;\r
364 }\r
365 \r
366 /*!\r
367 */\r
368 void CIrrDeviceStub::calculateGammaRamp ( u16 *ramp, f32 gamma, f32 relativebrightness, f32 relativecontrast )\r
369 {\r
370         s32 i;\r
371         s32 value;\r
372         s32 rbright = (s32) ( relativebrightness * (65535.f / 4 ) );\r
373         f32 rcontrast = 1.f / (255.f - ( relativecontrast * 127.5f ) );\r
374 \r
375         gamma = gamma > 0.f ? 1.0f / gamma : 0.f;\r
376 \r
377         for ( i = 0; i < 256; ++i )\r
378         {\r
379                 value = (s32)(pow( rcontrast * i, gamma)*65535.f + 0.5f );\r
380                 ramp[i] = (u16) core::s32_clamp ( value + rbright, 0, 65535 );\r
381         }\r
382 \r
383 }\r
384 \r
385 void CIrrDeviceStub::calculateGammaFromRamp ( f32 &gamma, const u16 *ramp )\r
386 {\r
387         /* The following is adapted from a post by Garrett Bass on OpenGL\r
388         Gamedev list, March 4, 2000.\r
389         */\r
390         f32 sum = 0.0;\r
391         s32 i, count = 0;\r
392 \r
393         gamma = 1.0;\r
394         for ( i = 1; i < 256; ++i ) {\r
395                 if ( (ramp[i] != 0) && (ramp[i] != 65535) ) {\r
396                         f32 B = (f32)i / 256.f;\r
397                         f32 A = ramp[i] / 65535.f;\r
398                         sum += (f32) ( logf(A) / logf(B) );\r
399                         count++;\r
400                 }\r
401         }\r
402         if ( count && sum ) {\r
403                 gamma = 1.0f / (sum / count);\r
404         }\r
405 \r
406 }\r
407 \r
408 //! Set the current Gamma Value for the Display\r
409 bool CIrrDeviceStub::setGammaRamp( f32 red, f32 green, f32 blue, f32 brightness, f32 contrast )\r
410 {\r
411         return false;\r
412 }\r
413 \r
414 //! Get the current Gamma Value for the Display\r
415 bool CIrrDeviceStub::getGammaRamp( f32 &red, f32 &green, f32 &blue, f32 &brightness, f32 &contrast )\r
416 {\r
417         return false;\r
418 }\r
419 \r
420 //! Set the maximal elapsed time between 2 clicks to generate doubleclicks for the mouse. It also affects tripleclick behavior.\r
421 void CIrrDeviceStub::setDoubleClickTime( u32 timeMs )\r
422 {\r
423         MouseMultiClicks.DoubleClickTime = timeMs;\r
424 }\r
425 \r
426 //! Get the maximal elapsed time between 2 clicks to generate double- and tripleclicks for the mouse.\r
427 u32 CIrrDeviceStub::getDoubleClickTime() const\r
428 {\r
429         return MouseMultiClicks.DoubleClickTime;\r
430 }\r
431 \r
432 //! Remove all messages pending in the system message loop\r
433 void CIrrDeviceStub::clearSystemMessages()\r
434 {\r
435 }\r
436 \r
437 //! Checks whether the input device should take input from the IME\r
438 bool CIrrDeviceStub::acceptsIME()\r
439 {\r
440         if (!GUIEnvironment)\r
441                 return false;\r
442         gui::IGUIElement *elem = GUIEnvironment->getFocus();\r
443         return elem && elem->acceptsIME();\r
444 }\r
445 \r
446 \r
447 } // end namespace irr\r
448 \r