Code Monkey home page Code Monkey logo

tile's Issues

How to access tile data?

Hi,

this is possibly a dumb question, but I'm not getting it yet :) Let's say I define grid this way:

type Cell struct {
	sprite string
}

grid := tile.NewGridOf[Cell](width, height)

When iterating over the grid, like this:

grid.Each(func(point tile.Point, t tile.Tile[Cell]) {
  // t.Value() returns an int16 which is 0 in all tiles, no Cell ???
})

Of course I can create another map of cells like this:

cells := make(map[tile.Point]Cell, width*height)
grid.Each(func(point tile.Point, t tile.Tile[Cell]) {
  cells[point] = &Cell{"...."}
})

But that way I'd have two grids, the one provided by the tile module and my "shadow" grid holding the cell data (where I'd put things like sprites, properties etc).

What am I doing wrong?

Thanks in advance,
Tom

Function "Within" is slower than iterating with "At"

I noticed that when using the Within on the grid to iterate through values of all tiles it's slower than when iterating by simply using At. Here are benchmarks I quickly wrote:

Benchmark testing the performance of the built-in Within:

func BenchmarkGridWithin(b *testing.B) {
	// Generate map
	theMap := tile.NewGridOf[tileData](900, 900)
	theMap.Each(func(pos point, currentTile tile.Tile[tileData]) {
		currentTile.Write(getRandomSliceIndex(tileImages))
	})

	// Benchmark
	chunkSize := 9
	for i := 0; i < b.N; i++ {
		pos := point{X: int16((i * 9) % 900), Y: int16((i * 45) % 900)}
		theMap.Within(pos, pos.Add(point{X: chunkSize * 3, Y: chunkSize * 3}), func(tilePos point, currentTile tile.Tile[tileData]) {
			value := currentTile.Value()
			_ = tileImages[int(value)]
		})
	}
}

Benchmark using At where whole chunk is scanned with a simple double for loop:

func BenchmarkGridAtScan(b *testing.B) {
	// Generate map
	theMap := tile.NewGridOf[tileData](900, 900)
	theMap.Each(func(pos point, currentTile tile.Tile[tileData]) {
		currentTile.Write(getRandomSliceIndex(tileImages))
	})

	// Benchmark
	chunkSize := 9
	for i := 0; i < b.N; i++ {
		pos := point{X: int16((i * 9) % 900), Y: int16((i * 45) % 900)}
		for x := pos.X; x < pos.X+chunkSize*3; x++ {
			for y := pos.Y; y < pos.Y+chunkSize*3; y++ {
				currentTile, ok := theMap.At(x, y)
				if !ok {
					continue
				}

				value := currentTile.Value()
				_ = tileImages[int(value)]
			}
		}
	}
}

A benchmark that tries to get values using At but working on a page at a time:

func BenchmarkGridAtPage(b *testing.B) {
	// Generate map
	theMap := tile.NewGridOf[tileData](900, 900)
	theMap.Each(func(pos point, currentTile tile.Tile[tileData]) {
		currentTile.Write(getRandomSliceIndex(tileImages))
	})

	// Benchmark
	chunkSize := 9
	for i := 0; i < b.N; i++ {
		pos := point{X: int16((i * 9) % 900), Y: int16((i * 45) % 900)}
                // Iterate through pages
		for x := pos.X; x < pos.X+chunkSize*3; x += 3 {
			for y := pos.Y; y < pos.Y+chunkSize*3; y += 3 {
                                // Get all tiles in a page
				for tileX := x; tileX < x+3; tileX++ {
					for tileY := y; tileY < y+3; tileY++ {
						currentTile, ok := theMap.At(tileX, tileY)
						if !ok {
							continue
						}

						value := currentTile.Value()
						_ = tileImages[int(value)]
					}
				}
			}
		}
	}
}

Results show that At is significantly faster than Within. Also accessing tile's values page-at-a-time increases performance by only โ‰ˆ0.82%:

goos: windows
goarch: amd64
pkg: tile-game
cpu: 12th Gen Intel(R) Core(TM) i9-12900KF
BenchmarkGridWithin-24            531561              2251 ns/op
BenchmarkGridAtScan-24            656180              1826 ns/op
BenchmarkGridAtPage-24            661200              1811 ns/op

I'm not sure if that's intended behavior but something tells me there's an opportunity for adding an optimized values getter.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.