Add global asset manager

This commit is contained in:
oakes
2014-04-30 18:18:00 -04:00
parent 332f83c6ca
commit 348612b62f
8 changed files with 161 additions and 65 deletions

View File

@@ -11,15 +11,13 @@
"gdx-array-map!" :methods} "gdx-array-map!" :methods}
"AssetManager" {"asset-manager" :methods "AssetManager" {"asset-manager" :methods
"asset-manager!" :methods} "asset-manager!" :methods}
"AtlasTmxMapLoader" {"loader :atlas-tmx-map" :constructors "AtlasTmxMapLoader" {"loader! :atlas-tmx-map" :methods}
"loader! :atlas-tmx-map" :methods}
"Audio" {"audio!" :methods} "Audio" {"audio!" :methods}
"Bezier" {"bezier" :methods "Bezier" {"bezier" :methods
"bezier!" :methods} "bezier!" :methods}
"BitmapFont" {"bitmap-font" :constructors "BitmapFont" {"bitmap-font" :methods
"bitmap-font!" :methods} "bitmap-font!" :methods}
"BitmapFontLoader" {"loader :bitmap-font" :constructors "BitmapFontLoader" {"loader! :bitmap-font" :methods}
"loader! :bitmap-font" :methods}
"BlendingAttribute" {"attribute :blending" :constructors "BlendingAttribute" {"attribute :blending" :constructors
"attribute-type :blending" :static-fields "attribute-type :blending" :static-fields
"attribute! :blending" :static-methods} "attribute! :blending" :static-methods}
@@ -82,8 +80,7 @@
"FrictionJointDef" {"joint-def :friction" :fields} "FrictionJointDef" {"joint-def :friction" :fields}
"Frustum" {"frustum" :methods "Frustum" {"frustum" :methods
"frustum!" :methods} "frustum!" :methods}
"G3dModelLoader" {"loader :g3d-model" :constructors "G3dModelLoader" {"loader! :g3d-model" :methods}
"loader! :g3d-model" :methods}
"GearJointDef" {"joint-def :gear" :fields} "GearJointDef" {"joint-def :gear" :fields}
"GeometryUtils" {"geometry!" :static-methods} "GeometryUtils" {"geometry!" :static-methods}
"Graphics" {"graphics!" :methods} "Graphics" {"graphics!" :methods}
@@ -141,11 +138,9 @@
"model-builder!" :methods} "model-builder!" :methods}
"ModelInstance" {"model" :methods "ModelInstance" {"model" :methods
"model!" :methods} "model!" :methods}
"ModelLoader" {"loader :model" :constructors "ModelLoader" {"loader! :model" :methods}
"loader! :model" :methods}
"MouseJointDef" {"joint-def :mouse" :fields} "MouseJointDef" {"joint-def :mouse" :fields}
"MusicLoader" {"loader :music" :constructors "MusicLoader" {"loader! :music" :methods}
"loader! :music" :methods}
"Net" {"net!" :methods} "Net" {"net!" :methods}
"NinePatch" {"nine-patch" :methods "NinePatch" {"nine-patch" :methods
"nine-patch!" :methods} "nine-patch!" :methods}
@@ -154,16 +149,14 @@
"orthogonal-tiled-map!" :methods} "orthogonal-tiled-map!" :methods}
"OrthographicCamera" {"orthographic" :methods "OrthographicCamera" {"orthographic" :methods
"orthographic!" :methods} "orthographic!" :methods}
"ObjLoader" {"loader :obj" :constructors "ObjLoader" {"loader! :obj" :methods}
"loader! :obj" :methods}
"ParticleEffect" {"particle-effect" :methods "ParticleEffect" {"particle-effect" :methods
"particle-effect!" :methods} "particle-effect!" :methods}
"PerspectiveCamera" {"perspective" :methods "PerspectiveCamera" {"perspective" :methods
"perspective!" :methods} "perspective!" :methods}
"Pixmap" {"pixmap" :constructors "Pixmap" {"pixmap" :methods
"pixmap!" :methods} "pixmap!" :methods}
"PixmapLoader" {"loader :pixmap" :constructors "PixmapLoader" {"loader! :pixmap" :methods}
"loader! :pixmap" :methods}
"Plane" {"plane" :methods "Plane" {"plane" :methods
"plane!" :methods} "plane!" :methods}
"Plane.PlaneSide" {"plane-side" :static-fields} "Plane.PlaneSide" {"plane-side" :static-fields}
@@ -202,14 +195,12 @@
"ShapeRenderer.ShapeType" {"shape-type" :static-fields} "ShapeRenderer.ShapeType" {"shape-type" :static-fields}
"Skin" {"skin" :methods "Skin" {"skin" :methods
"skin!" :methods} "skin!" :methods}
"SkinLoader" {"loader :skin" :constructors "SkinLoader" {"loader! :skin" :methods}
"loader! :skin" :methods}
"Slider" {"slider" :methods "Slider" {"slider" :methods
"slider!" :methods} "slider!" :methods}
"Sound" {"sound" :methods "Sound" {"sound" :methods
"sound!" :methods} "sound!" :methods}
"SoundLoader" {"loader :sound" :constructors "SoundLoader" {"loader! :sound" :methods}
"loader! :sound" :methods}
"Sphere" {"sphere" :methods "Sphere" {"sphere" :methods
"sphere!" :methods} "sphere!" :methods}
"SplitPane.SplitPaneStyle" {"style :split-pane" :constructors} "SplitPane.SplitPaneStyle" {"style :split-pane" :constructors}
@@ -231,16 +222,14 @@
"TextField" {"text-field" :methods "TextField" {"text-field" :methods
"text-field!" :methods} "text-field!" :methods}
"TextField.TextFieldStyle" {"style :text-field" :constructors} "TextField.TextFieldStyle" {"style :text-field" :constructors}
"TextureLoader" {"loader :texture" :constructors "TextureLoader" {"loader! :texture" :methods}
"loader! :texture" :methods}
"TextureMapObject" {"map-object :texture" :methods} "TextureMapObject" {"map-object :texture" :methods}
"TextureRegion" {"texture" :methods "TextureRegion" {"texture" :methods
"texture!" :methods} "texture!" :methods}
"TextureRegionDrawable" {"drawable :texture-region" :constructors} "TextureRegionDrawable" {"drawable :texture-region" :constructors}
"Tree" {"tree" :methods "Tree" {"tree" :methods
"tree!" :methods} "tree!" :methods}
"TmxMapLoader" {"loader :tmx-map" :constructors "TmxMapLoader" {"loader! :tmx-map" :methods}
"loader! :tmx-map" :methods}
"Touchpad.TouchpadStyle" {"style :touchpad" :constructors} "Touchpad.TouchpadStyle" {"style :touchpad" :constructors}
"TiledMap" {"tiled-map" :methods "TiledMap" {"tiled-map" :methods
"tiled-map!" :methods} "tiled-map!" :methods}

View File

@@ -8,10 +8,11 @@
[com.badlogic.gdx.audio Sound] [com.badlogic.gdx.audio Sound]
[com.badlogic.gdx.assets AssetManager] [com.badlogic.gdx.assets AssetManager]
[com.badlogic.gdx.assets.loaders AsynchronousAssetLoader] [com.badlogic.gdx.assets.loaders AsynchronousAssetLoader]
[com.badlogic.gdx.files FileHandle]
[com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera [com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera
PerspectiveCamera Pixmap Pixmap$Format PixmapIO Texture PerspectiveCamera Pixmap Pixmap$Format PixmapIO Texture
VertexAttributes$Usage] VertexAttributes$Usage]
[com.badlogic.gdx.graphics.g2d SpriteBatch] [com.badlogic.gdx.graphics.g2d ParticleEffect SpriteBatch]
[com.badlogic.gdx.graphics.g3d ModelBatch] [com.badlogic.gdx.graphics.g3d ModelBatch]
[com.badlogic.gdx.graphics.glutils ShapeRenderer] [com.badlogic.gdx.graphics.glutils ShapeRenderer]
[com.badlogic.gdx.input GestureDetector [com.badlogic.gdx.input GestureDetector
@@ -165,6 +166,20 @@ handle errors and perform other custom actions each time they run.
[wrapper-fn] [wrapper-fn]
(intern 'play-clj.core 'wrapper wrapper-fn)) (intern 'play-clj.core 'wrapper wrapper-fn))
(defn set-asset-manager!
"Sets a global asset manager, which will keep track of objects that need to
be manually disposed, such as `texture` entities and `pixmap` objects. The
asset manager will then allow you to dispose them all at once.
; create an asset manager
(def manager (asset-manager))
; set it to be used by play-clj
(set-asset-manager! manager)
; dispose all assets at once
(asset-manager! manager :clear)"
[am]
(intern 'play-clj.utils '*asset-manager* am))
(defn update! (defn update!
"Runs the equivalent of `(swap! screen-atom assoc ...)`, where `screen-atom` "Runs the equivalent of `(swap! screen-atom assoc ...)`, where `screen-atom`
is the atom storing the screen map behind the scenes. Returns the updated is the atom storing the screen map behind the scenes. Returns the updated

View File

@@ -140,19 +140,20 @@
(defn sound* (defn sound*
[path] [path]
(audio! :new-sound (if (string? path) (let [^FileHandle fh (if (string? path)
(files! :internal path) (files! :internal path)
path))) path)]
(or (u/load-asset (.path fh) Sound)
(audio! :new-sound fh))))
(defmacro sound (defmacro sound
"Returns a [Sound](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/audio/Sound.html). "Returns a [Sound](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/audio/Sound.html).
When no longer needed, you must call `(sound! object :dispose)` to free it from
memory.
(sound \"playerhurt.wav\") (sound \"playerhurt.wav\")
(sound \"playerhurt.wav\" :play)" (sound \"playerhurt.wav\" :play)"
[path & options] [path & options]
`(u/calls! ^Sound (sound* ~path) ~@options)) `(let [^Sound object# (sound* ~path)]
(u/calls! object# ~@options)))
(defmacro sound! (defmacro sound!
"Calls a single method on a `sound`. "Calls a single method on a `sound`.

View File

@@ -1,13 +1,20 @@
(in-ns 'play-clj.core) (in-ns 'play-clj.core)
(defn pixmap*
[path]
(let [^FileHandle fh (if (string? path)
(files! :internal path)
path)]
(or (u/load-asset (.path fh) Pixmap)
(Pixmap. fh))))
(defmacro pixmap (defmacro pixmap
"Returns a [Pixmap](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Pixmap.html). "Returns a [Pixmap](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Pixmap.html).
(pixmap \"image.png\")" (pixmap \"image.png\")"
[& args] [path & options]
`~(if (string? (first args)) `(let [^Pixmap object# (pixmap* ~path)]
`(Pixmap. (files! :internal ~(first args))) (u/calls! object# ~@options)))
`(Pixmap. ~@args)))
(defmacro pixmap! (defmacro pixmap!
"Calls a single method on a `pixmap`. "Calls a single method on a `pixmap`.
@@ -78,7 +85,8 @@ complicated shapes. If you use `assoc` to set the overall :x and :y of the
(TiledMap.)) (TiledMap.))
([s] ([s]
(if (string? s) (if (string? s)
(.load (TmxMapLoader.) s) (or (u/load-asset s TiledMap)
(.load (TmxMapLoader.) s))
s))) s)))
(defmacro tiled-map (defmacro tiled-map
@@ -237,8 +245,8 @@ in the `layer`.
"Returns a subclass of [MapObject](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/maps/MapObject.html). "Returns a subclass of [MapObject](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/maps/MapObject.html).
(map-object :circle)" (map-object :circle)"
[k & options] [type & options]
`(let [^MapObject object# (~(map-object-init k))] `(let [^MapObject object# (~(map-object-init type))]
(u/calls! object# ~@options))) (u/calls! object# ~@options)))
(defmacro map-object! (defmacro map-object!

View File

@@ -135,20 +135,35 @@ found."
; assets ; assets
(defn ^:private loader-init (defn ^:private loader-class
[k] [k]
(cond (cond
(contains? #{:atlas-tmx-map :tmx-map} k) (contains? #{:atlas-tmx-map :tmx-map} k)
(u/gdx :maps :tiled (str (u/key->pascal k) "Loader.")) (u/gdx :maps :tiled (str (u/key->pascal k) "Loader"))
(contains? #{:g3d-model :obj} k) (contains? #{:g3d-model :obj} k)
(u/gdx :graphics :g3d :loader (str (u/key->pascal k) "Loader.")) (u/gdx :graphics :g3d :loader (str (u/key->pascal k) "Loader"))
:else :else
(u/gdx :assets :loaders (str (u/key->pascal k) "Loader.")))) (u/gdx :assets :loaders (str (u/key->pascal k) "Loader"))))
(defmacro loader (defmacro loader
"Returns a subclass of [AsynchronousAssetLoader](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/assets/loaders/AsynchronousAssetLoader.html)." "Returns a subclass of [AssetLoader](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/assets/loaders/AssetLoader.html).
[type & options]
`(~(loader-init type) ~@options)) (loader :atlas-tmx-map (resolver :internal-file-handle))
(loader :bitmap-font (resolver :internal-file-handle))
(loader :g3d-model (resolver :internal-file-handle))
(loader :model (resolver :internal-file-handle))
(loader :music (resolver :internal-file-handle))
(loader :obj (resolver :internal-file-handle))
(loader :pixmap (resolver :internal-file-handle))
(loader :skin (resolver :internal-file-handle))
(loader :sound (resolver :internal-file-handle))
(loader :texture (resolver :internal-file-handle))
(loader :tmx-map (resolver :internal-file-handle))
(loader :tmx-map
(resolver :internal-file-handle)
(load [file-name] nil))"
[type resolver & options]
`(proxy [~(loader-class type)] [~resolver] ~@options))
(defmacro loader! (defmacro loader!
"Calls a single method in a subclass of [AsynchronousAssetLoader](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/assets/loaders/AsynchronousAssetLoader.html). "Calls a single method in a subclass of [AsynchronousAssetLoader](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/assets/loaders/AsynchronousAssetLoader.html).
@@ -158,11 +173,36 @@ found."
`(let [^AsynchronousAssetLoader object# ~object] `(let [^AsynchronousAssetLoader object# ~object]
(u/call! object# ~@options))) (u/call! object# ~@options)))
(defn ^:private resolver-class
[k]
(u/gdx :assets :loaders :resolvers (str (u/key->pascal k) "Resolver")))
(defmacro resolver
"Returns an implementation of [FileHandleResolver](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/assets/loaders/FileHandleResolver.html).
(resolver :internal-file-handle)
(resolver :internal-file-handle
(resolve [file-name]
(files! :internal file-name)))"
[type & options]
`(proxy [~(resolver-class type)] [] ~@options))
(defn ^:private set-loaders
[^AssetManager am]
(->> (loader :tmx-map (resolver :internal-file-handle))
(.setLoader am TiledMap))
(->> (loader :particle-effect
(resolver :internal-file-handle)
(load [am file-name fh param]
(doto (ParticleEffect.)
(.load fh (.parent fh)))))
(.setLoader am ParticleEffect)))
(defn asset-manager* (defn asset-manager*
([] ([]
(AssetManager.)) (doto (AssetManager.) set-loaders))
([resolver] ([resolver]
(AssetManager. resolver))) (doto (AssetManager. resolver) set-loaders)))
(defmacro asset-manager (defmacro asset-manager
"Returns an [AssetManager](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/assets/AssetManager.html). "Returns an [AssetManager](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/assets/AssetManager.html).

View File

@@ -1,19 +1,33 @@
(ns play-clj.g2d (ns play-clj.g2d
(:require [play-clj.entities] (:require [play-clj.entities]
[play-clj.utils :as u]) [play-clj.utils :as u])
(:import [com.badlogic.gdx.graphics Pixmap Texture] (:import [com.badlogic.gdx Files Gdx]
[com.badlogic.gdx.files FileHandle]
[com.badlogic.gdx.graphics Pixmap Texture]
[com.badlogic.gdx.graphics.g2d Animation BitmapFont NinePatch [com.badlogic.gdx.graphics.g2d Animation BitmapFont NinePatch
ParticleEffect TextureAtlas TextureRegion] ParticleEffect TextureAtlas TextureRegion]
[play_clj.entities TextureEntity NinePatchEntity [play_clj.entities TextureEntity NinePatchEntity
ParticleEffectEntity])) ParticleEffectEntity]))
(defn bitmap-font*
[path]
(if (nil? path)
(BitmapFont.)
(let [^Files files (Gdx/files)
^FileHandle fh (if (string? path)
(.internal files path)
path)]
(or (u/load-asset (.path fh) BitmapFont)
(BitmapFont. fh)))))
(defmacro bitmap-font (defmacro bitmap-font
"Returns a [BitmapFont](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/BitmapFont.html). "Returns a [BitmapFont](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/BitmapFont.html).
(bitmap-font) (bitmap-font)
(bitmap-font (files! :internal \"default.fnt\"))" (bitmap-font \"default.fnt\")"
[& options] [& [path & options]]
`(BitmapFont. ~@options)) `(let [^BitmapFont object# (bitmap-font* ~path)]
(u/calls! object# ~@options)))
(defmacro bitmap-font! (defmacro bitmap-font!
"Calls a single method on a `bitmap-font`." "Calls a single method on a `bitmap-font`."
@@ -27,7 +41,9 @@
(TextureEntity. (TextureEntity.
(cond (cond
(string? arg) (string? arg)
(-> ^String arg Texture. TextureRegion.) (-> (or (u/load-asset arg Texture)
(Texture. ^String arg))
TextureRegion.)
(isa? (type arg) Pixmap) (isa? (type arg) Pixmap)
(-> ^Pixmap arg Texture. TextureRegion.) (-> ^Pixmap arg Texture. TextureRegion.)
(isa? (type arg) TextureRegion) (isa? (type arg) TextureRegion)
@@ -70,12 +86,14 @@
(NinePatchEntity. (NinePatchEntity.
(cond (cond
(string? arg) (string? arg)
(-> ^String arg Texture. TextureRegion. NinePatch.) (-> (or (u/load-asset arg Texture)
(Texture. ^String arg))
TextureRegion.
NinePatch.)
(:object arg) (:object arg)
(NinePatch. (:object arg)) (NinePatch. (:object arg))
(map? arg) (map? arg)
(let [{:keys [region left right top bottom]} arg] (let [{:keys [region left right top bottom]} arg]
(assert (and region left right top bottom))
(NinePatch. region left right top bottom)) (NinePatch. region left right top bottom))
:else :else
arg))) arg)))
@@ -84,8 +102,7 @@
"Returns an entity based on [NinePatch](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/NinePatch.html). "Returns an entity based on [NinePatch](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/NinePatch.html).
(nine-patch \"image.png\") (nine-patch \"image.png\")
(nine-patch \"image.png\" :set-color (color :blue)) (nine-patch \"image.png\" :set-color (color :blue))"
(nine-patch {:image \"image.png\" :left 10 :right 10 :top 10 :bottom 10})"
[arg & options] [arg & options]
`(let [entity# (nine-patch* ~arg)] `(let [entity# (nine-patch* ~arg)]
(u/calls! ^NinePatch (u/get-obj entity# :object) ~@options) (u/calls! ^NinePatch (u/get-obj entity# :object) ~@options)
@@ -107,17 +124,25 @@
; particle-effect ; particle-effect
(defn particle-effect* (defn particle-effect*
[] [path]
(ParticleEffectEntity. (ParticleEffect.))) (ParticleEffectEntity.
(if (nil? path)
(ParticleEffect.)
(let [^Files files (Gdx/files)
^FileHandle fh (if (string? path)
(.internal files path)
path)]
(or (u/load-asset (.path fh) ParticleEffect)
(doto (ParticleEffect.)
(.load fh (.parent fh))))))))
(defmacro particle-effect (defmacro particle-effect
"Returns an entity based on [ParticleEffect](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/ParticleEffect.html). "Returns an entity based on [ParticleEffect](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/ParticleEffect.html).
(particle-effect :load (particle-effect)
(files! :internal \"fire.p\") (particle-effect \"particles/fire.p\")"
(files! :internal \"fire-images\"))" [& [path & options]]
[& options] `(let [entity# (particle-effect* ~path)]
`(let [entity# (particle-effect*)]
(u/calls! ^ParticleEffect (u/get-obj entity# :object) ~@options) (u/calls! ^ParticleEffect (u/get-obj entity# :object) ~@options)
entity#)) entity#))
@@ -137,7 +162,8 @@
(defn texture-atlas* (defn texture-atlas*
[^String path] [^String path]
(TextureAtlas. path)) (or (u/load-asset path TextureAtlas)
(TextureAtlas. path)))
(defmacro texture-atlas (defmacro texture-atlas
"Returns a [TextureAtlas](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/TextureAtlas.html). "Returns a [TextureAtlas](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/TextureAtlas.html).

View File

@@ -73,12 +73,19 @@
; model ; model
(defn model*
[^String path]
(or (u/load-asset path Model)
(throw (Exception. "Asset manager not found. See set-asset-manager!"))))
(defmacro model (defmacro model
"Returns an entity based on [ModelInstance](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g3d/ModelInstance.html)." "Returns an entity based on [ModelInstance](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g3d/ModelInstance.html)."
[& args] [& args]
`(ModelEntity. `(ModelEntity.
(let [arg1# ~(first args)] (let [arg1# ~(first args)]
(cond (cond
(string? arg1#)
(ModelInstance. (model* arg1#))
(:object arg1#) (:object arg1#)
(ModelInstance. ^ModelInstance (:object arg1#)) (ModelInstance. ^ModelInstance (:object arg1#))
(isa? arg1# ModelData) (isa? arg1# ModelData)

View File

@@ -1,9 +1,19 @@
(ns play-clj.utils (ns play-clj.utils
(:require [clojure.string :as s]) (:require [clojure.string :as s])
(:import [com.badlogic.gdx.utils Array ArrayMap])) (:import [com.badlogic.gdx.assets AssetManager]
[com.badlogic.gdx.utils Array ArrayMap]))
; misc ; misc
(def ^:dynamic *asset-manager* nil)
(defn load-asset
[path type]
(when-let [^AssetManager am *asset-manager*]
(.load am path type)
(.finishLoading am)
(.get am path type)))
(defn throw-key-not-found (defn throw-key-not-found
[k] [k]
(throw (Exception. (str "The keyword " k " is not found.")))) (throw (Exception. (str "The keyword " k " is not found."))))