From cd2c8e61bed1921543ae0df05ab2a4b78dbd9740 Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Tue, 31 Dec 2019 01:38:39 +0000 Subject: [PATCH] WIP: bounds checking --- Makefile | 4 ++-- cmd/view-map/main.go | 40 ++++++++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 192e289..715f64c 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ -srcfiles = Makefile $(shell find . -iname *.go) +srcfiles = Makefile go.mod $(shell find . -iname *.go) -GOBUILD ?= go build +GOBUILD ?= go build -tags ebitengl all: loader palette-idx view-obj view-map view-menu view-minimap view-set wh40k diff --git a/cmd/view-map/main.go b/cmd/view-map/main.go index 959ae26..3394636 100644 --- a/cmd/view-map/main.go +++ b/cmd/view-map/main.go @@ -79,8 +79,8 @@ func main() { } state := state{ - zoom: 1.0, - origin: image.Point{0, -3000}, // FIXME: haxxx + zoom: 0.5, + origin: image.Point{0, 3000}, // FIXME: haxxx } env := &env{ gameMap: gameMap, @@ -97,10 +97,10 @@ func main() { // TODO: click to view cell data - win.OnKeyUp(ebiten.KeyLeft, env.changeOrigin(+64, +0)) - win.OnKeyUp(ebiten.KeyRight, env.changeOrigin(-64, +0)) - win.OnKeyUp(ebiten.KeyUp, env.changeOrigin(+0, +64)) - win.OnKeyUp(ebiten.KeyDown, env.changeOrigin(+0, -64)) + win.OnKeyUp(ebiten.KeyLeft, env.changeOrigin(-64, +0)) + win.OnKeyUp(ebiten.KeyRight, env.changeOrigin(+64, +0)) + win.OnKeyUp(ebiten.KeyUp, env.changeOrigin(+0, -64)) + win.OnKeyUp(ebiten.KeyDown, env.changeOrigin(+0, +64)) win.OnMouseWheel(env.changeZoom) for i := 0; i <= 6; i++ { @@ -171,7 +171,6 @@ func (e *env) Draw(screen *ebiten.Image) error { func (e *env) renderCell(x, y, z int, screen *ebiten.Image) error { var sprites []*conv.Sprite - cell := e.gameMap.Cells.At(x, y, z) if spr, err := e.getSprite(e.set.Palette, cell.Surface); err != nil { @@ -198,17 +197,26 @@ func (e *env) renderCell(x, y, z int, screen *ebiten.Image) error { sprites = append(sprites, spr) } + iso := ebiten.GeoM{} + iso.Translate(cellToPix(float64(x), float64(y))) + iso.Translate(float64(-e.state.origin.X), float64(-e.state.origin.Y)) // Taking the Z index away *seems* to draw the object in the correct place. // FIXME: There are some artifacts, investigate more - fx, fy := cellToPix(float64(x), float64(y)) - fx += float64(e.state.origin.X) - fy += float64(e.state.origin.Y) - - iso := ebiten.GeoM{} - iso.Translate(fx, fy) iso.Translate(0.0, -float64(z*48.0)) // offset for Z index iso.Scale(e.state.zoom, e.state.zoom) // apply current zoom factor + // Check if we're going to draw far outside the viewport, cancel if so + minX, minY := iso.Apply(cellWidth, cellHeight) + if minX < 0.0 || minY < 0.0 { + return nil + } + + screenW, screenL := screen.Size() + maxX, maxY := iso.Apply(float64(screenW), float64(screenL)) + if int(maxX*e.state.zoom) > screenW || int(maxY*e.state.zoom) > screenL { + return nil + } + for _, sprite := range sprites { if err := screen.DrawImage(sprite.Image, &ebiten.DrawImageOptions{GeoM: iso}); err != nil { return err @@ -236,9 +244,9 @@ func (e *env) setZIdx(to int) func() { } } -var ( - cellWidth = 64.0 - cellHeight = 64.0 +const ( + cellWidth = 64 + cellHeight = 64 ) // Doesn't take the camera or Z level into account