-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (96 loc) · 2.15 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"runtime"
"strings"
_ "net/http/pprof"
"github.com/kkevinchou/kito/kito"
"github.com/kkevinchou/kito/kito/settings"
"github.com/veandco/go-sdl2/sdl"
)
func init() {
// We want to lock the main thread to this goroutine. Otherwise,
// SDL rendering will randomly panic
//
// For more details: https://github.com/golang/go/wiki/LockOSThread
runtime.LockOSThread()
}
const (
modeLocal string = "LOCAL"
modeClient string = "CLIENT"
modeServer string = "SERVER"
)
type Game interface {
Start()
}
func main() {
// memory ballast
ballast := make([]byte, 1<<34)
_ = ballast
configFile, err := os.Open("config.json")
if err != nil {
fmt.Printf("failed to load config.json, using defaults: %s\n", err)
} else {
configBytes, err := io.ReadAll(configFile)
if err != nil {
fmt.Println(err)
return
}
if err = configFile.Close(); err != nil {
fmt.Println(err)
return
}
var configSettings Config
err = json.Unmarshal(configBytes, &configSettings)
if err != nil {
fmt.Println(err)
return
}
loadConfig(configSettings)
}
var mode string = modeClient
if len(os.Args) > 1 {
mode = strings.ToUpper(os.Args[1])
if mode != modeLocal && mode != modeClient && mode != modeServer {
panic(fmt.Sprintf("unexpected mode %s", mode))
}
}
if settings.PProfEnabled {
go func() {
if mode == modeClient {
log.Println(http.ListenAndServe(fmt.Sprintf("localhost:%d", settings.PProfClientPort), nil))
} else {
log.Println(http.ListenAndServe(fmt.Sprintf("localhost:%d", settings.PProfServerPort), nil))
}
}()
}
fmt.Println("starting game on mode:", mode)
var game Game
if mode == modeClient {
game = kito.NewClientGame("_assets", "shaders")
} else if mode == modeServer {
game = kito.NewServerGame("_assets")
}
game.Start()
sdl.Quit()
}
func loadConfig(c Config) {
settings.Host = c.ServerIP
settings.Port = c.ServerPort
settings.Width = c.Width
settings.Height = c.Height
settings.Fullscreen = c.Fullscreen
}
type Config struct {
ServerIP string
ServerPort int
Mode string
Width int
Height int
Fullscreen bool
}