#include "CIrrDeviceStub.h"
#include "ISceneManager.h"
#include "IEventReceiver.h"
#include "IFileSystem.h"
#include "IGUIEnvironment.h"
#include "os.h"
#include "IrrCompileConfig.h"
#include "CTimer.h"
#include "CLogger.h"
#include "irrString.h"
namespace irr
{
CIrrDeviceStub::CIrrDeviceStub(const SIrrlichtCreationParameters& params)
: IrrlichtDevice(), VideoDriver(0), GUIEnvironment(0), SceneManager(0),
Timer(0), CursorControl(0), UserReceiver(params.EventReceiver), Logger(0), Operator(0),
FileSystem(0), InputReceivingSceneManager(0), CreationParams(params),
Close(false)
{
Timer = new CTimer(params.UsePerformanceTimer);
if (os::Printer::Logger)
{
os::Printer::Logger->grab();
Logger = (CLogger*)os::Printer::Logger;
Logger->setReceiver(UserReceiver);
}
else
{
Logger = new CLogger(UserReceiver);
os::Printer::Logger = Logger;
}
Logger->setLogLevel( CreationParams.LoggingLevel );
os::Printer::Logger = Logger;
FileSystem = io::createFileSystem();
core::stringc s = "Irrlicht Engine version ";
s.append(getVersion());
os::Printer::log(s.c_str(), ELL_INFORMATION);
checkVersion(params.SDK_version_do_not_use);
}
CIrrDeviceStub::~CIrrDeviceStub()
{
FileSystem->drop();
if (GUIEnvironment)
GUIEnvironment->drop();
if (VideoDriver)
VideoDriver->drop();
if (SceneManager)
SceneManager->drop();
if (InputReceivingSceneManager)
InputReceivingSceneManager->drop();
if (CursorControl)
CursorControl->drop();
if (Operator)
Operator->drop();
CursorControl = 0;
Timer->drop();
if (Logger->drop())
os::Printer::Logger = 0;
}
void CIrrDeviceStub::createGUIAndScene()
{
#ifdef _IRR_COMPILE_WITH_GUI_
GUIEnvironment = gui::createGUIEnvironment(FileSystem, VideoDriver, Operator);
#endif
SceneManager = scene::createSceneManager(VideoDriver, FileSystem, CursorControl, GUIEnvironment);
setEventReceiver(UserReceiver);
}
video::IVideoDriver* CIrrDeviceStub::getVideoDriver()
{
return VideoDriver;
}
io::IFileSystem* CIrrDeviceStub::getFileSystem()
{
return FileSystem;
}
gui::IGUIEnvironment* CIrrDeviceStub::getGUIEnvironment()
{
return GUIEnvironment;
}
scene::ISceneManager* CIrrDeviceStub::getSceneManager()
{
return SceneManager;
}
ITimer* CIrrDeviceStub::getTimer()
{
return Timer;
}
const char* CIrrDeviceStub::getVersion() const
{
return IRRLICHT_SDK_VERSION;
}
gui::ICursorControl* CIrrDeviceStub::getCursorControl()
{
return CursorControl;
}
video::IVideoModeList* CIrrDeviceStub::getVideoModeList()
{
return &VideoModeList;
}
bool CIrrDeviceStub::checkVersion(const char* version)
{
if (strcmp(getVersion(), version))
{
core::stringc w;
w = "Warning: The library version of the Irrlicht Engine (";
w += getVersion();
w += ") does not match the version the application was compiled with (";
w += version;
w += "). This may cause problems.";
os::Printer::log(w.c_str(), ELL_WARNING);
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return false;
}
return true;
}
u32 CIrrDeviceStub::checkSuccessiveClicks(s32 mouseX, s32 mouseY, EMOUSE_INPUT_EVENT inputEvent )
{
const s32 MAX_MOUSEMOVE = 3;
irr::u32 clickTime = getTimer()->getRealTime();
if ( (clickTime-MouseMultiClicks.LastClickTime) < MouseMultiClicks.DoubleClickTime
&& core::abs_(MouseMultiClicks.LastClick.X - mouseX ) <= MAX_MOUSEMOVE
&& core::abs_(MouseMultiClicks.LastClick.Y - mouseY ) <= MAX_MOUSEMOVE
&& MouseMultiClicks.CountSuccessiveClicks < 3
&& MouseMultiClicks.LastMouseInputEvent == inputEvent
)
{
++MouseMultiClicks.CountSuccessiveClicks;
}
else
{
MouseMultiClicks.CountSuccessiveClicks = 1;
}
MouseMultiClicks.LastMouseInputEvent = inputEvent;
MouseMultiClicks.LastClickTime = clickTime;
MouseMultiClicks.LastClick.X = mouseX;
MouseMultiClicks.LastClick.Y = mouseY;
return MouseMultiClicks.CountSuccessiveClicks;
}
bool CIrrDeviceStub::postEventFromUser(const SEvent& event)
{
bool absorbed = false;
if (UserReceiver)
absorbed = UserReceiver->OnEvent(event);
if (!absorbed && GUIEnvironment)
absorbed = GUIEnvironment->postEventFromUser(event);
scene::ISceneManager* inputReceiver = InputReceivingSceneManager;
if (!inputReceiver)
inputReceiver = SceneManager;
if (!absorbed && inputReceiver)
absorbed = inputReceiver->postEventFromUser(event);
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return absorbed;
}
void CIrrDeviceStub::setEventReceiver(IEventReceiver* receiver)
{
UserReceiver = receiver;
Logger->setReceiver(receiver);
if (GUIEnvironment)
GUIEnvironment->setUserEventReceiver(receiver);
}
IEventReceiver* CIrrDeviceStub::getEventReceiver()
{
return UserReceiver;
}
ILogger* CIrrDeviceStub::getLogger()
{
return Logger;
}
IOSOperator* CIrrDeviceStub::getOSOperator()
{
return Operator;
}
void CIrrDeviceStub::setInputReceivingSceneManager(scene::ISceneManager* sceneManager)
{
if (sceneManager)
sceneManager->grab();
if (InputReceivingSceneManager)
InputReceivingSceneManager->drop();
InputReceivingSceneManager = sceneManager;
}
bool CIrrDeviceStub::isFullscreen() const
{
return CreationParams.Fullscreen;
}
video::ECOLOR_FORMAT CIrrDeviceStub::getColorFormat() const
{
return video::ECF_R5G6B5;
}
bool CIrrDeviceStub::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
{
return false;
}
void CIrrDeviceStub::calculateGammaRamp ( u16 *ramp, f32 gamma, f32 relativebrightness, f32 relativecontrast )
{
s32 i;
s32 value;
s32 rbright = (s32) ( relativebrightness * (65535.f / 4 ) );
f32 rcontrast = 1.f / (255.f - ( relativecontrast * 127.5f ) );
gamma = gamma > 0.f ? 1.0f / gamma : 0.f;
for ( i = 0; i < 256; ++i )
{
value = (s32)(pow( rcontrast * i, gamma)*65535.f + 0.5f );
ramp[i] = (u16) core::s32_clamp ( value + rbright, 0, 65535 );
}
}
void CIrrDeviceStub::calculateGammaFromRamp ( f32 &gamma, const u16 *ramp )
{
f32 sum = 0.0;
s32 i, count = 0;
gamma = 1.0;
for ( i = 1; i < 256; ++i ) {
if ( (ramp[i] != 0) && (ramp[i] != 65535) ) {
f32 B = (f32)i / 256.f;
f32 A = ramp[i] / 65535.f;
sum += (f32) ( logf(A) / logf(B) );
count++;
}
}
if ( count && sum ) {
gamma = 1.0f / (sum / count);
}
}
bool CIrrDeviceStub::setGammaRamp( f32 red, f32 green, f32 blue, f32 brightness, f32 contrast )
{
return false;
}
bool CIrrDeviceStub::getGammaRamp( f32 &red, f32 &green, f32 &blue, f32 &brightness, f32 &contrast )
{
return false;
}
void CIrrDeviceStub::setDoubleClickTime( u32 timeMs )
{
MouseMultiClicks.DoubleClickTime = timeMs;
}
u32 CIrrDeviceStub::getDoubleClickTime() const
{
return MouseMultiClicks.DoubleClickTime;
}
void CIrrDeviceStub::clearSystemMessages()
{
}
}