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
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
View File

@@ -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)

View File

@@ -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,11 +422,25 @@ 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]
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 {
@@ -447,6 +464,8 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
scene.World.DrawImage(scene.Tiles.White, op)
}
}
*/
}
}