From 73553cb8b0babf88d5b9febac350720a30396a8d Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Tue, 31 Dec 2019 01:38:39 +0000 Subject: [PATCH] Bounds checking --- Makefile | 4 +-- cmd/view-map/main.go | 80 ++++++++++++++++++++++++++++---------------- 2 files changed, 53 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index 2628ded..1a2853e 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 bf94138..52bd222 100644 --- a/cmd/view-map/main.go +++ b/cmd/view-map/main.go @@ -80,7 +80,7 @@ func main() { state := state{ zoom: 1.0, - origin: image.Point{0, -3000}, // FIXME: haxxx + 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++ { @@ -148,20 +148,42 @@ func (e *env) getSprite(palette []string, ref maps.ObjRef) (*conv.Sprite, error) } func (e *env) Draw(screen *ebiten.Image) error { - // TODO: we should be able to perform bounds clipping on these - minX := int(e.gameMap.MinWidth) - maxX := int(e.gameMap.MaxWidth) - minY := int(e.gameMap.MinLength) - maxY := int(e.gameMap.MaxLength) - minZ := 0 - maxZ := int(e.state.zIdx) + 1 + // Bounds clipping + // http://www.java-gaming.org/index.php?topic=24922.0 + // https://stackoverflow.com/questions/892811/drawing-isometric-game-worlds + // https://gamedev.stackexchange.com/questions/25896/how-do-i-find-which-isometric-tiles-are-inside-the-cameras-current-view - for z := minZ; z < maxZ; z++ { - for y := minY; y < maxY; y++ { - for x := minX; x < maxX; x++ { - if err := e.renderCell(x, y, z, screen); err != nil { - return err - } + sw, sh := screen.Size() + + topLeftX, topLeftY := pixToCell( + float64(e.state.origin.X), + float64(e.state.origin.Y), + ) + topLeftX -= 1 // Otherwise we miss half a cell on alternate rows on the left + + bottomRightX, bottomRightY := pixToCell( + float64(e.state.origin.X+sw), + float64(e.state.origin.Y+sh), + ) + + // X+Y is constant for all tiles in a column + // X-Y is constant for all tiles in a row + for a := int(topLeftX + topLeftY); a <= int(bottomRightX+bottomRightY); a++ { + for b := int(topLeftX - topLeftY); b <= int(bottomRightX-bottomRightY); b++ { + if b&1 != a&1 { + continue + } + + x := (a + b) / 2 + y := (a - b) / 2 + + if x < int(e.gameMap.MinWidth) || x >= int(e.gameMap.MaxWidth) || + y < int(e.gameMap.MinLength) || y >= int(e.gameMap.MaxLength) { + continue + } + + for z := 0; z <= e.state.zIdx; z++ { + e.renderCell(x, y, z, screen) } } } @@ -171,7 +193,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,16 +219,17 @@ func (e *env) renderCell(x, y, z int, screen *ebiten.Image) error { sprites = append(sprites, spr) } + iso := ebiten.GeoM{} + iso.Translate(-float64(e.state.origin.X), -float64(e.state.origin.Y)) + + fx, fy := cellToPix(float64(x), float64(y)) + iso.Translate(fx, fy) + // 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.Translate(0.0, -float64(z*48.0)) // offset for Z index - 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 + // TODO: iso.Scale(e.state.zoom, e.state.zoom) // apply current zoom factor for _, sprite := range sprites { if err := screen.DrawImage(sprite.Image, &ebiten.DrawImageOptions{GeoM: iso}); err != nil { @@ -236,9 +258,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