-
Notifications
You must be signed in to change notification settings - Fork 6
/
golf.go
481 lines (417 loc) · 10.5 KB
/
golf.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package golf
import (
"math"
"syscall/js"
)
// Engine Screen Width and Height
const (
ScreenHeight = 192
ScreenWidth = 192
)
// Engine is the golf engine
type Engine struct {
RAM *[0xFFFF]byte
screenBufHook js.Value
Draw func()
Update func()
}
//go:generate ../generate/genTemplates packedTemplates.go templates golf
// NewEngine creates a new golf engine
func NewEngine(update func(), draw func()) *Engine {
ret := Engine{
RAM: &[0xFFFF]byte{},
Draw: draw,
Update: update,
}
doc := js.Global().Get("document")
ret.initKeyListener(doc)
ret.initMouseListener(js.Global().Get("golfcanvas"))
ret.RClip() // Reset the cliping box
// Set internal resources
base := internalSpriteBase
for i := 0; i < 0x0900; i++ {
ret.RAM[i+base] = internalSpriteSheet[i]
}
ret.RAM[startAnim] = 255
ret.PalA(0)
ret.PalB(1)
// Inject the nessisary JS
script := doc.Call("createElement", "script")
script.Set("innerHTML", string(drawTemplate[:]))
doc.Get("body").Call("appendChild", script)
// Hook into the injected js
ret.screenBufHook = js.Global().Get("screenBuff")
return &ret
}
// Run starts the game engine running
func (e *Engine) Run() {
var renderFrame js.Func
renderFrame = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
e.addFrame()
if e.Frames() < int(e.RAM[startAnim]) {
e.startupAnim()
} else {
e.Update()
e.Draw()
e.drawMouse()
e.tickKeyboard()
e.tickMouse()
}
js.CopyBytesToJS(e.screenBufHook, e.RAM[:screenPalSet+1])
js.Global().Call("drawScreen")
js.Global().Call("requestAnimationFrame", renderFrame)
return nil
})
done := make(chan struct{}, 0)
js.Global().Call("requestAnimationFrame", renderFrame)
<-done
}
// Frames is the number of frames since the engine was started
func (e *Engine) Frames() int {
return toInt(e.RAM[frames:frames+3], false)
}
func (e *Engine) addFrame() {
f := toInt(e.RAM[frames:frames+3], false)
f++
b := toBytes(f, 3, false)
e.RAM[frames] = b[0]
e.RAM[frames+1] = b[1]
e.RAM[frames+2] = b[2]
}
// DrawMouse sets the draw style
// 0 = none
// 1 = mouse
// 2 = hand
// 3 = cross
func (e *Engine) DrawMouse(style int) {
e.RAM[mouseBase] &= 0b00111111
e.RAM[mouseBase] |= byte(style << 6)
}
// drawMouse draws the mouse on the screen
func (e *Engine) drawMouse() {
e.setActiveSpriteBuff(internalSpriteBase)
cursor := e.RAM[mouseBase] >> 6
opt := SOp{Fixed: true, TCol: Col7}
if cursor == 1 {
e.Spr(18, float64(e.RAM[mouseX]), float64(e.RAM[mouseY]), opt)
}
if cursor == 2 {
e.Spr(50, float64(e.RAM[mouseX]), float64(e.RAM[mouseY]), opt)
}
if cursor == 3 {
e.Spr(82, float64(e.RAM[mouseX]), float64(e.RAM[mouseY]), opt)
}
e.setActiveSpriteBuff(spriteBase)
}
// Mouse returns the X, Y coords of the mouse
func (e *Engine) Mouse() (int, int) {
return int(e.RAM[mouseX]), int(e.RAM[mouseY])
}
// Cls fills the screen with col and resets TextL and TextR
func (e *Engine) Cls(col Col) {
textLline, textRline = 0, 0
c := col & 0b00000011
colBG := byte((c << 6) | (c << 4) | (c << 2) | c)
palBG := byte(0)
if col>>2 == 0b00100001 {
palBG = 0b11111111
}
for i := 0; i < screenPalSet; i++ {
e.RAM[i] = colBG
if (i+1)%3 == 0 {
e.RAM[i] = palBG
}
}
}
// Camera moves the camera which modifies all draw functions
func (e *Engine) Camera(x, y int) {
xb := toBytes(x, 2, true)
yb := toBytes(y, 2, true)
e.RAM[cameraX] = xb[0]
e.RAM[cameraX+1] = xb[1]
e.RAM[cameraY] = yb[0]
e.RAM[cameraY+1] = yb[1]
}
// Rect draws a rectangle border on the screen
func (e *Engine) Rect(x, y, w, h float64, col Col, fixed ...bool) {
f := false
if len(fixed) > 0 {
f = fixed[0]
}
if !f {
x -= toFloat(e.RAM[cameraX:cameraX+2], true)
y -= toFloat(e.RAM[cameraY:cameraY+2], true)
}
for r := 0.0; r < w; r++ {
e.Pset(x+r, y, col)
e.Pset(x+r, y+(h-1), col)
}
for c := 0.0; c < h; c++ {
e.Pset(x, y+c, col)
e.Pset(x+(w-1), y+(c), col)
}
}
// RectFill draws a filled rectangle one the screen
func (e *Engine) RectFill(x, y, w, h float64, col Col, fixed ...bool) {
f := false
if len(fixed) > 0 {
f = fixed[0]
}
if !f {
x -= toFloat(e.RAM[cameraX:cameraX+2], true)
y -= toFloat(e.RAM[cameraY:cameraY+2], true)
}
for r := 0.0; r < w; r++ {
for c := 0.0; c < h; c++ {
e.Pset(x+r, y+c, col)
}
}
}
// Line draws a colored line
func (e *Engine) Line(x1, y1, x2, y2 float64, col Col, fixed ...bool) {
f := false
if len(fixed) > 0 {
f = fixed[0]
}
if !f {
x1 -= toFloat(e.RAM[cameraX:cameraX+2], true)
x2 -= toFloat(e.RAM[cameraX:cameraX+2], true)
y1 -= toFloat(e.RAM[cameraY:cameraY+2], true)
y2 -= toFloat(e.RAM[cameraY:cameraY+2], true)
}
dx, dy := x2-x1, y2-y1
step := math.Abs(dy)
if math.Abs(dx) >= math.Abs(dy) {
step = math.Abs(dx)
}
dx = dx / step
dy = dy / step
x, y := x1, y1
for i := 0.0; i < step; i++ {
e.Pset(x, y, col)
x = x + dx
y = y + dy
}
}
// Circ draws a circle using Bresenham's algorithm
func (e *Engine) Circ(xc, yc, r float64, col Col, fixed ...bool) {
f := false
if len(fixed) > 0 {
f = fixed[0]
}
e.circ(xc, yc, r, col, false, f)
}
// CircFill draws a filled circle using Bresenham's algorithm
func (e *Engine) CircFill(xc, yc, r float64, col Col, fixed ...bool) {
f := false
if len(fixed) > 0 {
f = fixed[0]
}
e.circ(xc, yc, r, col, true, f)
}
// drawCirc8 draws 8 points on a circle
func (e *Engine) drawCirc8(xc, yc, x, y float64, c Col, filled bool, fixed bool) {
if filled {
e.Line(xc+x, yc+y, xc+x, yc-y, c, fixed)
e.Line(xc-x, yc+y, xc-x, yc-y, c, fixed)
e.Line(xc+y, yc+x, xc+y, yc-x, c, fixed)
e.Line(xc-y, yc+x, xc-y, yc-x, c, fixed)
}
if !fixed {
xc -= toFloat(e.RAM[cameraX:cameraX+2], true)
yc -= toFloat(e.RAM[cameraY:cameraY+2], true)
}
e.Pset(xc+x, yc+y, c)
e.Pset(xc-x, yc+y, c)
e.Pset(xc+x, yc-y, c)
e.Pset(xc-x, yc-y, c)
e.Pset(xc+y, yc+x, c)
e.Pset(xc-y, yc+x, c)
e.Pset(xc+y, yc-x, c)
e.Pset(xc-y, yc-x, c)
}
func (e *Engine) circ(xc, yc, r float64, c Col, filled bool, fixed bool) {
if r == 0 {
return
}
x, y := 0.0, r
d := 3 - 2*r
e.drawCirc8(xc, yc, x, y, c, filled, fixed)
for y >= x {
x++
if d > 0 {
y--
d = d + 4*(x-y) + 10
} else {
d = d + 4*x + 6
}
e.drawCirc8(xc, yc, x, y, c, filled, fixed)
}
}
// Clip clips all functions that draw to the screen
func (e *Engine) Clip(x, y, w, h int) {
e.RAM[clipX] = byte(x)
e.RAM[clipY] = byte(y)
e.RAM[clipW] = byte(w)
e.RAM[clipH] = byte(h)
}
// RClip resets the screen cliping
func (e *Engine) RClip() {
e.RAM[clipX] = 0
e.RAM[clipY] = 0
e.RAM[clipW] = 192
e.RAM[clipH] = 192
}
// sets a pixel in abitrary memory
// buffBase is the start of the pixel buffer in memory
// pxlWidth is the width of the pixel buffer in pixels
func (e *Engine) pset(x, y float64, col Col, buffBase, pxlWidth int) {
ix, iy := int(x), int(y)
i := ix + iy*pxlWidth
index := int(float64(i/4) / 2 * 3)
pIndex := index + (2 - index%3)
cshift := (ix % 4) * 2
pshift := ix % 8
color := byte(col&0b00000011) << cshift
pallet := byte(col&0b00000100) >> 2 << pshift
e.RAM[buffBase+index] &= (0b00000011 << cshift) ^ 0b11111111
e.RAM[buffBase+index] |= color
e.RAM[buffBase+pIndex] &= (0b00000001 << pshift) ^ 0b11111111
e.RAM[buffBase+pIndex] |= pallet
}
// Pset sets a pixel on the screen
func (e *Engine) Pset(x, y float64, col Col) {
if x < float64(e.RAM[clipX]) || x >= float64(e.RAM[clipX]+e.RAM[clipW]) ||
y < float64(e.RAM[clipY]) || y >= float64(e.RAM[clipY]+e.RAM[clipH]) {
return
}
e.pset(x, y, col, 0, 192)
}
// pget gets a pixel from abitrary memory
// buffBase is the start of the memory buffer
// pxlWidth is the width of the buffer in pixels
func (e *Engine) pget(x, y float64, buffBase, pxlWidth int) Col {
ix, iy := int(x), int(y)
i := ix + iy*pxlWidth
index := int(float64(i/4) / 2 * 3)
pIndex := index + (2 - index%3)
cshift := (ix % 4) * 2
pshift := ix % 8
color := (e.RAM[buffBase+index] >> cshift) & 0b00000011
pallet := (e.RAM[buffBase+pIndex] >> pshift) & 0b00000001
return Col((color | (pallet << 2)) | 0b10000000)
}
// Pget gets the color of a pixel on the screen
func (e *Engine) Pget(x, y float64) Col {
if x < 0 || x > 192 || y < 0 || y >= 192 {
return Col0
}
return e.pget(x, y, 0, 192)
}
// PalA sets pallet A
func (e *Engine) PalA(pallet Pal) {
e.RAM[screenPalSet] &= 0b00001111
e.RAM[screenPalSet] |= byte(pallet << 4)
}
// PalB sets pallet B
func (e *Engine) PalB(pallet Pal) {
e.RAM[screenPalSet] &= 0b11110000
e.RAM[screenPalSet] |= byte(pallet)
}
// PalGet gets the currently set pallets
func (e *Engine) PalGet() (Pal, Pal) {
return Pal(e.RAM[screenPalSet] >> 4), Pal(e.RAM[screenPalSet] & 0b00001111)
}
// Col is a screen color
type Col byte
// These are the pallet and color constants
const (
Col0 = Col(0b10000000)
Col1 = Col(0b10000001)
Col2 = Col(0b10000010)
Col3 = Col(0b10000011)
Col4 = Col(0b10000100)
Col5 = Col(0b10000101)
Col6 = Col(0b10000110)
Col7 = Col(0b10000111)
)
func color(pixel byte) Col {
pixel &= 0b00000011
pixel |= 0b10000000
return Col(pixel)
}
// Pal is a screen pallet
type Pal byte
// The list of all pallets
const (
Pal0 = Pal(0b00000000)
Pal1 = Pal(0b00000001)
Pal2 = Pal(0b00000010)
Pal3 = Pal(0b00000011)
Pal4 = Pal(0b00000100)
Pal5 = Pal(0b00000101)
Pal6 = Pal(0b00000110)
Pal7 = Pal(0b00000111)
Pal8 = Pal(0b00001000)
Pal9 = Pal(0b00001001)
Pal10 = Pal(0b00001010)
Pal11 = Pal(0b00001011)
Pal12 = Pal(0b00001100)
Pal13 = Pal(0b00001101)
Pal14 = Pal(0b00001110)
Pal15 = Pal(0b00001111)
)
// toInt converts a byte array to an integer
// byte arrays from len 1 to 4 are supported
func toInt(b []byte, signed bool) int {
pad := []byte{0, 0, 0, 0}
l := len(b)
neg := false
if signed && b[0] > 127 {
neg = true
}
for i := 0; i < 4; i++ {
if l-i-1 > -1 {
pad[3-i] = b[l-i-1]
}
}
if neg {
pad[4-len(b)] &= 0b01111111
}
ret := int(pad[0])<<24 | int(pad[1])<<16 | int(pad[2])<<8 | int(pad[3])
if neg {
return ret * -1
}
return ret
}
// toFloat converts a byte arra to float64
// byte arrays from len 1 to 4 are supported
func toFloat(b []byte, signed bool) float64 {
return float64(toInt(b, signed))
}
// toBytes converts an integer into a byte array
// signed bytes do not use twos complement
func toBytes(i int, l int, signed bool) []byte {
neg := false
if signed && i < 0 {
i *= -1
neg = true
}
ret := []byte{}
if l == 1 {
ret = []byte{byte(i)}
}
if l == 2 {
ret = []byte{byte(i >> 8), byte(i)}
}
if l == 3 {
ret = []byte{byte(i >> 16), byte(i >> 8), byte(i)}
}
if l == 4 {
ret = []byte{byte(i >> 24), byte(i >> 16), byte(i >> 8), byte(i)}
}
if neg {
ret[0] |= 0b10000000
}
return ret
}