From 2d0e55bce8f7367199498b94da6bd78da6f2a51d Mon Sep 17 00:00:00 2001
From: Arnaud Degroote <arnaud.degroote@laas.fr>
Date: Fri, 22 Nov 2013 09:20:28 +0100
Subject: [PATCH 2/2] Allow the possibility of an 'user clock'

---
 source/gameengine/Ketsji/KX_KetsjiEngine.cpp |   32 ++++++++++++++++++++------
 source/gameengine/Ketsji/KX_KetsjiEngine.h   |   18 +++++++++++++++
 source/gameengine/Ketsji/KX_PythonInit.cpp   |   30 ++++++++++++++++++++++++
 3 files changed, 73 insertions(+), 7 deletions(-)

diff --git a/source/gameengine/Ketsji/KX_KetsjiEngine.cpp b/source/gameengine/Ketsji/KX_KetsjiEngine.cpp
index 0e9e2cd..3dd943c 100644
--- a/source/gameengine/Ketsji/KX_KetsjiEngine.cpp
+++ b/source/gameengine/Ketsji/KX_KetsjiEngine.cpp
@@ -131,6 +131,7 @@ KX_KetsjiEngine::KX_KetsjiEngine(KX_ISystem* system)
 	m_bInitialized(false),
 	m_activecam(0),
 	m_bFixedTime(false),
+	m_bUserTime(false),
 	
 	m_firstframe(true),
 	
@@ -553,15 +554,19 @@ bool KX_KetsjiEngine::NextFrame()
 	//float dt = sClock.getTimeMicroseconds() * 0.000001f;
 	//sClock.reset();
 
-	if (m_bFixedTime) {
-		m_clockTime += timestep;
-	}
-	else {
-		// m_clockTime += dt;
-		m_clockTime = m_kxsystem->GetTimeInSeconds();
+	// do nothing if user set time
+	if (!m_bUserTime) {
+		if (m_bFixedTime) {
+			m_clockTime += timestep;
+		}
+		else {
+			// m_clockTime += dt;
+			m_clockTime = m_kxsystem->GetTimeInSeconds();
+		}
 	}
 	
 	double deltatime = m_clockTime - m_frameTime;
+
 	if (deltatime<0.f)
 	{
 		// We got here too quickly, which means there is nothing todo, just return and don't render.
@@ -569,7 +574,6 @@ bool KX_KetsjiEngine::NextFrame()
 		return false;
 	}
 
-
 	// Compute the number of logic frames to do each update (fixed tic bricks)
 	int frames =int(deltatime*m_ticrate+1e-6);
 //	if (frames>1)
@@ -1802,6 +1806,10 @@ void KX_KetsjiEngine::SetUseFixedTime(bool bUseFixedTime)
 	m_bFixedTime = bUseFixedTime;
 }
 
+void KX_KetsjiEngine::SetUseUserTime(bool bUseUserTime)
+{
+	m_bUserTime = bUseUserTime;
+}
 
 void	KX_KetsjiEngine::SetAnimRecordMode(bool animation_record, int startFrame)
 {
@@ -1819,6 +1827,11 @@ bool KX_KetsjiEngine::GetUseFixedTime(void) const
 	return m_bFixedTime;
 }
 
+bool KX_KetsjiEngine::GetUseUserTime(void) const
+{
+	return m_bUserTime;
+}
+
 double KX_KetsjiEngine::GetSuspendedDelta()
 {
 	return m_suspendeddelta;
@@ -1874,6 +1887,11 @@ double KX_KetsjiEngine::GetClockTime(void) const
 	return m_clockTime;
 }
 
+void KX_KetsjiEngine::SetClockTime(double time)
+{
+	m_clockTime = time;
+}
+
 double KX_KetsjiEngine::GetFrameTime(void) const
 {
 	return m_frameTime;
diff --git a/source/gameengine/Ketsji/KX_KetsjiEngine.h b/source/gameengine/Ketsji/KX_KetsjiEngine.h
index e7fb250..a457f6e 100644
--- a/source/gameengine/Ketsji/KX_KetsjiEngine.h
+++ b/source/gameengine/Ketsji/KX_KetsjiEngine.h
@@ -102,6 +102,7 @@ private:
 	bool				m_bInitialized;
 	int					m_activecam;
 	bool				m_bFixedTime;
+	bool				m_bUserTime;
 	
 	
 	bool				m_firstframe;
@@ -287,9 +288,26 @@ public:
 	bool GetUseFixedTime(void) const;
 
 	/**
+	 * Sets if we rely on a clock given by user
+	 */
+	void SetUseUserTime(bool bUseUserTimed);
+
+	/**
+	 * Returns if we rely on user clock
+	 * \return Current setting
+	 */
+	bool GetUseUserTime(void) const;
+
+	/**
 	 * Returns current render frame clock time
 	 */
 	double GetClockTime(void) const;
+
+	/**
+	 * Set the render frame clock time
+	 */
+	void SetClockTime(double);
+
 	/**
 	 * Returns current logic frame clock time
 	 */
diff --git a/source/gameengine/Ketsji/KX_PythonInit.cpp b/source/gameengine/Ketsji/KX_PythonInit.cpp
index f8b3eb0..9516980 100644
--- a/source/gameengine/Ketsji/KX_PythonInit.cpp
+++ b/source/gameengine/Ketsji/KX_PythonInit.cpp
@@ -506,11 +506,38 @@ static PyObject *gPyGetAverageFrameRate(PyObject *)
 	return PyFloat_FromDouble(KX_KetsjiEngine::GetAverageFrameRate());
 }
 
+static PyObject *gPyGetUseUserTime(PyObject *)
+{
+	return PyBool_FromLong(gp_KetsjiEngine->GetUseUserTime());
+}
+
+static PyObject *gPySetUseUserTime(PyObject*, PyObject *args)
+{
+	int bUseUserTime;
+
+	if (!PyArg_ParseTuple(args, "i:setUseUserTime", &bUseUserTime))
+		return NULL;
+
+	gp_KetsjiEngine->SetUseUserTime(bUseUserTime != 0);
+	Py_RETURN_NONE;
+}
+
 static PyObject *gPyGetClockTime(PyObject *)
 {
 	return PyFloat_FromDouble(gp_KetsjiEngine->GetClockTime());
 }
 
+static PyObject *gPySetClockTime(PyObject*, PyObject * args)
+{
+	double time;
+
+	if (!PyArg_ParseTuple(args, "d:SetClockTime", &time))
+		return NULL;
+
+	gp_KetsjiEngine->SetClockTime(time);
+	Py_RETURN_NONE;
+}
+
 static PyObject* gPyGetFrameTime(PyObject *)
 {
 	return PyFloat_FromDouble(gp_KetsjiEngine->GetFrameTime());
@@ -876,7 +903,10 @@ static struct PyMethodDef game_methods[] = {
 	{"setPhysicsTicRate", (PyCFunction) gPySetPhysicsTicRate, METH_VARARGS, (const char *)"Sets the physics tic rate"},
 	{"getExitKey", (PyCFunction) gPyGetExitKey, METH_NOARGS, (const char *)"Gets the key used to exit the game engine"},
 	{"setExitKey", (PyCFunction) gPySetExitKey, METH_VARARGS, (const char *)"Sets the key used to exit the game engine"},
+	{"getUseUserTime", (PyCFunction) gPyGetUseUserTime, METH_NOARGS, (const char*)"Get if we use the time provided by user"},
+	{"setUseUserTime", (PyCFunction) gPySetUseUserTime, METH_VARARGS, (const char*)"Set if we use the time provided by user"},
 	{"getClockTime", (PyCFunction) gPyGetClockTime, METH_NOARGS, (const char *)"Get the BGE clock time"},
+	{"setClockTime", (PyCFunction) gPySetClockTime, METH_VARARGS, (const char *)"Set the BGE clock time"},
 	{"getFrameTime", (PyCFunction) gPyGetFrameTime, METH_NOARGS, (const char *)"Get the BGE last frametime"},
 	{"getRealTime", (PyCFunction) gPyGetRealTime, METH_NOARGS, (const char *)"Get the real time"},
 	{"getAverageFrameRate", (PyCFunction) gPyGetAverageFrameRate, METH_NOARGS, (const char *)"Gets the estimated average frame rate"},
-- 
1.7.10.4

