add a bunch of mutexes and shit but still have racist conditions and panics LOL

This commit is contained in:
2025-01-22 20:14:58 +01:00
parent 417bf4ea63
commit 220a451475
7 changed files with 309 additions and 161 deletions

82
main.go
View File

@ -3,7 +3,10 @@ package main
import (
"flag"
"log"
"os"
"os/signal"
"strings"
"syscall"
"gitea.boner.be/bdnugget/goonscape/game"
"gitea.boner.be/bdnugget/goonscape/network"
@ -11,11 +14,24 @@ import (
)
func main() {
defer func() {
if r := recover(); r != nil {
log.Printf("Recovered from panic in main: %v", r)
}
}()
// Parse command line flags
verbose := flag.Bool("v", false, "Also show info logs (spammy)")
local := flag.Bool("local", false, "Connect to local server")
addr := flag.String("addr", "", "Server address (host or host:port)")
flag.Parse()
if *verbose {
rl.SetTraceLogLevel(rl.LogTrace)
} else {
rl.SetTraceLogLevel(rl.LogWarning)
}
// Set server address based on flags
if *local {
if *addr != "" {
@ -32,29 +48,63 @@ func main() {
rl.InitWindow(1024, 768, "GoonScape")
rl.SetExitKey(0)
defer rl.CloseWindow()
rl.InitAudioDevice()
defer rl.CloseAudioDevice()
gameInstance := game.New()
if err := gameInstance.LoadAssets(); err != nil {
log.Printf("Failed to load assets: %v", err)
return
}
defer func() {
gameInstance.Cleanup()
rl.CloseWindow()
rl.CloseAudioDevice()
}()
rl.SetTargetFPS(60)
game := game.New()
if err := game.LoadAssets(); err != nil {
log.Fatalf("Failed to load assets: %v", err)
}
defer game.Cleanup()
rl.PlayMusicStream(gameInstance.Music)
rl.SetMusicVolume(gameInstance.Music, 0.5)
rl.PlayMusicStream(game.Music)
rl.SetMusicVolume(game.Music, 0.5)
// Handle OS signals for clean shutdown
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
if gameInstance != nil {
gameInstance.Shutdown()
}
}()
// Keep game loop in main thread for Raylib
for !rl.WindowShouldClose() {
deltaTime := rl.GetFrameTime()
rl.UpdateMusicStream(game.Music)
game.Update(deltaTime)
game.Render()
}
rl.UpdateMusicStream(gameInstance.Music)
// Wait for clean shutdown
<-game.QuitChan
func() {
defer func() {
if r := recover(); r != nil {
log.Printf("Recovered from panic in game update: %v", r)
}
}()
gameInstance.Update(deltaTime)
}()
func() {
defer func() {
if r := recover(); r != nil {
log.Printf("Recovered from panic in game render: %v", r)
}
}()
gameInstance.Render()
}()
// Check if game requested shutdown
select {
case <-gameInstance.QuitChan():
return
default:
}
}
}