2024-05-20 20:19:11 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2024-05-26 12:33:16 +02:00
|
|
|
"fmt"
|
2024-05-20 20:19:11 +02:00
|
|
|
"log"
|
2024-05-21 19:01:08 +02:00
|
|
|
"os"
|
2024-05-27 13:38:14 +02:00
|
|
|
"runtime/pprof"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
_ "net/http/pprof"
|
2024-05-20 20:19:11 +02:00
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2024-05-21 19:01:08 +02:00
|
|
|
)
|
|
|
|
|
|
2024-05-27 20:20:42 +02:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
|
2024-05-20 20:19:11 +02:00
|
|
|
func main() {
|
2024-05-27 13:38:14 +02:00
|
|
|
config, err := ParseCommandline()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2024-05-21 19:01:08 +02:00
|
|
|
|
2024-05-26 12:33:16 +02:00
|
|
|
if config.ShowVersion {
|
|
|
|
|
fmt.Printf("This is golsky version %s\n", VERSION)
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-27 20:20:42 +02:00
|
|
|
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)
|
2024-05-26 20:26:13 +02:00
|
|
|
|
2024-05-27 13:38:14 +02:00
|
|
|
if config.ProfileFile != "" {
|
|
|
|
|
// enable cpu profiling and use fake game loop
|
|
|
|
|
fd, err := os.Create(config.ProfileFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer fd.Close()
|
2024-05-26 20:26:13 +02:00
|
|
|
|
2024-05-27 13:38:14 +02:00
|
|
|
pprof.StartCPUProfile(fd)
|
|
|
|
|
defer pprof.StopCPUProfile()
|
2024-05-26 20:26:13 +02:00
|
|
|
|
2024-05-27 13:38:14 +02:00
|
|
|
Ebitfake(game)
|
|
|
|
|
|
|
|
|
|
pprof.StopCPUProfile()
|
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
}
|
2024-05-20 20:19:11 +02:00
|
|
|
|
2024-05-27 20:20:42 +02:00
|
|
|
fd, err := os.Create("cpu.profile")
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
|
|
pprof.StartCPUProfile(fd)
|
|
|
|
|
defer pprof.StopCPUProfile()
|
2024-05-23 14:27:42 +02:00
|
|
|
// main loop
|
2024-05-20 20:19:11 +02:00
|
|
|
if err := ebiten.RunGame(game); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-27 13:38:14 +02:00
|
|
|
|
|
|
|
|
// 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++
|
|
|
|
|
}
|
|
|
|
|
}
|