Fix segfaults by avoiding models and animations for now
This commit is contained in:
67
main.go
67
main.go
@ -7,6 +7,7 @@ import (
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"gitea.boner.be/bdnugget/goonscape/game"
|
||||
"gitea.boner.be/bdnugget/goonscape/network"
|
||||
@ -14,9 +15,12 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Set up panic recovery at the top level
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("Recovered from panic in main: %v", r)
|
||||
log.Printf("Recovered from fatal panic in main: %v", r)
|
||||
// Give the user a chance to see the error
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
}()
|
||||
|
||||
@ -46,26 +50,61 @@ func main() {
|
||||
network.SetServerAddr(*addr)
|
||||
}
|
||||
|
||||
// Initialize window with error handling
|
||||
rl.SetConfigFlags(rl.FlagMsaa4xHint) // Enable MSAA for smoother rendering
|
||||
rl.InitWindow(1024, 768, "GoonScape")
|
||||
rl.SetExitKey(0)
|
||||
rl.InitAudioDevice()
|
||||
|
||||
gameInstance := game.New()
|
||||
if err := gameInstance.LoadAssets(); err != nil {
|
||||
log.Printf("Failed to load assets: %v", err)
|
||||
return
|
||||
rl.SetExitKey(0)
|
||||
|
||||
// Initialize audio with error handling
|
||||
if !rl.IsAudioDeviceReady() {
|
||||
rl.InitAudioDevice()
|
||||
if !rl.IsAudioDeviceReady() {
|
||||
log.Println("Warning: Failed to initialize audio device, continuing without audio")
|
||||
}
|
||||
}
|
||||
|
||||
// Use a maximum of 3 attempts to load assets
|
||||
var gameInstance *game.Game
|
||||
var loadErr error
|
||||
maxAttempts := 3
|
||||
|
||||
for attempt := 1; attempt <= maxAttempts; attempt++ {
|
||||
gameInstance = game.New()
|
||||
loadErr = gameInstance.LoadAssets()
|
||||
if loadErr == nil {
|
||||
break
|
||||
}
|
||||
|
||||
log.Printf("Attempt %d/%d: Failed to load assets: %v", attempt, maxAttempts, loadErr)
|
||||
if attempt < maxAttempts {
|
||||
log.Println("Retrying...")
|
||||
gameInstance.Cleanup() // Cleanup before retrying
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
if loadErr != nil {
|
||||
log.Printf("Failed to load assets after %d attempts. Starting with default assets.", maxAttempts)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
gameInstance.Cleanup()
|
||||
if gameInstance != nil {
|
||||
gameInstance.Cleanup()
|
||||
}
|
||||
rl.CloseWindow()
|
||||
rl.CloseAudioDevice()
|
||||
if rl.IsAudioDeviceReady() {
|
||||
rl.CloseAudioDevice()
|
||||
}
|
||||
}()
|
||||
|
||||
rl.SetTargetFPS(60)
|
||||
|
||||
rl.PlayMusicStream(gameInstance.Music)
|
||||
rl.SetMusicVolume(gameInstance.Music, 0.5)
|
||||
// Play music if available
|
||||
if gameInstance.Music.Stream.Buffer != nil {
|
||||
rl.PlayMusicStream(gameInstance.Music)
|
||||
rl.SetMusicVolume(gameInstance.Music, 0.5)
|
||||
}
|
||||
|
||||
// Handle OS signals for clean shutdown
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
@ -80,7 +119,11 @@ func main() {
|
||||
// Keep game loop in main thread for Raylib
|
||||
for !rl.WindowShouldClose() {
|
||||
deltaTime := rl.GetFrameTime()
|
||||
rl.UpdateMusicStream(gameInstance.Music)
|
||||
|
||||
// Update music if available
|
||||
if gameInstance.Music.Stream.Buffer != nil {
|
||||
rl.UpdateMusicStream(gameInstance.Music)
|
||||
}
|
||||
|
||||
func() {
|
||||
defer func() {
|
||||
|
||||
Reference in New Issue
Block a user