From 6d76fe33a9ded94865e1f324bd4adba0017f227c Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Thu, 4 Aug 2016 08:02:26 -0700 Subject: [PATCH] making stuff a tiny bit faster. --- src/play_clj/core.clj | 41 +++++++++--------- src/play_clj/core_listeners.clj | 74 ++++++++++++++++----------------- src/play_clj/core_utils.clj | 2 +- src/play_clj/g2d.clj | 2 +- src/play_clj/g2d_physics.clj | 8 ++-- src/play_clj/g3d_physics.clj | 4 +- 6 files changed, 65 insertions(+), 66 deletions(-) diff --git a/src/play_clj/core.clj b/src/play_clj/core.clj index 248dda8..7dc6e05 100755 --- a/src/play_clj/core.clj +++ b/src/play_clj/core.clj @@ -46,11 +46,7 @@ (defn ^:private normalize [entities] - (some->> entities - list - flatten - (remove nil?) - vec)) + entities) (defn ^:private wrapper [screen-atom screen-fn] @@ -76,16 +72,19 @@ [screen entities {:keys [on-show on-render on-hide on-pause on-resize on-resume on-timer] :as options}] - (let [execute-fn! (fn [func & {:keys [] :as options}] - (when func - (let [screen-map (merge @screen options) - old-entities @entities] - (some->> (with-meta - #(normalize (func screen-map old-entities)) - (meta func)) - (wrapper screen) - (reset-changed! entities old-entities) - (update-screen! @screen))))) + (let [execute-fn! (fn exec + ([func] + (exec func {})) + ([func options] + (when func + (let [screen-map (merge @screen options) + old-entities @entities] + (some->> (with-meta + #(normalize (func screen-map old-entities)) + (meta func)) + (wrapper screen) + (reset-changed! entities old-entities) + (update-screen! @screen)))))) execute-fn-on-gl! (fn [& args] (on-gl (apply execute-fn! args))) update-fn! (fn [func & args] @@ -118,12 +117,11 @@ update-screen!)) :render (fn [d] (swap! screen update-in [:total-time] #(+ (or %1 0) %2) d) - (some->> (execute-fn! on-render :delta-time d) - (add-to-timeline! screen))) + (execute-fn! on-render (assoc @screen :delta-time d))) :hide #(execute-fn! on-hide) :pause #(execute-fn! on-pause) :resize (fn [w h] - (execute-fn! on-resize :width w :height h) + (execute-fn! on-resize {:width w :height h}) (update-screen! @screen)) :resume #(execute-fn! on-resume)})) @@ -488,7 +486,7 @@ keywords and functions in pairs." (intern *ns* map-sym# (atom {})))) entities-sym# (symbol (str '~n "-entities")) entities# (deref (or (resolve entities-sym#) - (intern *ns* entities-sym# (atom []))))] + (intern *ns* entities-sym# (atom {}))))] (def ~n (defscreen* screen# entities# fn-syms#)))) (defn defgame* @@ -559,6 +557,7 @@ is the atom storing the screen map behind the scenes. Returns the updated (update! screen :renderer (stage))" [screen & args] + (println screen) (doto (apply (:update-fn! screen) assoc args) update-screen!)) @@ -568,8 +567,8 @@ of key-value pairs, which will be given to the function via its screen map. (screen! my-other-screen :on-show) (screen! my-other-screen :on-change-color :color :blue)" - [screen-object fn-name & options] + [screen-object fn-name options] (let [execute-fn! (:execute-fn! screen-object) screen-fn (-> screen-object :options (get fn-name))] - (apply execute-fn! screen-fn options) + (execute-fn! screen-fn options) nil)) diff --git a/src/play_clj/core_listeners.clj b/src/play_clj/core_listeners.clj index 54c638b..d876973 100755 --- a/src/play_clj/core_listeners.clj +++ b/src/play_clj/core_listeners.clj @@ -8,28 +8,28 @@ execute-fn!] (reify InputProcessor (keyDown [this k] - (execute-fn! on-key-down :key k) + (execute-fn! on-key-down {:key k}) false) (keyTyped [this c] - (execute-fn! on-key-typed :character c) + (execute-fn! on-key-typed {:character c}) false) (keyUp [this k] - (execute-fn! on-key-up :key k) + (execute-fn! on-key-up {:key k}) false) (mouseMoved [this sx sy] - (execute-fn! on-mouse-moved :input-x sx :input-y sy) + (execute-fn! on-mouse-moved {:input-x sx :input-y sy}) false) (scrolled [this a] - (execute-fn! on-scrolled :amount a) + (execute-fn! on-scrolled {:amount a}) false) (touchDown [this sx sy p b] - (execute-fn! on-touch-down :input-x sx :input-y sy :pointer p :button b) + (execute-fn! on-touch-down {:input-x sx :input-y sy :pointer p :button b}) false) (touchDragged [this sx sy p] - (execute-fn! on-touch-dragged :input-x sx :input-y sy :pointer p) + (execute-fn! on-touch-dragged {:input-x sx :input-y sy :pointer p}) false) (touchUp [this sx sy p b] - (execute-fn! on-touch-up :input-x sx :input-y sy :pointer p :button b) + (execute-fn! on-touch-up {:input-x sx :input-y sy :pointer p :button b}) false))) (defmacro input-processor! @@ -45,29 +45,29 @@ in the `screen`." execute-fn!] (reify GestureDetector$GestureListener (fling [this vx vy b] - (execute-fn! on-fling :velocity-x vx :velocity-y vy :button b) + (execute-fn! on-fling {:velocity-x vx :velocity-y vy :button b}) (some? on-fling)) (longPress [this x y] - (execute-fn! on-long-press :input-x x :input-y y) + (execute-fn! on-long-press {:input-x x :input-y y}) (some? on-long-press)) (pan [this x y dx dy] - (execute-fn! on-pan :input-x x :input-y y :delta-x dx :delta-y dy) + (execute-fn! on-pan {:input-x x :input-y y :delta-x dx :delta-y dy}) (some? on-pan)) (panStop [this x y p b] - (execute-fn! on-pan-stop :input-x x :input-y y :pointer p :button b) + (execute-fn! on-pan-stop {:input-x x :input-y y :pointer p :button b}) (some? on-pan-stop)) (pinch [this ip1 ip2 p1 p2] (execute-fn! on-pinch - :initial-pointer-1 ip1 :initial-pointer-2 ip2 - :pointer-1 p1 :pointer-2 p2) + {:initial-pointer-1 ip1 :initial-pointer-2 ip2 + :pointer-1 p1 :pointer-2 p2}) (some? on-pinch)) (tap [this x y c b] - (execute-fn! on-tap :input-x x :input-y y :count c :button b) + (execute-fn! on-tap {:input-x x :input-y y :count c :button b}) (some? on-tap)) (touchDown [this x y p b] false) (zoom [this id d] - (execute-fn! on-zoom :initial-distance id :distance d) + (execute-fn! on-zoom {:initial-distance id :distance d}) (some? on-zoom)))) (defn ^:private gesture-detector @@ -96,30 +96,30 @@ in the `screen`." (proxy [ActorGestureListener] [] (fling [e vx vy b] (execute-fn! on-ui-fling - :event e :velocity-x vx :velocity-y vy :button b)) + {:event e :velocity-x vx :velocity-y vy :button b})) (longPress [a x y] - (execute-fn! on-ui-long-press :actor a :input-x x :input-y y) + (execute-fn! on-ui-long-press {:actor a :input-x x :input-y y}) (some? on-ui-long-press)) (pan [e x y dx dy] (execute-fn! on-ui-pan - :event e :input-x x :input-y y :delta-x dx :delta-y dy)) + { :event e :input-x x :input-y y :delta-x dx :delta-y dy})) (pinch [e ip1 ip2 p1 p2] (execute-fn! on-ui-pinch - :event e :initial-pointer-1 ip1 :initial-pointer-2 ip2 - :pointer1 p1 :pointer2 p2)) + { :event e :initial-pointer-1 ip1 :initial-pointer-2 ip2 + :pointer1 p1 :pointer2 p2})) (tap [e x y p b] (execute-fn! on-ui-tap - :event e :input-x x :input-y y :pointer p :button b)) + { :event e :input-x x :input-y y :pointer p :button b})) ;(touchDown [e x y p b]) ;(touchUp [e x y p b]) (zoom [e id d] - (execute-fn! on-ui-zoom :event e :initial-distance id :distance d)))) + (execute-fn! on-ui-zoom { :event e :initial-distance id :distance d})))) (defn ^:private change-listener [{:keys [on-ui-changed]} execute-fn!] (proxy [ChangeListener] [] (changed [e a] - (execute-fn! on-ui-changed :event e :actor a)))) + (execute-fn! on-ui-changed { :event e :actor a})))) (defn ^:private click-listener [{:keys [on-ui-clicked on-ui-enter on-ui-exit @@ -127,23 +127,23 @@ in the `screen`." execute-fn!] (proxy [ClickListener] [] (clicked [e x y] - (execute-fn! on-ui-clicked :event e :input-x x :input-y y)) + (execute-fn! on-ui-clicked { :event e :input-x x :input-y y})) (enter [e x y p a] (execute-fn! on-ui-enter - :event e :input-x x :input-y y :pointer p :actor a)) + { :event e :input-x x :input-y y :pointer p :actor a})) (exit [e x y p a] (execute-fn! on-ui-exit - :event e :input-x x :input-y y :pointer p :actor a)) + { :event e :input-x x :input-y y :pointer p :actor a})) (touchDown [e x y p b] (execute-fn! on-ui-touch-down - :event e :input-x x :input-y y :pointer p :button b) + { :event e :input-x x :input-y y :pointer p :button b}) (some? on-ui-touch-down)) (touchDragged [e x y p] (execute-fn! on-ui-touch-dragged - :event e :input-x x :input-y y :pointer p)) + { :event e :input-x x :input-y y :pointer p})) (touchUp [e x y p b] (execute-fn! on-ui-touch-up - :event e :input-x x :input-y y :pointer p :button b)))) + { :event e :input-x x :input-y y :pointer p :button b})))) (defn ^:private drag-listener [{:keys [on-ui-drag on-ui-drag-start on-ui-drag-stop]} @@ -153,20 +153,20 @@ in the `screen`." ;(touchDragged [e x y p]) ;(touchUp [e x y p b]) (drag [e x y p] - (execute-fn! on-ui-drag :event e :input-x x :input-y y :pointer p)) + (execute-fn! on-ui-drag { :event e :input-x x :input-y y :pointer p})) (dragStart [e x y p] - (execute-fn! on-ui-drag-start :event e :input-x x :input-y y :pointer p)) + (execute-fn! on-ui-drag-start { :event e :input-x x :input-y y :pointer p})) (dragStop [e x y p] - (execute-fn! on-ui-drag-stop :event e :input-x x :input-y y :pointer p)))) + (execute-fn! on-ui-drag-stop { :event e :input-x x :input-y y :pointer p})))) (defn ^:private focus-listener [{:keys [on-ui-keyboard-focus-changed on-ui-scroll-focus-changed]} execute-fn!] (proxy [FocusListener] [] (keyboardFocusChanged [e a f] - (execute-fn! on-ui-keyboard-focus-changed :event e :actor a :focused? f)) + (execute-fn! on-ui-keyboard-focus-changed { :event e :actor a :focused? f})) (scrollFocusChanged [e a f] - (execute-fn! on-ui-scroll-focus-changed :event e :actor a :focused? f)))) + (execute-fn! on-ui-scroll-focus-changed { :event e :actor a :focused? f})))) (defn ^:private ui-listeners [options execute-fn!] @@ -190,7 +190,7 @@ in the `screen`." (doto (.getViewport renderer) (.setCamera camera) (.setWorldSize (. camera viewportWidth) (. camera viewportHeight))))) - ([{:keys [^Stage renderer ui-listeners]} [entities]] + ([{:keys [^Stage renderer ui-listeners]} entities] (let [actor-set (->> entities vals (map :object) @@ -206,7 +206,7 @@ in the `screen`." (add-input! renderer)))) (defmulti update-physics! - (fn [screen & [entities]] (some-> screen :world class .getName)) + (fn [screen & entities] (some-> screen :world class .getName)) :default nil) (defmethod update-physics! nil [_ & _]) diff --git a/src/play_clj/core_utils.clj b/src/play_clj/core_utils.clj index 217c061..30b8024 100755 --- a/src/play_clj/core_utils.clj +++ b/src/play_clj/core_utils.clj @@ -116,7 +116,7 @@ entities vector." [{:keys [execute-fn! on-timer]} id] (proxy [Timer$Task] [] (run [] - (execute-fn! on-timer :id id)))) + (execute-fn! on-timer {:id id})))) (defn ^:private timer* [] diff --git a/src/play_clj/g2d.clj b/src/play_clj/g2d.clj index c127b5f..bee5565 100755 --- a/src/play_clj/g2d.clj +++ b/src/play_clj/g2d.clj @@ -44,7 +44,7 @@ (-> ^Pixmap arg Texture. TextureRegion.) (instance? Texture arg) (-> ^Texture arg TextureRegion.) - (instance? TextureEntity ) + (instance? TextureEntity arg) (-> ^TextureRegion (:object arg) TextureRegion.) :else arg))) diff --git a/src/play_clj/g2d_physics.clj b/src/play_clj/g2d_physics.clj index 16a534d..a5b8505 100755 --- a/src/play_clj/g2d_physics.clj +++ b/src/play_clj/g2d_physics.clj @@ -228,13 +228,13 @@ such as :on-begin-contact." execute-fn!] (reify ContactListener (beginContact [this c] - (execute-fn! on-begin-contact :contact c)) + (execute-fn! on-begin-contact { :contact c})) (endContact [this c] - (execute-fn! on-end-contact :contact c)) + (execute-fn! on-end-contact { :contact c})) (postSolve [this c i] - (execute-fn! on-post-solve :contact c :impulse i)) + (execute-fn! on-post-solve { :contact c :impulse i})) (preSolve [this c m] - (execute-fn! on-pre-solve :contact c :old-manifold m)))) + (execute-fn! on-pre-solve { :contact c :old-manifold m})))) (defmethod c/update-physics! "com.badlogic.gdx.physics.box2d.World" diff --git a/src/play_clj/g3d_physics.clj b/src/play_clj/g3d_physics.clj index 615809a..03bc6b3 100755 --- a/src/play_clj/g3d_physics.clj +++ b/src/play_clj/g3d_physics.clj @@ -273,9 +273,9 @@ such as :on-begin-contact." execute-fn!] (ContactListener3D. (fn [a b] - (execute-fn! on-begin-contact :first-body a :second-body b)) + (execute-fn! on-begin-contact { :first-body a :second-body b})) (fn [a b] - (execute-fn! on-end-contact :first-body a :second-body b)))) + (execute-fn! on-end-contact { :first-body a :second-body b})))) (defn ^:private get-bodies [screen]