A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(native): a frame reports the time that actually passed
yhhc230631cd439
diff --git a/native/host/Host.cpp b/native/host/Host.cppindex d2db2a823..dffda3617 100644--- a/native/host/Host.cpp+++ b/native/host/Host.cpp@@ -28,6 +28,8 @@#include <glm/gtc/matrix_transform.hpp>+#include <chrono>+using namespace esengine;namespace eshost {@@ -130,7 +132,12 @@ bool bindSurface() {}void surfaceLost() {- if (hostAlive()) host().surfaceReady = false;+ if (!hostAlive()) return;+ host().surfaceReady = false;+ // The next frame is the first of a new run of them. Without this the game is+ // handed however long the app spent backgrounded — clamped, but still a+ // quarter-second jump the moment it comes back.+ host().haveLastFrame = false;}bool booted() { return hostAlive() && host().ready; }@@ -195,7 +202,29 @@ void frame() {if (!bindSurface()) return;}- JSValue dt = JS_NewFloat64(h.js, 1.0 / 60.0);+ // How much time actually passed, not how much we wish had. A host is stepped+ // by the display — CADisplayLink on iOS, the Choreographer on Android — so the+ // rate is the panel's, and a fixed 1/60 is only right on a 60 Hz one. A 120 Hz+ // phone ran this game at 2.02x real speed for exactly that reason, which reads+ // as "the animations are too fast" rather than as a clock bug.+ //+ // Same policy as the web loop (App's rAF path): clamp the delta, because the+ // alternative to a long frame is a game that teleports through it. The ceiling+ // matches App.maxDeltaTime — one number, so a game behaves the same after a+ // stall wherever it runs.+ constexpr double kMaxDelta = 0.25;+ constexpr double kFirstFrameDelta = 1.0 / 60.0;+ const auto nowAt = std::chrono::steady_clock::now();+ double deltaSeconds = kFirstFrameDelta;+ if (h.haveLastFrame) {+ deltaSeconds = std::chrono::duration<double>(nowAt - h.lastFrameAt).count();+ if (deltaSeconds > kMaxDelta) deltaSeconds = kMaxDelta;+ if (deltaSeconds < 0.0) deltaSeconds = 0.0; // a clock that went backwards+ }+ h.lastFrameAt = nowAt;+ h.haveLastFrame = true;++ JSValue dt = JS_NewFloat64(h.js, deltaSeconds);callJs(h, "update", 1, &dt);JS_FreeValue(h.js, dt);pumpJs(h); // run the App tick's async systems and any timers they setdiff --git a/native/host/Runtime.hpp b/native/host/Runtime.hppindex e2a1c336c..8fb38e3e5 100644--- a/native/host/Runtime.hpp+++ b/native/host/Runtime.hpp@@ -29,6 +29,7 @@ extern "C" {#include "esengine/ecs/Registry.hpp"#include "esengine/renderer/webgpu/WebGPUDevice.hpp"+#include <chrono>#include <cstdint>#include <string>#include <unordered_map>@@ -75,6 +76,11 @@ struct HostState {bool ready = false; ///< engine + JS booted oncebool surfaceReady = false; ///< a live window surface is bound (false while screen off)uint64_t frame = 0;+ /// When the last frame was stepped, for the delta the next one reports. Unset+ /// until the first frame and cleared whenever the surface goes away, so a+ /// resume does not hand the game the time the app spent in the background.+ std::chrono::steady_clock::time_point lastFrameAt{};+ bool haveLastFrame = false;};/** The running host. Valid from the first line of {@link boot}; a binding can only