From 29a4bb6803abefccb805090976cd8a825639869b Mon Sep 17 00:00:00 2001 From: Onek8 Date: Sun, 1 Jun 2025 23:14:43 +0000 Subject: [PATCH] moisesjpelaez - Add fixedUpdate for Physics --- leenkx/Sources/iron/App.hx | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/leenkx/Sources/iron/App.hx b/leenkx/Sources/iron/App.hx index 6d0f6f6..eb4f653 100644 --- a/leenkx/Sources/iron/App.hx +++ b/leenkx/Sources/iron/App.hx @@ -12,6 +12,7 @@ class App { static var traitInits: ArrayVoid> = []; static var traitUpdates: ArrayVoid> = []; static var traitLateUpdates: ArrayVoid> = []; + static var traitFixedUpdates: ArrayVoid> = []; static var traitRenders: ArrayVoid> = []; static var traitRenders2D: ArrayVoid> = []; public static var framebuffer: kha.Framebuffer; @@ -23,6 +24,8 @@ class App { public static var renderPathTime: Float; public static var endFrameCallbacks: ArrayVoid> = []; #end + static var last = 0.0; + static var time = 0.0; static var lastw = -1; static var lasth = -1; public static var onResize: Void->Void = null; @@ -41,6 +44,7 @@ class App { traitInits = []; traitUpdates = []; traitLateUpdates = []; + traitFixedUpdates = []; traitRenders = []; traitRenders2D = []; if (onResets != null) for (f in onResets) f(); @@ -77,6 +81,13 @@ class App { l <= traitLateUpdates.length ? i++ : l = traitLateUpdates.length; } + time += iron.system.Time.realTime() - last; + last = iron.system.Time.realTime(); + while (time >= iron.system.Time.fixedStep) { + for (f in traitFixedUpdates) f(); + time -= iron.system.Time.fixedStep; + } + if (onEndFrames != null) for (f in onEndFrames) f(); #if lnx_debug @@ -172,6 +183,14 @@ class App { traitLateUpdates.remove(f); } + public static function notifyOnFixedUpdate(f: Void->Void) { + traitFixedUpdates.push(f); + } + + public static function removeFixedUpdate(f: Void->Void) { + traitFixedUpdates.remove(f); + } + public static function notifyOnRender(f: kha.graphics4.Graphics->Void) { traitRenders.push(f); }