Fixes memory leak and input issue.

The input issue is caused by the fact that the previous screen is
still rendered, even after .setScreen is called. Executing on
the gl thread fixes this.

The documentation says that old screens need to be disposed manually.
This instantly freed up a lot of memory when running my game.
This commit is contained in:
2015-03-07 08:01:32 -08:00
parent 6f37552ed6
commit 4120eed58a

View File

@@ -515,20 +515,24 @@ keywords and functions in pairs."
(let [run-fn! (fn [k & args] (let [run-fn! (fn [k & args]
(doseq [screen screen-objects] (doseq [screen screen-objects]
(apply (get screen k) args)))] (apply (get screen k) args)))]
(.setScreen game-object (when-let [old-screen (.getScreen game-object)]
(reify Screen (.dispose old-screen))
(show [this] (on-gl
(input! :set-input-processor (InputMultiplexer.)) (.setScreen game-object
(run-fn! :show) (reify Screen
(doseq [{:keys [screen]} screen-objects] (show [this]
(doseq [[_ listener] (:input-listeners @screen)] (input! :set-input-processor (InputMultiplexer.))
(add-input! listener)))) (run-fn! :show)
(render [this d] (run-fn! :render d))
(hide [this] (run-fn! :hide)) (doseq [{:keys [screen]} screen-objects]
(pause [this] (run-fn! :pause)) (doseq [[_ listener] (:input-listeners @screen)]
(resize [this w h] (run-fn! :resize w h)) (add-input! listener))))
(resume [this] (run-fn! :resume)) (render [this d] (run-fn! :render d))
(dispose [this]))))) (hide [this] (run-fn! :hide))
(pause [this] (run-fn! :pause))
(resize [this w h] (run-fn! :resize w h))
(resume [this] (run-fn! :resume))
(dispose [this]))))))
(defn set-screen-wrapper! (defn set-screen-wrapper!
"Sets a function that wraps around all screen functions, allowing you to "Sets a function that wraps around all screen functions, allowing you to