From 7115252b623d7b6d6cb268813c867785d866efa5 Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Wed, 23 Dec 2020 22:17:33 -0500 Subject: [PATCH] Implement tileset switching --- asm/asm.fnl | 4 ++++ asm/vm.fnl | 4 ++-- editor/gfxedit.fnl | 16 +++++++++------- editor/tiledraw.fnl | 18 ++++++++++-------- editor/tileedit.fnl | 7 ++++--- game/defs.fnl | 10 +++++----- game/gfx.fnl | 9 ++++++++- game/player.fnl | 27 ++++++++++++++------------- game/tiles.fnl | 8 +++++--- game/tiles.json | 2 +- 10 files changed, 62 insertions(+), 43 deletions(-) diff --git a/asm/asm.fnl b/asm/asm.fnl index 39de6d2..93216cf 100644 --- a/asm/asm.fnl +++ b/asm/asm.fnl @@ -151,6 +151,7 @@ (fn dat-parser.export [label block] (tset block.globals (. label 2) true) nil) + (fn dat-parser.pad [pad] {:type :pad :align (. pad 2)}) (local pdat-processor { :op {} @@ -158,6 +159,7 @@ :ref {} :raw {} :block {} + :pad {} }) (fn process-pdat [pdat process default ...] @@ -176,6 +178,7 @@ (fn pdat-processor.op.size [op] (size op.mode)) (fn pdat-processor.var.size [d] d.size) (fn pdat-processor.ref.size [r] 2) + (fn pdat-processor.pad.size [pad] (- pad.align (% pad.addr pad.align))) (fn pdat-processor.op.bytes [op env] (local bytegen (. opcodes op.opcode)) @@ -205,6 +208,7 @@ (fn pdat-processor.ref.bytes [ref env] (int16-to-bytes (env:lookup-addr ref.target))) + (fn pdat-processor.pad.bytes [pad] (string.rep "\0" pad.size)) (fn pdat-processor.block.symbols [block] (lume.keys block.symbols)) diff --git a/asm/vm.fnl b/asm/vm.fnl index 399e726..ef79fba 100644 --- a/asm/vm.fnl +++ b/asm/vm.fnl @@ -68,7 +68,7 @@ :ST1H :0x7f :ST2 :0x7c :ST2H :0x7d - :RSTACK :0x6000 + :RSTACK :0x100 :true 0xffff :false 0 :code code1 @@ -492,7 +492,7 @@ [:lda vm.TOP :x] [:ora vm.ST1 :x] [:sta vm.ST1 :x] [:lda vm.TOPH :x] [:ora vm.ST1H :x] [:sta vm.ST1H :x] (vm:drop)) - + (vm:def :shl4 ; n -- n [:asl vm.TOP :x] [:rol vm.TOPH :x] [:asl vm.TOP :x] [:rol vm.TOPH :x] diff --git a/editor/gfxedit.fnl b/editor/gfxedit.fnl index 07898dd..a7336db 100644 --- a/editor/gfxedit.fnl +++ b/editor/gfxedit.fnl @@ -25,10 +25,10 @@ (local itile (+ self.itile ditile)) (when (>= itile 1) (set self.itile itile)))) -(fn GraphicsEditView.draw-sprite [self x y itile] - (love.graphics.draw (self.tilecache:sprite itile) x y 0 self.sprite-scale self.sprite-scale)) +(fn GraphicsEditView.draw-sprite [self x y itile ?key] + (love.graphics.draw (self.tilecache:sprite itile ?key) x y 0 self.sprite-scale self.sprite-scale)) -(fn GraphicsEditView.draw-tile-selector [self x y w] +(fn GraphicsEditView.draw-tile-selector [self x y w ?key] (var tilex x) (var tiley y) (var (pixw pixh) (self:tilesize)) @@ -36,14 +36,16 @@ (local tilew (* self.sprite-scale pixw)) (local tileh (* self.sprite-scale pixh)) (for [itile 1 (length self.tilecache.tiles)] - (self:draw-sprite tilex tiley itile) - (when (= itile self.itile) + (self:draw-sprite tilex tiley itile ?key) + (when (and (= itile self.itile) (= ?key self.tilekey)) (love.graphics.rectangle :line (- tilex 2) (- tiley 2) (+ tilew 4) (+ tileh 4))) (when (button self [:tile itile] tilex tiley tilew tileh) - (set self.itile itile)) + (set self.itile itile) + (set self.tilekey ?key)) (set tilex (+ tilex tilew 4)) (when (>= (+ tilex tilew) (+ x w)) (set tilex x) - (set tiley (+ tiley tileh 4))))) + (set tiley (+ tiley tileh 4)))) + (+ tiley tileh (- y))) GraphicsEditView diff --git a/editor/tiledraw.fnl b/editor/tiledraw.fnl index 655646c..660d79f 100644 --- a/editor/tiledraw.fnl +++ b/editor/tiledraw.fnl @@ -68,7 +68,7 @@ (set (state prevpal) (draw-byte tile (+ y 1) x y state prevpal)) (set (state prevpal) (draw-byte tile (+ y 17) (+ x 7) y state prevpal))))))) -(fn tile-to-sprite [tile] (tilestrip-to-sprite [tile])) +(fn tile-to-sprite [tile] (if tile (tilestrip-to-sprite [tile]) (make-canvas 14 16 #nil))) (fn portrait-to-sprite [gfx] (local top (tilestrip-to-sprite [(gfx:sub 1 32) (gfx:sub 65 96)])) @@ -87,12 +87,13 @@ :spritegen (or ?spritegen tile-to-sprite) :tilesprites [] :tile (fn [self itile] (or (. self.tiles itile) {:flags {}})) + :cachekey (fn [itile ?key] (.. (or ?key :gfx) itile)) :update-tile - (fn [self itile tile] + (fn [self itile tile ?key] (tset self.tiles itile (-> (self:tile itile) - (doto (tset :gfx tile)))) - (tset self.tilesprites itile nil)) + (doto (tset (or ?key :gfx) tile)))) + (tset self.tilesprites (self.cachekey itile ?key) nil)) :set-flag (fn [self itile flag clear] (tset (. self.tiles itile :flags) flag (if clear nil true))) @@ -101,10 +102,11 @@ (set self.tiles tiles) (set self.tilesprites [])) :sprite - (fn [self itile] - (when (and (= nil (. self.tilesprites itile)) (not= nil (. self.tiles itile))) - (tset self.tilesprites itile (self.spritegen (. self.tiles itile :gfx)))) - (. self.tilesprites itile))}) + (fn [self itile ?key] + (local key (self.cachekey itile ?key)) + (when (and (= nil (. self.tilesprites key)) (not= nil (. self.tiles itile))) + (tset self.tilesprites key (self.spritegen (. self.tiles itile (or ?key :gfx))))) + (. self.tilesprites key))}) {: tile-to-sprite : tilestrip-to-sprite : portrait-to-sprite : char-to-sprite : pal-from-bit : pal-from-byte : TileCache} diff --git a/editor/tileedit.fnl b/editor/tileedit.fnl index 5f62b18..24b160e 100644 --- a/editor/tileedit.fnl +++ b/editor/tileedit.fnl @@ -47,7 +47,7 @@ (fn TileView.tile [self] (local (w h) (self:tilesize)) - (or (-?> self.tilecache.tiles (. self.itile) (. :gfx)) (string.rep "\0" (/ (* w h) 8)))) + (or (-?> self.tilecache.tiles (. self.itile) (. (or self.tilekey :gfx))) (string.rep "\0" (/ (* w h) 8)))) (fn TileView.draw-tile-editor [self tile x y] (when (not (active? self :tile)) @@ -85,7 +85,7 @@ (self:draw-tile-flag flagname x (+ y (* (+ iflag 1) (+ pixel-size 4)))))) (fn TileView.update-tile [self newtile] - (self.tilecache:update-tile self.itile newtile)) + (self.tilecache:update-tile self.itile newtile self.tilekey)) (fn TileView.save [self] (tiles.savegfx (self:filename) self.tilecache.tiles)) @@ -94,7 +94,8 @@ (local (x y) (values (+ self.position.x 10) (+ self.position.y 10))) (local (editor-w editor-h) (self:draw-tile-editor (self:tile) x y)) (self:draw-tile-flags (+ x editor-w pixel-size) y) - (self:draw-tile-selector x (+ y editor-h pixel-size) (- self.size.x 20))) + (local top-selector-h (self:draw-tile-selector x (+ y editor-h pixel-size) (- self.size.x 20))) + (self:draw-tile-selector x (+ y editor-h pixel-size top-selector-h pixel-size) (- self.size.x 20) :neut)) (fn TileView.get_name [self] "Tile Editor") diff --git a/game/defs.fnl b/game/defs.fnl index 1081c55..53e199b 100644 --- a/game/defs.fnl +++ b/game/defs.fnl @@ -17,11 +17,11 @@ }) (local org { - :tiles (prg:org 0x4100) - :map (prg:org 0x4a00) - :font (prg:org 0x4b00) - :entity (prg:org 0x4d00) - :levelcode (prg:org 0x4e00) + :tiles (prg:org 0x4000) + :font (prg:org 0x4e00) + :map (prg:org 0x5000) + :entity (prg:org 0x5100) + :levelcode (prg:org 0x5200) :code vm.code }) diff --git a/game/gfx.fnl b/game/gfx.fnl index fa2dd27..f2f8116 100644 --- a/game/gfx.fnl +++ b/game/gfx.fnl @@ -124,15 +124,22 @@ :