瀏覽代碼

Added proper timestamps for OIS input events

Marko Pintera 12 年之前
父節點
當前提交
047dd0ffcd

+ 3 - 3
BansheeEngine/Include/BsInputConfiguration.h

@@ -69,8 +69,8 @@ namespace BansheeEngine
 		void registerButton(const CM::String& name, CM::ButtonCode buttonCode, VButtonModifier modifiers = VButtonModifier::None, bool repeatable = false);
 		void unregisterButton(const CM::String& name);
 
-		void setRepeatInterval(float seconds) { mRepeatInterval = seconds; }
-		float getRepeatInterval() const { return mRepeatInterval; }
+		void setRepeatInterval(CM::UINT64 milliseconds) { mRepeatInterval = milliseconds; }
+		CM::UINT64 getRepeatInterval() const { return mRepeatInterval; }
 
 		bool getButton(CM::ButtonCode code, CM::UINT32 modifiers, VirtualButton& btn, VIRTUAL_BUTTON_DESC& btnDesc) const;
 
@@ -78,6 +78,6 @@ namespace BansheeEngine
 	private:
 		CM::Vector<VirtualButtonData>::type mButtons[CM::BC_Count];
 
-		float mRepeatInterval;
+		CM::UINT64 mRepeatInterval;
 	};
 }

+ 1 - 1
BansheeEngine/Include/BsVirtualInput.h

@@ -20,7 +20,7 @@ namespace BansheeEngine
 		{
 			VirtualButton button;
 			ButtonState state;
-			float timestamp;
+			CM::UINT64 timestamp;
 			bool allowRepeat;
 		};
 

+ 1 - 1
BansheeEngine/Source/BsInputConfiguration.cpp

@@ -33,7 +33,7 @@ namespace BansheeEngine
 	}
 
 	InputConfiguration::InputConfiguration()
-		:mRepeatInterval(1.0f)
+		:mRepeatInterval(1000)
 	{ }
 
 	void InputConfiguration::registerButton(const CM::String& name, CM::ButtonCode buttonCode, VButtonModifier modifiers, bool repeatable)

+ 3 - 3
BansheeEngine/Source/BsVirtualInput.cpp

@@ -69,8 +69,8 @@ namespace BansheeEngine
 		}
 
 		bool hasEvents = true;
-		float repeatInternal = mInputConfiguration->getRepeatInterval();
-		float currentTime = gTime().getTime();
+		UINT64 repeatInternal = mInputConfiguration->getRepeatInterval();
+		UINT64 currentTime = gTime().getTimeMs();
 
 		// Trigger all events
 		while(hasEvents)
@@ -104,7 +104,7 @@ namespace BansheeEngine
 				if(!state.second.allowRepeat)
 					continue;
 
-				float diff = currentTime - state.second.timestamp;
+				UINT64 diff = currentTime - state.second.timestamp;
 				if(diff >= repeatInternal)
 				{
 					state.second.timestamp += repeatInternal;

+ 2 - 2
CamelotCore/Include/CmInput.h

@@ -78,8 +78,8 @@ namespace CamelotFramework
 		RawAxisState mAxes[RawInputAxis::Count];
 		ButtonState mKeyState[BC_Count];
 
-		void buttonDown(ButtonCode code, float timestamp);
-		void buttonUp(ButtonCode code, float timestamp);
+		void buttonDown(ButtonCode code, UINT64 timestamp);
+		void buttonUp(ButtonCode code, UINT64 timestamp);
 
 		void charInput(UINT32 chr);
 

+ 1 - 1
CamelotCore/Include/CmInputFwd.h

@@ -237,7 +237,7 @@ namespace CamelotFramework
 		{ }
 
 		ButtonCode buttonCode;
-		float timestamp;
+		UINT64 timestamp;
 
 		bool isKeyboard() const { return (buttonCode & 0xC0000000) == 0; }
 		bool isMouse() const { return (buttonCode & 0x80000000) != 0; }

+ 2 - 2
CamelotCore/Include/CmRawInputHandler.h

@@ -46,8 +46,8 @@ namespace CamelotFramework
 		RawInputHandler() {}
 		virtual ~RawInputHandler() {}
 
-		boost::signal<void(ButtonCode, float)> onButtonDown;
-		boost::signal<void(ButtonCode, float)> onButtonUp;
+		boost::signal<void(ButtonCode, UINT64)> onButtonDown;
+		boost::signal<void(ButtonCode, UINT64)> onButtonUp;
 
 		boost::signal<void(const RawAxisState&, RawInputAxis)> onAxisMoved;
 

+ 2 - 2
CamelotCore/Source/CmInput.cpp

@@ -104,7 +104,7 @@ namespace CamelotFramework
 			mOSInputHandler->inputWindowChanged(win);
 	}
 
-	void Input::buttonDown(ButtonCode code, float timestamp)
+	void Input::buttonDown(ButtonCode code, UINT64 timestamp)
 	{
 		mKeyState[code & 0x0000FFFF] = ButtonState::ToggledOn;
 
@@ -118,7 +118,7 @@ namespace CamelotFramework
 		}
 	}
 
-	void Input::buttonUp(ButtonCode code, float timestamp)
+	void Input::buttonUp(ButtonCode code, UINT64 timestamp)
 	{
 		mKeyState[code & 0x0000FFFF] = ButtonState::ToggledOff;
 

+ 2 - 0
CamelotOISInput/Include/CmInputHandlerOIS.h

@@ -21,6 +21,8 @@ namespace CamelotFramework
 		OIS::Mouse*			mMouse;
 		OIS::Keyboard*		mKeyboard;
 
+		UINT64				mTimestampClockOffset;
+
 		virtual bool keyPressed(const OIS::KeyEvent& arg);
 		virtual bool keyReleased(const OIS::KeyEvent& arg);
 		virtual bool mouseMoved(const OIS::MouseEvent& arg);

+ 9 - 5
CamelotOISInput/Source/CmInputHandlerOIS.cpp

@@ -2,11 +2,12 @@
 #include "CmVector2I.h"
 #include "OIS/OISException.h"
 #include "CmRenderWindow.h"
+#include "CmTime.h"
 
 namespace CamelotFramework
 {
 	InputHandlerOIS::InputHandlerOIS(unsigned int hWnd)
-		:mInputManager(nullptr), mKeyboard(nullptr), mMouse(nullptr)
+		:mInputManager(nullptr), mKeyboard(nullptr), mMouse(nullptr), mTimestampClockOffset(0)
 	{
 		OIS::ParamList pl;
 		std::ostringstream windowHndStr;
@@ -36,6 +37,9 @@ namespace CamelotFramework
 		mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, true));
 		mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, true));
 
+		// OIS reports times since system start but we use time since program start
+		mTimestampClockOffset = gTime().getStartTimeMs();
+
 		mMouse->setEventCallback(this);
 		mKeyboard->setEventCallback(this);
 	}
@@ -73,13 +77,13 @@ namespace CamelotFramework
 
 	bool InputHandlerOIS::keyPressed(const OIS::KeyEvent &arg)
 	{
-		onButtonDown(keyCodeToButtonCode(arg.key), 0.0f);
+		onButtonDown(keyCodeToButtonCode(arg.key), arg.timestamp - mTimestampClockOffset);
 		return true;
 	}
 
 	bool InputHandlerOIS::keyReleased(const OIS::KeyEvent& arg)
 	{
-		onButtonUp(keyCodeToButtonCode(arg.key), 0.0f);
+		onButtonUp(keyCodeToButtonCode(arg.key), arg.timestamp - mTimestampClockOffset);
 		return true;
 	}
 
@@ -102,14 +106,14 @@ namespace CamelotFramework
 
 	bool InputHandlerOIS::mousePressed(const OIS::MouseEvent& arg, OIS::MouseButtonID id)
 	{
-		onButtonDown(mouseButtonToButtonCode(id), 0.0f);
+		onButtonDown(mouseButtonToButtonCode(id), arg.timestamp - mTimestampClockOffset);
 
 		return true;
 	}
 
 	bool InputHandlerOIS::mouseReleased(const OIS::MouseEvent& arg, OIS::MouseButtonID id)
 	{
-		onButtonUp(mouseButtonToButtonCode(id), 0.0f);
+		onButtonUp(mouseButtonToButtonCode(id), arg.timestamp - mTimestampClockOffset);
 
 		return true;
 	}

+ 9 - 1
CamelotUtility/Include/CmTime.h

@@ -58,6 +58,14 @@ namespace CamelotFramework
 		 */
 		UINT64 getTimePrecise() const;
 
+		/**
+		 * @brief	Gets the time at which the application was started, counting
+		 * 			from system start.
+		 *
+		 * @return	The time since system to application start, in milliseconds.
+		 */
+		UINT64 getStartTimeMs() const { return mAppStartTime; }
+
 		/**
 		 * @brief	Called every frame. Should only be called by Application.
 		 */
@@ -68,7 +76,7 @@ namespace CamelotFramework
 		float mTimeSinceStart; // Time since start in seconds
 		UINT64 mTimeSinceStartMs;
 
-		unsigned long mAppStartTime; // Time the application started, in microseconds
+		UINT64 mAppStartTime; // Time the application started, in microseconds
 		unsigned long mLastFrameTime; // Time since last runOneFrame call, In microseconds
 		unsigned long mCurrentFrame;
 

+ 7 - 0
CamelotUtility/Include/Win32/CmTimerImp.h

@@ -99,6 +99,13 @@ namespace CamelotFramework
 
 		/** Returns microseconds since initialisation or last reset, only CPU time measured */
 		unsigned long getMicrosecondsCPU();
+
+		/**
+		 * @brief	Returns the time at which the timer was initialized, in milliseconds.
+		 *
+		 * @return	Time in milliseconds.
+		 */
+		unsigned long getStartMs() const;
     };
 	/** @} */
 	/** @} */

+ 1 - 1
CamelotUtility/Source/CmTime.cpp

@@ -9,7 +9,7 @@ namespace CamelotFramework
 		:mAppStartTime(0), mLastFrameTime(0), mFrameDelta(0.0f), mTimeSinceStart(0.0f), mCurrentFrame(0)
 	{
 		mTimer = cm_new<Timer>();
-		mAppStartTime = mTimer->getMicroseconds();
+		mAppStartTime = (UINT64)mTimer->getStartMs();
 	}
 
 	Time::~Time()

+ 7 - 0
CamelotUtility/Source/Win32/CmTimer.cpp

@@ -154,6 +154,13 @@ unsigned long Timer::getMilliseconds()
     return newTicks;
 }
 
+unsigned long Timer::getStartMs() const
+{
+	unsigned long newTicks = (unsigned long) (1000 * mStartTime.QuadPart / mFrequency.QuadPart);
+
+	return newTicks;
+}
+
 //-------------------------------------------------------------------------
 unsigned long Timer::getMicroseconds()
 {