-
Notifications
You must be signed in to change notification settings - Fork 3
/
kero.go
91 lines (71 loc) · 1.91 KB
/
kero.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
package kero
import (
"github.com/galaco/kero/framework/event"
"github.com/galaco/kero/framework/window"
"github.com/galaco/kero/game"
"github.com/galaco/kero/gui"
"github.com/galaco/kero/messages"
"github.com/galaco/kero/middleware"
"github.com/galaco/kero/physics"
"github.com/galaco/kero/renderer"
"github.com/galaco/kero/scene"
"time"
)
// Kero provides a game loop
type Kero struct {
isRunning bool
scene *scene.Scene
physics *physics.PhysicsSystem
input *middleware.Input
renderer *renderer.Renderer
ui *gui.Gui
}
// RegisterGameDefinitions sets up provided game-specific configuration
func (kero *Kero) RegisterGameDefinitions(def game.Definition) {
def.RegisterEntityClasses()
}
// Start runs the game loop
func (kero *Kero) Start() {
middleware.AddInitialConvars()
kero.input = middleware.InitializeInput()
kero.renderer = renderer.NewRenderer()
kero.ui = gui.NewGui()
kero.scene = scene.NewScene()
kero.physics = physics.NewPhysicsSystem()
kero.isRunning = true
kero.physics.Initialize()
kero.scene.Initialize()
kero.renderer.Initialize()
kero.ui.Initialize()
event.Get().AddListener(messages.TypeEngineQuit, kero.onQuit)
kero.mainLoop()
kero.exit()
}
func (kero *Kero) mainLoop() {
dt := 0.0
startingTime := time.Now().UTC()
for kero.isRunning && (window.CurrentWindow() != nil && !window.CurrentWindow().ShouldClose()) {
kero.input.Poll()
kero.physics.Update(dt)
kero.scene.Update(dt)
kero.renderer.Render()
kero.ui.Render()
window.CurrentWindow().SwapBuffers()
kero.renderer.FinishFrame()
dt = float64(time.Now().UTC().Sub(startingTime).Nanoseconds()/1000000) / 1000
startingTime = time.Now().UTC()
}
}
func (kero *Kero) onQuit(e interface{}) {
window.CurrentWindow().Close()
}
func (kero *Kero) exit() {
kero.physics.Cleanup()
kero.renderer.Cleanup()
}
// NewKero returns a new Kero instance
func NewKero() *Kero {
return &Kero{
isRunning: false,
}
}