Add 2d and global functions

This commit is contained in:
oakes
2013-12-29 20:22:14 -05:00
parent e5e140cab7
commit 6b92812ab2
4 changed files with 124 additions and 39 deletions

View File

@@ -0,0 +1,45 @@
(in-ns 'play-clj.core)
; drawing
(defmulti sprite-batch #(-> % :renderer class) :default nil)
(defmethod sprite-batch nil [screen]
(SpriteBatch.))
(defmethod sprite-batch BatchTiledMapRenderer [screen]
(.getSpriteBatch (:renderer screen)))
(defn draw!
([screen]
(draw! screen (:entities screen)))
([screen entities]
(let [batch (sprite-batch screen)]
(.begin batch)
(doseq [{:keys [image x y width height]} entities]
(when (and image x y width height)
(.draw batch image (float x) (float y) (float width) (float height))))
(.end batch)
batch)))
; textures
(defn image
[^String internal-path]
(-> internal-path Texture. TextureRegion.))
(defn split-image
([^String internal-path size]
(split-image internal-path size size))
([^String internal-path width height]
(-> internal-path image (.split width height))))
(defmacro animation
[& args]
`(Animation. ~@args))
(defn get-animation-frame
([screen ^Animation animation]
(get-animation-frame screen animation true))
([screen ^Animation animation is-looping?]
(.getKeyFrame animation (:total-time screen) is-looping?)))

View File

@@ -1,8 +1,8 @@
(ns play-clj.core
(:import [com.badlogic.gdx Game Gdx Screen]
[com.badlogic.gdx.graphics.g2d SpriteBatch]
[com.badlogic.gdx.graphics
Camera Color GL20 OrthographicCamera PerspectiveCamera]
(:import [com.badlogic.gdx Game Gdx Input$Keys Screen]
[com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera
PerspectiveCamera Texture]
[com.badlogic.gdx.graphics.g2d Animation SpriteBatch TextureRegion]
[com.badlogic.gdx.maps.tiled TmxMapLoader]
[com.badlogic.gdx.maps.tiled.renderers
BatchTiledMapRenderer
@@ -11,14 +11,18 @@
IsometricTiledMapRenderer
OrthogonalTiledMapRenderer]))
(load "2d")
(load "global")
(load "render")
(defn clear!
([] (clear! 0 0 0 0))
([r g b a]
(doto (Gdx/gl)
(.glClearColor r g b a)
(.glClear GL20/GL_COLOR_BUFFER_BIT))))
(defn find-pos
[val coll]
(let [pos (if (number? val)
val
(.indexOf coll val))]
(if (and (>= pos 0) (< pos (count coll)))
pos
nil)))
(defn defscreen*
[{:keys [on-show on-render on-dispose on-hide on-pause on-resize on-resume
@@ -37,12 +41,15 @@
(conj (:entities @screen))
(swap! screen assoc :entities)))
rem-entity (fn [entity]
(->> (:entities @screen)
(remove #(= % entity))
(swap! screen assoc :entities)))
(when-let [pos (find-pos entity (:entities @screen))]
(->> (subvec (:entities @screen) (inc pos))
(concat (subvec (:entities @screen) 0 pos))
vec
(swap! screen assoc :entities))))
upd-entity (fn [entity args]
(rem-entity entity)
(add-entity (apply assoc entity args)))]
(when-let [pos (find-pos entity (:entities @screen))]
(swap! screen assoc-in
[:entities pos] (apply assoc entity args))))]
(proxy [Screen] []
(show []
(swap! screen assoc

View File

@@ -0,0 +1,57 @@
(in-ns 'play-clj.core)
; graphics
(defn clear!
([] (clear! 0 0 0 0))
([r g b a]
(doto (Gdx/gl)
(.glClearColor r g b a)
(.glClear GL20/GL_COLOR_BUFFER_BIT))))
(defn game*
[key]
(case key
:width `(.getWidth (Gdx/graphics))
:height `(.getHeight (Gdx/graphics))
:is-fullscreen? `(.isFullscreen (Gdx/graphics))
:is-touched? `(.isTouched (Gdx/input))
:x `(.getX (Gdx/input))
:y `(.getY (Gdx/input))
nil))
(defmacro game
[key]
`~(game* key))
; input
(defn resolve-key
[key]
(if (keyword? key)
(case key
:up Input$Keys/DPAD_UP
:down Input$Keys/DPAD_DOWN
:left Input$Keys/DPAD_LEFT
:right Input$Keys/DPAD_RIGHT
nil)
key))
(defn resolve-touch
[key]
(case key
:down `(> (game :y) (* (game :height) (/ 2 3)))
:up `(< (game :y) (/ (game :height) 3))
:left `(< (game :x) (/ (game :width) 3))
:right `(> (game :x) (* (game :width) (/ 2 3)))
false))
(defmacro is-pressed?
[key]
`(.isKeyPressed (Gdx/input) ~(resolve-key key)))
(defmacro is-touched?
([]
`(game :is-touched?))
([key]
`(and (game :is-touched?) ~(resolve-touch key))))

View File

@@ -1,5 +1,3 @@
(ns play-clj.render)
(in-ns 'play-clj.core)
; renderers
@@ -46,25 +44,3 @@
(defn resize-camera!
[{:keys [^Camera camera]} width height]
(.setToOrtho camera false width height))
; draw entities
(defmulti sprite-batch #(-> % :renderer class) :default nil)
(defmethod sprite-batch nil [screen]
(SpriteBatch.))
(defmethod sprite-batch BatchTiledMapRenderer [screen]
(.getSpriteBatch (:renderer screen)))
(defn draw!
([screen]
(draw! screen (:entities screen)))
([screen entities]
(let [batch (sprite-batch screen)]
(.begin batch)
(doseq [{:keys [image x y width height]} entities]
(when (and image x y width height)
(.draw batch image (float x) (float y) (float width) (float height))))
(.end batch)
batch)))