From a72ecf06e041287ee4827e473a30237bbf413714 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Fri, 24 May 2024 17:08:46 +0200 Subject: [PATCH] fixed crash when painting outside canvas, refactor --- game.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/game.go b/game.go index 36b6610..105d195 100644 --- a/game.go +++ b/game.go @@ -149,12 +149,7 @@ func (game *Game) CheckInput() { } if inpututil.IsKeyJustPressed(ebiten.KeyS) { - filename := GetFilename(game.Generations) - err := game.Grids[game.Index].SaveState(filename) - if err != nil { - log.Printf("failed to save game state to %s: %s", filename, err) - } - log.Printf("saved game state to %s at generation %d\n", filename, game.Generations) + game.SaveState() } if game.Paused { @@ -164,6 +159,15 @@ func (game *Game) CheckInput() { } } +func (game *Game) SaveState() { + filename := GetFilename(game.Generations) + err := game.Grids[game.Index].SaveState(filename) + if err != nil { + log.Printf("failed to save game state to %s: %s", filename, err) + } + log.Printf("saved game state to %s at generation %d\n", filename, game.Generations) +} + // Check dragging input. move the canvas with the mouse while pressing // the middle mouse button, zoom in and out using the wheel. func (game *Game) CheckDraggingInput() { @@ -254,8 +258,10 @@ func (game *Game) ToggleCellOnCursorPos(alive int64) { //fmt.Printf("cell at %d,%d\n", x, y) - game.Grids[game.Index].Data[y][x] = alive - game.History.Data[y][x] = 1 + if x > -1 && y > -1 { + game.Grids[game.Index].Data[y][x] = alive + game.History.Data[y][x] = 1 + } } // draw the new grid state