-
Notifications
You must be signed in to change notification settings - Fork 0
/
monoverse.go
193 lines (158 loc) · 3.4 KB
/
monoverse.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
package main
import (
"github.com/veandco/go-sdl2/sdl"
"sync"
"time"
)
type B bool
type F1 float64
type F2 struct {
X float64
Y float64
}
type F3 struct {
X float64
Y float64
Z float64
}
type Monoverse struct {
graphics *Graphics
grid Grid
views []View
running bool
}
func (m *Monoverse) AddView(v View) {
if v != nil {
v.Configure()
m.views = append(m.views, v)
}
}
func (m *Monoverse) Configure() {
w := 1920.0
h := 1080.0
m.grid = Grid{
size: F2{w, h},
cell: F2{1920 / 12, 28},
}
m.graphics, _ = NewGraphics(F3{w, h, 0})
world := NewWorld("Verse", 512, m.grid.GetLocation(F2{0, 0}), m.grid.GetSize(F2{10, 36}))
world.Populate()
m.AddView(world)
// list := NewList("Scene Statistics", m.grid.GetLocation(F2{10, 30}), m.grid.GetSize(F2{2, 7}))
// verse.stats = &list
// m.AddView(&list)
//
// physics := &Physics{
// location: m.grid.GetLocation(F2{10, 6}),
// size: m.grid.GetSize(F2{2, 24}),
// }
// verse.physics = physics
// m.AddView(physics)
//
// timer := &Timer{
// bounds: m.grid.GetBounds(F2{0, 33}, F2{10, 4}),
// }
// verse.timer = timer
// m.AddView(timer)
widget := &fpsWidget{
location: m.grid.GetLocation(F2{10, 0}),
size: m.grid.GetSize(F2{2, 6}),
updateDelta: 0,
renderDelta: 0,
}
m.AddView(widget)
}
// type Timer interface {
// Configure()
// TickCommence(float64)
// TickConclude(float64)
// }
type UpdateClock struct {
inner float64
innerDelta float64
outer float64
outerDelta float64
ticks float64
history []time.Duration
ready bool
}
func (u *UpdateClock) TickCommence(now float64) {
if u.ready {
u.ready = false
} else {
u.TickConclude(now)
}
u.outerDelta = now - u.outer
u.inner = now
}
func (u *UpdateClock) TickConclude(now float64) {
u.innerDelta = now - u.inner
u.outer = now
u.ticks += 1.0
u.ready = true
}
func (u *UpdateClock) GetUpsPerSecond() float64 {
return u.outerDelta
}
func (u *UpdateClock) Configure() {
}
func (m *Monoverse) Run() {
m.Configure()
m.running = true
updateClock := 0.0
updateInterval := 1000.0 / 120.0 // ms
renderInterval := 1000.0 / 120.0 // ms
renderClock := 0.0
delta := 0.0
prevTime := 0.0
for m.running {
delta = float64(sdl.GetTicks()) - prevTime
prevTime = float64(sdl.GetTicks())
updateClock += delta
wg := new(sync.WaitGroup)
// Since the duration of the update cycle can range from nanoseconds to hours or days,
// so instead of a fixed frame rate, we run this for loop for as long as a rendered frame is not due.
if updateClock > updateInterval {
wg.Add(len(m.views))
for _, view := range m.views {
go func(v View) {
v.Update()
wg.Done()
}(view)
}
wg.Wait()
updateClock -= updateInterval
}
renderClock = float64(sdl.GetTicks()) - prevTime
for renderClock > renderInterval {
m.HandleEvents()
m.graphics.Clear()
for _, view := range m.views {
m.graphics.RenderView(view)
}
m.graphics.Render()
renderClock = 0
}
}
}
func (m *Monoverse) HandleEvents() {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
for _, view := range m.views {
view.HandleEvent(event)
}
switch t := event.(type) {
case *sdl.QuitEvent:
m.running = false
break
case *sdl.MouseButtonEvent:
case *sdl.MouseMotionEvent:
break
case *sdl.MouseWheelEvent:
case *sdl.WindowEvent:
if t.Type == sdl.WINDOWEVENT_RESIZED {
// Some day
}
break
}
}
}