-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbanana.go
242 lines (198 loc) · 4.92 KB
/
banana.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
package banana
import (
"fmt"
"image/color"
"runtime"
"time"
"github.com/dfirebaugh/banana/graphics"
"github.com/dfirebaugh/banana/graphics/opengl"
"github.com/dfirebaugh/banana/pkg/input"
"github.com/go-gl/gl/v4.6-core/gl"
)
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 banana = &engine{}
func setup() {
runtime.LockOSThread()
banana.inputState = input.NewInputState()
var err error
gb, err := opengl.NewGraphicsBackend(windowWidth, windowHeight)
if err != nil {
panic(err.Error())
}
banana.graphicsBackend = gb
banana.hasSetupCompleted = true
}
func initWindow() {
SetWindowSize(windowWidth, windowHeight)
SetTitle("banana")
}
func ensureSetupCompletion() {
if banana.hasSetupCompleted {
return
}
setup()
initWindow()
}
func run(updateFn func(), renderFn func()) {
ensureSetupCompletion()
defer close()
banana.fpsCounter = newFPSCounter()
// width, height := GetWindowSize()
// gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
// gl.Viewport(0, 0, int32(width), int32(height))
banana.graphicsBackend.SetInputCallback(func(eventChan chan input.Event) {
evt := <-eventChan
handleEvent(evt, banana.inputState)
})
targetFPS := 120.0
targetFrameDuration := time.Second / time.Duration(targetFPS)
var lastUpdateTime time.Time
var accumulator time.Duration
lastUpdateTime = time.Now()
for banana.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
}
if frameRendered {
if renderFn != nil {
renderFn()
}
calculateFPS()
banana.graphicsBackend.Draw()
banana.graphicsBackend.SwapBuffers()
banana.inputState.ResetJustPressed()
}
}
}
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() {
banana.fpsCounter.Frame()
fps := banana.fpsCounter.GetFPS()
title := banana.windowTitle
if fps != 0 && fpsEnabled {
title = fmt.Sprintf("%s -- %d\n", title, int(fps))
}
if banana.graphicsBackend.IsDisposed() {
return
}
banana.graphicsBackend.SetWindowTitle(title)
}
func GetFPS() float64 {
return banana.fpsCounter.GetFPS()
}
func Close() {
close()
}
func close() {
banana.graphicsBackend.Close()
}
func Draw() {
ensureSetupCompletion()
banana.graphicsBackend.Draw()
}
// Clear clears the screen with the specified color.
func Clear(c color.Color) {
ensureSetupCompletion()
banana.graphicsBackend.Clear(c)
}
// SetTitle sets the title of the window.
func SetTitle(title string) {
ensureSetupCompletion()
banana.windowTitle = title
}
// GetWindowSize retrieves the current window size.
func GetWindowSize() (int, int) {
ensureSetupCompletion()
return banana.graphicsBackend.GetWindowSize()
}
func GetWindowWidth() int {
ensureSetupCompletion()
w, _ := banana.graphicsBackend.GetWindowSize()
return w
}
func GetWindowHeight() int {
ensureSetupCompletion()
_, h := banana.graphicsBackend.GetWindowSize()
return h
}
func GetWindowPosition() (int, int) {
ensureSetupCompletion()
return banana.graphicsBackend.GetWindowPosition()
}
func GetViewportSize() (int, int) {
ensureSetupCompletion()
return banana.graphicsBackend.GetViewportSize()
}
func SetWindowPosition(x, y int) {
ensureSetupCompletion()
banana.graphicsBackend.SetWindowPosition(x, y)
}
func SetWindowSize(width, height int) {
ensureSetupCompletion()
banana.graphicsBackend.SetWindowSize(width, height)
windowWidth = width
windowHeight = height
}
func DisableWindowResize() {
ensureSetupCompletion()
banana.graphicsBackend.DisableWindowResize()
SetWindowSize(windowWidth, windowHeight)
}
func SetBorderlessWindowed(v bool) {
ensureSetupCompletion()
banana.graphicsBackend.SetBorderlessWindowed(v)
}
func SetFullScreenBorderless(v bool) {
ensureSetupCompletion()
banana.graphicsBackend.SetFullScreenBorderless(v)
}
func SetResizeCallback(fn func(physicalWidth, physicalHeight uint32)) {
ensureSetupCompletion()
banana.graphicsBackend.SetResizedCallback(fn)
}
func BindFramebuffer(fb graphics.Framebuffer) {
ensureSetupCompletion()
banana.graphicsBackend.BindFramebuffer(fb)
}
func UnbindFramebuffer() {
ensureSetupCompletion()
banana.graphicsBackend.UnbindFramebuffer()
windowWidth, windowHeight := GetWindowSize()
Viewport(0, 0, int32(windowWidth), int32(windowHeight))
}
func Viewport(x int32, y int32, width int32, height int32) {
ensureSetupCompletion()
gl.Viewport(x, y, width, height)
}