More accurate multitile previews

This commit is contained in:
Jeremy Penner 2020-11-22 12:16:48 -05:00
parent ffdef1485a
commit 30045823e1
3 changed files with 97 additions and 74 deletions

View file

@ -41,7 +41,7 @@
(love.graphics.rectangle :line (- tilex 2) (- tiley 2) (+ tilew 4) (+ tileh 4))) (love.graphics.rectangle :line (- tilex 2) (- tiley 2) (+ tilew 4) (+ tileh 4)))
(when (button self [:tile itile] tilex tiley tilew tileh) (when (button self [:tile itile] tilex tiley tilew tileh)
(set self.itile itile)) (set self.itile itile))
(set tilex (+ tilex tilew)) (set tilex (+ tilex tilew 4))
(when (>= (+ tilex tilew) (+ x w)) (when (>= (+ tilex tilew) (+ x w))
(set tilex x) (set tilex x)
(set tiley (+ tiley tileh 4))))) (set tiley (+ tiley tileh 4)))))

View file

@ -1,8 +1,9 @@
(local GraphicsEditView (require :editor.gfxedit)) (local GraphicsEditView (require :editor.gfxedit))
(local style (require :core.style)) (local style (require :core.style))
(local util (require :lib.util)) (local util (require :lib.util))
(local lume (require :lib.lume)) (local lume (require :lib.lume))
(local {: mouse-inside : activate : active? : checkbox : textfield : textbutton} (util.require :editor.imstate)) (local {: mouse-inside : activate : active? : checkbox : textfield : textbutton} (util.require :editor.imstate))
(local {: tilestrip-to-sprite} (util.require :editor.tiledraw))
(local MapEditView (GraphicsEditView:extend)) (local MapEditView (GraphicsEditView:extend))
(local sprite-scale 3) (local sprite-scale 3)
@ -14,6 +15,7 @@
(fn MapEditView.new [self] (fn MapEditView.new [self]
(MapEditView.super.new self) (MapEditView.super.new self)
(set self.sprite-scale sprite-scale) (set self.sprite-scale sprite-scale)
(set self.stripcache {})
(self:reload)) (self:reload))
; map is stored bottom-to-top ; map is stored bottom-to-top
@ -74,38 +76,54 @@
(love.graphics.circle :line xEnd yEnd (/ tilew 5)) (love.graphics.circle :line xEnd yEnd (/ tilew 5))
(love.graphics.setColor 1 1 1))) (love.graphics.setColor 1 1 1)))
(fn MapEditView.draw-tilestrip [self x y my]
; stripcache leaks but honestly who cares
(local tilestrip [])
(var stripid "")
(for [mx 1 mapw]
(local itile (self:itile-from-xy mx my))
(local tile (. self.tilecache.tiles itile :gfx))
(table.insert tilestrip tile)
(set stripid (.. stripid (string.char itile))))
(var sprite (. self.stripcache stripid))
(when (= sprite nil)
(set sprite (tilestrip-to-sprite tilestrip))
(tset self.stripcache stripid sprite))
(love.graphics.draw sprite x y 0 self.sprite-scale self.sprite-scale))
(fn MapEditView.draw-map-editor [self x y] (fn MapEditView.draw-map-editor [self x y]
(activate self :map x y (* tilew mapw) (* tileh maph)) (activate self :map x y (* tilew mapw) (* tileh maph))
(var iobject-over nil) (var iobject-over nil)
(for [mx 1 mapw] (for [my 1 maph] (for [my 1 maph]
(local tilex (+ x (* (- mx 1) tilew)))
(local tiley (+ y (* (- my 1) tileh))) (local tiley (+ y (* (- my 1) tileh)))
(local itile (self:itile-from-xy mx my)) (self:draw-tilestrip x tiley my)
(local iobject (self:iobject-from-xy mx my)) (for [mx 1 mapw]
(self:draw-sprite tilex tiley itile) (local tilex (+ x (* (- mx 1) tilew)))
(when (and (not= iobject nil) (= self.itile nil)) (local itile (self:itile-from-xy mx my))
(love.graphics.setColor 1 0 (if (and (= self.itile nil) (= iobject self.iobject)) 1 0)) (local iobject (self:iobject-from-xy mx my))
(love.graphics.setLineWidth 3) (when (and (not= iobject nil) (= self.itile nil))
(love.graphics.rectangle :line tilex tiley tilew tileh) (love.graphics.setColor 1 0 (if (and (= self.itile nil) (= iobject self.iobject)) 1 0))
(love.graphics.setColor 1 1 1)) (love.graphics.setLineWidth 3)
(when (and (mouse-inside tilex tiley tilew tileh) (not= iobject nil)) (love.graphics.rectangle :line tilex tiley tilew tileh)
(set iobject-over iobject)) (love.graphics.setColor 1 1 1))
(when (and self.itile (active? self :map) (mouse-inside tilex tiley tilew tileh) (not= itile self.itile)) (when (and (mouse-inside tilex tiley tilew tileh) (not= iobject nil))
(self:set-tile mx my self.itile)) (set iobject-over iobject))
(when (and (= self.itile nil) (active? self :map) (mouse-inside tilex tiley tilew tileh)) (when (and self.itile (active? self :map) (mouse-inside tilex tiley tilew tileh) (not= itile self.itile))
(match self.imstate.left (self:set-tile mx my self.itile))
:pressed (set self.iobject-linking iobject) (when (and (= self.itile nil) (active? self :map) (mouse-inside tilex tiley tilew tileh))
:released (match self.imstate.left
(if (and (not= iobject nil) (= self.iobject-linking iobject)) :pressed (set self.iobject-linking iobject)
(set self.iobject iobject) :released
(if (and (not= iobject nil) (= self.iobject-linking iobject))
(set self.iobject iobject)
(not= self.iobject-linking nil) (not= self.iobject-linking nil)
(tset (self:linking-obj) :link iobject) (tset (self:linking-obj) :link iobject)
(= iobject nil) (= iobject nil)
(let [tile (self.tilecache:tile itile)] (let [tile (self.tilecache:tile itile)]
(table.insert self.level.objects {:x mx :y my :func (or tile.word "")}) (table.insert self.level.objects {:x mx :y my :func (or tile.word "")})
(set self.iobject (length self.level.objects)))))))) (set self.iobject (length self.level.objects))))))))
(when (= self.itile nil) (when (= self.itile nil)
(for [iobject 1 (length self.level.objects)] (for [iobject 1 (length self.level.objects)]
(self:draw-link-line x y iobject [0 0 1 0.3])) (self:draw-link-line x y iobject [0 0 1 0.3]))

View file

@ -13,65 +13,69 @@
(fn make-canvas [w h f] (fn make-canvas [w h f]
(local canvas (love.graphics.newCanvas w h)) (local canvas (love.graphics.newCanvas w h))
(local prevcanvas (love.graphics.getCanvas))
(canvas:setFilter :nearest :nearest) (canvas:setFilter :nearest :nearest)
(local scissor [(love.graphics.getScissor)]) (local scissor [(love.graphics.getScissor)])
(love.graphics.setScissor) (love.graphics.setScissor)
(love.graphics.setCanvas canvas) (love.graphics.setCanvas canvas)
(love.graphics.clear 0 0 0) (love.graphics.clear 0 0 0)
(f canvas) (f canvas)
(love.graphics.setCanvas) (love.graphics.setCanvas prevcanvas)
(love.graphics.setScissor (table.unpack scissor)) (love.graphics.setScissor (table.unpack scissor))
(love.graphics.setColor 1 1 1 1) (love.graphics.setColor 1 1 1 1)
canvas) canvas)
(fn tile-to-sprite [tile] (fn draw-byte [bytes ibyte xoffset y ?state ?prevpal]
(make-canvas 14 16 (fn [canvas] (local byte (string.byte (bytes:sub ibyte ibyte)))
(var prevstate nil)
(var state (or ?state :off))
(var pal [(pal-from-byte byte)])
(var prevpal (or ?prevpal pal))
(for [bitx 0 6]
(local x (+ bitx xoffset))
(local bit (not= 0 (bit.band byte (bit.lshift 1 bitx))))
(local prevart (. prevpal (+ 1 (% x 2))))
(local art (. pal (+ 1 (% x 2))))
(set prevstate state)
(set prevpal pal)
(set state
(match [prevstate bit]
[:off false] :off
[:off true] :rising
[:rising false] :falling
[:rising true] :on
[:falling false] :off
[:falling true] :rising
[:on true] :on
[:on false] :falling))
(local white [255 255 255])
(local (prevcolor color)
(match [prevstate state]
[_ :on] (values white white)
[:off :rising] (values nil art)
[:falling :rising] (values prevart art)))
(putpixel (- x 1) y prevcolor)
(putpixel x y color))
(values state pal))
(fn tilestrip-to-sprite [tiles]
(make-canvas (* 14 (length tiles)) 16 (fn [canvas]
(for [y 0 15] (for [y 0 15]
(local byte1 (string.byte (tile:sub (+ y 1) (+ y 1)))) (var state nil)
(local byte2 (string.byte (tile:sub (+ y 17) (+ y 17)))) (var prevpal nil)
(local pal1 [(pal-from-byte byte1)]) (each [itile tile (ipairs tiles)]
(local pal2 [(pal-from-byte byte2)]) (local x (* (- itile 1) 14))
(var prevstate :off) (set (state prevpal) (draw-byte tile (+ y 1) x y state prevpal))
(var state :off) (set (state prevpal) (draw-byte tile (+ y 17) (+ x 7) y state prevpal)))))))
(for [x 0 13]
(local byte (if (< x 7) byte1 byte2)) (fn tile-to-sprite [tile] (tilestrip-to-sprite [tile]))
(local bitx (if (< x 7) x (- x 7)))
(local bit (not= 0 (bit.band byte (bit.lshift 1 bitx))))
(local prevpal (if (< x 8) pal1 pal2))
(local pal (if (< x 7) pal1 pal2))
(local prevart (. prevpal (+ 1 (% x 2))))
(local art (. pal (+ 1 (% x 2))))
(set prevstate state)
(set state
(match [prevstate bit]
[:off false] :off
[:off true] :rising
[:rising false] :falling
[:rising true] :on
[:falling false] :off
[:falling true] :rising
[:on true] :on
[:on false] :falling))
(local white [255 255 255])
(local (prevcolor color)
(match [prevstate state]
[_ :on] (values white white)
[:off :rising] (values nil art)
[:falling :rising] (values prevart art)))
(putpixel (- x 1) y prevcolor)
(putpixel x y color))))))
(fn portrait-to-sprite [gfx] (fn portrait-to-sprite [gfx]
(print "generating portrait") (local top (tilestrip-to-sprite [(gfx:sub 1 32) (gfx:sub 65 96)]))
(local tl (tile-to-sprite (gfx:sub 1 32))) (local bottom (tilestrip-to-sprite [(gfx:sub 33 64) (gfx:sub 97 128)]))
(local bl (tile-to-sprite (gfx:sub 33 64)))
(local tr (tile-to-sprite (gfx:sub 65 96)))
(local br (tile-to-sprite (gfx:sub 97 128)))
(make-canvas 28 32 (fn [canvas] (make-canvas 28 32 (fn [canvas]
(love.graphics.draw tl 0 0) (love.graphics.draw top 0 0)
(love.graphics.draw bl 0 16) (love.graphics.draw bottom 0 16))))
(love.graphics.draw tr 14 0)
(love.graphics.draw br 14 16))))
(fn TileCache [tiles ?spritegen] (fn TileCache [tiles ?spritegen]
{: tiles {: tiles
@ -97,4 +101,5 @@
(tset self.tilesprites itile (self.spritegen (. self.tiles itile :gfx)))) (tset self.tilesprites itile (self.spritegen (. self.tiles itile :gfx))))
(. self.tilesprites itile))}) (. self.tilesprites itile))})
{: tile-to-sprite : portrait-to-sprite : pal-from-bit : pal-from-byte : TileCache} {: tile-to-sprite : tilestrip-to-sprite : portrait-to-sprite : pal-from-bit : pal-from-byte : TileCache}