attemt to speed up using writepixels

This commit is contained in:
2024-06-05 17:22:15 +02:00
parent 162d141b34
commit df0f43a607

View File

@@ -48,6 +48,8 @@ type ScenePlay struct {
Mark, Point image.Point // area to marks+save
RunOneStep bool // mutable flags from config
TPG int
Pixels []byte
PixelColor []byte
}
func NewPlayScene(game *Game, config *Config) Scene {
@@ -130,6 +132,7 @@ func (scene *ScenePlay) UpdateCells() {
// change state of current cell in next grid
scene.Grids[next].Data[y][x] = nextstate
if scene.Config.ShowEvolution {
// set history to current generation so we can infer the
// age of the cell's state during rendering and use it to
// deduce the color to use if evolution tracing is enabled
@@ -138,6 +141,7 @@ func (scene *ScenePlay) UpdateCells() {
}
}
}
}
// switch grid for rendering
scene.Index ^= 1
@@ -154,6 +158,75 @@ func (scene *ScenePlay) UpdateCells() {
scene.TicksElapsed = 0
}
// 2: works but still too slow
func (scene *ScenePlay) UpdatePixels() {
var col byte
width := scene.World.Bounds().Dx()
idx := 0
for y := 0; y < scene.Config.Height; y++ {
for x := 0; x < scene.Config.Width; x++ {
col = scene.PixelColor[scene.Grids[scene.Index].Data[y][x]]
ypx := y * scene.Config.Cellsize
for Y := ypx; Y < ypx+scene.Config.Cellsize; Y++ {
xpx := x * scene.Config.Cellsize
for X := xpx; X < xpx+scene.Config.Cellsize; X++ {
idx = 4 * (X + Y*width)
scene.Pixels[idx] = col
scene.Pixels[idx+1] = col
scene.Pixels[idx+2] = col
scene.Pixels[idx+3] = 0xff
idx++
}
}
}
}
scene.Cache.WritePixels(scene.Pixels)
}
// 1: works, but a little slower than 2
func (scene *ScenePlay) _UpdatePixels() {
var col byte
gridx := 0
gridy := 0
idx := 0
width := scene.World.Bounds().Dx()
height := scene.World.Bounds().Dy()
for y := 0; y < height; y++ {
gridy = y / scene.Config.Cellsize // compute once per row
for x := 0; x < width; x++ {
gridx = x / scene.Config.Cellsize
col = scene.PixelColor[scene.Grids[scene.Index].Data[gridy][gridx]]
if scene.Config.ShowGrid {
if x%scene.Config.Cellsize == 0 || y%scene.Config.Cellsize == 0 {
col = scene.PixelColor[2]
}
}
idx = 4 * (x + y*width)
scene.Pixels[idx] = col
scene.Pixels[idx+1] = col
scene.Pixels[idx+2] = col
scene.Pixels[idx+3] = 0xff
idx++
}
}
scene.Cache.WritePixels(scene.Pixels)
}
func (scene *ScenePlay) Reset() {
scene.Config.Paused = true
scene.InitGrid()
@@ -433,9 +506,12 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
// a nice grey grid with grid lines
op := &ebiten.DrawImageOptions{}
scene.UpdatePixels()
op.GeoM.Translate(0, 0)
scene.World.DrawImage(scene.Cache, op)
/*
var age int64
for y := 0; y < scene.Config.Height; y++ {
@@ -473,7 +549,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
}
}
}
*/
scene.DrawMark(scene.World)
scene.Camera.Render(scene.World, screen)
@@ -649,6 +725,8 @@ func (scene *ScenePlay) Init() {
scene.Config.Height*scene.Config.Cellsize,
)
scene.Pixels = make([]byte, scene.World.Bounds().Dx()*scene.World.Bounds().Dy()*scene.Config.Cellsize)
scene.InitTiles()
scene.InitCache()
@@ -672,6 +750,11 @@ func (scene *ScenePlay) Init() {
}
scene.Camera.Setup()
scene.PixelColor = make([]byte, 3)
scene.PixelColor[0] = 0xff
scene.PixelColor[1] = 0x00
scene.PixelColor[2] = 0xff
}
// count the living neighbors of a cell