forked from vova616/GarageEngine
/
Engine.go
441 lines (362 loc) · 9.34 KB
/
Engine.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package engine
import (
"github.com/go-gl/gl"
//"log"
"github.com/LaPingvino/GarageEngine/engine/bt"
"github.com/LaPingvino/GarageEngine/engine/cr"
"github.com/LaPingvino/GarageEngine/engine/input"
//"os"
"fmt"
"github.com/go-gl/glfw"
"github.com/vova616/chipmunk"
"github.com/vova616/chipmunk/vect"
"math"
"runtime"
"time"
)
func init() {
fmt.Print()
}
const (
RadianConst = math.Pi / 180
DegreeConst = 180 / math.Pi
MouseTag = "Mouse"
)
var (
Inf = vect.Float(math.Inf(1))
scenes []Scene = make([]Scene, 0)
activeScenes []Scene = make([]Scene, 0)
mainScene Scene
nextScene Scene = nil
running = false
insideGameloop = false
Space *chipmunk.Space = nil
deltaTime float64
fixedTime float64
gameTime time.Time
steps = float64(1)
stepTime = float64(1) / float64(60) / steps
maxPhysicsTime = float64(1) / float64(40)
lastTime time.Time = time.Now()
CorrectWrongPhysics = true
EnablePhysics = true
Debug = false
InternalFPS = float64(100)
internalFPSObject *FPS
BehaviorTicks = 5
windowTitle = "Engine Test"
Width = 1280
Height = 720
terminated chan bool
)
func LoadScene(scene Scene) {
if insideGameloop {
nextScene = scene
return
}
ResourceManager.Release()
bt.Clear()
cr.Clear()
if Space != nil {
for _, g := range mainScene.SceneBase().gameObjects {
if g != nil {
g.Destroy()
}
}
iter(&mainScene.SceneBase().gameObjects, destoyGameObject)
mainScene.SceneBase().gameObjects = nil
Space.Destory()
runtime.GC()
Space = chipmunk.NewSpace()
} else {
Space = chipmunk.NewSpace()
}
input.ClearInput()
sn := scene.New()
mainScene = sn
sn.Load()
internalFPSGObject := NewGameObject("InternalFPS")
internalFPSObject = NewFPS()
internalFPSGObject.AddComponent(internalFPSObject)
}
func GetScene() Scene {
return mainScene
}
func CurrentCamera() *Camera {
return mainScene.SceneBase().Camera
}
func AddScene(scene Scene) {
scenes = append(scenes, scene)
}
func Terminate() {
glfw.Terminate()
}
func DeltaTime() float64 {
return deltaTime
}
func GameTime() time.Time {
return gameTime
}
func SetTitle(title string) {
glfw.SetWindowTitle(title)
windowTitle = title
}
func Title() string {
return windowTitle
}
func StartEngine() {
runtime.GOMAXPROCS(runtime.NumCPU())
runtime.LockOSThread()
fmt.Println("Enginge started!")
var err error
if err = glfw.Init(); err != nil {
panic(err)
}
fmt.Println("GLFW Initialized!")
glfw.OpenWindowHint(glfw.Accelerated, 1)
if err = glfw.OpenWindow(Width, Height, 8, 8, 8, 8, 8, 8, glfw.Windowed); err != nil {
panic(err)
}
glfw.SetSwapInterval(1) //0 to disable vsync, 1 to enable it
glfw.SetWindowTitle(windowTitle)
glfw.SetWindowSizeCallback(onResize)
glfw.SetKeyCallback(input.OnKey)
glfw.SetCharCallback(input.OnChar)
glfw.SetMouseButtonCallback(input.ButtonPress)
glfw.SetMouseWheel(0)
glfw.SetMouseWheelCallback(input.MouseWheelCallback)
input.MouseWheelPosition = glfw.MouseWheel
input.MousePosition = glfw.MousePos
if err = initGL(); err != nil {
panic(err)
}
fmt.Println("Opengl Initialized!")
TextureMaterial = NewBasicMaterial(spriteVertexShader, spriteFragmentShader)
err = TextureMaterial.Load()
if err != nil {
fmt.Println(err)
}
SDFMaterial = NewBasicMaterial(sdfVertexShader, sdfFragmentShader)
err = SDFMaterial.Load()
if err != nil {
fmt.Println(err)
}
internalMaterial = NewBasicMaterial(spriteVertexShader, spriteFragmentShader)
err = internalMaterial.Load()
if err != nil {
fmt.Println(err)
}
initDefaultPlane()
glfw.SwapBuffers()
gameTime = time.Time{}
lastTime = time.Now()
dl = glfw.Time()
}
func MainLoop() bool {
running = true
if nextScene != nil {
s := nextScene
nextScene = nil
LoadScene(s)
}
insideGameloop = true
if running && glfw.WindowParam(glfw.Opened) == 1 {
Run()
} else {
return false
}
insideGameloop = false
return true
}
var dd = float64(0)
var dl = float64(0)
func Run() {
timeNow := time.Now()
gameTime = gameTime.Add(timeNow.Sub(lastTime))
//deltaTime = float64(timeNow.Sub(lastTime).Nanoseconds()) / float64(time.Second)
lastTime = timeNow
before := timeNow
dn := glfw.Time()
dd = dn - dl
dl = dn
deltaTime = dd
timer := NewTimer()
timer.Start()
CurrentCamera().Clear()
var destroyDelta,
startDelta,
fixedUpdateDelta,
physicsDelta,
updateDelta,
lateUpdateDelta,
drawDelta,
coroutinesDelta,
stepDelta,
behaviorDelta,
startPhysicsDelta,
endPhysicsDelta time.Duration
if mainScene != nil {
d := deltaTime
if d > maxPhysicsTime {
d = maxPhysicsTime
}
fixedTime += d
sd := mainScene.SceneBase()
arr := &sd.gameObjects
timer.StartCustom("Destory routines")
iter(arr, destoyGameObject)
destroyDelta = timer.StopCustom("Destory routines")
//Better to not do it every frame.
mainScene.SceneBase().cleanNil()
timer.StartCustom("Start routines")
iter(arr, startGameObject)
startDelta = timer.StopCustom("Start routines")
//
timer.StartCustom("Physics time")
if EnablePhysics {
timer.StartCustom("Physics step time")
for fixedTime >= stepTime {
timer.StartCustom("FixedUpdate routines")
iter(arr, fixedUdpateGameObject)
fixedUpdateDelta = timer.StopCustom("FixedUpdate routines")
timer.StartCustom("PreStep Physics Delta")
iter(arr, preStepGameObject)
startPhysicsDelta = timer.StopCustom("PreStep Physics Delta")
Space.Step(vect.Float(stepTime))
fixedTime -= stepTime
_ = timer.StopCustom("Physics step time")
timer.StartCustom("PostStep Physics Delta")
iter(arr, postStepGameObject)
endPhysicsDelta = timer.StopCustom("PostStep Physics Delta")
}
if fixedTime > 0 && fixedTime < stepTime {
iter(arr, interpolateGameObject)
}
}
physicsDelta = timer.StopCustom("Physics time")
timer.StartCustom("Update routines")
internalFPSObject.Update()
iter(arr, udpateGameObject)
updateDelta = timer.StopCustom("Update routines")
timer.StartCustom("LateUpdate routines")
iter(arr, lateudpateGameObject)
lateUpdateDelta = timer.StopCustom("LateUpdate routines")
timer.StartCustom("Draw routines")
depthMap.Iter(drawGameObject)
drawDelta = timer.StopCustom("Draw routines")
timer.StartCustom("coroutines")
cr.Run()
coroutinesDelta = timer.StopCustom("coroutines")
timer.StartCustom("BehaviorTree")
bt.Run(BehaviorTicks)
behaviorDelta = timer.StopCustom("BehaviorTree")
input.UpdateInput()
stepDelta = timer.Stop()
}
timer.StartCustom("SwapBuffers")
glfw.SwapBuffers()
swapBuffersDelta := timer.StopCustom("SwapBuffers")
now := time.Now()
deltaDur := now.Sub(before)
if Debug {
fmt.Println()
fmt.Println("##################")
if InternalFPS < 20 {
fmt.Println("FPS is lower than 20. FPS:", InternalFPS)
} else if InternalFPS < 30 {
fmt.Println("FPS is lower than 30. FPS:", InternalFPS)
} else if InternalFPS < 40 {
fmt.Println("FPS is lower than 40. FPS:", InternalFPS)
}
if stepDelta > 17*time.Millisecond {
fmt.Println("StepDelta time is lower than normal")
}
fmt.Println("Debugging Times:")
if (deltaDur.Nanoseconds() / int64(time.Millisecond)) != 0 {
fmt.Println("Expected FPS", 1000/(deltaDur.Nanoseconds()/int64(time.Millisecond)))
}
fmt.Println("Step time", stepDelta)
fmt.Println("Destroy time", destroyDelta)
fmt.Println("Start time", startDelta)
fmt.Println("FixedUpdate time", fixedUpdateDelta)
fmt.Println("Update time", updateDelta)
fmt.Println("LateUpdate time", lateUpdateDelta)
fmt.Println("Draw time", drawDelta)
fmt.Println("Delta time", deltaDur, deltaTime)
fmt.Println("SwapBuffers time", swapBuffersDelta)
fmt.Println("Coroutines time", coroutinesDelta)
fmt.Println("BehaviorTree time", behaviorDelta)
fmt.Println("------------------")
fmt.Println("Physics time:", physicsDelta)
fmt.Println("PreStepDelta time", startPhysicsDelta)
fmt.Println("PostStepDelta time", endPhysicsDelta)
fmt.Println("StepTime time", Space.StepTime)
fmt.Println("ApplyImpulse time", Space.ApplyImpulsesTime)
fmt.Println("ReindexQueryTime time", Space.ReindexQueryTime)
fmt.Println("Arbiters", len(Space.Arbiters))
fmt.Println("##################")
fmt.Println()
}
}
func initGL() (err error) {
gl.Init()
gl.ShadeModel(gl.SMOOTH)
gl.ClearColor(0, 0, 0, 0)
gl.ClearDepth(1)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST)
gl.DepthFunc(gl.NEVER)
gl.Enable(gl.BLEND)
gl.DepthMask(true)
//loadShader()
return
}
func drawQuad(srcwidth, destwidth, srcheight, destheight float32) {
gl.Begin(gl.QUADS)
gl.TexCoord2i(0, 0)
gl.Vertex2f(-1, -1)
gl.TexCoord2i(int(srcwidth), 0)
gl.Vertex2f(-1+destwidth, -1)
gl.TexCoord2i(int(srcwidth), int(srcheight))
gl.Vertex2f(-1+destwidth, -1+destheight)
gl.TexCoord2i(0, int(srcheight))
gl.Vertex2f(-1, -1+destheight)
gl.End()
}
func onResize(w, h int) {
if h <= 0 {
h = 1
}
if w <= 0 {
w = 1
}
Height = h
Width = w
gl.Viewport(0, 0, w, h)
if GetScene() != nil && CurrentCamera() != nil {
CurrentCamera().UpdateResolution()
}
}
func PanicPath() string {
fullPath := ""
skip := 3
for i := skip; ; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
if i > skip {
fullPath += ", "
}
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
file = short
fullPath += fmt.Sprintf("%s:%d", file, line)
}
return fullPath
}