-
Notifications
You must be signed in to change notification settings - Fork 0
/
hlg.go
206 lines (167 loc) · 3.96 KB
/
hlg.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
package hlg
import (
"fmt"
"image/color"
"runtime"
"time"
"github.com/dfirebaugh/hlg/graphics"
"github.com/dfirebaugh/hlg/graphics/webgpu"
"github.com/dfirebaugh/hlg/pkg/input"
)
type Game interface {
Update()
Render()
}
var (
windowWidth = 240
windowHeight = 160
)
type engine struct {
graphicsBackend graphics.GraphicsBackend
inputState *input.InputState
windowTitle string
fpsCounter *fpsCounter
hasSetupCompleted bool
}
var hlg = &engine{}
func setup() {
runtime.LockOSThread()
hlg.inputState = input.NewInputState()
var err error
hlg.graphicsBackend, err = webgpu.NewGraphicsBackend(windowWidth, windowHeight)
if err != nil {
panic(err.Error())
}
hlg.hasSetupCompleted = true
}
func initWindow() {
SetWindowSize(windowWidth, windowHeight)
SetTitle("hlg")
}
func ensureSetupCompletion() {
if hlg.hasSetupCompleted {
return
}
setup()
initWindow()
}
func run(updateFn func(), renderFn func()) {
ensureSetupCompletion()
defer close()
hlg.fpsCounter = newFPSCounter()
hlg.graphicsBackend.SetInputCallback(func(eventChan chan input.Event) {
evt := <-eventChan
handleEvent(evt, hlg.inputState)
})
targetFPS := 120.0
targetFrameDuration := time.Second / time.Duration(targetFPS)
var lastUpdateTime time.Time
var accumulator time.Duration
lastUpdateTime = time.Now()
for hlg.graphicsBackend.PollEvents() {
currentTime := time.Now()
deltaTime := currentTime.Sub(lastUpdateTime)
lastUpdateTime = currentTime
accumulator += deltaTime
frameRendered := false
for accumulator >= targetFrameDuration {
if updateFn != nil {
updateFn()
}
accumulator -= targetFrameDuration
frameRendered = true
hlg.inputState.ResetJustPressed()
}
if frameRendered {
hlg.graphicsBackend.Render()
if renderFn != nil {
renderFn()
}
calculateFPS()
}
}
}
func RunGame(game Game) {
run(game.Update, game.Render)
}
func RunApp(game Game) {
run(game.Update, game.Render)
}
// Run is the main update function called to refresh the engine state.
func Run(updateFn func(), renderFn func()) {
run(updateFn, renderFn)
}
func calculateFPS() {
hlg.fpsCounter.Frame()
fps := hlg.fpsCounter.GetFPS()
title := hlg.windowTitle
if fps != 0 && fpsEnabled {
title = fmt.Sprintf("%s -- %d\n", title, int(fps))
}
if hlg.graphicsBackend.IsDisposed() {
return
}
hlg.graphicsBackend.SetWindowTitle(title)
}
func GetFPS() float64 {
return hlg.fpsCounter.GetFPS()
}
func Close() {
close()
}
func close() {
hlg.graphicsBackend.Close()
}
// Clear clears the screen with the specified color.
func Clear(c color.RGBA) {
ensureSetupCompletion()
hlg.graphicsBackend.Clear(c)
}
// SetTitle sets the title of the window.
func SetTitle(title string) {
ensureSetupCompletion()
hlg.windowTitle = title
}
// GetWindowSize retrieves the current window size.
func GetWindowSize() (int, int) {
return hlg.graphicsBackend.GetWindowSize()
}
func GetWindowWidth() int {
w, _ := hlg.graphicsBackend.GetWindowSize()
return w
}
func GetWindowHeight() int {
_, h := hlg.graphicsBackend.GetWindowSize()
return h
}
func GetWindowPosition() (int, int) {
return hlg.graphicsBackend.GetWindowPosition()
}
func GetScreenSize() (int, int) {
ensureSetupCompletion()
return hlg.graphicsBackend.GetScreenSize()
}
// SetScreenSize sets the size of the screen.
func SetScreenSize(width, height int) {
ensureSetupCompletion()
hlg.graphicsBackend.SetScreenSize(width, height)
}
// SetWindowSize sets the size of the window.
func SetWindowSize(width, height int) {
ensureSetupCompletion()
hlg.graphicsBackend.SetScreenSize(width, height)
hlg.graphicsBackend.SetWindowSize(width, height)
windowWidth = width
windowHeight = height
}
func CreateRenderQueue() graphics.RenderQueue {
return hlg.graphicsBackend.CreateRenderQueue()
}
func DisableWindowResize() {
ensureSetupCompletion()
hlg.graphicsBackend.DisableWindowResize()
SetWindowSize(windowWidth, windowHeight)
}
func SetBorderlessWindowed(v bool) {
hlg.graphicsBackend.SetBorderlessWindowed(v)
}