mirror of
https://codeberg.org/scip/golsky.git
synced 2025-12-18 21:11:04 +01:00
Compare commits
1 Commits
uint8
...
performanc
| Author | SHA1 | Date | |
|---|---|---|---|
| df0f43a607 |
@@ -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 [][]uint8, filename, rule string, width, height int) error {
|
func StoreGridToRLE(grid [][]int64, 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
|
||||||
|
|||||||
10
src/grid.go
10
src/grid.go
@@ -13,7 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Grid struct {
|
type Grid struct {
|
||||||
Data [][]uint8
|
Data [][]int64
|
||||||
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([][]uint8, height),
|
Data: make([][]int64, height),
|
||||||
Empty: empty,
|
Empty: empty,
|
||||||
}
|
}
|
||||||
|
|
||||||
for y := 0; y < height; y++ {
|
for y := 0; y < height; y++ {
|
||||||
grid.Data[y] = make([]uint8, width)
|
grid.Data[y] = make([]int64, width)
|
||||||
}
|
}
|
||||||
|
|
||||||
return grid
|
return grid
|
||||||
@@ -233,12 +233,12 @@ func (grid *Grid) SaveState(filename, rule string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// generate filenames for dumps
|
// generate filenames for dumps
|
||||||
func GetFilename(generations uint64) string {
|
func GetFilename(generations int64) string {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
return fmt.Sprintf("dump-%s-%d.lif", now.Format("20060102150405"), generations)
|
return fmt.Sprintf("dump-%s-%d.lif", now.Format("20060102150405"), generations)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetFilenameRLE(generations uint64) string {
|
func GetFilenameRLE(generations int64) string {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
return fmt.Sprintf("rect-%s-%d.rle", now.Format("20060102150405"), generations)
|
return fmt.Sprintf("rect-%s-%d.rle", now.Format("20060102150405"), generations)
|
||||||
}
|
}
|
||||||
|
|||||||
186
src/play.go
186
src/play.go
@@ -30,10 +30,10 @@ type ScenePlay struct {
|
|||||||
|
|
||||||
Clear bool
|
Clear bool
|
||||||
|
|
||||||
Grids []*Grid // 2 grids: one current, one next
|
Grids []*Grid // 2 grids: one current, one next
|
||||||
History [][]uint64 // holds state of past dead cells for evolution traces
|
History *Grid // holds state of past dead cells for evolution traces
|
||||||
Index int // points to current grid
|
Index int // points to current grid
|
||||||
Generations uint64 // Stats
|
Generations int64 // Stats
|
||||||
Black, White, Grey, Old color.RGBA
|
Black, White, Grey, Old color.RGBA
|
||||||
AgeColor1, AgeColor2, AgeColor3, AgeColor4 color.RGBA
|
AgeColor1, AgeColor2, AgeColor3, AgeColor4 color.RGBA
|
||||||
TicksElapsed int // tick counter for game speed
|
TicksElapsed int // tick counter for game speed
|
||||||
@@ -48,6 +48,8 @@ type ScenePlay struct {
|
|||||||
Mark, Point image.Point // area to marks+save
|
Mark, Point image.Point // area to marks+save
|
||||||
RunOneStep bool // mutable flags from config
|
RunOneStep bool // mutable flags from config
|
||||||
TPG int
|
TPG int
|
||||||
|
Pixels []byte
|
||||||
|
PixelColor []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPlayScene(game *Game, config *Config) Scene {
|
func NewPlayScene(game *Game, config *Config) Scene {
|
||||||
@@ -85,8 +87,8 @@ func (scene *ScenePlay) SetNext(next SceneName) {
|
|||||||
scene.Next = next
|
scene.Next = next
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scene *ScenePlay) CheckRule(state uint8, neighbors uint8) uint8 {
|
func (scene *ScenePlay) CheckRule(state int64, neighbors int64) int64 {
|
||||||
var nextstate uint8
|
var nextstate int64
|
||||||
|
|
||||||
// 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
|
||||||
@@ -130,12 +132,13 @@ func (scene *ScenePlay) UpdateCells() {
|
|||||||
// change state of current cell in next grid
|
// change state of current cell in next grid
|
||||||
scene.Grids[next].Data[y][x] = nextstate
|
scene.Grids[next].Data[y][x] = nextstate
|
||||||
|
|
||||||
// set history to current generation so we can infer the
|
if scene.Config.ShowEvolution {
|
||||||
// age of the cell's state during rendering and use it to
|
// set history to current generation so we can infer the
|
||||||
// deduce the color to use if evolution tracing is enabled
|
// age of the cell's state during rendering and use it to
|
||||||
// FIXME: unbranch somehow
|
// deduce the color to use if evolution tracing is enabled
|
||||||
if state != nextstate {
|
if state != nextstate {
|
||||||
scene.History[y][x] = scene.Generations
|
scene.History.Data[y][x] = scene.Generations
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -155,6 +158,75 @@ func (scene *ScenePlay) UpdateCells() {
|
|||||||
scene.TicksElapsed = 0
|
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() {
|
func (scene *ScenePlay) Reset() {
|
||||||
scene.Config.Paused = true
|
scene.Config.Paused = true
|
||||||
scene.InitGrid()
|
scene.InitGrid()
|
||||||
@@ -364,10 +436,10 @@ func (scene *ScenePlay) SaveRectRLE() {
|
|||||||
height = scene.Mark.Y - scene.Point.Y
|
height = scene.Mark.Y - scene.Point.Y
|
||||||
}
|
}
|
||||||
|
|
||||||
grid := make([][]uint8, height)
|
grid := make([][]int64, height)
|
||||||
|
|
||||||
for y := 0; y < height; y++ {
|
for y := 0; y < height; y++ {
|
||||||
grid[y] = make([]uint8, width)
|
grid[y] = make([]int64, 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]
|
||||||
@@ -415,15 +487,15 @@ func (scene *ScenePlay) Update() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// set a cell to alive or dead
|
// set a cell to alive or dead
|
||||||
func (scene *ScenePlay) ToggleCellOnCursorPos(state uint8) {
|
func (scene *ScenePlay) ToggleCellOnCursorPos(alive int64) {
|
||||||
// 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
|
||||||
y := int(worldY) / scene.Config.Cellsize
|
y := int(worldY) / scene.Config.Cellsize
|
||||||
|
|
||||||
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] = state
|
scene.Grids[scene.Index].Data[y][x] = alive
|
||||||
scene.History[y][x] = 1
|
scene.History.Data[y][x] = 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -434,47 +506,50 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
|
|||||||
// a nice grey grid with grid lines
|
// a nice grey grid with grid lines
|
||||||
op := &ebiten.DrawImageOptions{}
|
op := &ebiten.DrawImageOptions{}
|
||||||
|
|
||||||
|
scene.UpdatePixels()
|
||||||
|
|
||||||
op.GeoM.Translate(0, 0)
|
op.GeoM.Translate(0, 0)
|
||||||
scene.World.DrawImage(scene.Cache, op)
|
scene.World.DrawImage(scene.Cache, op)
|
||||||
|
|
||||||
var age uint64
|
/*
|
||||||
|
var age int64
|
||||||
|
|
||||||
for y := 0; y < scene.Config.Height; y++ {
|
for y := 0; y < scene.Config.Height; y++ {
|
||||||
for x := 0; x < scene.Config.Width; x++ {
|
for x := 0; x < scene.Config.Width; x++ {
|
||||||
op.GeoM.Reset()
|
op.GeoM.Reset()
|
||||||
op.GeoM.Translate(
|
op.GeoM.Translate(
|
||||||
float64(x*scene.Config.Cellsize),
|
float64(x*scene.Config.Cellsize),
|
||||||
float64(y*scene.Config.Cellsize),
|
float64(y*scene.Config.Cellsize),
|
||||||
)
|
)
|
||||||
|
|
||||||
age = scene.Generations - scene.History[y][x]
|
age = scene.Generations - scene.History.Data[y][x]
|
||||||
|
|
||||||
switch scene.Grids[scene.Index].Data[y][x] {
|
switch scene.Grids[scene.Index].Data[y][x] {
|
||||||
case Alive:
|
case Alive:
|
||||||
if age > 50 && scene.Config.ShowEvolution {
|
if age > 50 && scene.Config.ShowEvolution {
|
||||||
scene.World.DrawImage(scene.Tiles.Old, op)
|
scene.World.DrawImage(scene.Tiles.Old, op)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
scene.World.DrawImage(scene.Tiles.Black, op)
|
scene.World.DrawImage(scene.Tiles.Black, op)
|
||||||
}
|
}
|
||||||
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[y][x] > 1 && scene.Config.ShowEvolution {
|
if scene.History.Data[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)
|
||||||
case age < 20:
|
case age < 20:
|
||||||
scene.World.DrawImage(scene.Tiles.Age2, op)
|
scene.World.DrawImage(scene.Tiles.Age2, op)
|
||||||
case age < 30:
|
case age < 30:
|
||||||
scene.World.DrawImage(scene.Tiles.Age3, op)
|
scene.World.DrawImage(scene.Tiles.Age3, op)
|
||||||
default:
|
default:
|
||||||
scene.World.DrawImage(scene.Tiles.Age4, op)
|
scene.World.DrawImage(scene.Tiles.Age4, op)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
*/
|
||||||
|
|
||||||
scene.DrawMark(scene.World)
|
scene.DrawMark(scene.World)
|
||||||
|
|
||||||
scene.Camera.Render(scene.World, screen)
|
scene.Camera.Render(scene.World, screen)
|
||||||
@@ -540,6 +615,7 @@ 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
|
||||||
@@ -580,10 +656,7 @@ func (scene *ScenePlay) InitGrid() {
|
|||||||
gridb,
|
gridb,
|
||||||
}
|
}
|
||||||
|
|
||||||
scene.History = make([][]uint64, scene.Config.Height)
|
scene.History = history
|
||||||
for y := 0; y < scene.Config.Height; y++ {
|
|
||||||
scene.History[y] = make([]uint64, scene.Config.Width)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepare tile images
|
// prepare tile images
|
||||||
@@ -652,6 +725,8 @@ func (scene *ScenePlay) Init() {
|
|||||||
scene.Config.Height*scene.Config.Cellsize,
|
scene.Config.Height*scene.Config.Cellsize,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
scene.Pixels = make([]byte, scene.World.Bounds().Dx()*scene.World.Bounds().Dy()*scene.Config.Cellsize)
|
||||||
|
|
||||||
scene.InitTiles()
|
scene.InitTiles()
|
||||||
scene.InitCache()
|
scene.InitCache()
|
||||||
|
|
||||||
@@ -675,11 +750,16 @@ func (scene *ScenePlay) Init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
scene.Camera.Setup()
|
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
|
// count the living neighbors of a cell
|
||||||
func (scene *ScenePlay) CountNeighbors(x, y int) uint8 {
|
func (scene *ScenePlay) CountNeighbors(x, y int) int64 {
|
||||||
var sum uint8
|
var sum int64
|
||||||
|
|
||||||
for nbgX := -1; nbgX < 2; nbgX++ {
|
for nbgX := -1; nbgX < 2; nbgX++ {
|
||||||
for nbgY := -1; nbgY < 2; nbgY++ {
|
for nbgY := -1; nbgY < 2; nbgY++ {
|
||||||
|
|||||||
12
src/rule.go
12
src/rule.go
@@ -9,22 +9,22 @@ import (
|
|||||||
// a GOL rule
|
// a GOL rule
|
||||||
type Rule struct {
|
type Rule struct {
|
||||||
Definition string
|
Definition string
|
||||||
Birth []uint8
|
Birth []int64
|
||||||
Death []uint8
|
Death []int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse one part of a GOL rule into rule slice
|
// parse one part of a GOL rule into rule slice
|
||||||
func NumbersToList(numbers string) []uint8 {
|
func NumbersToList(numbers string) []int64 {
|
||||||
list := []uint8{}
|
list := []int64{}
|
||||||
|
|
||||||
items := strings.Split(numbers, "")
|
items := strings.Split(numbers, "")
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
num, err := strconv.ParseInt(item, 10, 8)
|
num, err := strconv.ParseInt(item, 10, 64)
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
list = append(list, uint8(num))
|
list = append(list, num)
|
||||||
}
|
}
|
||||||
|
|
||||||
return list
|
return list
|
||||||
|
|||||||
@@ -238,14 +238,20 @@ func (game *Game) Draw(screen *ebiten.Image) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// state := 1
|
//x := 1
|
||||||
// nextstate := 0
|
//y := 0
|
||||||
v := 0
|
col := 1 >> 0xff
|
||||||
|
|
||||||
for i := 0; i < 600; i++ {
|
fmt.Printf("col: %d\n", col)
|
||||||
v = ((v + 1) & 255)
|
|
||||||
fmt.Println(i, i&255)
|
x := 1
|
||||||
}
|
y := 2
|
||||||
|
c := 4
|
||||||
|
|
||||||
|
xm := x & (c - 1)
|
||||||
|
ym := y & (c - 1)
|
||||||
|
|
||||||
|
fmt.Println(xm & ym)
|
||||||
}
|
}
|
||||||
|
|
||||||
func _main() {
|
func _main() {
|
||||||
|
|||||||
Reference in New Issue
Block a user