changed grid data type to bool, save mem and better perf

This commit is contained in:
2024-06-06 18:58:31 +02:00
parent a5dbd69976
commit ab22e0f4e2
5 changed files with 53 additions and 49 deletions

View File

@@ -120,7 +120,7 @@ func removeWhitespace(input string) string {
} }
// Store a grid to an RLE file // Store a grid to an RLE file
func StoreGridToRLE(grid [][]int64, filename, rule string, width, height int) error { func StoreGridToRLE(grid [][]bool, filename, rule string, width, height int) error {
fd, err := os.Create(filename) fd, err := os.Create(filename)
if err != nil { if err != nil {
return err return err
@@ -131,12 +131,12 @@ func StoreGridToRLE(grid [][]int64, filename, rule string, width, height int) er
for y := 0; y < height; y++ { for y := 0; y < height; y++ {
line := "" line := ""
for x := 0; x < width; x++ { for x := 0; x < width; x++ {
switch grid[y][x] { char := "b"
case 0: if grid[y][x] {
line += "b" char = "o"
case 1:
line += "o"
} }
line += char
} }
// if first row is: 001011110, then line is now: // if first row is: 001011110, then line is now:

View File

@@ -42,8 +42,8 @@ type Config struct {
const ( const (
VERSION = "v0.0.8" VERSION = "v0.0.8"
Alive = 1 Alive = true
Dead = 0 Dead = false
DEFAULT_GRID_WIDTH = 600 DEFAULT_GRID_WIDTH = 600
DEFAULT_GRID_HEIGHT = 400 DEFAULT_GRID_HEIGHT = 400

View File

@@ -13,7 +13,7 @@ import (
) )
type Grid struct { type Grid struct {
Data [][]int64 Data [][]bool
Width, Height, Density int Width, Height, Density int
Empty bool Empty bool
} }
@@ -24,12 +24,12 @@ func NewGrid(width, height, density int, empty bool) *Grid {
Height: height, Height: height,
Width: width, Width: width,
Density: density, Density: density,
Data: make([][]int64, height), Data: make([][]bool, height),
Empty: empty, Empty: empty,
} }
for y := 0; y < height; y++ { for y := 0; y < height; y++ {
grid.Data[y] = make([]int64, width) grid.Data[y] = make([]bool, width)
} }
return grid return grid
@@ -59,7 +59,7 @@ func (grid *Grid) Copy(other *Grid) {
func (grid *Grid) Clear() { func (grid *Grid) Clear() {
for y := range grid.Data { for y := range grid.Data {
for x := range grid.Data[y] { for x := range grid.Data[y] {
grid.Data[y][x] = 0 grid.Data[y][x] = false
} }
} }
} }
@@ -70,7 +70,7 @@ func (grid *Grid) FillRandom() {
for y := range grid.Data { for y := range grid.Data {
for x := range grid.Data[y] { for x := range grid.Data[y] {
if rand.Intn(grid.Density) == 1 { if rand.Intn(grid.Density) == 1 {
grid.Data[y][x] = 1 grid.Data[y][x] = true
} }
} }
} }
@@ -80,7 +80,7 @@ func (grid *Grid) FillRandom() {
func (grid *Grid) Dump() { func (grid *Grid) Dump() {
for y := 0; y < grid.Height; y++ { for y := 0; y < grid.Height; y++ {
for x := 0; x < grid.Width; x++ { for x := 0; x < grid.Width; x++ {
if grid.Data[y][x] == 1 { if grid.Data[y][x] {
fmt.Print("XX") fmt.Print("XX")
} else { } else {
fmt.Print(" ") fmt.Print(" ")
@@ -103,7 +103,7 @@ func (grid *Grid) LoadRLE(pattern *rle.RLE) {
x = colIndex + startX x = colIndex + startX
y = rowIndex + startY y = rowIndex + startY
grid.Data[y][x] = 1 grid.Data[y][x] = true
} }
} }
} }
@@ -213,12 +213,9 @@ func (grid *Grid) SaveState(filename, rule string) error {
for y := range grid.Data { for y := range grid.Data {
for _, cell := range grid.Data[y] { for _, cell := range grid.Data[y] {
row := "" row := "."
switch cell { if cell {
case 1: row = "o"
row += "o"
case 0:
row += "."
} }
_, err := file.WriteString(row) _, err := file.WriteString(row)

View File

@@ -5,6 +5,7 @@ import (
"image" "image"
"image/color" "image/color"
"log" "log"
"unsafe"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil" "github.com/hajimehoshi/ebiten/v2/inpututil"
@@ -31,7 +32,7 @@ type ScenePlay struct {
Clear bool Clear bool
Grids []*Grid // 2 grids: one current, one next Grids []*Grid // 2 grids: one current, one next
History *Grid // holds state of past dead cells for evolution traces History [][]int64 // holds state of past dead cells for evolution traces
Index int // points to current grid Index int // points to current grid
Generations int64 // Stats Generations int64 // Stats
Black, White, Grey, Old color.RGBA Black, White, Grey, Old color.RGBA
@@ -85,8 +86,8 @@ func (scene *ScenePlay) SetNext(next SceneName) {
scene.Next = next scene.Next = next
} }
func (scene *ScenePlay) CheckRule(state int64, neighbors int64) int64 { func (scene *ScenePlay) CheckRule(state bool, neighbors int) bool {
var nextstate int64 var nextstate bool
// The standard Scene of Life is symbolized in rule-string notation // The standard Scene of Life is symbolized in rule-string notation
// as B3/S23 (23/3 here). A cell is born if it has exactly three // as B3/S23 (23/3 here). A cell is born if it has exactly three
@@ -94,9 +95,9 @@ func (scene *ScenePlay) CheckRule(state int64, neighbors int64) int64 {
// and dies otherwise. The first number, or list of numbers, is // and dies otherwise. The first number, or list of numbers, is
// what is required for a dead cell to be born. // what is required for a dead cell to be born.
if state == 0 && Contains(scene.Config.Rule.Birth, neighbors) { if !state && Contains(scene.Config.Rule.Birth, neighbors) {
nextstate = Alive nextstate = Alive
} else if state == 1 && Contains(scene.Config.Rule.Death, neighbors) { } else if state && Contains(scene.Config.Rule.Death, neighbors) {
nextstate = Alive nextstate = Alive
} else { } else {
nextstate = Dead nextstate = Dead
@@ -135,7 +136,7 @@ func (scene *ScenePlay) UpdateCells() {
// deduce the color to use if evolution tracing is enabled // deduce the color to use if evolution tracing is enabled
// 60FPS: // 60FPS:
if state != nextstate { if state != nextstate {
scene.History.Data[y][x] = scene.Generations scene.History[y][x] = scene.Generations
} }
// 10FPS: // 10FPS:
@@ -367,10 +368,10 @@ func (scene *ScenePlay) SaveRectRLE() {
height = scene.Mark.Y - scene.Point.Y height = scene.Mark.Y - scene.Point.Y
} }
grid := make([][]int64, height) grid := make([][]bool, height)
for y := 0; y < height; y++ { for y := 0; y < height; y++ {
grid[y] = make([]int64, width) grid[y] = make([]bool, width)
for x := 0; x < width; x++ { for x := 0; x < width; x++ {
grid[y][x] = scene.Grids[scene.Index].Data[y+starty][x+startx] grid[y][x] = scene.Grids[scene.Index].Data[y+starty][x+startx]
@@ -418,7 +419,7 @@ func (scene *ScenePlay) Update() error {
} }
// set a cell to alive or dead // set a cell to alive or dead
func (scene *ScenePlay) ToggleCellOnCursorPos(alive int64) { func (scene *ScenePlay) ToggleCellOnCursorPos(alive bool) {
// use cursor pos relative to the world // use cursor pos relative to the world
worldX, worldY := scene.Camera.ScreenToWorld(ebiten.CursorPosition()) worldX, worldY := scene.Camera.ScreenToWorld(ebiten.CursorPosition())
x := int(worldX) / scene.Config.Cellsize x := int(worldX) / scene.Config.Cellsize
@@ -426,7 +427,7 @@ func (scene *ScenePlay) ToggleCellOnCursorPos(alive int64) {
if x > -1 && y > -1 && x < scene.Config.Width && y < scene.Config.Height { if x > -1 && y > -1 && x < scene.Config.Width && y < scene.Config.Height {
scene.Grids[scene.Index].Data[y][x] = alive scene.Grids[scene.Index].Data[y][x] = alive
scene.History.Data[y][x] = 1 scene.History[y][x] = 1
} }
} }
@@ -451,7 +452,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
) )
//age = scene.Generations - scene.History.Data[y][x] //age = scene.Generations - scene.History.Data[y][x]
age = scene.History.Data[y][x] age = scene.History[y][x]
switch scene.Grids[scene.Index].Data[y][x] { switch scene.Grids[scene.Index].Data[y][x] {
case Alive: case Alive:
@@ -463,7 +464,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
} }
case Dead: case Dead:
// only draw dead cells in case evolution trace is enabled // only draw dead cells in case evolution trace is enabled
if scene.History.Data[y][x] > 1 && scene.Config.ShowEvolution { if scene.History[y][x] > 1 && scene.Config.ShowEvolution {
switch { switch {
case age < 10: case age < 10:
scene.World.DrawImage(scene.Tiles.Age1, op) scene.World.DrawImage(scene.Tiles.Age1, op)
@@ -544,7 +545,6 @@ func (scene *ScenePlay) DrawDebug(screen *ebiten.Image) {
// load a pre-computed pattern from RLE file // load a pre-computed pattern from RLE file
func (scene *ScenePlay) InitPattern() { func (scene *ScenePlay) InitPattern() {
scene.Grids[0].LoadRLE(scene.Config.RLE) scene.Grids[0].LoadRLE(scene.Config.RLE)
scene.History.LoadRLE(scene.Config.RLE)
} }
// pre-render offscreen cache image // pre-render offscreen cache image
@@ -574,18 +574,19 @@ func (scene *ScenePlay) InitCache() {
func (scene *ScenePlay) InitGrid() { func (scene *ScenePlay) InitGrid() {
grida := NewGrid(scene.Config.Width, scene.Config.Height, scene.Config.Density, scene.Config.Empty) grida := NewGrid(scene.Config.Width, scene.Config.Height, scene.Config.Density, scene.Config.Empty)
gridb := NewGrid(scene.Config.Width, scene.Config.Height, scene.Config.Density, scene.Config.Empty) gridb := NewGrid(scene.Config.Width, scene.Config.Height, scene.Config.Density, scene.Config.Empty)
history := NewGrid(scene.Config.Width, scene.Config.Height, scene.Config.Density, scene.Config.Empty)
// startup is delayed until user has selected options // startup is delayed until user has selected options
grida.FillRandom() grida.FillRandom()
grida.Copy(history)
scene.Grids = []*Grid{ scene.Grids = []*Grid{
grida, grida,
gridb, gridb,
} }
scene.History = history scene.History = make([][]int64, scene.Config.Height)
for y := 0; y < scene.Config.Height; y++ {
scene.History[y] = make([]int64, scene.Config.Width)
}
} }
// prepare tile images // prepare tile images
@@ -679,9 +680,15 @@ func (scene *ScenePlay) Init() {
scene.Camera.Setup() scene.Camera.Setup()
} }
func bool2int(b bool) int {
return int(*(*byte)(unsafe.Pointer(&b)))
}
// count the living neighbors of a cell // count the living neighbors of a cell
func (scene *ScenePlay) CountNeighbors(x, y int) int64 { func (scene *ScenePlay) CountNeighbors(x, y int) int {
var sum int64 var sum int
grid := scene.Grids[scene.Index].Data
for nbgX := -1; nbgX < 2; nbgX++ { for nbgX := -1; nbgX < 2; nbgX++ {
for nbgY := -1; nbgY < 2; nbgY++ { for nbgY := -1; nbgY < 2; nbgY++ {
@@ -703,12 +710,12 @@ func (scene *ScenePlay) CountNeighbors(x, y int) int64 {
row = y + nbgY row = y + nbgY
} }
sum += scene.Grids[scene.Index].Data[row][col] sum += bool2int(grid[row][col])
} }
} }
// don't count ourselfes though // don't count ourselfes though
sum -= scene.Grids[scene.Index].Data[y][x] sum -= bool2int(grid[y][x])
return sum return sum
} }

View File

@@ -9,17 +9,17 @@ import (
// a GOL rule // a GOL rule
type Rule struct { type Rule struct {
Definition string Definition string
Birth []int64 Birth []int
Death []int64 Death []int
} }
// parse one part of a GOL rule into rule slice // parse one part of a GOL rule into rule slice
func NumbersToList(numbers string) []int64 { func NumbersToList(numbers string) []int {
list := []int64{} list := []int{}
items := strings.Split(numbers, "") items := strings.Split(numbers, "")
for _, item := range items { for _, item := range items {
num, err := strconv.ParseInt(item, 10, 64) num, err := strconv.Atoi(item)
if err != nil { if err != nil {
log.Fatalf("failed to parse game rule part <%s>: %s", numbers, err) log.Fatalf("failed to parse game rule part <%s>: %s", numbers, err)
} }