84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
|
|
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/config"
|
|
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/game"
|
|
"gitea.wisellama.rocks/Wisellama/gosimpleconf"
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
)
|
|
|
|
func main() {
|
|
// Setup some initial context for gracefully killing
|
|
// the program with system interrupt signals like ctrl+c
|
|
// https://pace.dev/blog/2020/02/17/repond-to-ctrl-c-interrupt-signals-gracefully-with-context-in-golang-by-mat-ryer.html
|
|
ctx := context.Background()
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
signalChan := make(chan os.Signal, 1)
|
|
signal.Notify(signalChan, os.Interrupt)
|
|
defer func() {
|
|
signal.Stop(signalChan)
|
|
cancel()
|
|
}()
|
|
go func() {
|
|
select {
|
|
case <-signalChan:
|
|
// Graceful exit on ctrl+c
|
|
log.Printf("Attempting to exit gracefully...\n")
|
|
cancel()
|
|
case <-ctx.Done():
|
|
}
|
|
<-signalChan // Hard exit on second ctrl+c
|
|
log.Printf("Hard kill\n")
|
|
os.Exit(2)
|
|
}()
|
|
|
|
// Run the program
|
|
err := run(ctx)
|
|
if err != nil {
|
|
log.Printf("%v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
log.Printf("Exited gracefully!\n")
|
|
}
|
|
|
|
func run(ctx context.Context) error {
|
|
var err error
|
|
|
|
// Read configuration
|
|
configMap, err := config.Configure("game.conf")
|
|
if err != nil {
|
|
log.Fatalf("error during configure: %v\n", err)
|
|
}
|
|
|
|
// Setup logging
|
|
writeToFile := gosimpleconf.Bool(configMap["log.writeToFile"])
|
|
utcTime := gosimpleconf.Bool(configMap["log.utcTime"])
|
|
logFilename := configMap["log.file"]
|
|
logWriter, err := config.SetupLogging(writeToFile, utcTime, logFilename)
|
|
if err != nil {
|
|
log.Fatalf("error setting up logging: %v\n", err)
|
|
}
|
|
defer logWriter.Cleanup()
|
|
|
|
// Setup Random Number Generator seed
|
|
config.InitRNG()
|
|
|
|
// Start everything with the SDL goroutine context
|
|
log.Printf("=== Starting %v ===", configMap["game.title"])
|
|
sdl.Main(func() {
|
|
err = game.Run(ctx, configMap)
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|