From d90bfeca7412201e15a309513ad2001707de57b0 Mon Sep 17 00:00:00 2001 From: Arnaud Degroote Date: Wed, 27 Nov 2013 10:28:23 +0100 Subject: [PATCH 4/4] Add support for an user controlled clock --- source/gameengine/Ketsji/KX_KetsjiEngine.cpp | 37 ++++++++++++++++++-------- source/gameengine/Ketsji/KX_KetsjiEngine.h | 18 +++++++++++++ source/gameengine/Ketsji/KX_PythonInit.cpp | 30 +++++++++++++++++++++ 3 files changed, 74 insertions(+), 11 deletions(-) diff --git a/source/gameengine/Ketsji/KX_KetsjiEngine.cpp b/source/gameengine/Ketsji/KX_KetsjiEngine.cpp index bb6e9e9..0fbc99d 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), @@ -554,17 +555,18 @@ bool KX_KetsjiEngine::NextFrame() //float dt = sClock.getTimeMicroseconds() * 0.000001f; //sClock.reset(); - if (m_bFixedTime) { - m_clockTime += timestep; - } - else { - double current_time = m_kxsystem->GetTimeInSeconds(); - double dt = current_time - m_previousRealTime; - m_previousRealTime = current_time; - // m_clockTime += dt; - m_clockTime += dt * m_timescale; + if (!m_bUserTime) { + if (m_bFixedTime) { + m_clockTime += timestep; + } + else { + double current_time = m_kxsystem->GetTimeInSeconds(); + double dt = current_time - m_previousRealTime; + m_previousRealTime = current_time; + // m_clockTime += dt; + m_clockTime += dt * m_timescale; + } } - double deltatime = m_clockTime - m_frameTime; if (deltatime<0.f) @@ -574,7 +576,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*m_timescale+1e-6); // if (frames>1) @@ -1808,6 +1809,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) { @@ -1825,6 +1830,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; @@ -1890,6 +1900,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 b86899a..ebc322e 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 e748df8..46cd477 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()); @@ -892,7 +919,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