Skip to content
This repository has been archived by the owner on Jun 17, 2020. It is now read-only.

Commit

Permalink
Pass display into fns rather than using atoms
Browse files Browse the repository at this point in the history
  • Loading branch information
harto committed Jan 20, 2013
1 parent 499fec9 commit b801980
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 30 deletions.
23 changes: 6 additions & 17 deletions common/src/cljx/enoki/engine.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,18 @@
)
(:require-macros [enoki.util.logging-macros :as log]))

(def get-display
"Function that returns the graphics display"
(atom nil))

(defn- init-platform-bindings!
"Populates the environment with various platform-specific functions."
[{:keys [display]}]
(log/info "Establishing platform-specific bindings")
(reset! get-display (constantly display)))

(defn render [display state]
(-> (g/context display)
(defn render [ctx state]
(-> ctx
(g/clear!)
(g/draw-text! "Hello, world" 10 20)))

(defn tick [state]
(render (@get-display) state))
(defn tick [{:keys [state display]}]
(render (g/context display) state))

(defn start
"Enters the game loop. This function might return immediately or once the game
loop is exited, depending on the implementation of loop-fn."
[env initial-state]
(init-platform-bindings! env)
[env]
(log/info "Entering game loop")
(let [loop-forever (:loop-fn env)]
(loop-forever tick initial-state)))
(loop-forever tick env)))
6 changes: 3 additions & 3 deletions swing/src/enoki/engine_impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(defn loop-forever
"A naïve game loop implementation that sleeps for 1 millisecond between
calls to `tick'."
[tick initial-state]
(loop [state initial-state]
[tick env]
(loop [env env]
(Thread/sleep 1)
(recur (tick state))))
(recur (tick env))))
4 changes: 2 additions & 2 deletions swing/src/enoki/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

(defn start
"Engine entry point"
[env initial-state]
(engine/start (assoc env :loop-fn impl/loop-forever) initial-state))
[env]
(engine/start (assoc env :loop-fn impl/loop-forever)))
4 changes: 2 additions & 2 deletions swing/src/game/swing/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
(defn init []
(let [screen (create-screen)]
(seesaw/invoke-now (seesaw/show! (create-frame screen)))
(enoki/start {:display (gfx/->JComponentDisplay screen)}
{})))
(enoki/start {:display (gfx/->JComponentDisplay screen)
:state {}})))

(defn -main [& args]
(init))
4 changes: 2 additions & 2 deletions web/src/cljs/enoki/engine_impl.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns enoki.engine-impl
(:require [goog.Timer :as timer]))

(defn loop-forever [tick state]
(timer/callOnce #(loop-forever tick (tick state)) 1))
(defn loop-forever [tick env]
(timer/callOnce #(loop-forever tick (tick env)) 1))
4 changes: 2 additions & 2 deletions web/src/cljs/enoki/main.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

(defn start
"Engine entry point"
[env initial-state]
[env]
(repl/connect "http://localhost:9000/repl")
(logging/init!)
(logging/set-level! :info)
(engine/start (assoc env :loop-fn impl/loop-forever) initial-state))
(engine/start (assoc env :loop-fn impl/loop-forever)))
4 changes: 2 additions & 2 deletions web/src/cljs/game/web/main.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
[enoki.util.dom :as dom]))

(defn ^:export init []
(enoki/start {:display (gfx/->CanvasDisplay (dom/get-element "screen"))}
{}))
(enoki/start {:display (gfx/->CanvasDisplay (dom/get-element "screen"))
:state {}}))

0 comments on commit b801980

Please sign in to comment.