From 52f6b384779c2b615d70c84d2a18460b3606577c Mon Sep 17 00:00:00 2001 From: oakes Date: Sun, 16 Mar 2014 01:00:37 -0400 Subject: [PATCH] Break camera code into separate file and add getter functions --- src/play_clj/core.clj | 3 +- .../{core_global.clj => core_basics.clj} | 0 src/play_clj/core_cameras.clj | 177 ++++++++++++++++++ src/play_clj/core_graphics.clj | 117 ------------ 4 files changed, 179 insertions(+), 118 deletions(-) rename src/play_clj/{core_global.clj => core_basics.clj} (100%) create mode 100644 src/play_clj/core_cameras.clj diff --git a/src/play_clj/core.clj b/src/play_clj/core.clj index 932d321..4b44bc4 100644 --- a/src/play_clj/core.clj +++ b/src/play_clj/core.clj @@ -27,7 +27,8 @@ [com.badlogic.gdx.utils Timer$Task] [play_clj.utils Entity])) -(load "core_global") +(load "core_basics") +(load "core_cameras") (load "core_graphics") (load "core_listeners") (load "core_utils") diff --git a/src/play_clj/core_global.clj b/src/play_clj/core_basics.clj similarity index 100% rename from src/play_clj/core_global.clj rename to src/play_clj/core_basics.clj diff --git a/src/play_clj/core_cameras.clj b/src/play_clj/core_cameras.clj new file mode 100644 index 0000000..5286a8d --- /dev/null +++ b/src/play_clj/core_cameras.clj @@ -0,0 +1,177 @@ +(in-ns 'play-clj.core) + +(defn orthographic* + "The function version of `orthographic`" + [] + (OrthographicCamera.)) + +(defmacro orthographic + "Returns an [OrthographicCamera](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/OrthographicCamera.html) + + (orthographic)" + [& options] + `(let [^OrthographicCamera object# (orthographic*)] + (u/calls! object# ~@options))) + +(defmacro orthographic! + "Calls a single method on an `orthographic`" + [screen k & options] + `(let [^OrthographicCamera object# (u/get-obj ~screen :camera)] + (u/call! object# ~k ~@options))) + +(defn perspective* + "The function version of `perspective`" + ([] + (PerspectiveCamera.)) + ([field-of-view viewport-width viewport-height] + (PerspectiveCamera. field-of-view viewport-width viewport-height))) + +(defmacro perspective + "Returns an [PerspectiveCamera](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/PerspectiveCamera.html) + + (perspective)" + [fov vw vh & options] + `(let [^PerspectiveCamera object# (perspective* ~fov ~vw ~vh)] + (u/calls! object# ~@options))) + +(defmacro perspective! + "Calls a single method on a `perspective`" + [screen k & options] + `(let [^PerspectiveCamera object# (u/get-obj ~screen :camera)] + (u/call! object# ~k ~@options))) + +(defn size! + "Sets the size of the camera in `screen` + + (size! screen 480 360)" + [screen width height] + (let [^OrthographicCamera camera (u/get-obj screen :camera)] + (.setToOrtho camera false width height))) + +(defn width! + "Sets the width of the camera in `screen`, adjusting the height so the ratio +remains in tact + + (width! screen 480)" + [screen new-width] + (size! screen new-width (* new-width (/ (game :height) (game :width))))) + +(defn width + "Returns the width of the camera in `screen` + + (width screen)" + [screen] + (let [^Camera camera (u/get-obj screen :camera)] + (. camera viewportWidth))) + +(defn height! + "Sets the height of the camera in `screen`, adjusting the width so the ratio +remains in tact + + (height! screen 360)" + [screen new-height] + (size! screen (* new-height (/ (game :width) (game :height))) new-height)) + +(defn height + "Returns the height of the camera in `screen` + + (height screen)" + [screen] + (let [^Camera camera (u/get-obj screen :camera)] + (. camera viewportHeight))) + +(defn x! + "Sets only the x position of the camera in `screen`" + [screen x-val] + (let [^Camera camera (u/get-obj screen :camera)] + (set! (. (. camera position) x) x-val) + (.update camera))) + +(defn x + "Returns the x position of the camera in `screen`" + [screen] + (let [^Camera camera (u/get-obj screen :camera)] + (. (. camera position) x))) + +(defn y! + "Sets only the y position of the camera in `screen`" + [screen y-val] + (let [^Camera camera (u/get-obj screen :camera)] + (set! (. (. camera position) y) y-val) + (.update camera))) + +(defn y + "Returns the y position of the camera in `screen`" + [screen] + (let [^Camera camera (u/get-obj screen :camera)] + (. (. camera position) y))) + +(defn z! + "Sets only the z position of the camera in `screen`" + [screen z-val] + (let [^Camera camera (u/get-obj screen :camera)] + (set! (. (. camera position) z) z-val) + (.update camera))) + +(defn z + "Returns the z position of the camera in `screen`" + [screen] + (let [^Camera camera (u/get-obj screen :camera)] + (. (. camera position) z))) + +(defn position! + "Sets the position of the camera in `screen`" + ([screen pos] + (let [^Camera camera (u/get-obj screen :camera)] + (set! (. camera position) pos))) + ([screen x y] + (position! screen x y nil)) + ([screen x y z] + (when x (x! screen x)) + (when y (y! screen y)) + (when z (z! screen z)))) + +(defn position + "Returns the position of the camera in `screen`" + [screen] + (let [^Camera camera (u/get-obj screen :camera)] + (. camera position))) + +(defn direction! + "Sets the direction of the camera in `screen`" + [screen x y z] + (let [^Camera camera (u/get-obj screen :camera)] + (.lookAt camera x y z) + (.update camera))) + +(defn direction + "Returns the direction of the camera in `screen`" + [screen] + (let [^Camera camera (u/get-obj screen :camera)] + (. camera direction))) + +(defn near! + "Sets the near clipping plane distance of the camera in `screen`" + [screen n] + (let [^Camera camera (u/get-obj screen :camera)] + (set! (. camera near) n) + (.update camera))) + +(defn near + "Returns the near clipping plane distance of the camera in `screen`" + [screen] + (let [^Camera camera (u/get-obj screen :camera)] + (. camera near))) + +(defn far! + "Sets the far clipping plane distance of the camera in `screen`" + [screen n] + (let [^Camera camera (u/get-obj screen :camera)] + (set! (. camera far) n) + (.update camera))) + +(defn far + "Returns the far clipping plane distance of the camera in `screen`" + [screen] + (let [^Camera camera (u/get-obj screen :camera)] + (. camera far))) diff --git a/src/play_clj/core_graphics.clj b/src/play_clj/core_graphics.clj index 8451c32..b06ca56 100644 --- a/src/play_clj/core_graphics.clj +++ b/src/play_clj/core_graphics.clj @@ -384,120 +384,3 @@ specify which layers to render with or without (u/draw-entity! entity batch))) (.end batch)) entities) - -; cameras - -(defn orthographic* - "The function version of `orthographic`" - [] - (OrthographicCamera.)) - -(defmacro orthographic - "Returns an [OrthographicCamera](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/OrthographicCamera.html) - - (orthographic)" - [& options] - `(let [^OrthographicCamera object# (orthographic*)] - (u/calls! object# ~@options))) - -(defmacro orthographic! - "Calls a single method on an `orthographic`" - [screen k & options] - `(let [^OrthographicCamera object# (u/get-obj ~screen :camera)] - (u/call! object# ~k ~@options))) - -(defn perspective* - "The function version of `perspective`" - ([] - (PerspectiveCamera.)) - ([field-of-view viewport-width viewport-height] - (PerspectiveCamera. field-of-view viewport-width viewport-height))) - -(defmacro perspective - "Returns an [PerspectiveCamera](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/PerspectiveCamera.html) - - (perspective)" - [fov vw vh & options] - `(let [^PerspectiveCamera object# (perspective* ~fov ~vw ~vh)] - (u/calls! object# ~@options))) - -(defmacro perspective! - "Calls a single method on a `perspective`" - [screen k & options] - `(let [^PerspectiveCamera object# (u/get-obj ~screen :camera)] - (u/call! object# ~k ~@options))) - -(defn size! - "Sets the size of the camera in `screen` - - (size! screen 480 360)" - [screen width height] - (let [^OrthographicCamera camera (u/get-obj screen :camera)] - (.setToOrtho camera false width height))) - -(defn width! - "Sets the width of the camera in `screen`, adjusting the height so the ratio -remains in tact - - (width! screen 480)" - [screen new-width] - (size! screen new-width (* new-width (/ (game :height) (game :width))))) - -(defn height! - "Sets the height of the camera in `screen`, adjusting the width so the ratio -remains in tact - - (height! screen 360)" - [screen new-height] - (size! screen (* new-height (/ (game :width) (game :height))) new-height)) - -(defn x! - "Sets only the x position of the camera in `screen`" - [screen x] - (let [^Camera camera (u/get-obj screen :camera)] - (set! (. (. camera position) x) x) - (.update camera))) - -(defn y! - "Sets only the y position of the camera in `screen`" - [screen y] - (let [^Camera camera (u/get-obj screen :camera)] - (set! (. (. camera position) y) y) - (.update camera))) - -(defn z! - "Sets only the z position of the camera in `screen`" - [screen z] - (let [^Camera camera (u/get-obj screen :camera)] - (set! (. (. camera position) z) z) - (.update camera))) - -(defn position! - "Sets the position of the camera in `screen`" - ([screen x y] - (position! screen x y nil)) - ([screen x y z] - (when x (x! screen x)) - (when y (y! screen y)) - (when z (z! screen z)))) - -(defn direction! - "Sets the directino of the camera in `screen`" - [screen x y z] - (let [^Camera camera (u/get-obj screen :camera)] - (.lookAt camera x y z) - (.update camera))) - -(defn near! - "Sets the near clipping plane distance of the camera in `screen`" - [screen n] - (let [^Camera camera (u/get-obj screen :camera)] - (set! (. camera near) n) - (.update camera))) - -(defn far! - "Sets the far clipping plane distance of the camera in `screen`" - [screen n] - (let [^Camera camera (u/get-obj screen :camera)] - (set! (. camera far) n) - (.update camera)))