Skip to content

Commit 388b6e7

Browse files
authored
Add Director::setMaxDeltaTime to cap the global maximum delta time (#3258)
1 parent c392e48 commit 388b6e7

3 files changed

Lines changed: 114 additions & 9 deletions

File tree

core/base/Director.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ THE SOFTWARE.
3232

3333
// standard includes
3434
#include <string>
35+
#include <algorithm>
3536

3637
#include "2d/SpriteFrameCache.h"
3738
#include "platform/FileUtils.h"
@@ -380,16 +381,8 @@ void Director::calculateDeltaTime()
380381
_deltaTime = std::chrono::duration_cast<std::chrono::microseconds>(now - _lastUpdate).count() / 1000000.0f;
381382
_lastUpdate = now;
382383
}
383-
_deltaTime = MAX(0, _deltaTime);
384+
_deltaTime = std::clamp(_deltaTime, 0.0f, _maxDeltaTime);
384385
}
385-
386-
#if _AX_DEBUG
387-
// If we are debugging our code, prevent big delta time
388-
if (_deltaTime > 0.2f)
389-
{
390-
_deltaTime = 1 / 60.0f;
391-
}
392-
#endif
393386
}
394387

395388
float Director::getDeltaTime() const
@@ -463,6 +456,11 @@ void Director::setNextDeltaTimeZero(bool nextDeltaTimeZero)
463456
_nextDeltaTimeZero = nextDeltaTimeZero;
464457
}
465458

459+
void Director::setMaxDeltaTime(float maxDeltaTime)
460+
{
461+
_maxDeltaTime = MAX(maxDeltaTime, 1.0f / 60);
462+
}
463+
466464
//
467465
// FIXME TODO
468466
// Matrix code MUST NOT be part of the Director

core/base/Director.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ class AX_DLL Director
194194
*/
195195
void setNextDeltaTimeZero(bool nextDeltaTimeZero);
196196

197+
/** Gets the maximum delta time allowed per frame. */
198+
float getMaxDeltaTime() const { return _maxDeltaTime; }
199+
200+
/** Sets the maximum delta time allowed per frame. Default is 1/3 second, minimum is 1/60 second. */
201+
void setMaxDeltaTime(float maxDeltaTime);
202+
197203
/** Whether or not the Director is paused. */
198204
bool isPaused() { return _paused; }
199205

@@ -593,6 +599,8 @@ class AX_DLL Director
593599
EventCustom* _beforeSetNextScene = nullptr;
594600
EventCustom* _afterSetNextScene = nullptr;
595601

602+
/* maximum delta time allowed per frame, default is 1/3 second */
603+
float _maxDeltaTime = 1.0f / 3;
596604
/* delta time since last tick to main loop */
597605
float _deltaTime = 0.0f;
598606
bool _deltaTimePassedByCaller = false;

extensions/scripting/lua-bindings/auto/axlua_base_auto.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15200,6 +15200,103 @@ int lua_ax_base_Director_setNextDeltaTimeZero(lua_State* tolua_S)
1520015200

1520115201
return 0;
1520215202
}
15203+
int lua_ax_base_Director_getMaxDeltaTime(lua_State* tolua_S)
15204+
{
15205+
int argc = 0;
15206+
ax::Director* obj = nullptr;
15207+
bool ok = true;
15208+
15209+
#if _AX_DEBUG >= 1
15210+
tolua_Error tolua_err;
15211+
#endif
15212+
15213+
15214+
#if _AX_DEBUG >= 1
15215+
if (!tolua_isusertype(tolua_S,1,"ax.Director",0,&tolua_err)) goto tolua_lerror;
15216+
#endif
15217+
15218+
obj = (ax::Director*)tolua_tousertype(tolua_S,1,0);
15219+
15220+
#if _AX_DEBUG >= 1
15221+
if (!obj)
15222+
{
15223+
tolua_error(tolua_S,"invalid 'obj' in function 'lua_ax_base_Director_getMaxDeltaTime'", nullptr);
15224+
return 0;
15225+
}
15226+
#endif
15227+
15228+
argc = lua_gettop(tolua_S)-1;
15229+
if (argc == 0)
15230+
{
15231+
if(!ok)
15232+
{
15233+
tolua_error(tolua_S,"invalid arguments in function 'lua_ax_base_Director_getMaxDeltaTime'", nullptr);
15234+
return 0;
15235+
}
15236+
auto&& ret = obj->getMaxDeltaTime();
15237+
tolua_pushnumber(tolua_S,(lua_Number)ret);
15238+
return 1;
15239+
}
15240+
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "ax.Director:getMaxDeltaTime",argc, 0);
15241+
return 0;
15242+
15243+
#if _AX_DEBUG >= 1
15244+
tolua_lerror:
15245+
tolua_error(tolua_S,"#ferror in function 'lua_ax_base_Director_getMaxDeltaTime'.",&tolua_err);
15246+
#endif
15247+
15248+
return 0;
15249+
}
15250+
int lua_ax_base_Director_setMaxDeltaTime(lua_State* tolua_S)
15251+
{
15252+
int argc = 0;
15253+
ax::Director* obj = nullptr;
15254+
bool ok = true;
15255+
15256+
#if _AX_DEBUG >= 1
15257+
tolua_Error tolua_err;
15258+
#endif
15259+
15260+
15261+
#if _AX_DEBUG >= 1
15262+
if (!tolua_isusertype(tolua_S,1,"ax.Director",0,&tolua_err)) goto tolua_lerror;
15263+
#endif
15264+
15265+
obj = (ax::Director*)tolua_tousertype(tolua_S,1,0);
15266+
15267+
#if _AX_DEBUG >= 1
15268+
if (!obj)
15269+
{
15270+
tolua_error(tolua_S,"invalid 'obj' in function 'lua_ax_base_Director_setMaxDeltaTime'", nullptr);
15271+
return 0;
15272+
}
15273+
#endif
15274+
15275+
argc = lua_gettop(tolua_S)-1;
15276+
if (argc == 1)
15277+
{
15278+
double arg0;
15279+
15280+
ok &= luaval_to_number(tolua_S, 2,&arg0, "ax.Director:setMaxDeltaTime");
15281+
if(!ok)
15282+
{
15283+
tolua_error(tolua_S,"invalid arguments in function 'lua_ax_base_Director_setMaxDeltaTime'", nullptr);
15284+
return 0;
15285+
}
15286+
obj->setMaxDeltaTime(arg0);
15287+
lua_settop(tolua_S, 1);
15288+
return 1;
15289+
}
15290+
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "ax.Director:setMaxDeltaTime",argc, 1);
15291+
return 0;
15292+
15293+
#if _AX_DEBUG >= 1
15294+
tolua_lerror:
15295+
tolua_error(tolua_S,"#ferror in function 'lua_ax_base_Director_setMaxDeltaTime'.",&tolua_err);
15296+
#endif
15297+
15298+
return 0;
15299+
}
1520315300
int lua_ax_base_Director_isPaused(lua_State* tolua_S)
1520415301
{
1520515302
int argc = 0;
@@ -17996,6 +18093,8 @@ int lua_register_ax_base_Director(lua_State* tolua_S)
1799618093
tolua_function(tolua_S,"getTextureCache",lua_ax_base_Director_getTextureCache);
1799718094
tolua_function(tolua_S,"isNextDeltaTimeZero",lua_ax_base_Director_isNextDeltaTimeZero);
1799818095
tolua_function(tolua_S,"setNextDeltaTimeZero",lua_ax_base_Director_setNextDeltaTimeZero);
18096+
tolua_function(tolua_S,"getMaxDeltaTime",lua_ax_base_Director_getMaxDeltaTime);
18097+
tolua_function(tolua_S,"setMaxDeltaTime",lua_ax_base_Director_setMaxDeltaTime);
1799918098
tolua_function(tolua_S,"isPaused",lua_ax_base_Director_isPaused);
1800018099
tolua_function(tolua_S,"getTotalFrames",lua_ax_base_Director_getTotalFrames);
1800118100
tolua_function(tolua_S,"setProjection",lua_ax_base_Director_setProjection);

0 commit comments

Comments
 (0)