From 4988bf7d75da0765fc6721e95925ae8f0c8a8850 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Tue, 24 Oct 2017 17:23:25 +0200 Subject: [PATCH] "real" target-fps calculation --- src/cljs/topdown2d/core.cljs | 39 ++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/cljs/topdown2d/core.cljs b/src/cljs/topdown2d/core.cljs index 2233af2..6b04b76 100644 --- a/src/cljs/topdown2d/core.cljs +++ b/src/cljs/topdown2d/core.cljs @@ -36,8 +36,6 @@ } }) -(aset (:ctx gamestate) "font" "10px monospace") - (defn set-timing "sets the current time at the given key" [state timingkey] @@ -82,6 +80,21 @@ (update-scene) (set-timing :prev))) +(defn draw-fps + "draws the current fps" + [gamestate] + (let [ctx (:ctx gamestate)] + (aset ctx "fillStyle" "white") + (.fillRect + ctx + 0 0 13 13) + (aset ctx "fillStyle" "black") + (aset ctx "font" "10px monospace") + (.fillText + (:ctx gamestate) + (int (get-in gamestate [:timing :fps])) + 0 10))) + (defn draw-step "clears the canvas, draws fps and invokes the scene draw function" [gamestate] @@ -93,10 +106,7 @@ scene (scenekey (:scenes gamestate)) drawfunc (:draw scene)] (drawfunc gamestate scene)) - (.fillText - (:ctx gamestate) - (int (get-in gamestate [:timing :fps])) - 0 10)) + (draw-fps gamestate)) (defn mainloop "transforms the given gamestate by invoking a series of update @@ -105,11 +115,18 @@ [gamestate] (let [newstate (update-step gamestate)] (draw-step newstate) - (.setTimeout js/window - (fn [] - (.requestAnimationFrame js/window - #(mainloop newstate))) - (/ 1000 (:target-fps gamestate))))) + ; calculate the duration of update-step and draw-step + ; substract that from the wait time to reach target-fps + ; more accurately + (let [now (get-in newstate [:timing :now]) + duration (- (.now js/performance) now)] + (.setTimeout js/window + (fn [] + (.requestAnimationFrame js/window + #(mainloop newstate))) + (/ + (- 1000 duration) + (:target-fps gamestate)))))) (defn init-scenes "initiates the scene data maps using their respective init functions"