|
|
@ -5,44 +5,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
(enable-console-print!)
|
|
|
|
(enable-console-print!)
|
|
|
|
|
|
|
|
|
|
|
|
(def gamestate {
|
|
|
|
(def gamestate
|
|
|
|
:canvas (.getElementById js/document "gamecanvas")
|
|
|
|
{:canvas (.getElementById js/document "gamecanvas")
|
|
|
|
:ctx (.getContext (.getElementById js/document "gamecanvas") "2d")
|
|
|
|
:ctx (.getContext (.getElementById js/document "gamecanvas") "2d")
|
|
|
|
:target-fps 40
|
|
|
|
:target-fps 40
|
|
|
|
:timing {
|
|
|
|
:timing {;; msecs of previous frame
|
|
|
|
; msecs of previous frame
|
|
|
|
|
|
|
|
:prev 0
|
|
|
|
:prev 0
|
|
|
|
; msecs of current frame
|
|
|
|
;; msecs of current frame
|
|
|
|
:now 0
|
|
|
|
:now 0
|
|
|
|
; fps resulting of prev and now
|
|
|
|
;; fps resulting of prev and now
|
|
|
|
:fps 0
|
|
|
|
:fps 0
|
|
|
|
; difference between prev and now in seconds
|
|
|
|
;; difference between prev and now in seconds
|
|
|
|
:elapsed 0
|
|
|
|
:elapsed 0}
|
|
|
|
}
|
|
|
|
;; width and height of the canvas
|
|
|
|
; width and height of the canvas
|
|
|
|
:dimensions {:w 600
|
|
|
|
:dimensions {
|
|
|
|
:h 400}
|
|
|
|
:w 600
|
|
|
|
:input {:dir :?}
|
|
|
|
:h 400
|
|
|
|
;; currently active scene
|
|
|
|
}
|
|
|
|
|
|
|
|
:input {
|
|
|
|
|
|
|
|
:dir :?
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
; currently active scene
|
|
|
|
|
|
|
|
:scene :demo
|
|
|
|
:scene :demo
|
|
|
|
:scenes {
|
|
|
|
:scenes {:demo {:update demoscene/update-scene
|
|
|
|
:demo {
|
|
|
|
|
|
|
|
:update demoscene/update-scene
|
|
|
|
|
|
|
|
:draw demoscene/draw-scene
|
|
|
|
:draw demoscene/draw-scene
|
|
|
|
:init demoscene/init
|
|
|
|
:init demoscene/init
|
|
|
|
:data {}
|
|
|
|
:data {}}}})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defn set-timing
|
|
|
|
(defn set-timing
|
|
|
|
"sets the current time at the given key"
|
|
|
|
"sets the current time at the given key"
|
|
|
|
[state timingkey]
|
|
|
|
[state timingkey]
|
|
|
|
(assoc-in state
|
|
|
|
(assoc-in
|
|
|
|
|
|
|
|
state
|
|
|
|
[:timing timingkey]
|
|
|
|
[:timing timingkey]
|
|
|
|
(.now js/performance)))
|
|
|
|
(.now js/performance)))
|
|
|
|
|
|
|
|
|
|
|
@ -56,11 +46,10 @@
|
|
|
|
(defn set-elapsed-seconds
|
|
|
|
(defn set-elapsed-seconds
|
|
|
|
"calculates and writes the elapsed seconds since the last frame"
|
|
|
|
"calculates and writes the elapsed seconds since the last frame"
|
|
|
|
[gamestate]
|
|
|
|
[gamestate]
|
|
|
|
(assoc-in gamestate
|
|
|
|
(assoc-in
|
|
|
|
|
|
|
|
gamestate
|
|
|
|
[:timing :elapsed]
|
|
|
|
[:timing :elapsed]
|
|
|
|
(/
|
|
|
|
(/ (-(get-in gamestate [:timing :now])
|
|
|
|
(-
|
|
|
|
|
|
|
|
(get-in gamestate [:timing :now])
|
|
|
|
|
|
|
|
(get-in gamestate [:timing :prev]))
|
|
|
|
(get-in gamestate [:timing :prev]))
|
|
|
|
1000)))
|
|
|
|
1000)))
|
|
|
|
|
|
|
|
|
|
|
@ -119,14 +108,15 @@
|
|
|
|
[gamestate]
|
|
|
|
[gamestate]
|
|
|
|
(let [newstate (update-step gamestate)]
|
|
|
|
(let [newstate (update-step gamestate)]
|
|
|
|
(draw-step newstate)
|
|
|
|
(draw-step newstate)
|
|
|
|
; calculate the duration of update-step and draw-step
|
|
|
|
;; calculate the duration of update-step and draw-step
|
|
|
|
; substract that from the wait time to reach target-fps
|
|
|
|
;; substract that from the wait time to reach target-fps
|
|
|
|
; more accurately
|
|
|
|
;; more accurately
|
|
|
|
(let [now (get-in newstate [:timing :now])
|
|
|
|
(let [now (get-in newstate [:timing :now])
|
|
|
|
duration (- (.now js/performance) now)]
|
|
|
|
duration (- (.now js/performance) now)]
|
|
|
|
(.setTimeout js/window
|
|
|
|
(.setTimeout js/window
|
|
|
|
(fn []
|
|
|
|
(fn []
|
|
|
|
(.requestAnimationFrame js/window
|
|
|
|
(.requestAnimationFrame
|
|
|
|
|
|
|
|
js/window
|
|
|
|
#(mainloop newstate)))
|
|
|
|
#(mainloop newstate)))
|
|
|
|
(/
|
|
|
|
(/
|
|
|
|
(- 1000 duration)
|
|
|
|
(- 1000 duration)
|
|
|
@ -142,7 +132,8 @@
|
|
|
|
(fn [scenes [scenekey scenedata]]
|
|
|
|
(fn [scenes [scenekey scenedata]]
|
|
|
|
(let [initfunc (:init scenedata)
|
|
|
|
(let [initfunc (:init scenedata)
|
|
|
|
newdata (initfunc gamestate scenedata)]
|
|
|
|
newdata (initfunc gamestate scenedata)]
|
|
|
|
(assoc scenes
|
|
|
|
(assoc
|
|
|
|
|
|
|
|
scenes
|
|
|
|
scenekey newdata)))
|
|
|
|
scenekey newdata)))
|
|
|
|
{}
|
|
|
|
{}
|
|
|
|
(:scenes gamestate))))
|
|
|
|
(:scenes gamestate))))
|
|
|
|