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
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
12 #include "IrrCompileConfig.h"
\r
14 #include "CLogger.h"
\r
15 #include "irrString.h"
\r
16 #include "IRandomizer.h"
\r
21 CIrrDeviceStub::CIrrDeviceStub(const SIrrlichtCreationParameters& params)
\r
22 : IrrlichtDevice(), VideoDriver(0), GUIEnvironment(0), SceneManager(0),
\r
23 Timer(0), CursorControl(0), UserReceiver(params.EventReceiver),
\r
24 Logger(0), Operator(0), Randomizer(0), FileSystem(0),
\r
25 InputReceivingSceneManager(0), VideoModeList(0), ContextManager(0),
\r
26 CreationParams(params), Close(false)
\r
28 Timer = new CTimer(params.UsePerformanceTimer);
\r
29 if (os::Printer::Logger)
\r
31 os::Printer::Logger->grab();
\r
32 Logger = (CLogger*)os::Printer::Logger;
\r
33 Logger->setReceiver(UserReceiver);
\r
37 Logger = new CLogger(UserReceiver);
\r
38 os::Printer::Logger = Logger;
\r
40 Logger->setLogLevel(CreationParams.LoggingLevel);
\r
42 os::Printer::Logger = Logger;
\r
43 Randomizer = createDefaultRandomizer();
\r
45 FileSystem = io::createFileSystem();
\r
46 VideoModeList = new video::CVideoModeList();
\r
48 core::stringc s = "Irrlicht Engine version ";
\r
49 s.append(getVersion());
\r
50 os::Printer::log(s.c_str(), ELL_INFORMATION);
\r
52 checkVersion(params.SDK_version_do_not_use);
\r
56 CIrrDeviceStub::~CIrrDeviceStub()
\r
58 VideoModeList->drop();
\r
61 GUIEnvironment->drop();
\r
64 SceneManager->drop();
\r
67 VideoDriver->drop();
\r
70 ContextManager->drop();
\r
75 if (InputReceivingSceneManager)
\r
76 InputReceivingSceneManager->drop();
\r
79 CursorControl->drop();
\r
93 os::Printer::Logger = 0;
\r
97 void CIrrDeviceStub::createGUIAndScene()
\r
99 #ifdef _IRR_COMPILE_WITH_GUI_
\r
100 // create gui environment
\r
101 GUIEnvironment = gui::createGUIEnvironment(FileSystem, VideoDriver, Operator);
\r
104 // create Scene manager
\r
105 SceneManager = scene::createSceneManager(VideoDriver, FileSystem, CursorControl, GUIEnvironment);
\r
107 setEventReceiver(UserReceiver);
\r
111 //! returns the video driver
\r
112 video::IVideoDriver* CIrrDeviceStub::getVideoDriver()
\r
114 return VideoDriver;
\r
118 //! return file system
\r
119 io::IFileSystem* CIrrDeviceStub::getFileSystem()
\r
126 //! returns the gui environment
\r
127 gui::IGUIEnvironment* CIrrDeviceStub::getGUIEnvironment()
\r
129 return GUIEnvironment;
\r
134 //! returns the scene manager
\r
135 scene::ISceneManager* CIrrDeviceStub::getSceneManager()
\r
137 return SceneManager;
\r
141 //! \return Returns a pointer to the ITimer object. With it the
\r
142 //! current Time can be received.
\r
143 ITimer* CIrrDeviceStub::getTimer()
\r
149 //! Returns the version of the engine.
\r
150 const char* CIrrDeviceStub::getVersion() const
\r
152 return IRRLICHT_SDK_VERSION;
\r
155 //! \return Returns a pointer to the mouse cursor control interface.
\r
156 gui::ICursorControl* CIrrDeviceStub::getCursorControl()
\r
158 return CursorControl;
\r
162 //! \return Returns a pointer to a list with all video modes supported
\r
163 //! by the gfx adapter.
\r
164 video::IVideoModeList* CIrrDeviceStub::getVideoModeList()
\r
166 return VideoModeList;
\r
169 //! return the context manager
\r
170 video::IContextManager* CIrrDeviceStub::getContextManager()
\r
172 return ContextManager;
\r
175 //! checks version of sdk and prints warning if there might be a problem
\r
176 bool CIrrDeviceStub::checkVersion(const char* version)
\r
178 if (strcmp(getVersion(), version))
\r
181 w = "Warning: The library version of the Irrlicht Engine (";
\r
183 w += ") does not match the version the application was compiled with (";
\r
185 w += "). This may cause problems.";
\r
186 os::Printer::log(w.c_str(), ELL_WARNING);
\r
195 //! Compares to the last call of this function to return double and triple clicks.
\r
196 u32 CIrrDeviceStub::checkSuccessiveClicks(s32 mouseX, s32 mouseY, EMOUSE_INPUT_EVENT inputEvent )
\r
198 const s32 MAX_MOUSEMOVE = 3;
\r
200 irr::u32 clickTime = getTimer()->getRealTime();
\r
202 if ( (clickTime-MouseMultiClicks.LastClickTime) < MouseMultiClicks.DoubleClickTime
\r
203 && core::abs_(MouseMultiClicks.LastClick.X - mouseX ) <= MAX_MOUSEMOVE
\r
204 && core::abs_(MouseMultiClicks.LastClick.Y - mouseY ) <= MAX_MOUSEMOVE
\r
205 && MouseMultiClicks.CountSuccessiveClicks < 3
\r
206 && MouseMultiClicks.LastMouseInputEvent == inputEvent
\r
209 ++MouseMultiClicks.CountSuccessiveClicks;
\r
213 MouseMultiClicks.CountSuccessiveClicks = 1;
\r
216 MouseMultiClicks.LastMouseInputEvent = inputEvent;
\r
217 MouseMultiClicks.LastClickTime = clickTime;
\r
218 MouseMultiClicks.LastClick.X = mouseX;
\r
219 MouseMultiClicks.LastClick.Y = mouseY;
\r
221 return MouseMultiClicks.CountSuccessiveClicks;
\r
225 //! send the event to the right receiver
\r
226 bool CIrrDeviceStub::postEventFromUser(const SEvent& event)
\r
228 bool absorbed = false;
\r
231 absorbed = UserReceiver->OnEvent(event);
\r
233 if (!absorbed && GUIEnvironment)
\r
234 absorbed = GUIEnvironment->postEventFromUser(event);
\r
236 scene::ISceneManager* inputReceiver = InputReceivingSceneManager;
\r
237 if (!inputReceiver)
\r
238 inputReceiver = SceneManager;
\r
240 if (!absorbed && inputReceiver)
\r
241 absorbed = inputReceiver->postEventFromUser(event);
\r
247 //! Sets a new event receiver to receive events
\r
248 void CIrrDeviceStub::setEventReceiver(IEventReceiver* receiver)
\r
250 UserReceiver = receiver;
\r
251 Logger->setReceiver(receiver);
\r
252 if (GUIEnvironment)
\r
253 GUIEnvironment->setUserEventReceiver(receiver);
\r
257 //! Returns poinhter to the current event receiver. Returns 0 if there is none.
\r
258 IEventReceiver* CIrrDeviceStub::getEventReceiver()
\r
260 return UserReceiver;
\r
264 //! \return Returns a pointer to the logger.
\r
265 ILogger* CIrrDeviceStub::getLogger()
\r
271 //! Returns the operation system opertator object.
\r
272 IOSOperator* CIrrDeviceStub::getOSOperator()
\r
278 //! Provides access to the engine's currently set randomizer.
\r
279 IRandomizer* CIrrDeviceStub::getRandomizer() const
\r
284 //! Sets a new randomizer.
\r
285 void CIrrDeviceStub::setRandomizer(IRandomizer* r)
\r
290 Randomizer->drop();
\r
293 Randomizer->grab();
\r
299 struct SDefaultRandomizer : public IRandomizer
\r
301 virtual void reset(s32 value=0x0f0f0f0f) _IRR_OVERRIDE_
\r
303 os::Randomizer::reset(value);
\r
306 virtual s32 rand() const _IRR_OVERRIDE_
\r
308 return os::Randomizer::rand();
\r
311 virtual f32 frand() const _IRR_OVERRIDE_
\r
313 return os::Randomizer::frand();
\r
316 virtual s32 randMax() const _IRR_OVERRIDE_
\r
318 return os::Randomizer::randMax();
\r
323 //! Creates a new default randomizer.
\r
324 IRandomizer* CIrrDeviceStub::createDefaultRandomizer() const
\r
326 IRandomizer* r = new SDefaultRandomizer();
\r
333 //! Sets the input receiving scene manager.
\r
334 void CIrrDeviceStub::setInputReceivingSceneManager(scene::ISceneManager* sceneManager)
\r
337 sceneManager->grab();
\r
338 if (InputReceivingSceneManager)
\r
339 InputReceivingSceneManager->drop();
\r
341 InputReceivingSceneManager = sceneManager;
\r
345 //! Checks if the window is running in fullscreen mode
\r
346 bool CIrrDeviceStub::isFullscreen() const
\r
348 return CreationParams.Fullscreen;
\r
352 //! returns color format
\r
353 video::ECOLOR_FORMAT CIrrDeviceStub::getColorFormat() const
\r
355 return video::ECF_R5G6B5;
\r
358 //! No-op in this implementation
\r
359 bool CIrrDeviceStub::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
\r
364 //! No-op in this implementation
\r
365 bool CIrrDeviceStub::activateAccelerometer(float updateInterval)
\r
370 //! No-op in this implementation
\r
371 bool CIrrDeviceStub::deactivateAccelerometer()
\r
376 //! No-op in this implementation
\r
377 bool CIrrDeviceStub::isAccelerometerActive()
\r
382 //! No-op in this implementation
\r
383 bool CIrrDeviceStub::isAccelerometerAvailable()
\r
388 //! No-op in this implementation
\r
389 bool CIrrDeviceStub::activateGyroscope(float updateInterval)
\r
394 //! No-op in this implementation
\r
395 bool CIrrDeviceStub::deactivateGyroscope()
\r
400 //! No-op in this implementation
\r
401 bool CIrrDeviceStub::isGyroscopeActive()
\r
406 //! No-op in this implementation
\r
407 bool CIrrDeviceStub::isGyroscopeAvailable()
\r
412 //! No-op in this implementation
\r
413 bool CIrrDeviceStub::activateDeviceMotion(float updateInterval)
\r
418 //! No-op in this implementation
\r
419 bool CIrrDeviceStub::deactivateDeviceMotion()
\r
424 //! No-op in this implementation
\r
425 bool CIrrDeviceStub::isDeviceMotionActive()
\r
430 //! No-op in this implementation
\r
431 bool CIrrDeviceStub::isDeviceMotionAvailable()
\r
438 void CIrrDeviceStub::calculateGammaRamp ( u16 *ramp, f32 gamma, f32 relativebrightness, f32 relativecontrast )
\r
442 s32 rbright = (s32) ( relativebrightness * (65535.f / 4 ) );
\r
443 f32 rcontrast = 1.f / (255.f - ( relativecontrast * 127.5f ) );
\r
445 gamma = gamma > 0.f ? 1.0f / gamma : 0.f;
\r
447 for ( i = 0; i < 256; ++i )
\r
449 value = (s32)(pow( rcontrast * i, gamma)*65535.f + 0.5f );
\r
450 ramp[i] = (u16) core::s32_clamp ( value + rbright, 0, 65535 );
\r
455 void CIrrDeviceStub::calculateGammaFromRamp ( f32 &gamma, const u16 *ramp )
\r
457 /* The following is adapted from a post by Garrett Bass on OpenGL
\r
458 Gamedev list, March 4, 2000.
\r
464 for ( i = 1; i < 256; ++i ) {
\r
465 if ( (ramp[i] != 0) && (ramp[i] != 65535) ) {
\r
466 f32 B = (f32)i / 256.f;
\r
467 f32 A = ramp[i] / 65535.f;
\r
468 sum += (f32) ( logf(A) / logf(B) );
\r
472 if ( count && sum ) {
\r
473 gamma = 1.0f / (sum / count);
\r
478 //! Set the current Gamma Value for the Display
\r
479 bool CIrrDeviceStub::setGammaRamp( f32 red, f32 green, f32 blue, f32 brightness, f32 contrast )
\r
484 //! Get the current Gamma Value for the Display
\r
485 bool CIrrDeviceStub::getGammaRamp( f32 &red, f32 &green, f32 &blue, f32 &brightness, f32 &contrast )
\r
490 //! Set the maximal elapsed time between 2 clicks to generate doubleclicks for the mouse. It also affects tripleclick behavior.
\r
491 void CIrrDeviceStub::setDoubleClickTime( u32 timeMs )
\r
493 MouseMultiClicks.DoubleClickTime = timeMs;
\r
496 //! Get the maximal elapsed time between 2 clicks to generate double- and tripleclicks for the mouse.
\r
497 u32 CIrrDeviceStub::getDoubleClickTime() const
\r
499 return MouseMultiClicks.DoubleClickTime;
\r
502 //! Remove all messages pending in the system message loop
\r
503 void CIrrDeviceStub::clearSystemMessages()
\r
507 //! Checks whether the input device should take input from the IME
\r
508 bool CIrrDeviceStub::acceptsIME()
\r
510 if (!GUIEnvironment)
\r
512 gui::IGUIElement *elem = GUIEnvironment->getFocus();
\r
513 return elem && elem->acceptsIME();
\r
517 } // end namespace irr
\r