mirror of
https://codeberg.org/scip/golsky.git
synced 2025-12-16 12:10:58 +01:00
temporary variant using a shader instead of direct draw
This commit is contained in:
4
game.go
4
game.go
@@ -7,14 +7,16 @@ type Game struct {
|
||||
Scenes map[SceneName]Scene
|
||||
CurrentScene SceneName
|
||||
Config *Config
|
||||
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]
|
||||
|
||||
32
main.go
32
main.go
@@ -12,6 +12,22 @@ import (
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
var Shader string = `
|
||||
//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)
|
||||
}
|
||||
`
|
||||
|
||||
func main() {
|
||||
config, err := ParseCommandline()
|
||||
if err != nil {
|
||||
@@ -23,7 +39,13 @@ func main() {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
game := NewGame(config, Play)
|
||||
shader, err := ebiten.NewShader([]byte(Shader))
|
||||
if err != nil {
|
||||
fmt.Println(Shader)
|
||||
log.Fatalf("failed to compile shader: %s\n", err)
|
||||
}
|
||||
|
||||
game := NewGame(config, shader, Play)
|
||||
|
||||
if config.ProfileFile != "" {
|
||||
// enable cpu profiling and use fake game loop
|
||||
@@ -44,6 +66,14 @@ func main() {
|
||||
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)
|
||||
|
||||
@@ -409,7 +409,10 @@ 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{}
|
||||
|
||||
fmt.Println(ebiten.ActualFPS())
|
||||
|
||||
if scene.Config.NoGrid {
|
||||
scene.World.Fill(scene.White)
|
||||
@@ -419,34 +422,50 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
|
||||
|
||||
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]
|
||||
|
||||
age := scene.Generations - scene.History.Data[y][x]
|
||||
|
||||
switch scene.Grids[scene.Index].Data[y][x] {
|
||||
case 1:
|
||||
if age > 50 && scene.Config.ShowEvolution {
|
||||
scene.World.DrawImage(scene.Tiles.Old, op)
|
||||
} else {
|
||||
scene.World.DrawImage(scene.Tiles.Black, op)
|
||||
}
|
||||
case 0:
|
||||
if scene.History.Data[y][x] > 1 && scene.Config.ShowEvolution {
|
||||
switch {
|
||||
case age < 10:
|
||||
scene.World.DrawImage(scene.Tiles.Age1, op)
|
||||
case age < 20:
|
||||
scene.World.DrawImage(scene.Tiles.Age2, op)
|
||||
case age < 30:
|
||||
scene.World.DrawImage(scene.Tiles.Age3, op)
|
||||
default:
|
||||
scene.World.DrawImage(scene.Tiles.Age4, op)
|
||||
}
|
||||
} else {
|
||||
scene.World.DrawImage(scene.Tiles.White, op)
|
||||
}
|
||||
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,
|
||||
)
|
||||
|
||||
/*
|
||||
switch scene.Grids[scene.Index].Data[y][x] {
|
||||
case 1:
|
||||
if age > 50 && scene.Config.ShowEvolution {
|
||||
scene.World.DrawImage(scene.Tiles.Old, op)
|
||||
} else {
|
||||
scene.World.DrawImage(scene.Tiles.Black, op)
|
||||
}
|
||||
case 0:
|
||||
if scene.History.Data[y][x] > 1 && scene.Config.ShowEvolution {
|
||||
switch {
|
||||
case age < 10:
|
||||
scene.World.DrawImage(scene.Tiles.Age1, op)
|
||||
case age < 20:
|
||||
scene.World.DrawImage(scene.Tiles.Age2, op)
|
||||
case age < 30:
|
||||
scene.World.DrawImage(scene.Tiles.Age3, op)
|
||||
default:
|
||||
scene.World.DrawImage(scene.Tiles.Age4, op)
|
||||
}
|
||||
} else {
|
||||
scene.World.DrawImage(scene.Tiles.White, op)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user