From d36a72e56459a14076cbe6fa57739060827d5599 Mon Sep 17 00:00:00 2001 From: oakes Date: Tue, 15 Apr 2014 14:11:59 -0400 Subject: [PATCH] Add set-screen-wrapper! function --- src/play_clj/core.clj | 19 ++++++++++++++++++- src/play_clj/core_utils.clj | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/play_clj/core.clj b/src/play_clj/core.clj index dd98e75..bf9dd34 100644 --- a/src/play_clj/core.clj +++ b/src/play_clj/core.clj @@ -120,7 +120,7 @@ "Creates and displays a screen for the `game` object, using one or more `screen` maps in the order they were provided. - (set-screen! hello-world main-screen text-screen)" + (set-screen! my-game main-screen text-screen)" [^Game game & screens] (let [add-inputs! (fn [] (input! :set-input-processor (InputMultiplexer.)) @@ -139,6 +139,23 @@ (resume [this] (run-fn! :resume)) (dispose [this]))))) +(defn set-screen-wrapper! + "Sets a function that wraps around all screen functions, allowing you to +handle errors and perform other custom actions each time they run. + + ; default behavior + (set-screen-wrapper! (fn [screen screen-fn] + (screen-fn))) + ; if there is an error, print it out and switch to a blank screen + ; (this is useful because it makes error recovery easier in a REPL) + (set-screen-wrapper! (fn [screen screen-fn] + (try (screen-fn) + (catch Exception e + (.printStackTrace e) + (set-screen! my-game blank-screen)))))" + [wrapper-fn] + (intern 'play-clj.core 'wrapper wrapper-fn)) + (defn update! "Runs the equivalent of `(swap! screen-atom assoc ...)`, where `screen-atom` is the atom storing the screen map behind the scenes. Returns the updated diff --git a/src/play_clj/core_utils.clj b/src/play_clj/core_utils.clj index c91749b..7a20fdd 100644 --- a/src/play_clj/core_utils.clj +++ b/src/play_clj/core_utils.clj @@ -3,7 +3,7 @@ (defmacro on-gl "Runs the macro body on the GL thread. - (on-gl (set-screen! hello-world main-screen))" + (on-gl (set-screen! my-game main-screen))" [& body] `(app! :post-runnable (fn [] ~@body)))