-
Notifications
You must be signed in to change notification settings - Fork 0
/
goGame32.go
362 lines (287 loc) · 9.29 KB
/
goGame32.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
package main
import (
"fmt"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"github.com/pilzhere/GoGame32/screen"
"github.com/pilzhere/GoGame32/screen/menuScreen"
"github.com/pilzhere/GoGame32/screen/playScreen"
"github.com/pilzhere/GoGame32/utils"
"strconv"
"time"
)
var (
frameTick *time.Ticker
now time.Time
dt float64
oneSecondTimer float64
fps int32
canvasFbo *pixelgl.Canvas
windowScale float64
windowScaleOld float64
canvasFboWidth float64
canvasFboHeight float64
windowOriginalPosX float64
windowOriginalPosY float64
windowIsFullscreen = false
windowedWindowOldScale float64
screens []screen.Screener
currentScreen screen.Screener
assetsMan utils.AssetsManager
windowRepositionTimer float64
windowRepositionToggle bool
spriteBatch *pixel.Batch
T = 0.0
DT = 0.01
currentTime = time.Now()
accumulator = 0.0
sdt = 1.0
)
const (
windowOriginalWidth float64 = 426
windowOriginalHeight float64 = 240
fpsLimit int = 60 // FIXME Game is designed for 60 fps ONLY. Increasing or lowering this will alter the physics (mostly jumps) of the game (there is no interpolation in the code).
)
func main() {
pixelgl.Run(run)
}
func run() {
windowScale = 1
canvasFboWidth = windowOriginalWidth
canvasFboHeight = windowOriginalHeight
// Create window configuration
cfg := pixelgl.WindowConfig{
Title: "GoGame32",
Bounds: pixel.R(0, 0, windowOriginalWidth*windowScale, windowOriginalHeight*windowScale), // Same height as a NES game.
VSync: false, // 75 fps on my screen.
Resizable: true,
Maximized: false,
}
// Create window using configuration
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
// Save original window positions for when resetting window scale to 1.
windowOriginalPosX = win.GetPos().X
windowOriginalPosY = win.GetPos().Y
setFPS(fpsLimit)
canvasFbo = pixelgl.NewCanvas(pixel.R(0, 0, canvasFboWidth, canvasFboHeight))
// Load assets
bg01, _ := utils.LoadPicture("assets/testBg.png")
tilesheet01, _ := utils.LoadPicture("assets/tilesheet01.png")
block01, _ := utils.LoadPicture("assets/block.png")
assetsMan = utils.AssetsManager{
PicTilesheet01: tilesheet01,
PicBlock01: block01,
Bg01: bg01,
}
/*spriteBatch = pixel.Batch{}
spriteBatch.MakePicture(tilesheet01)
spriteBatch.MakeTriangles(&pixel.TrianglesData{})
spriteBatch.SetMatrix(pixel.IM)
spriteBatch.SetColorMask(color.Alpha{A: 1})*/
spriteBatch = pixel.NewBatch(&pixel.TrianglesData{}, bg01)
//fmt.Println("bathc: ", spriteBatch)
PushScreen(menuScreen.NewMenuScreen(spriteBatch, &assetsMan, win))
for !win.Closed() {
dt = calculateDeltaTime()
/*if dt > 1/60.0 {
sdt = dt / (1/60.0)
} else {
sdt = 1.0
}*/
if windowRepositionToggle {
windowRepositionTimer += dt
if windowRepositionTimer > 1/24 { // wait 0.0416 ms.
windowRepositionTimer = 0
windowRepositionToggle = false
}
}
// Input
if win.JustPressed(pixelgl.KeyEscape) {
exitGame(win)
return // exit while loop
}
if win.JustPressed(pixelgl.KeyP) {
fmt.Println("Pressed P")
if !windowRepositionToggle {
if !windowIsFullscreen {
windowScaleOld = windowScale
if windowScale != 5 {
windowScale++
resizeWindowedWindow(win)
}
}
}
}
if win.JustPressed(pixelgl.KeyM) {
fmt.Println("Pressed M")
if !windowRepositionToggle {
if !windowIsFullscreen {
windowScaleOld = windowScale
if windowScale != 1 {
windowScale--
resizeWindowedWindow(win)
}
}
}
}
if win.JustPressed(pixelgl.KeyE) {
fmt.Println("Pressed E")
spriteBatch = pixel.NewBatch(&pixel.TrianglesData{}, tilesheet01)
PushScreen(playScreen.NewPlayScreen(spriteBatch, &assetsMan, win))
}
if win.JustPressed(pixelgl.KeyQ) {
fmt.Println("Pressed Q")
PopScreen()
}
//fmt.Println(len(screens))
if win.JustPressed(pixelgl.KeyF) {
fmt.Println("Pressed F")
if !windowIsFullscreen {
monitorWidth, monitorHeight := pixelgl.PrimaryMonitor().Size()
windowedWindowOldScale = windowScale
win.SetBounds(pixel.R(0, 0, monitorWidth, monitorHeight))
win.SetMonitor(pixelgl.PrimaryMonitor())
} else {
windowScale = windowedWindowOldScale
win.SetMonitor(nil) // To use current monitor.
win.SetBounds(pixel.R(0, 0, windowOriginalWidth*windowScale, windowOriginalHeight*windowScale))
}
windowIsFullscreen = !windowIsFullscreen
}
oneSecondTimer += dt
if oneSecondTimer >= 1 { // Has one second passed?
//fmt.Printf("fps: %d dt: %f ms\n", fps, dt)
strDt := strconv.FormatFloat(dt, 'f', 6, 64)
strFps := strconv.FormatInt(int64(fps), 10)
var title = "GoGame32 fps: " + strFps + " dt: " + strDt + " ms"
win.SetTitle(title)
fps = 0
oneSecondTimer = 0 // Reset timer
}
currentScreen.HandleInput(dt)
fmt.Println("-new tick-")
currentScreen.Tick(dt)
for accumulator >= DT {
//previousState = currentState;
//integrate( currentState, t, dt )
T += DT
accumulator -= dt
}
//alpha := accumulator / DT
var canvasBounds = pixel.Rect{
Min: pixel.Vec{},
Max: pixel.Vec{X: windowOriginalWidth, Y: windowOriginalHeight},
}
win.Canvas().SetBounds(canvasBounds) // Scale down window canvas
canvasFbo.SetBounds(canvasBounds) // Scale down FBO canvas
currentScreen.Render(dt)
if !windowIsFullscreen {
canvasBounds = pixel.Rect{
Min: pixel.Vec{},
Max: pixel.Vec{X: windowOriginalWidth * windowScale, Y: windowOriginalHeight * windowScale},
}
} else {
width, height := pixelgl.PrimaryMonitor().Size()
canvasBounds = pixel.Rect{
Min: pixel.Vec{},
Max: pixel.Vec{X: width, Y: height},
}
}
canvasFbo.SetPixels(win.Canvas().Pixels()) // Paint CanvasFBO with window's canvas pixels.
win.Canvas().SetBounds(canvasBounds) // Update window's canvas size. Else we can't paint CanvasFBO on top of it.
//win.Clear(colornames.Brown) // test
// Move canvasFBO to middle of the window's bounds.
var canvasFboPoint = pixel.Vec{}
if !windowIsFullscreen {
// Render CanvasFBO in middle, scaled up to window's bounds.
moveWidth := -win.Bounds().W()/2*(win.Bounds().W()/canvasFboWidth) + canvasFboWidth*windowScale/2
moveHeight := -win.Bounds().H()/2*(win.Bounds().H()/canvasFboHeight) + canvasFboHeight*windowScale/2
var canvasFboMove = pixel.Vec{X: moveWidth, Y: moveHeight}
canvasFbo.Draw(win, pixel.IM.Moved(win.Canvas().Bounds().Center()).Scaled(canvasFboPoint, windowScale).Moved(canvasFboMove))
} else {
// Render CanvasFBO in bottom-left corner, scaled up to monitor's/window's bounds.
monitorWidth, monitorHeight := pixelgl.PrimaryMonitor().Size()
xPos := win.Canvas().Bounds().Min.X + canvasFboWidth/2
yPos := win.Canvas().Bounds().Min.Y + canvasFboHeight/2
pos := pixel.Vec{X: xPos, Y: yPos}
canvasFbo.Draw(win, pixel.IM.Moved(pos).ScaledXY(canvasFboPoint, pixel.Vec{X: monitorWidth / canvasFboWidth, Y: monitorHeight / canvasFboHeight}))
}
win.Update() // Frame is finished, swap!
if frameTick != nil {
<-frameTick.C
}
fps++
}
}
func PushScreen(screen screen.Screener) {
screens = append(screens, screen) // push screen on top
currentScreen = screens[len(screens)-1] // currentScreen is top
}
func PopScreen() {
if len(screens) > 1 {
currentScreen.Destroy()
top := len(screens) - 1
screens = (screens)[:top]
currentScreen = screens[len(screens)-1] // currentScreen is top
}
}
func PopAllScreens() {
fmt.Println("Popping all", len(screens), "screens")
for len(screens) > 0 {
currentScreen.Destroy()
top := len(screens) - 1
screens = (screens)[:top]
if len(screens) != 0 {
currentScreen = screens[len(screens)-1] // currentScreen is top
} else {
currentScreen = nil
}
}
}
func resizeWindowedWindow(window *pixelgl.Window) {
newBounds := pixel.R(0, 0, windowOriginalWidth*windowScale, windowOriginalHeight*windowScale)
window.SetBounds(newBounds)
var newPos pixel.Vec
if windowScale != 1 {
if windowScaleOld < windowScale {
newPos = pixel.Vec{X: window.GetPos().X - (newBounds.W()/windowScale)/2, Y: window.GetPos().Y - (windowOriginalHeight * windowScale / (windowScale * 2))}
} else {
newPos = pixel.Vec{X: window.GetPos().X + (newBounds.W()/windowScale)/2, Y: window.GetPos().Y + (windowOriginalHeight * windowScale / (windowScale * 2))}
}
} else {
newPos = pixel.Vec{X: windowOriginalPosX, Y: windowOriginalPosY}
}
window.SetPos(newPos)
windowRepositionToggle = true
}
func setFPS(fps int) {
if fps <= 0 {
frameTick = nil
} else {
frameTick = time.NewTicker(time.Second / time.Duration(fps))
}
}
func calculateDeltaTime() float64 {
last := now
now = time.Now()
deltaTime := now.Sub(last).Seconds()
return deltaTime
}
func calculateAlpha() float64 {
newTime := time.Now()
frameTime := newTime.Sub(time.Now()).Seconds()
if frameTime > 0.25 {
frameTime = 0.25
}
currentTime = newTime
accumulator += frameTime
return 0
}
func exitGame(window *pixelgl.Window) {
PopAllScreens()
window.SetClosed(true)
fmt.Println("Game exiting... Thanks for playing! <3")
}