From 08f779ac4b67e0995819d3125148cc9d0fcf7981 Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Wed, 22 Nov 2023 22:40:27 -0500 Subject: [PATCH] implement sprite pooling --- src/consoom.fnl | 12 +++++++----- src/pool.fnl | 23 +++++++++++++++++++++++ src/sprite.fnl | 11 ++++++++++- 3 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 src/pool.fnl diff --git a/src/consoom.fnl b/src/consoom.fnl index cc785c6..30be9f2 100644 --- a/src/consoom.fnl +++ b/src/consoom.fnl @@ -6,24 +6,25 @@ (local iter (require :meta.iter)) (local state (require :state)) (local cache (require :cache)) +(local pool (require :pool)) (local {: make-image : image : defsprite : sprite : dirty : with-bg-draw : with-image : with-collision} (require :sprite)) (local gfx playdate.graphics) -(local devmode false) +(local devmode true) (local update (defmulti #$1)) (local children (defmulti #$2)) (local scene (defmulti #$1)) -(fn destroy-all [type ctx] (cache.destroy type) (tset ctx type nil)) -(fn destroy-key [type id ctx ?key] (cache.destroy type id) (tset (. ctx type) (or ?key id) nil)) -(fn destroy-item [type id ctx ?index] (cache.destroy type id) (table.remove (. ctx type) (or ?index id))) - (update:defmethod :default #nil) (children:defmethod :default (fn [val] (iter.ivalues val))) (scene:defmethod :default #nil) +(fn destroy-all [type ctx] (cache.destroy type) (tset ctx type nil)) +(fn destroy-key [type id ctx ?key] (cache.destroy type id) (tset (. ctx type) (or ?key id) nil)) +(fn destroy-item [type id ctx ?index] (cache.destroy type id) (table.remove (. ctx type) (or ?index id))) + (fn descend [t k] (children (. t k) k t)) (fn do-to-children [keys f ?t] @@ -184,5 +185,6 @@ (gfx.sprite.update) (playdate.timer.updateTimers) (when (reload.check) + (pool.purge) (cache.purge) (dirty :background))) diff --git a/src/pool.fnl b/src/pool.fnl new file mode 100644 index 0000000..cbb8ba1 --- /dev/null +++ b/src/pool.fnl @@ -0,0 +1,23 @@ +(local {: defmulti} (require :meta.multimethod)) +(local tbl (require :meta.table)) + +(local Pool { + :create (defmulti #$1) + :clear (defmulti #$1) + :pools {}}) + +(Pool.clear:defmethod :default #nil) + +(fn Pool.get [type] + (let [pool (. Pool.pools type)] + (if (and pool (. pool 1)) + (table.remove pool) + (Pool.create type)))) + +(fn Pool.return [type val] + (Pool.clear type val) + (table.insert (tbl.ensure Pool.pools type) val)) + +(fn Pool.purge [] (set Pool.pools {})) + +Pool \ No newline at end of file diff --git a/src/sprite.fnl b/src/sprite.fnl index 4e9fc0e..58e9ff6 100644 --- a/src/sprite.fnl +++ b/src/sprite.fnl @@ -1,7 +1,16 @@ (local cache (require :cache)) +(local pool (require :pool)) (import :CoreLibs/sprites) -(cache.register-asset :sprite #(doto (playdate.graphics.sprite.new) (: :add) (: :setUpdatesEnabled false)) #($1:remove)) +(pool.create:defmethod :sprite #(doto (playdate.graphics.sprite.new) (: :setUpdatesEnabled false))) +(pool.clear:defmethod :sprite #(doto $2 + (: :remove) (: :setImage nil) (: :setGroupMask 0) (: :setCollidesWithGroupsMask 0) (: :setIgnoresDrawOffset false) + (: :setCenter 0.5 0.5) (: :setVisible true) (: :setZIndex 0) (: :setTag 0))) + +(cache.register-asset :sprite + #(doto (pool.get :sprite) (: :add)) + #(pool.return :sprite $1)) + (cache.register-asset :image #(playdate.graphics.image.new $1)) (cache.register-asset :imagetable #(playdate.graphics.imagetable.new $1))