Files
golsky/main.go

110 lines
3.0 KiB
Go
Raw Normal View History

2024-05-20 20:19:11 +02:00
package main
import (
2024-05-21 19:01:08 +02:00
"fmt"
2024-05-20 20:19:11 +02:00
"log"
2024-05-21 19:01:08 +02:00
"os"
2024-05-20 20:19:11 +02:00
2024-05-23 18:25:02 +02:00
"github.com/tlinden/golsky/rle"
2024-05-20 20:19:11 +02:00
"github.com/hajimehoshi/ebiten/v2"
2024-05-21 19:01:08 +02:00
"github.com/spf13/pflag"
)
const (
VERSION = "v0.0.6"
2024-05-21 19:01:08 +02:00
Alive = 1
Dead = 0
2024-05-20 20:19:11 +02:00
)
func GetRLE(filename string) *rle.RLE {
if filename == "" {
return nil
2024-05-22 19:01:58 +02:00
}
content, err := os.ReadFile(filename)
if err != nil {
log.Fatal(err)
2024-05-22 19:01:58 +02:00
}
parsedRle, err := rle.Parse(string(content))
if err != nil {
log.Fatalf("failed to load RLE pattern file: %s", err)
2024-05-20 20:19:11 +02:00
}
return &parsedRle
2024-05-20 20:19:11 +02:00
}
func main() {
2024-05-21 19:01:08 +02:00
game := &Game{}
showversion := false
var rule string
var rlefile string
2024-05-21 19:01:08 +02:00
// commandline params, most configure directly game flags
2024-05-21 19:01:08 +02:00
pflag.IntVarP(&game.Width, "width", "W", 40, "grid width in cells")
pflag.IntVarP(&game.Height, "height", "H", 40, "grid height in cells")
pflag.IntVarP(&game.Cellsize, "cellsize", "c", 8, "cell size in pixels")
pflag.IntVarP(&game.Density, "density", "D", 10, "density of random cells")
pflag.IntVarP(&game.TPG, "ticks-per-generation", "t", 10,
"game speed: the higher the slower (default: 10)")
2024-05-21 19:01:08 +02:00
pflag.StringVarP(&rule, "rule", "r", "B3/S23", "game rule")
pflag.StringVarP(&rlefile, "rle-file", "f", "", "RLE pattern file")
pflag.StringVarP(&game.Statefile, "load-state-file", "l", "", "game state file")
2024-05-21 19:01:08 +02:00
pflag.BoolVarP(&showversion, "version", "v", false, "show version")
pflag.BoolVarP(&game.Paused, "paused", "p", false, "do not start simulation (use space to start)")
2024-05-21 19:01:08 +02:00
pflag.BoolVarP(&game.Debug, "debug", "d", false, "show debug info")
pflag.BoolVarP(&game.NoGrid, "nogrid", "n", false, "do not draw grid lines")
2024-05-21 19:01:08 +02:00
pflag.BoolVarP(&game.Empty, "empty", "e", false, "start with an empty screen")
pflag.BoolVarP(&game.Invert, "invert", "i", false, "invert colors (dead cell: black)")
pflag.BoolVarP(&game.ShowEvolution, "show-evolution", "s", false, "show evolution tracks")
pflag.BoolVarP(&game.Wrap, "wrap-around", "w", false, "wrap around grid mode")
2024-05-21 19:01:08 +02:00
pflag.Parse()
if showversion {
2024-05-23 18:25:02 +02:00
fmt.Printf("This is golsky version %s\n", VERSION)
2024-05-21 19:01:08 +02:00
os.Exit(0)
}
// check if we have been given an RLE file to load
game.RLE = GetRLE(rlefile)
if game.RLE != nil {
if game.RLE.Width > game.Width || game.RLE.Height > game.Height {
game.Width = game.RLE.Width * 2
game.Height = game.RLE.Height * 2
fmt.Printf("rlew: %d, rleh: %d, w: %d, h: %d\n",
game.RLE.Width, game.RLE.Height, game.Width, game.Height)
}
// RLE needs an empty grid
game.Empty = true
// it may come with its own rule
if game.RLE.Rule != "" {
game.Rule = ParseGameRule(game.RLE.Rule)
}
}
// load rule from commandline when no rule came from RLE file,
// default is B3/S23, aka conways game of life
if game.Rule == nil {
game.Rule = ParseGameRule(rule)
}
// bootstrap the game
2024-05-20 20:19:11 +02:00
game.Init()
// setup environment
2024-05-20 20:19:11 +02:00
ebiten.SetWindowSize(game.ScreenWidth, game.ScreenHeight)
ebiten.SetWindowTitle("Game of life")
2024-05-21 19:01:08 +02:00
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
2024-05-20 20:19:11 +02:00
// main loop
2024-05-20 20:19:11 +02:00
if err := ebiten.RunGame(game); err != nil {
log.Fatal(err)
}
}