/
main.go
208 lines (193 loc) · 5.75 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"fmt"
gl "github.com/chsc/gogl/gl21"
"github.com/runningwild/glop/gin"
"github.com/runningwild/glop/gos"
"github.com/runningwild/glop/gui"
"github.com/runningwild/glop/render"
"github.com/runningwild/glop/system"
g2 "github.com/runningwild/jota/gui"
"time"
// "math"
"github.com/runningwild/cgf"
_ "github.com/runningwild/jota/ability"
_ "github.com/runningwild/jota/ability/control_point"
_ "github.com/runningwild/jota/ability/creep"
"github.com/runningwild/jota/base"
_ "github.com/runningwild/jota/effects"
"github.com/runningwild/jota/game"
_ "github.com/runningwild/jota/script"
_ "image/jpeg"
_ "image/png"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
)
var (
sys system.System
datadir string
ui *gui.Gui
wdx, wdy int
key_map base.KeyMap
)
func init() {
runtime.LockOSThread()
sys = system.Make(gos.GetSystemInterface())
datadir = filepath.Join(os.Args[0], "..", "..")
base.SetDatadir(datadir)
base.Log().Printf("Setting datadir: %s", datadir)
wdx = 1000
wdy = 800
var key_binds base.KeyBinds
base.LoadJson(filepath.Join(datadir, "key_binds.json"), &key_binds)
fmt.Printf("Prething: %v\n", key_binds)
key_map = key_binds.MakeKeyMap()
base.SetDefaultKeyMap(key_map)
}
func debugHookup(version string) *cgf.Engine {
var err error
for false && len(sys.GetActiveDevices()[gin.DeviceTypeController]) < 2 {
time.Sleep(time.Millisecond * 100)
sys.Think()
}
var engine *cgf.Engine
if version != "host" {
engine, err = cgf.NewClientEngine(17, "thunderingvictory.dyndns.org", 20007, base.EmailCrashReport, base.Log())
if err != nil {
base.Log().Printf("Unable to connect: %v", err)
base.Error().Fatalf("%v", err.Error())
}
} else {
sys.Think()
g := game.MakeGame()
if version == "host" {
engine, err = cgf.NewHostEngine(g, 17, "", 20007, base.EmailCrashReport, base.Log())
if err != nil {
panic(err)
}
err = cgf.Host(20007, "thunderball")
if err != nil {
panic(err)
}
} else {
engine, err = cgf.NewLocalEngine(g, 17, base.EmailCrashReport, base.Log())
}
if err != nil {
base.Error().Fatalf("%v", err.Error())
}
}
engine.Pause()
engine.GetState().(*game.Game).SetSystem(sys)
engine.Unpause()
base.Log().Printf("Engine Id: %v", engine.Id())
base.Log().Printf("All Ids: %v", engine.Ids())
return engine
}
func mainLoop(engine *cgf.Engine, mode string) {
defer engine.Kill()
var profile_output *os.File
var contention_output *os.File
var num_mem_profiles int
// ui.AddChild(base.MakeConsole())
ticker := time.Tick(time.Millisecond * 17)
ui := g2.Make(0, 0, wdx, wdy)
ui.AddChild(&game.GameWindow{Engine: engine, Dims: g2.Dims{wdx - 50, wdy - 50}}, g2.AnchorDeadCenter)
ui.AddChild(g2.MakeConsole(wdx-50, wdy-50), g2.AnchorDeadCenter)
// side0Index := gin.In().BindDerivedKeyFamily("Side0", gin.In().MakeBindingFamily(gin.Key1, []gin.KeyIndex{gin.EitherControl}, []bool{true}))
// side1Index := gin.In().BindDerivedKeyFamily("Side1", gin.In().MakeBindingFamily(gin.Key2, []gin.KeyIndex{gin.EitherControl}, []bool{true}))
// side2Index := gin.In().BindDerivedKeyFamily("Side2", gin.In().MakeBindingFamily(gin.Key3, []gin.KeyIndex{gin.EitherControl}, []bool{true}))
// side0Key := gin.In().GetKeyFlat(side0Index, gin.DeviceTypeAny, gin.DeviceIndexAny)
// side1Key := gin.In().GetKeyFlat(side1Index, gin.DeviceTypeAny, gin.DeviceIndexAny)
// side2Key := gin.In().GetKeyFlat(side2Index, gin.DeviceTypeAny, gin.DeviceIndexAny)
defer ui.StopEventListening()
for {
<-ticker
if gin.In().GetKey(gin.AnyEscape).FramePressCount() != 0 {
return
}
start := time.Now()
sys.Think()
start = time.Now()
render.Queue(func() {
ui.Draw()
})
render.Queue(func() {
start = time.Now()
sys.SwapBuffers()
})
render.Purge()
start = time.Now()
// TODO: Replace the 'P' key with an appropriate keybind
var err error
if gin.In().GetKey(gin.AnyKeyP).FramePressCount() > 0 {
if profile_output == nil {
profile_output, err = os.Create(filepath.Join(datadir, "cpu.prof"))
if err == nil {
err = pprof.StartCPUProfile(profile_output)
if err != nil {
base.Log().Printf("Unable to start CPU profile: %v\n", err)
profile_output.Close()
profile_output = nil
}
base.Log().Printf("cpu prof: %v\n", profile_output)
} else {
base.Log().Printf("Unable to start CPU profile: %v\n", err)
}
} else {
pprof.StopCPUProfile()
profile_output.Close()
profile_output = nil
}
}
if gin.In().GetKey(gin.AnyKeyL).FramePressCount() > 0 {
if contention_output == nil {
contention_output, err = os.Create(filepath.Join(datadir, "contention.prof"))
if err == nil {
runtime.SetBlockProfileRate(1)
base.Log().Printf("contention prof: %v\n", contention_output)
} else {
base.Log().Printf("Unable to start contention profile: %v\n", err)
}
} else {
pprof.Lookup("block").WriteTo(contention_output, 0)
contention_output.Close()
contention_output = nil
}
}
// TODO: Replace the 'M' key with an appropriate keybind
if gin.In().GetKey(gin.AnyKeyM).FramePressCount() > 0 {
f, err := os.Create(filepath.Join(datadir, fmt.Sprintf("mem.%d.prof", num_mem_profiles)))
if err != nil {
base.Error().Printf("Unable to write mem profile: %v", err)
}
pprof.WriteHeapProfile(f)
f.Close()
num_mem_profiles++
}
}
}
func main() {
defer base.StackCatcher()
sys.Startup()
err := gl.Init()
if err != nil {
base.Error().Fatalf("%v", err)
}
render.Init()
render.Queue(func() {
sys.CreateWindow(10, 10, wdx, wdy)
sys.EnableVSync(true)
})
base.InitShaders()
runtime.GOMAXPROCS(10)
sys.Think()
base.LoadAllDictionaries()
if Version() != "standard" {
engine := debugHookup(Version())
mainLoop(engine, "standard")
} else {
// TODO: Reimplement standard hookup
}
}