mirror of
https://codeberg.org/scip/golsky.git
synced 2025-12-17 12:40:56 +01:00
Compare commits
1 Commits
menu2subsc
...
shader
| Author | SHA1 | Date | |
|---|---|---|---|
| 329d213325 |
3
Makefile
3
Makefile
@@ -23,13 +23,12 @@ PREFIX = /usr/local
|
||||
UID = root
|
||||
GID = 0
|
||||
HAVE_POD := $(shell pod2text -h 2>/dev/null)
|
||||
#TAGS = -tags=ebitenginedebug
|
||||
|
||||
all: buildlocal
|
||||
|
||||
|
||||
buildlocal:
|
||||
go build $(TAGS) -o $(tool)
|
||||
go build -o $(tool)
|
||||
|
||||
install: buildlocal
|
||||
install -d -o $(UID) -g $(GID) $(PREFIX)/bin
|
||||
|
||||
@@ -18,7 +18,7 @@ Based on: https://youtu.be/FWSR_7kZuYg?si=ix1dmo76D8AmF25F
|
||||
|
||||
* flexible parameters as grid and cell size
|
||||
* colors can be inverted
|
||||
* evolution traces can be shown, with age the cells color fades and
|
||||
* evolution tracks can be shown, with age the cells color fades and
|
||||
old life cells will be drawn in red
|
||||
* game grid lines can be enabled or disabled
|
||||
* game speed can be adjusted on startup and in-game
|
||||
@@ -61,7 +61,7 @@ Usage of ./golsky:
|
||||
-p, --paused do not start simulation (use space to start)
|
||||
-f, --rle-file string RLE pattern file
|
||||
-r, --rule string game rule (default "B3/S23")
|
||||
-s, --show-evolution show evolution traces
|
||||
-s, --show-evolution show evolution tracks
|
||||
-t, --ticks-per-generation int game speed: the higher the slower (default: 10) (default 10)
|
||||
-v, --version show version
|
||||
-W, --width int grid width in cells (default 40)
|
||||
@@ -77,12 +77,10 @@ While it runs, there are a couple of commands you can use:
|
||||
* page down: slow down
|
||||
* Mouse wheel: zoom in or out
|
||||
* move mouse while middle mouse button pressed: move canvas
|
||||
* r: reset to 1:1 zoom
|
||||
* escape: open menu
|
||||
* escape: reset to 1:1 zoom
|
||||
* s: save game state to file (can be loaded with -l)
|
||||
* c: enter copy mode. Mark a rectangle with the mouse, when you
|
||||
release the mous button it is being saved to an RLE file
|
||||
* d: toggle debug output
|
||||
* q: quit
|
||||
|
||||
# Report bugs
|
||||
|
||||
18
TODO.md
18
TODO.md
@@ -1,17 +1 @@
|
||||
- add all other options like size etc
|
||||
|
||||
- changing options mid-game has no effect in most cases, even after a restart
|
||||
|
||||
- Statefile loading does not work correclty anymore. With larger grids
|
||||
everything is empty. With square grids part of the grid is cut
|
||||
off. Smaller grids load though
|
||||
|
||||
- Also when loading a state file, centering doesn't work anymore, I
|
||||
think the geom calculation is overthrown by the parser func. So, put
|
||||
this calc into its own func and always call. Or - as stated below -
|
||||
put it onto camera.go and call from Init().
|
||||
|
||||
- Zoom 0 on reset only works when world<screen. otherwise zoom would
|
||||
be negative So, on Init() memoize centered camera position or add a
|
||||
Center() function to camera.go. Then on reset calculate the zoom
|
||||
level so that the world fits into the screen.
|
||||
- Implement RLE writing on scene.MarkDone
|
||||
|
||||
Binary file not shown.
@@ -1,13 +0,0 @@
|
||||
//kage:unit pixels
|
||||
|
||||
package main
|
||||
|
||||
var Alife int
|
||||
|
||||
func Fragment(_ vec4, pos vec2, _ vec4) vec4 {
|
||||
if Alife == 1 {
|
||||
return vec4(0.0)
|
||||
}
|
||||
|
||||
return vec4(1.0)
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 269 B |
Binary file not shown.
|
Before Width: | Height: | Size: 263 B |
Binary file not shown.
|
Before Width: | Height: | Size: 269 B |
Binary file not shown.
|
Before Width: | Height: | Size: 289 B |
Binary file not shown.
|
Before Width: | Height: | Size: 271 B |
Binary file not shown.
Binary file not shown.
92
config.go
92
config.go
@@ -3,12 +3,12 @@ package main
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"runtime/pprof"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/alecthomas/repr"
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/tlinden/golsky/rle"
|
||||
)
|
||||
@@ -18,20 +18,14 @@ type Config struct {
|
||||
Width, Height, Cellsize, Density int // measurements
|
||||
ScreenWidth, ScreenHeight int
|
||||
TPG int // ticks per generation/game speed, 1==max
|
||||
Debug, Empty, Invert, Paused, Markmode bool // game modi
|
||||
ShowEvolution, ShowGrid, RunOneStep bool // flags
|
||||
Debug, Empty, Invert, Paused bool // game modi
|
||||
ShowEvolution, NoGrid, RunOneStep bool // flags
|
||||
Rule *Rule // which rule to use, default: B3/S23
|
||||
RLE *rle.RLE // loaded GOL pattern from RLE file
|
||||
Statefile string // load game state from it if non-nil
|
||||
StateGrid *Grid // a grid from a statefile
|
||||
Wrap bool // wether wraparound mode is in place or not
|
||||
ShowVersion bool
|
||||
UseShader bool // to use a shader to render alife cells
|
||||
Restart, RestartGrid, RestartCache bool
|
||||
StartWithMenu bool
|
||||
Zoomfactor int
|
||||
InitialCamPos []float64
|
||||
DelayedStart bool // if true game, we wait. like pause but program induced
|
||||
|
||||
// for internal profiling
|
||||
ProfileFile string
|
||||
@@ -40,19 +34,19 @@ type Config struct {
|
||||
}
|
||||
|
||||
const (
|
||||
VERSION = "v0.0.8"
|
||||
VERSION = "v0.0.7"
|
||||
Alive = 1
|
||||
Dead = 0
|
||||
|
||||
DEFAULT_GRID_WIDTH = 600
|
||||
DEFAULT_GRID_HEIGHT = 400
|
||||
DEFAULT_CELLSIZE = 4
|
||||
DEFAULT_ZOOMFACTOR = 150
|
||||
DEFAULT_GEOM = "640x384"
|
||||
)
|
||||
|
||||
// parse given window geometry and adjust game settings according to it
|
||||
func (config *Config) ParseGeom(geom string) error {
|
||||
if geom == "" {
|
||||
config.ScreenWidth = config.Cellsize * config.Width
|
||||
config.ScreenHeight = config.Cellsize * config.Height
|
||||
return nil
|
||||
}
|
||||
|
||||
// force a geom
|
||||
geometry := strings.Split(geom, "x")
|
||||
if len(geometry) != 2 {
|
||||
@@ -69,27 +63,18 @@ func (config *Config) ParseGeom(geom string) error {
|
||||
return errors.New("failed to parse height, expecting integer")
|
||||
}
|
||||
|
||||
config.ScreenWidth = width
|
||||
config.ScreenHeight = height
|
||||
// adjust dimensions, account for grid width+height so that cells
|
||||
// fit into window
|
||||
config.ScreenWidth = width - (width % config.Width)
|
||||
config.ScreenHeight = height - (height % config.Height)
|
||||
|
||||
config.Cellsize = DEFAULT_CELLSIZE
|
||||
config.Zoomfactor = DEFAULT_ZOOMFACTOR
|
||||
|
||||
// calculate the initial cam pos. It is negative if the total grid
|
||||
// size is smaller than the screen in a centered position, but
|
||||
// it's zero if it's equal or larger than the screen.
|
||||
config.InitialCamPos = make([]float64, 2)
|
||||
|
||||
config.InitialCamPos[0] = float64(((config.ScreenWidth - (config.Width * config.Cellsize)) / 2) * -1)
|
||||
if config.Width*config.Cellsize >= config.ScreenWidth {
|
||||
// must be positive if world wider than screen
|
||||
config.InitialCamPos[0] = math.Abs(config.InitialCamPos[0])
|
||||
if config.ScreenWidth == 0 || config.ScreenHeight == 0 {
|
||||
return errors.New("the number of requested cells don't fit into the requested window size")
|
||||
}
|
||||
|
||||
if config.Height*config.Cellsize > config.ScreenHeight {
|
||||
config.InitialCamPos[1] = math.Abs(float64(((config.ScreenHeight - (config.Height * config.Cellsize)) / 2)))
|
||||
}
|
||||
config.Cellsize = config.ScreenWidth / config.Width
|
||||
|
||||
repr.Println(config)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -118,9 +103,6 @@ func (config *Config) ParseRLE(rlefile string) error {
|
||||
config.Cellsize = config.ScreenWidth / config.Width
|
||||
}
|
||||
|
||||
fmt.Printf("width: %d, screenwidth: %d, rlewidth: %d, cellsize: %d\n",
|
||||
config.Width, config.ScreenWidth, config.RLE.Width, config.Cellsize)
|
||||
|
||||
// RLE needs an empty grid
|
||||
config.Empty = true
|
||||
|
||||
@@ -133,7 +115,7 @@ func (config *Config) ParseRLE(rlefile string) error {
|
||||
}
|
||||
|
||||
// parse a state file, if given, and adjust game settings accordingly
|
||||
func (config *Config) ParseStatefile() error {
|
||||
func (config *Config) ParseStatefile(statefile string) error {
|
||||
if config.Statefile == "" {
|
||||
return nil
|
||||
}
|
||||
@@ -175,10 +157,10 @@ func ParseCommandline() (*Config, error) {
|
||||
)
|
||||
|
||||
// commandline params, most configure directly config flags
|
||||
pflag.IntVarP(&config.Width, "width", "W", DEFAULT_GRID_WIDTH, "grid width in cells")
|
||||
pflag.IntVarP(&config.Height, "height", "H", DEFAULT_GRID_HEIGHT, "grid height in cells")
|
||||
pflag.IntVarP(&config.Width, "width", "W", 40, "grid width in cells")
|
||||
pflag.IntVarP(&config.Height, "height", "H", 40, "grid height in cells")
|
||||
pflag.IntVarP(&config.Cellsize, "cellsize", "c", 8, "cell size in pixels")
|
||||
pflag.StringVarP(&geom, "geom", "G", DEFAULT_GEOM, "window geometry in WxH in pixels, overturns -c")
|
||||
pflag.StringVarP(&geom, "geom", "g", "", "window geometry in WxH in pixels, overturns -c")
|
||||
|
||||
pflag.IntVarP(&config.Density, "density", "D", 10, "density of random cells")
|
||||
pflag.IntVarP(&config.TPG, "ticks-per-generation", "t", 10,
|
||||
@@ -191,12 +173,11 @@ func ParseCommandline() (*Config, error) {
|
||||
pflag.BoolVarP(&config.ShowVersion, "version", "v", false, "show version")
|
||||
pflag.BoolVarP(&config.Paused, "paused", "p", false, "do not start simulation (use space to start)")
|
||||
pflag.BoolVarP(&config.Debug, "debug", "d", false, "show debug info")
|
||||
pflag.BoolVarP(&config.ShowGrid, "show-grid", "g", false, "draw grid lines")
|
||||
pflag.BoolVarP(&config.NoGrid, "nogrid", "n", false, "do not draw grid lines")
|
||||
pflag.BoolVarP(&config.Empty, "empty", "e", false, "start with an empty screen")
|
||||
pflag.BoolVarP(&config.Invert, "invert", "i", false, "invert colors (dead cell: black)")
|
||||
pflag.BoolVarP(&config.ShowEvolution, "show-evolution", "s", false, "show evolution traces")
|
||||
pflag.BoolVarP(&config.ShowEvolution, "show-evolution", "s", false, "show evolution tracks")
|
||||
pflag.BoolVarP(&config.Wrap, "wrap-around", "w", false, "wrap around grid mode")
|
||||
pflag.BoolVarP(&config.UseShader, "use-shader", "k", false, "use shader for cell rendering")
|
||||
|
||||
pflag.StringVarP(&config.ProfileFile, "profile-file", "", "", "enable profiling")
|
||||
pflag.BoolVarP(&config.ProfileDraw, "profile-draw", "", false, "profile draw method (default false)")
|
||||
@@ -214,36 +195,11 @@ func ParseCommandline() (*Config, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = config.ParseStatefile()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// load rule from commandline when no rule came from RLE file,
|
||||
// default is B3/S23, aka conways game of life
|
||||
if config.Rule == nil {
|
||||
config.Rule = ParseGameRule(rule)
|
||||
}
|
||||
|
||||
//repr.Println(config)
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
func (config *Config) TogglePaused() {
|
||||
config.Paused = !config.Paused
|
||||
}
|
||||
|
||||
func (config *Config) ToggleDebugging() {
|
||||
fmt.Println("DEBUG TOGGLED")
|
||||
config.Debug = !config.Debug
|
||||
}
|
||||
|
||||
func (config *Config) ToggleInvert() {
|
||||
config.Invert = !config.Invert
|
||||
config.RestartCache = true
|
||||
}
|
||||
|
||||
func (config *Config) ToggleGridlines() {
|
||||
config.ShowGrid = !config.ShowGrid
|
||||
config.RestartCache = true
|
||||
}
|
||||
|
||||
42
game.go
42
game.go
@@ -1,39 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
import "github.com/hajimehoshi/ebiten/v2"
|
||||
|
||||
type Game struct {
|
||||
ScreenWidth, ScreenHeight, ReadlWidth, Cellsize int
|
||||
ScreenWidth, ScreenHeight, Cellsize int
|
||||
Scenes map[SceneName]Scene
|
||||
CurrentScene SceneName
|
||||
Config *Config
|
||||
Scale float32
|
||||
Screen *ebiten.Image
|
||||
Shader *ebiten.Shader
|
||||
}
|
||||
|
||||
func NewGame(config *Config, startscene SceneName) *Game {
|
||||
func NewGame(config *Config, shader *ebiten.Shader, startscene SceneName) *Game {
|
||||
game := &Game{
|
||||
Config: config,
|
||||
Scenes: map[SceneName]Scene{},
|
||||
ScreenWidth: config.ScreenWidth,
|
||||
ScreenHeight: config.ScreenHeight,
|
||||
Shader: shader,
|
||||
}
|
||||
|
||||
// setup scene[s]
|
||||
game.CurrentScene = startscene
|
||||
game.Scenes[Play] = NewPlayScene(game, config)
|
||||
game.Scenes[Menu] = NewMenuScene(game, config)
|
||||
game.Scenes[Options] = NewOptionsScene(game, config)
|
||||
|
||||
// setup environment
|
||||
ebiten.SetWindowSize(game.ScreenWidth, game.ScreenHeight)
|
||||
ebiten.SetWindowTitle("golsky - conway's game of life")
|
||||
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
|
||||
ebiten.SetScreenClearedEveryFrame(true)
|
||||
|
||||
game.Screen = ebiten.NewImage(game.ScreenWidth, game.ScreenHeight)
|
||||
return game
|
||||
}
|
||||
|
||||
@@ -42,8 +36,6 @@ func (game *Game) GetCurrentScene() Scene {
|
||||
}
|
||||
|
||||
func (game *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||
game.ReadlWidth = outsideWidth
|
||||
game.Scale = float32(game.ScreenWidth) / float32(outsideWidth)
|
||||
return game.ScreenWidth, game.ScreenHeight
|
||||
}
|
||||
|
||||
@@ -52,8 +44,12 @@ func (game *Game) Update() error {
|
||||
scene.Update()
|
||||
|
||||
next := scene.GetNext()
|
||||
|
||||
if next != game.CurrentScene {
|
||||
// make sure we stay on the selected scene
|
||||
scene.ResetNext()
|
||||
|
||||
// finally switch
|
||||
game.CurrentScene = next
|
||||
}
|
||||
|
||||
@@ -61,19 +57,13 @@ func (game *Game) Update() error {
|
||||
}
|
||||
|
||||
func (game *Game) Draw(screen *ebiten.Image) {
|
||||
// first draw primary scene[s], although there are only 1
|
||||
for current, scene := range game.Scenes {
|
||||
if scene.IsPrimary() {
|
||||
// primary scenes always draw
|
||||
scene.Draw(screen)
|
||||
|
||||
if current == game.CurrentScene {
|
||||
// avoid to redraw it in the next step
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scene := game.GetCurrentScene()
|
||||
|
||||
if scene.Clearscreen() {
|
||||
ebiten.SetScreenClearedEveryFrame(true)
|
||||
} else {
|
||||
ebiten.SetScreenClearedEveryFrame(false)
|
||||
}
|
||||
|
||||
scene.Draw(screen)
|
||||
}
|
||||
|
||||
5
go.mod
5
go.mod
@@ -13,12 +13,7 @@ require (
|
||||
github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895 // indirect
|
||||
github.com/ebitengine/hideconsole v1.0.0 // indirect
|
||||
github.com/ebitengine/purego v0.7.0 // indirect
|
||||
github.com/ebitenui/ebitenui v0.5.6 // indirect
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
||||
github.com/jezek/xgb v1.1.1 // indirect
|
||||
github.com/tinne26/etxt v0.0.8 // indirect
|
||||
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
|
||||
golang.org/x/sync v0.7.0 // indirect
|
||||
golang.org/x/sys v0.20.0 // indirect
|
||||
golang.org/x/text v0.15.0 // indirect
|
||||
)
|
||||
|
||||
12
go.sum
12
go.sum
@@ -6,27 +6,15 @@ github.com/ebitengine/hideconsole v1.0.0 h1:5J4U0kXF+pv/DhiXt5/lTz0eO5ogJ1iXb8Yj
|
||||
github.com/ebitengine/hideconsole v1.0.0/go.mod h1:hTTBTvVYWKBuxPr7peweneWdkUwEuHuB3C1R/ielR1A=
|
||||
github.com/ebitengine/purego v0.7.0 h1:HPZpl61edMGCEW6XK2nsR6+7AnJ3unUxpTZBkkIXnMc=
|
||||
github.com/ebitengine/purego v0.7.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
|
||||
github.com/ebitenui/ebitenui v0.5.6 h1:qyJRU5j+lQo1lamxB48IBwMxMfz1xNb5iWUayCtA0Wk=
|
||||
github.com/ebitenui/ebitenui v0.5.6/go.mod h1:I0rVbTOUi7gWKTPet2gzbvhOdkHp5pJXMM6c6b3dRoE=
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
|
||||
github.com/hajimehoshi/ebiten/v2 v2.7.4 h1:X+heODRQ3Ie9F9QFjm24gEZqQd5FSfR9XuT2XfHwgf8=
|
||||
github.com/hajimehoshi/ebiten/v2 v2.7.4/go.mod h1:H2pHVgq29rfm5yeQ7jzWOM3VHsjo7/AyucODNLOhsVY=
|
||||
github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4=
|
||||
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/tinne26/etxt v0.0.8 h1:rjb58jkMkapRGLmhBMWnT76E/nMTXC5P1Q956BRZkoc=
|
||||
github.com/tinne26/etxt v0.0.8/go.mod h1:QM/hlNkstsKC39elTFNKAR34xsMb9QoVosf+g9wlYxM=
|
||||
github.com/tinne26/etxt v0.0.9-alpha.6.0.20240409152929-91bfc562becc h1:+USGSXbkrRAy6bz3Qm4GUczhqeXe7XlRfkRexCSFxkw=
|
||||
github.com/tinne26/etxt v0.0.9-alpha.6.0.20240409152929-91bfc562becc/go.mod h1:Icbd4bDjrXag1oYIhB51CrkMYqRb7YMv0AsrOSfNKfU=
|
||||
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ=
|
||||
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
|
||||
golang.org/x/image v0.16.0 h1:9kloLAKhUufZhA12l5fwnx2NZW39/we1UhBesW433jw=
|
||||
golang.org/x/image v0.16.0/go.mod h1:ugSZItdV4nOxyqp56HmXwH0Ry0nBCpjnZdpDaIHdoPs=
|
||||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
|
||||
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
|
||||
72
grid.go
72
grid.go
@@ -9,8 +9,6 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tlinden/golsky/rle"
|
||||
)
|
||||
|
||||
type Grid struct {
|
||||
@@ -36,7 +34,6 @@ func NewGrid(width, height, density int, empty bool) *Grid {
|
||||
return grid
|
||||
}
|
||||
|
||||
// Create a new 1:1 instance
|
||||
func (grid *Grid) Clone() *Grid {
|
||||
newgrid := &Grid{}
|
||||
|
||||
@@ -47,16 +44,6 @@ func (grid *Grid) Clone() *Grid {
|
||||
return newgrid
|
||||
}
|
||||
|
||||
// copy data
|
||||
func (grid *Grid) Copy(other *Grid) {
|
||||
for y := range grid.Data {
|
||||
for x := range grid.Data[y] {
|
||||
other.Data[y][x] = grid.Data[y][x]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// delete all contents
|
||||
func (grid *Grid) Clear() {
|
||||
for y := range grid.Data {
|
||||
for x := range grid.Data[y] {
|
||||
@@ -65,8 +52,7 @@ func (grid *Grid) Clear() {
|
||||
}
|
||||
}
|
||||
|
||||
// initialize with random life cells using the given density
|
||||
func (grid *Grid) FillRandom() {
|
||||
func (grid *Grid) FillRandom(game *ScenePlay) {
|
||||
if !grid.Empty {
|
||||
for y := range grid.Data {
|
||||
for x := range grid.Data[y] {
|
||||
@@ -78,43 +64,16 @@ func (grid *Grid) FillRandom() {
|
||||
}
|
||||
}
|
||||
|
||||
func (grid *Grid) Dump() {
|
||||
for y := 0; y < grid.Height; y++ {
|
||||
for x := 0; x < grid.Width; x++ {
|
||||
if grid.Data[y][x] == 1 {
|
||||
fmt.Print("XX")
|
||||
} else {
|
||||
fmt.Print(" ")
|
||||
}
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
func GetFilename(generations int64) string {
|
||||
now := time.Now()
|
||||
return fmt.Sprintf("dump-%s-%d.gol", now.Format("20060102150405"), generations)
|
||||
}
|
||||
|
||||
// initialize using a given RLE pattern
|
||||
func (grid *Grid) LoadRLE(pattern *rle.RLE) {
|
||||
if pattern != nil {
|
||||
startX := (grid.Width / 2) - (pattern.Width / 2)
|
||||
startY := (grid.Height / 2) - (pattern.Height / 2)
|
||||
var y, x int
|
||||
|
||||
for rowIndex, patternRow := range pattern.Pattern {
|
||||
for colIndex := range patternRow {
|
||||
if pattern.Pattern[rowIndex][colIndex] > 0 {
|
||||
x = colIndex + startX
|
||||
y = rowIndex + startY
|
||||
|
||||
grid.Data[y][x] = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//grid.Dump()
|
||||
}
|
||||
func GetFilenameRLE(generations int64) string {
|
||||
now := time.Now()
|
||||
return fmt.Sprintf("rect-%s-%d.rle", now.Format("20060102150405"), generations)
|
||||
}
|
||||
|
||||
// save the contents of the whole grid as a simple mcell alike
|
||||
// file. One line per row, 0 for dead and 1 for life cell.
|
||||
func (grid *Grid) SaveState(filename string) error {
|
||||
file, err := os.Create(filename)
|
||||
if err != nil {
|
||||
@@ -122,7 +81,7 @@ func (grid *Grid) SaveState(filename string) error {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
for y := range grid.Data {
|
||||
for y, _ := range grid.Data {
|
||||
for _, cell := range grid.Data[y] {
|
||||
_, err := file.WriteString(strconv.FormatInt(cell, 10))
|
||||
if err != nil {
|
||||
@@ -135,7 +94,6 @@ func (grid *Grid) SaveState(filename string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// the reverse of the above, load a mcell file
|
||||
func LoadState(filename string) (*Grid, error) {
|
||||
fd, err := os.Open(filename)
|
||||
if err != nil {
|
||||
@@ -181,8 +139,7 @@ func LoadState(filename string) (*Grid, error) {
|
||||
}
|
||||
|
||||
if explen != length {
|
||||
return nil, fmt.Errorf(
|
||||
fmt.Sprintf("all rows must be in the same length, got: %d, expected: %d",
|
||||
return nil, fmt.Errorf(fmt.Sprintf("all rows must be in the same length, got: %d, expected: %d",
|
||||
length, explen))
|
||||
}
|
||||
|
||||
@@ -194,14 +151,3 @@ func LoadState(filename string) (*Grid, error) {
|
||||
|
||||
return grid, nil
|
||||
}
|
||||
|
||||
// generate filenames for dumps
|
||||
func GetFilename(generations int64) string {
|
||||
now := time.Now()
|
||||
return fmt.Sprintf("dump-%s-%d.gol", now.Format("20060102150405"), generations)
|
||||
}
|
||||
|
||||
func GetFilenameRLE(generations int64) string {
|
||||
now := time.Now()
|
||||
return fmt.Sprintf("rect-%s-%d.rle", now.Format("20060102150405"), generations)
|
||||
}
|
||||
|
||||
113
loader-fonts.go
113
loader-fonts.go
@@ -1,113 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/golang/freetype/truetype"
|
||||
"github.com/tinne26/etxt"
|
||||
"golang.org/x/image/font"
|
||||
)
|
||||
|
||||
var FontRenderer = LoadFonts("assets/fonts")
|
||||
|
||||
const (
|
||||
GameFont string = "NotoSans-Regular"
|
||||
GameFontETXT string = "Noto Sans"
|
||||
FontSizeBig int = 48
|
||||
FontSizeNormal int = 24
|
||||
FontSizeSmall int = 12
|
||||
)
|
||||
|
||||
type Texter struct {
|
||||
Renderer *etxt.Renderer
|
||||
FontNormal *font.Face
|
||||
FontBig *font.Face
|
||||
FontSmall *font.Face
|
||||
}
|
||||
|
||||
func LoadFonts(dir string) Texter {
|
||||
// load the font for use with ebitenui
|
||||
fontbytes, err := assetfs.ReadFile(dir + "/" + GameFont + ".ttf")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
gamefont, err := truetype.Parse(fontbytes)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
gameface := truetype.NewFace(gamefont, &truetype.Options{
|
||||
Size: float64(FontSizeNormal),
|
||||
DPI: 72,
|
||||
Hinting: font.HintingFull,
|
||||
})
|
||||
|
||||
biggameface := truetype.NewFace(gamefont, &truetype.Options{
|
||||
Size: float64(FontSizeBig),
|
||||
DPI: 72,
|
||||
Hinting: font.HintingFull,
|
||||
})
|
||||
|
||||
smallgameface := truetype.NewFace(gamefont, &truetype.Options{
|
||||
Size: float64(FontSizeSmall),
|
||||
DPI: 72,
|
||||
Hinting: font.HintingFull,
|
||||
})
|
||||
|
||||
// load the font for use with etxt
|
||||
fontlib := etxt.NewFontLibrary()
|
||||
_, _, err = fontlib.ParseEmbedDirFonts(dir, assetfs)
|
||||
if err != nil {
|
||||
log.Fatalf("Error while loading fonts: %s", err.Error())
|
||||
}
|
||||
|
||||
/*
|
||||
err = fontlib.EachFont(
|
||||
func(fontName string, font *etxt.Font) error {
|
||||
fmt.Printf("font: %s\n", fontName)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
*/
|
||||
|
||||
if !fontlib.HasFont(GameFontETXT) {
|
||||
log.Fatal("missing font: " + GameFontETXT)
|
||||
}
|
||||
|
||||
err = fontlib.EachFont(checkMissingRunes)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
renderer := etxt.NewStdRenderer()
|
||||
|
||||
glyphsCache := etxt.NewDefaultCache(10 * 1024 * 1024) // 10MB
|
||||
renderer.SetCacheHandler(glyphsCache.NewHandler())
|
||||
renderer.SetFont(fontlib.GetFont(GameFontETXT))
|
||||
|
||||
return Texter{
|
||||
Renderer: renderer,
|
||||
FontNormal: &gameface,
|
||||
FontBig: &biggameface,
|
||||
FontSmall: &smallgameface,
|
||||
}
|
||||
}
|
||||
|
||||
// helper function used with FontLibrary.EachFont to make sure
|
||||
// all loaded fonts contain the characters or alphabet we want
|
||||
func checkMissingRunes(name string, font *etxt.Font) error {
|
||||
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
const symbols = "0123456789 .,;:!?-()[]{}_&#@"
|
||||
|
||||
missing, err := etxt.GetMissingRunes(font, letters+symbols)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
log.Fatalf("Font '%s' missing runes: %s", name, string(missing))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"log/slog"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
type ShaderRegistry map[string]*ebiten.Shader
|
||||
|
||||
var Shaders = LoadShaders("assets/shaders")
|
||||
|
||||
func LoadShaders(dir string) ShaderRegistry {
|
||||
shaders := ShaderRegistry{}
|
||||
|
||||
entries, err := assetfs.ReadDir(dir)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to read shaders dir %s: %s", dir, err)
|
||||
}
|
||||
|
||||
for _, file := range entries {
|
||||
path := path.Join(dir, file.Name())
|
||||
fd, err := assetfs.Open(path)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open shader file %s: %s", file.Name(), err)
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
name := strings.TrimSuffix(file.Name(), ".kg")
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(fd)
|
||||
|
||||
shader, err := ebiten.NewShader([]byte(buf.Bytes()))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
shaders[name] = shader
|
||||
|
||||
slog.Debug("loaded shader asset", "path", path)
|
||||
}
|
||||
|
||||
return shaders
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"image"
|
||||
_ "image/png"
|
||||
"io/fs"
|
||||
"log"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
// Maps image name to image data
|
||||
type AssetRegistry map[string]*ebiten.Image
|
||||
|
||||
// A helper to pass the registry easier around
|
||||
type assetData struct {
|
||||
Registry AssetRegistry
|
||||
}
|
||||
|
||||
//go:embed assets/sprites/*.png assets/fonts/*.ttf assets/shaders/*.kg
|
||||
var assetfs embed.FS
|
||||
|
||||
// Called at build time, creates the global asset and animation registries
|
||||
var Assets = LoadImages("assets/sprites")
|
||||
|
||||
// load pngs and json files
|
||||
func LoadImages(dir string) AssetRegistry {
|
||||
Registry := AssetRegistry{}
|
||||
|
||||
// we use embed.FS to iterate over all files in ./assets/
|
||||
entries, err := assetfs.ReadDir(dir)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to read assets dir %s: %s", dir, err)
|
||||
}
|
||||
|
||||
for _, imagefile := range entries {
|
||||
path := path.Join(dir, imagefile.Name())
|
||||
|
||||
fd, err := assetfs.Open(path)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open file %s: %s", imagefile.Name(), err)
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
switch {
|
||||
case strings.HasSuffix(path, ".png"):
|
||||
name, image := ReadImage(imagefile, fd)
|
||||
Registry[name] = image
|
||||
}
|
||||
}
|
||||
|
||||
return Registry
|
||||
}
|
||||
|
||||
func ReadImage(imagefile fs.DirEntry, fd fs.File) (string, *ebiten.Image) {
|
||||
name := strings.TrimSuffix(imagefile.Name(), ".png")
|
||||
|
||||
img, _, err := image.Decode(fd)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to decode image %s: %s", imagefile.Name(), err)
|
||||
}
|
||||
|
||||
image := ebiten.NewImageFromImage(img)
|
||||
|
||||
return name, image
|
||||
}
|
||||
77
main.go
77
main.go
@@ -5,19 +5,30 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"runtime/pprof"
|
||||
"time"
|
||||
|
||||
_ "net/http/pprof"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var directstart bool
|
||||
var Shader string = `
|
||||
//kage:unit pixels
|
||||
|
||||
if len(os.Args) > 1 {
|
||||
directstart = true
|
||||
package main
|
||||
|
||||
var Alife int
|
||||
|
||||
func Fragment(_ vec4, pos vec2, _ vec4) vec4 {
|
||||
if Alife == 1 {
|
||||
return vec4(0.0)
|
||||
}
|
||||
|
||||
return vec4(1.0)
|
||||
}
|
||||
`
|
||||
|
||||
func main() {
|
||||
config, err := ParseCommandline()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -28,16 +39,16 @@ func main() {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
start := Play
|
||||
if !directstart {
|
||||
start = Menu
|
||||
config.DelayedStart = true
|
||||
shader, err := ebiten.NewShader([]byte(Shader))
|
||||
if err != nil {
|
||||
fmt.Println(Shader)
|
||||
log.Fatalf("failed to compile shader: %s\n", err)
|
||||
}
|
||||
game := NewGame(config, SceneName(start))
|
||||
|
||||
game := NewGame(config, shader, Play)
|
||||
|
||||
if config.ProfileFile != "" {
|
||||
// enable cpu profiling. Do NOT use q to stop the game but
|
||||
// close the window to get a profile
|
||||
// enable cpu profiling and use fake game loop
|
||||
fd, err := os.Create(config.ProfileFile)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -46,10 +57,54 @@ func main() {
|
||||
|
||||
pprof.StartCPUProfile(fd)
|
||||
defer pprof.StopCPUProfile()
|
||||
|
||||
Ebitfake(game)
|
||||
|
||||
pprof.StopCPUProfile()
|
||||
fd.Close()
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
fd, err := os.Create("cpu.profile")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
pprof.StartCPUProfile(fd)
|
||||
defer pprof.StopCPUProfile()
|
||||
// main loop
|
||||
if err := ebiten.RunGame(game); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// fake game loop, required to be able to profile the program using
|
||||
// pprof. Otherwise any kind of program exit leads to an empty profile
|
||||
// file.
|
||||
func Ebitfake(game *Game) {
|
||||
screen := ebiten.NewImage(game.ScreenWidth, game.ScreenHeight)
|
||||
|
||||
var loops int64
|
||||
|
||||
for {
|
||||
err := game.Update()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if game.Config.ProfileDraw {
|
||||
game.Draw(screen)
|
||||
}
|
||||
|
||||
fmt.Print(".")
|
||||
time.Sleep(16 * time.Millisecond) // around 60 TPS
|
||||
|
||||
if loops >= game.Config.ProfileMaxLoops {
|
||||
break
|
||||
}
|
||||
|
||||
loops++
|
||||
}
|
||||
}
|
||||
|
||||
130
scene-menu.go
130
scene-menu.go
@@ -1,130 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"os"
|
||||
|
||||
"github.com/ebitenui/ebitenui"
|
||||
"github.com/ebitenui/ebitenui/widget"
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||
)
|
||||
|
||||
type SceneMenu struct {
|
||||
Game *Game
|
||||
Config *Config
|
||||
Next SceneName
|
||||
Whoami SceneName
|
||||
Ui *ebitenui.UI
|
||||
FontColor color.RGBA
|
||||
First bool
|
||||
}
|
||||
|
||||
func NewMenuScene(game *Game, config *Config) Scene {
|
||||
scene := &SceneMenu{
|
||||
Whoami: Menu,
|
||||
Game: game,
|
||||
Next: Menu,
|
||||
Config: config,
|
||||
FontColor: color.RGBA{255, 30, 30, 0xff},
|
||||
}
|
||||
|
||||
scene.Init()
|
||||
|
||||
return scene
|
||||
}
|
||||
|
||||
func (scene *SceneMenu) GetNext() SceneName {
|
||||
return scene.Next
|
||||
}
|
||||
|
||||
func (scene *SceneMenu) ResetNext() {
|
||||
scene.Next = scene.Whoami
|
||||
}
|
||||
|
||||
func (scene *SceneMenu) SetNext(next SceneName) {
|
||||
scene.Next = next
|
||||
}
|
||||
|
||||
func (scene *SceneMenu) Update() error {
|
||||
scene.Ui.Update()
|
||||
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) || inpututil.IsKeyJustPressed(ebiten.KeyQ) {
|
||||
scene.Config.DelayedStart = false
|
||||
scene.Leave()
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (scene *SceneMenu) IsPrimary() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (scene *SceneMenu) Draw(screen *ebiten.Image) {
|
||||
scene.Ui.Draw(screen)
|
||||
}
|
||||
|
||||
func (scene *SceneMenu) Leave() {
|
||||
scene.SetNext(Play)
|
||||
}
|
||||
|
||||
func (scene *SceneMenu) Init() {
|
||||
rowContainer := NewRowContainer("Main Menu")
|
||||
|
||||
empty := NewMenuButton("Start with empty grid",
|
||||
func(args *widget.ButtonClickedEventArgs) {
|
||||
scene.Config.Empty = true
|
||||
scene.Config.Restart = true
|
||||
scene.Leave()
|
||||
})
|
||||
|
||||
random := NewMenuButton("Start with random patterns",
|
||||
func(args *widget.ButtonClickedEventArgs) {
|
||||
scene.Config.Empty = false
|
||||
scene.Config.Restart = true
|
||||
scene.Leave()
|
||||
})
|
||||
|
||||
copy := NewMenuButton("Save Copy as RLE",
|
||||
func(args *widget.ButtonClickedEventArgs) {
|
||||
scene.Config.Markmode = true
|
||||
scene.Config.Paused = true
|
||||
scene.Leave()
|
||||
})
|
||||
|
||||
options := NewMenuButton("Options",
|
||||
func(args *widget.ButtonClickedEventArgs) {
|
||||
scene.SetNext(Options)
|
||||
})
|
||||
|
||||
separator1 := NewSeparator()
|
||||
separator2 := NewSeparator()
|
||||
separator3 := NewSeparator()
|
||||
|
||||
cancel := NewMenuButton("Back",
|
||||
func(args *widget.ButtonClickedEventArgs) {
|
||||
scene.Leave()
|
||||
})
|
||||
|
||||
quit := NewMenuButton("Exit Golsky",
|
||||
func(args *widget.ButtonClickedEventArgs) {
|
||||
os.Exit(0)
|
||||
})
|
||||
|
||||
rowContainer.AddChild(empty)
|
||||
rowContainer.AddChild(random)
|
||||
rowContainer.AddChild(separator1)
|
||||
rowContainer.AddChild(options)
|
||||
rowContainer.AddChild(copy)
|
||||
rowContainer.AddChild(separator2)
|
||||
rowContainer.AddChild(cancel)
|
||||
rowContainer.AddChild(separator3)
|
||||
rowContainer.AddChild(quit)
|
||||
|
||||
scene.Ui = &ebitenui.UI{
|
||||
Container: rowContainer.Container(),
|
||||
}
|
||||
|
||||
}
|
||||
121
scene-options.go
121
scene-options.go
@@ -1,121 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
|
||||
"github.com/ebitenui/ebitenui"
|
||||
"github.com/ebitenui/ebitenui/widget"
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||
)
|
||||
|
||||
type SceneOptions struct {
|
||||
Game *Game
|
||||
Config *Config
|
||||
Next SceneName
|
||||
Whoami SceneName
|
||||
Ui *ebitenui.UI
|
||||
FontColor color.RGBA
|
||||
}
|
||||
|
||||
func NewOptionsScene(game *Game, config *Config) Scene {
|
||||
scene := &SceneOptions{
|
||||
Whoami: Options,
|
||||
Game: game,
|
||||
Next: Options,
|
||||
Config: config,
|
||||
FontColor: color.RGBA{255, 30, 30, 0xff},
|
||||
}
|
||||
|
||||
scene.Init()
|
||||
|
||||
return scene
|
||||
}
|
||||
|
||||
func (scene *SceneOptions) GetNext() SceneName {
|
||||
return scene.Next
|
||||
}
|
||||
|
||||
func (scene *SceneOptions) ResetNext() {
|
||||
scene.Next = scene.Whoami
|
||||
}
|
||||
|
||||
func (scene *SceneOptions) SetNext(next SceneName) {
|
||||
scene.Next = next
|
||||
}
|
||||
|
||||
func (scene *SceneOptions) IsPrimary() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (scene *SceneOptions) Update() error {
|
||||
scene.Ui.Update()
|
||||
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) || inpututil.IsKeyJustPressed(ebiten.KeyQ) {
|
||||
scene.SetNext(Play)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (scene *SceneOptions) Draw(screen *ebiten.Image) {
|
||||
scene.Ui.Draw(screen)
|
||||
}
|
||||
|
||||
func (scene *SceneOptions) SetInitialValue(w *widget.LabeledCheckbox, value bool) {
|
||||
var intval int
|
||||
if value {
|
||||
intval = 1
|
||||
}
|
||||
|
||||
w.SetState(
|
||||
widget.WidgetState(intval),
|
||||
)
|
||||
}
|
||||
|
||||
func (scene *SceneOptions) Init() {
|
||||
rowContainer := NewRowContainer("Options")
|
||||
|
||||
pause := NewCheckbox("Pause",
|
||||
func(args *widget.CheckboxChangedEventArgs) {
|
||||
scene.Config.TogglePaused()
|
||||
})
|
||||
|
||||
debugging := NewCheckbox("Debugging",
|
||||
func(args *widget.CheckboxChangedEventArgs) {
|
||||
scene.Config.ToggleDebugging()
|
||||
})
|
||||
scene.SetInitialValue(debugging, scene.Config.Debug)
|
||||
|
||||
invert := NewCheckbox("Invert",
|
||||
func(args *widget.CheckboxChangedEventArgs) {
|
||||
scene.Config.Invert = true
|
||||
})
|
||||
scene.SetInitialValue(invert, scene.Config.Invert)
|
||||
|
||||
gridlines := NewCheckbox("Show grid lines",
|
||||
func(args *widget.CheckboxChangedEventArgs) {
|
||||
scene.Config.ToggleGridlines()
|
||||
})
|
||||
scene.SetInitialValue(gridlines, scene.Config.ShowGrid)
|
||||
|
||||
separator := NewSeparator()
|
||||
|
||||
cancel := NewMenuButton("Close",
|
||||
func(args *widget.ButtonClickedEventArgs) {
|
||||
scene.SetNext(Menu)
|
||||
})
|
||||
|
||||
rowContainer.AddChild(pause)
|
||||
rowContainer.AddChild(debugging)
|
||||
rowContainer.AddChild(invert)
|
||||
rowContainer.AddChild(gridlines)
|
||||
rowContainer.AddChild(separator)
|
||||
rowContainer.AddChild(cancel)
|
||||
|
||||
scene.Ui = &ebitenui.UI{
|
||||
Container: rowContainer.Container(),
|
||||
}
|
||||
|
||||
}
|
||||
280
scene-play.go
280
scene-play.go
@@ -5,9 +5,11 @@ import (
|
||||
"image"
|
||||
"image/color"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||
"github.com/tlinden/golsky/rle"
|
||||
@@ -18,20 +20,14 @@ type Images struct {
|
||||
Black, White, Age1, Age2, Age3, Age4, Old *ebiten.Image
|
||||
}
|
||||
|
||||
const (
|
||||
DEBUG_FORMAT = "FPS: %0.2f, TPG: %d, M: %0.2fMB, Generations: %d\nScale: %.02f, Zoom: %d, Cam: %.02f,%.02f Cursor: %d,%d %s"
|
||||
)
|
||||
|
||||
type ScenePlay struct {
|
||||
Game *Game
|
||||
Config *Config
|
||||
Next SceneName
|
||||
Whoami SceneName
|
||||
|
||||
Clear bool
|
||||
|
||||
Grids []*Grid // 2 grids: one current, one next
|
||||
History *Grid // holds state of past dead cells for evolution traces
|
||||
History *Grid // holds state of past dead cells for evolution tracks
|
||||
Index int // points to current grid
|
||||
Generations int64 // Stats
|
||||
Black, White, Grey, Old color.RGBA
|
||||
@@ -39,14 +35,15 @@ type ScenePlay struct {
|
||||
TicksElapsed int // tick counter for game speed
|
||||
Tiles Images // pre-computed tiles for dead and alife cells
|
||||
Camera Camera // for zoom+move
|
||||
World, Cache *ebiten.Image // actual image we render to
|
||||
World *ebiten.Image // actual image we render to
|
||||
WheelTurned bool // when user turns wheel multiple times, zoom faster
|
||||
Dragging bool // middle mouse is pressed, move canvas
|
||||
LastCursorPos []int // used to check if the user is dragging
|
||||
Markmode bool // enabled with 'c'
|
||||
MarkTaken bool // true when mouse1 pressed
|
||||
MarkDone bool // true when mouse1 released, copy cells between Mark+Point
|
||||
Mark, Point image.Point // area to marks+save
|
||||
RunOneStep bool // mutable flags from config
|
||||
Paused, RunOneStep bool // mutable flags from config
|
||||
TPG int
|
||||
}
|
||||
|
||||
@@ -56,6 +53,7 @@ func NewPlayScene(game *Game, config *Config) Scene {
|
||||
Game: game,
|
||||
Next: Play,
|
||||
Config: config,
|
||||
Paused: config.Paused,
|
||||
TPG: config.TPG,
|
||||
RunOneStep: config.RunOneStep,
|
||||
}
|
||||
@@ -65,10 +63,6 @@ func NewPlayScene(game *Game, config *Config) Scene {
|
||||
return scene
|
||||
}
|
||||
|
||||
func (scene *ScenePlay) IsPrimary() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (scene *ScenePlay) GetNext() SceneName {
|
||||
return scene.Next
|
||||
}
|
||||
@@ -81,6 +75,10 @@ func (scene *ScenePlay) SetNext(next SceneName) {
|
||||
scene.Next = next
|
||||
}
|
||||
|
||||
func (scene *ScenePlay) Clearscreen() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (scene *ScenePlay) CheckRule(state int64, neighbors int64) int64 {
|
||||
var nextstate int64
|
||||
|
||||
@@ -128,7 +126,7 @@ func (scene *ScenePlay) UpdateCells() {
|
||||
|
||||
// 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
|
||||
// deduce the color to use if evolution tracking is enabled
|
||||
if state != nextstate {
|
||||
scene.History.Data[y][x] = scene.Generations
|
||||
}
|
||||
@@ -151,9 +149,9 @@ func (scene *ScenePlay) UpdateCells() {
|
||||
}
|
||||
|
||||
func (scene *ScenePlay) Reset() {
|
||||
scene.Config.Paused = true
|
||||
scene.Paused = true
|
||||
scene.InitGrid(nil)
|
||||
scene.Config.Paused = false
|
||||
scene.Paused = false
|
||||
}
|
||||
|
||||
// check user input
|
||||
@@ -162,42 +160,38 @@ func (scene *ScenePlay) CheckInput() {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
|
||||
scene.SetNext(Menu)
|
||||
}
|
||||
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyC) {
|
||||
fmt.Println("mark mode on")
|
||||
scene.Config.Markmode = true
|
||||
scene.Config.Paused = true
|
||||
scene.Markmode = true
|
||||
scene.Paused = true
|
||||
}
|
||||
|
||||
if scene.Config.Markmode {
|
||||
if scene.Markmode {
|
||||
return
|
||||
}
|
||||
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeySpace) || inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
|
||||
scene.Config.TogglePaused()
|
||||
scene.Paused = !scene.Paused
|
||||
}
|
||||
|
||||
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
|
||||
scene.ToggleCellOnCursorPos(Alive)
|
||||
scene.Config.Paused = true // drawing while running makes no sense
|
||||
scene.Paused = true // drawing while running makes no sense
|
||||
}
|
||||
|
||||
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonRight) {
|
||||
scene.ToggleCellOnCursorPos(Dead)
|
||||
scene.Config.Paused = true // drawing while running makes no sense
|
||||
scene.Paused = true // drawing while running makes no sense
|
||||
}
|
||||
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyPageDown) {
|
||||
if scene.TPG < 120 {
|
||||
scene.TPG++
|
||||
if ebiten.IsKeyPressed(ebiten.KeyPageDown) {
|
||||
if scene.Config.TPG < 120 {
|
||||
scene.Config.TPG++
|
||||
}
|
||||
}
|
||||
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyPageUp) {
|
||||
if scene.TPG >= 1 {
|
||||
if ebiten.IsKeyPressed(ebiten.KeyPageUp) {
|
||||
if scene.TPG > 1 {
|
||||
scene.TPG--
|
||||
}
|
||||
}
|
||||
@@ -206,11 +200,11 @@ func (scene *ScenePlay) CheckInput() {
|
||||
scene.SaveState()
|
||||
}
|
||||
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyD) {
|
||||
scene.Config.Debug = !scene.Config.Debug
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyR) {
|
||||
scene.Reset()
|
||||
}
|
||||
|
||||
if scene.Config.Paused {
|
||||
if scene.Paused {
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyN) {
|
||||
scene.Config.RunOneStep = true
|
||||
}
|
||||
@@ -220,7 +214,7 @@ func (scene *ScenePlay) CheckInput() {
|
||||
// Check dragging input. move the canvas with the mouse while pressing
|
||||
// the middle mouse button, zoom in and out using the wheel.
|
||||
func (scene *ScenePlay) CheckDraggingInput() {
|
||||
if scene.Config.Markmode {
|
||||
if scene.Markmode {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -264,12 +258,28 @@ func (scene *ScenePlay) CheckDraggingInput() {
|
||||
|
||||
// Zoom
|
||||
_, dy := ebiten.Wheel()
|
||||
step := 1
|
||||
|
||||
if dy != 0 {
|
||||
scene.Camera.ZoomFactor += (int(dy) * 5)
|
||||
if scene.WheelTurned {
|
||||
// if keep scrolling the wheel, zoom faster
|
||||
step = 50
|
||||
} else {
|
||||
scene.WheelTurned = false
|
||||
}
|
||||
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyR) {
|
||||
if dy < 0 {
|
||||
if scene.Camera.ZoomFactor > -2400 {
|
||||
scene.Camera.ZoomFactor -= step
|
||||
}
|
||||
}
|
||||
|
||||
if dy > 0 {
|
||||
if scene.Camera.ZoomFactor < 2400 {
|
||||
scene.Camera.ZoomFactor += step
|
||||
}
|
||||
}
|
||||
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
|
||||
scene.Camera.Reset()
|
||||
}
|
||||
|
||||
@@ -284,7 +294,7 @@ func (scene *ScenePlay) GetWorldCursorPos() image.Point {
|
||||
}
|
||||
|
||||
func (scene *ScenePlay) CheckMarkInput() {
|
||||
if !scene.Config.Markmode {
|
||||
if !scene.Markmode {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -298,7 +308,7 @@ func (scene *ScenePlay) CheckMarkInput() {
|
||||
scene.Point = scene.GetWorldCursorPos()
|
||||
//fmt.Printf("Mark: %v, Point: %v\n", scene.Mark, scene.Point)
|
||||
} else if inpututil.IsMouseButtonJustReleased(ebiten.MouseButton0) {
|
||||
scene.Config.Markmode = false
|
||||
scene.Markmode = false
|
||||
scene.MarkTaken = false
|
||||
scene.MarkDone = true
|
||||
|
||||
@@ -368,25 +378,11 @@ func (scene *ScenePlay) SaveRectRLE() {
|
||||
}
|
||||
|
||||
func (scene *ScenePlay) Update() error {
|
||||
if scene.Config.Restart {
|
||||
scene.Config.Restart = false
|
||||
scene.InitGrid(nil)
|
||||
scene.InitCache()
|
||||
return nil
|
||||
}
|
||||
|
||||
if scene.Config.RestartCache {
|
||||
scene.Config.RestartCache = false
|
||||
scene.InitTiles()
|
||||
scene.InitCache()
|
||||
return nil
|
||||
}
|
||||
|
||||
scene.CheckInput()
|
||||
scene.CheckDraggingInput()
|
||||
scene.CheckMarkInput()
|
||||
|
||||
if !scene.Config.Paused || scene.RunOneStep {
|
||||
if !scene.Paused || scene.RunOneStep {
|
||||
scene.UpdateCells()
|
||||
}
|
||||
|
||||
@@ -400,7 +396,9 @@ func (scene *ScenePlay) ToggleCellOnCursorPos(alive int64) {
|
||||
x := int(worldX) / scene.Config.Cellsize
|
||||
y := int(worldY) / scene.Config.Cellsize
|
||||
|
||||
if x > -1 && y > -1 && x < scene.Config.Width && y < scene.Config.Height {
|
||||
//fmt.Printf("cell at %d,%d\n", x, y)
|
||||
|
||||
if x > -1 && y > -1 {
|
||||
scene.Grids[scene.Index].Data[y][x] = alive
|
||||
scene.History.Data[y][x] = 1
|
||||
}
|
||||
@@ -411,33 +409,46 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
|
||||
// we fill the whole screen with a background color, the cells
|
||||
// themselfes will be 1px smaller as their nominal size, producing
|
||||
// a nice grey grid with grid lines
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
//op := &ebiten.DrawImageOptions{}
|
||||
shaderop := &ebiten.DrawRectShaderOptions{}
|
||||
|
||||
op.GeoM.Translate(0, 0)
|
||||
scene.World.DrawImage(scene.Cache, op)
|
||||
fmt.Println(ebiten.ActualFPS())
|
||||
|
||||
var age int64
|
||||
if scene.Config.NoGrid {
|
||||
scene.World.Fill(scene.White)
|
||||
} else {
|
||||
scene.World.Fill(scene.Grey)
|
||||
}
|
||||
|
||||
for y := 0; y < scene.Config.Height; y++ {
|
||||
for x := 0; x < scene.Config.Width; x++ {
|
||||
op.GeoM.Reset()
|
||||
op.GeoM.Translate(
|
||||
float64(x*scene.Config.Cellsize),
|
||||
float64(y*scene.Config.Cellsize),
|
||||
// op.GeoM.Reset()
|
||||
// op.GeoM.Translate(float64(x*scene.Config.Cellsize), float64(y*scene.Config.Cellsize))
|
||||
// age := scene.Generations - scene.History.Data[y][x]
|
||||
|
||||
shaderop.GeoM.Reset()
|
||||
shaderop.Uniforms = map[string]any{
|
||||
"Alife": scene.Grids[scene.Index].Data[y][x],
|
||||
}
|
||||
|
||||
shaderop.GeoM.Translate(float64(x*scene.Config.Cellsize), float64(y*scene.Config.Cellsize))
|
||||
|
||||
scene.World.DrawRectShader(
|
||||
scene.Config.Cellsize,
|
||||
scene.Config.Cellsize,
|
||||
scene.Game.Shader,
|
||||
shaderop,
|
||||
)
|
||||
|
||||
age = scene.Generations - scene.History.Data[y][x]
|
||||
|
||||
/*
|
||||
switch scene.Grids[scene.Index].Data[y][x] {
|
||||
case Alive:
|
||||
case 1:
|
||||
if age > 50 && scene.Config.ShowEvolution {
|
||||
scene.World.DrawImage(scene.Tiles.Old, op)
|
||||
|
||||
} else {
|
||||
scene.World.DrawImage(scene.Tiles.Black, op)
|
||||
}
|
||||
case Dead:
|
||||
// only draw dead cells in case evolution trace is enabled
|
||||
case 0:
|
||||
if scene.History.Data[y][x] > 1 && scene.Config.ShowEvolution {
|
||||
switch {
|
||||
case age < 10:
|
||||
@@ -449,30 +460,24 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
|
||||
default:
|
||||
scene.World.DrawImage(scene.Tiles.Age4, op)
|
||||
}
|
||||
} else {
|
||||
scene.World.DrawImage(scene.Tiles.White, op)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
scene.DrawMark(scene.World)
|
||||
|
||||
scene.Camera.Render(scene.World, screen)
|
||||
|
||||
scene.DrawDebug(screen)
|
||||
|
||||
op.GeoM.Reset()
|
||||
op.GeoM.Translate(0, 0)
|
||||
|
||||
scene.Game.Screen.DrawImage(screen, op)
|
||||
}
|
||||
|
||||
func (scene *ScenePlay) DrawMark(screen *ebiten.Image) {
|
||||
if scene.Config.Markmode && scene.MarkTaken {
|
||||
if scene.Markmode && scene.MarkTaken {
|
||||
x := float32(scene.Mark.X * scene.Config.Cellsize)
|
||||
y := float32(scene.Mark.Y * scene.Config.Cellsize)
|
||||
w := float32((scene.Point.X - scene.Mark.X) * scene.Config.Cellsize)
|
||||
h := float32((scene.Point.Y - scene.Mark.Y) * scene.Config.Cellsize)
|
||||
|
||||
// fmt.Printf("%d,%d=>%0.0f,%0.0f to %d,%d=>%0.0f,%0.0f\n",
|
||||
// scene.Mark.X, scene.Mark.Y, x, y, scene.Point.X, scene.Point.Y, w, h)
|
||||
|
||||
vector.StrokeRect(
|
||||
scene.World,
|
||||
x+1, y+1,
|
||||
@@ -480,68 +485,45 @@ func (scene *ScenePlay) DrawMark(screen *ebiten.Image) {
|
||||
1.0, scene.Old, false,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func (scene *ScenePlay) DrawDebug(screen *ebiten.Image) {
|
||||
scene.Camera.Render(scene.World, screen)
|
||||
|
||||
if scene.Config.Debug {
|
||||
paused := ""
|
||||
if scene.Config.Paused {
|
||||
if scene.Paused {
|
||||
paused = "-- paused --"
|
||||
}
|
||||
|
||||
x, y := ebiten.CursorPosition()
|
||||
debug := fmt.Sprintf(
|
||||
DEBUG_FORMAT,
|
||||
ebiten.ActualTPS(), scene.TPG, GetMem(), scene.Generations,
|
||||
scene.Game.Scale, scene.Camera.ZoomFactor,
|
||||
scene.Camera.Position[0], scene.Camera.Position[1],
|
||||
x, y,
|
||||
paused)
|
||||
|
||||
FontRenderer.Renderer.SetSizePx(10 + int(scene.Game.Scale*10))
|
||||
FontRenderer.Renderer.SetTarget(screen)
|
||||
|
||||
FontRenderer.Renderer.SetColor(scene.Black)
|
||||
FontRenderer.Renderer.Draw(debug, 31, 31)
|
||||
|
||||
FontRenderer.Renderer.SetColor(scene.Old)
|
||||
FontRenderer.Renderer.Draw(debug, 30, 30)
|
||||
|
||||
fmt.Println(debug)
|
||||
ebitenutil.DebugPrint(
|
||||
screen,
|
||||
fmt.Sprintf("FPS: %0.2f, TPG: %d, Mem: %0.2f MB, Generations: %d %s",
|
||||
ebiten.ActualTPS(), scene.TPG, GetMem(), scene.Generations, paused),
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// FIXME: move these into Grid
|
||||
// load a pre-computed pattern from RLE file
|
||||
func (scene *ScenePlay) InitPattern() {
|
||||
scene.Grids[0].LoadRLE(scene.Config.RLE)
|
||||
scene.History.LoadRLE(scene.Config.RLE)
|
||||
}
|
||||
if scene.Config.RLE != nil {
|
||||
startX := (scene.Config.Width / 2) - (scene.Config.RLE.Width / 2)
|
||||
startY := (scene.Config.Height / 2) - (scene.Config.RLE.Height / 2)
|
||||
var y, x int
|
||||
|
||||
// pre-render offscreen cache image
|
||||
func (scene *ScenePlay) InitCache() {
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
for rowIndex, patternRow := range scene.Config.RLE.Pattern {
|
||||
for colIndex := range patternRow {
|
||||
if scene.Config.RLE.Pattern[rowIndex][colIndex] > 0 {
|
||||
x = colIndex + startX
|
||||
y = rowIndex + startY
|
||||
|
||||
if scene.Config.ShowGrid {
|
||||
scene.Cache.Fill(scene.Grey)
|
||||
} else {
|
||||
scene.Cache.Fill(scene.White)
|
||||
scene.History.Data[y][x] = 1
|
||||
scene.Grids[0].Data[y][x] = 1
|
||||
}
|
||||
}
|
||||
|
||||
for y := 0; y < scene.Config.Height; y++ {
|
||||
for x := 0; x < scene.Config.Width; x++ {
|
||||
op.GeoM.Reset()
|
||||
op.GeoM.Translate(
|
||||
float64(x*scene.Config.Cellsize),
|
||||
float64(y*scene.Config.Cellsize),
|
||||
)
|
||||
|
||||
scene.Cache.DrawImage(scene.Tiles.White, op)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// initialize grid[s], either using pre-computed from state or rle file, or random
|
||||
func (scene *ScenePlay) InitGrid(grid *Grid) {
|
||||
if grid != nil {
|
||||
// use pre-loaded grid
|
||||
@@ -559,9 +541,16 @@ func (scene *ScenePlay) InitGrid(grid *Grid) {
|
||||
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
|
||||
grida.FillRandom()
|
||||
grida.Copy(history)
|
||||
for y := 0; y < scene.Config.Height; y++ {
|
||||
if !scene.Config.Empty {
|
||||
for x := 0; x < scene.Config.Width; x++ {
|
||||
if rand.Intn(scene.Config.Density) == 1 {
|
||||
history.Data[y][x] = 1
|
||||
grida.Data[y][x] = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scene.Grids = []*Grid{
|
||||
grida,
|
||||
@@ -618,6 +607,7 @@ func (scene *ScenePlay) Init() {
|
||||
|
||||
if scene.Config.StateGrid != nil {
|
||||
grid = scene.Config.StateGrid
|
||||
|
||||
}
|
||||
|
||||
scene.Camera = Camera{
|
||||
@@ -627,40 +617,16 @@ func (scene *ScenePlay) Init() {
|
||||
},
|
||||
}
|
||||
|
||||
scene.World = ebiten.NewImage(
|
||||
scene.Config.Width*scene.Config.Cellsize,
|
||||
scene.Config.Height*scene.Config.Cellsize,
|
||||
)
|
||||
scene.World = ebiten.NewImage(scene.Config.ScreenWidth, scene.Config.ScreenHeight)
|
||||
|
||||
scene.Cache = ebiten.NewImage(
|
||||
scene.Config.Width*scene.Config.Cellsize,
|
||||
scene.Config.Height*scene.Config.Cellsize,
|
||||
)
|
||||
|
||||
scene.InitTiles()
|
||||
scene.InitCache()
|
||||
|
||||
if scene.Config.DelayedStart && !scene.Config.Empty {
|
||||
scene.Config.Empty = true
|
||||
scene.InitGrid(grid)
|
||||
scene.Config.Empty = false
|
||||
} else {
|
||||
scene.InitGrid(grid)
|
||||
}
|
||||
|
||||
scene.InitPattern()
|
||||
scene.InitTiles()
|
||||
|
||||
scene.Index = 0
|
||||
scene.TicksElapsed = 0
|
||||
|
||||
scene.LastCursorPos = make([]int, 2)
|
||||
|
||||
if scene.Config.Zoomfactor < 0 || scene.Config.Zoomfactor > 0 {
|
||||
scene.Camera.ZoomFactor = scene.Config.Zoomfactor
|
||||
}
|
||||
|
||||
scene.Camera.Position[0] = scene.Config.InitialCamPos[0]
|
||||
scene.Camera.Position[1] = scene.Config.InitialCamPos[1]
|
||||
}
|
||||
|
||||
// count the living neighbors of a cell
|
||||
|
||||
3
scene.go
3
scene.go
@@ -14,13 +14,12 @@ type Scene interface {
|
||||
SetNext(SceneName)
|
||||
GetNext() SceneName
|
||||
ResetNext()
|
||||
Clearscreen() bool
|
||||
Update() error
|
||||
Draw(screen *ebiten.Image)
|
||||
IsPrimary() bool // if true, this scene will be always drawn
|
||||
}
|
||||
|
||||
const (
|
||||
Menu = iota // main top level menu
|
||||
Play // actual playing happens here
|
||||
Options
|
||||
)
|
||||
|
||||
161
widgets.go
161
widgets.go
@@ -1,161 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
|
||||
"github.com/ebitenui/ebitenui/image"
|
||||
"github.com/ebitenui/ebitenui/widget"
|
||||
)
|
||||
|
||||
func NewMenuButton(
|
||||
text string,
|
||||
action func(args *widget.ButtonClickedEventArgs)) *widget.Button {
|
||||
|
||||
buttonImage, _ := LoadButtonImage()
|
||||
|
||||
return widget.NewButton(
|
||||
widget.ButtonOpts.WidgetOpts(
|
||||
widget.WidgetOpts.LayoutData(widget.RowLayoutData{
|
||||
Position: widget.RowLayoutPositionCenter,
|
||||
Stretch: true,
|
||||
MaxWidth: 200,
|
||||
MaxHeight: 100,
|
||||
}),
|
||||
),
|
||||
|
||||
widget.ButtonOpts.Image(buttonImage),
|
||||
|
||||
widget.ButtonOpts.Text(text, *FontRenderer.FontSmall, &widget.ButtonTextColor{
|
||||
Idle: color.NRGBA{0xdf, 0xf4, 0xff, 0xff},
|
||||
}),
|
||||
|
||||
widget.ButtonOpts.TextPadding(widget.Insets{
|
||||
Left: 5,
|
||||
Right: 5,
|
||||
Top: 5,
|
||||
Bottom: 5,
|
||||
}),
|
||||
|
||||
widget.ButtonOpts.ClickedHandler(action),
|
||||
)
|
||||
}
|
||||
|
||||
func NewCheckbox(
|
||||
text string,
|
||||
action func(args *widget.CheckboxChangedEventArgs)) *widget.LabeledCheckbox {
|
||||
|
||||
checkboxImage, _ := LoadCheckboxImage()
|
||||
buttonImage, _ := LoadButtonImage()
|
||||
|
||||
return widget.NewLabeledCheckbox(
|
||||
widget.LabeledCheckboxOpts.CheckboxOpts(
|
||||
widget.CheckboxOpts.ButtonOpts(
|
||||
widget.ButtonOpts.Image(buttonImage),
|
||||
),
|
||||
widget.CheckboxOpts.Image(checkboxImage),
|
||||
widget.CheckboxOpts.StateChangedHandler(action),
|
||||
),
|
||||
widget.LabeledCheckboxOpts.LabelOpts(
|
||||
widget.LabelOpts.Text(text, *FontRenderer.FontSmall,
|
||||
&widget.LabelColor{
|
||||
Idle: color.NRGBA{0xdf, 0xf4, 0xff, 0xff},
|
||||
}),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func NewSeparator() widget.PreferredSizeLocateableWidget {
|
||||
c := widget.NewContainer(
|
||||
widget.ContainerOpts.Layout(widget.NewRowLayout(
|
||||
widget.RowLayoutOpts.Direction(widget.DirectionVertical),
|
||||
widget.RowLayoutOpts.Padding(widget.Insets{
|
||||
Top: 3,
|
||||
Bottom: 0,
|
||||
}))),
|
||||
widget.ContainerOpts.WidgetOpts(
|
||||
widget.WidgetOpts.LayoutData(
|
||||
widget.RowLayoutData{Stretch: true})))
|
||||
return c
|
||||
}
|
||||
|
||||
type RowContainer struct {
|
||||
Root *widget.Container
|
||||
Row *widget.Container
|
||||
}
|
||||
|
||||
func (container *RowContainer) AddChild(child widget.PreferredSizeLocateableWidget) {
|
||||
container.Row.AddChild(child)
|
||||
}
|
||||
|
||||
func (container *RowContainer) Container() *widget.Container {
|
||||
return container.Root
|
||||
}
|
||||
|
||||
// set arg to false if no background needed
|
||||
func NewRowContainer(title string) *RowContainer {
|
||||
buttonImageHover := image.NewNineSlice(Assets["button-9slice3"], [3]int{3, 3, 3}, [3]int{3, 3, 3})
|
||||
|
||||
uiContainer := widget.NewContainer(
|
||||
widget.ContainerOpts.Layout(widget.NewAnchorLayout()),
|
||||
)
|
||||
|
||||
titleLabel := widget.NewText(
|
||||
widget.TextOpts.WidgetOpts(widget.WidgetOpts.LayoutData(widget.RowLayoutData{
|
||||
Stretch: true,
|
||||
})),
|
||||
widget.TextOpts.Text(title, *FontRenderer.FontNormal, color.NRGBA{0xdf, 0xf4, 0xff, 0xff}))
|
||||
|
||||
rowContainer := widget.NewContainer(
|
||||
widget.ContainerOpts.WidgetOpts(
|
||||
widget.WidgetOpts.LayoutData(widget.AnchorLayoutData{
|
||||
HorizontalPosition: widget.AnchorLayoutPositionCenter,
|
||||
VerticalPosition: widget.AnchorLayoutPositionCenter,
|
||||
}),
|
||||
),
|
||||
widget.ContainerOpts.Layout(widget.NewRowLayout(
|
||||
widget.RowLayoutOpts.Direction(widget.DirectionVertical),
|
||||
widget.RowLayoutOpts.Padding(widget.NewInsetsSimple(8)),
|
||||
widget.RowLayoutOpts.Spacing(0),
|
||||
)),
|
||||
widget.ContainerOpts.BackgroundImage(buttonImageHover),
|
||||
)
|
||||
|
||||
rowContainer.AddChild(titleLabel)
|
||||
|
||||
uiContainer.AddChild(rowContainer)
|
||||
|
||||
return &RowContainer{
|
||||
Root: uiContainer,
|
||||
Row: rowContainer,
|
||||
}
|
||||
}
|
||||
|
||||
func LoadButtonImage() (*widget.ButtonImage, error) {
|
||||
idle := image.NewNineSlice(Assets["button-9slice2"], [3]int{3, 3, 3}, [3]int{3, 3, 3})
|
||||
hover := image.NewNineSlice(Assets["button-9slice3"], [3]int{3, 3, 3}, [3]int{3, 3, 3})
|
||||
pressed := image.NewNineSlice(Assets["button-9slice1"], [3]int{3, 3, 3}, [3]int{3, 3, 3})
|
||||
|
||||
return &widget.ButtonImage{
|
||||
Idle: idle,
|
||||
Hover: hover,
|
||||
Pressed: pressed,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func LoadCheckboxImage() (*widget.CheckboxGraphicImage, error) {
|
||||
unchecked := &widget.ButtonImageImage{
|
||||
Idle: Assets["checkbox-9slice2"],
|
||||
Disabled: Assets["checkbox-9slice2"],
|
||||
}
|
||||
|
||||
checked := &widget.ButtonImageImage{
|
||||
Idle: Assets["checkbox-9slice1"],
|
||||
Disabled: Assets["checkbox-9slice1"],
|
||||
}
|
||||
|
||||
return &widget.CheckboxGraphicImage{
|
||||
Checked: checked,
|
||||
Unchecked: unchecked,
|
||||
Greyed: unchecked,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user