temporary variant using a shader instead of direct draw

This commit is contained in:
2024-05-27 20:20:42 +02:00
parent ba247d0606
commit 329d213325
3 changed files with 80 additions and 29 deletions

View File

@@ -7,14 +7,16 @@ type Game struct {
Scenes map[SceneName]Scene Scenes map[SceneName]Scene
CurrentScene SceneName CurrentScene SceneName
Config *Config Config *Config
Shader *ebiten.Shader
} }
func NewGame(config *Config, startscene SceneName) *Game { func NewGame(config *Config, shader *ebiten.Shader, startscene SceneName) *Game {
game := &Game{ game := &Game{
Config: config, Config: config,
Scenes: map[SceneName]Scene{}, Scenes: map[SceneName]Scene{},
ScreenWidth: config.ScreenWidth, ScreenWidth: config.ScreenWidth,
ScreenHeight: config.ScreenHeight, ScreenHeight: config.ScreenHeight,
Shader: shader,
} }
// setup scene[s] // setup scene[s]

32
main.go
View File

@@ -12,6 +12,22 @@ import (
"github.com/hajimehoshi/ebiten/v2" "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() { func main() {
config, err := ParseCommandline() config, err := ParseCommandline()
if err != nil { if err != nil {
@@ -23,7 +39,13 @@ func main() {
os.Exit(0) 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 != "" { if config.ProfileFile != "" {
// enable cpu profiling and use fake game loop // enable cpu profiling and use fake game loop
@@ -44,6 +66,14 @@ func main() {
os.Exit(0) 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 // main loop
if err := ebiten.RunGame(game); err != nil { if err := ebiten.RunGame(game); err != nil {
log.Fatal(err) log.Fatal(err)

View File

@@ -409,7 +409,10 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
// we fill the whole screen with a background color, the cells // we fill the whole screen with a background color, the cells
// themselfes will be 1px smaller as their nominal size, producing // themselfes will be 1px smaller as their nominal size, producing
// a nice grey grid with grid lines // a nice grey grid with grid lines
op := &ebiten.DrawImageOptions{} //op := &ebiten.DrawImageOptions{}
shaderop := &ebiten.DrawRectShaderOptions{}
fmt.Println(ebiten.ActualFPS())
if scene.Config.NoGrid { if scene.Config.NoGrid {
scene.World.Fill(scene.White) 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 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(float64(x*scene.Config.Cellsize), float64(y*scene.Config.Cellsize)) // 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] shaderop.GeoM.Reset()
shaderop.Uniforms = map[string]any{
switch scene.Grids[scene.Index].Data[y][x] { "Alife": 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.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)
}
}
*/
} }
} }