forked from EngoEngine/engo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engo_js.go
354 lines (299 loc) · 9.43 KB
/
engo_js.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
//+build netgo
package engo
import (
"bytes"
"fmt"
"io"
"log"
"math"
"net/http"
"strconv"
"strings"
"sync"
"time"
"engo.io/gl"
"github.com/gopherjs/gopherjs/js"
"honnef.co/go/js/dom"
"honnef.co/go/js/xhr"
)
var (
// Gl is the current OpenGL context
Gl *gl.Context
devicePixelRatio float64
poll = make(map[int]bool)
pollLock sync.Mutex
document dom.HTMLDocument
)
// CreateWindow creates a window with the specified parameters
func CreateWindow(title string, width, height int, fullscreen bool, msaa int) {
document = dom.GetWindow().Document().(dom.HTMLDocument)
rafPolyfill()
CurrentBackEnd = BackEndWeb
canvas := document.CreateElement("canvas").(*dom.HTMLCanvasElement)
devicePixelRatio = js.Global.Get("devicePixelRatio").Float()
canvas.Width = int(float64(width) + 0.5) // Nearest non-negative int.
canvas.Height = int(float64(height) + 0.5) // Nearest non-negative int.
canvas.Style().SetProperty("width", fmt.Sprintf("%vpx", width), "")
canvas.Style().SetProperty("height", fmt.Sprintf("%vpx", height), "")
log.Println("devicePixelRatio", devicePixelRatio)
if document.Body() == nil {
js.Global.Get("document").Set("body", js.Global.Get("document").Call("createElement", "body"))
log.Println("Creating body, since it doesn't exist.")
}
document.Body().Style().SetProperty("margin", "0", "")
document.Body().AppendChild(canvas)
document.SetTitle(title)
var err error
Gl, err = gl.NewContext(canvas.Underlying(), nil) // TODO: we can add arguments here
if err != nil {
log.Println("Could not create context:", err)
return
}
fmt.Println("Gl.Viewport(0, 0,", width, ",", height, ")")
Gl.GetExtension("OES_texture_float")
// DEBUG: Add framebuffer information div.
if false {
//canvas.Height -= 30
text := document.CreateElement("div")
textContent := fmt.Sprintf("%v %v (%v %v %v) @%v", dom.GetWindow().InnerWidth(), canvas.Width, float64(width)*devicePixelRatio, GameWidth(), CanvasWidth(), devicePixelRatio)
text.SetTextContent(textContent)
document.Body().AppendChild(text)
}
gameWidth = float32(width)
gameHeight = float32(height)
windowWidth = WindowWidth()
windowHeight = WindowHeight()
ResizeXOffset = gameWidth - CanvasWidth()
ResizeYOffset = gameHeight - CanvasHeight()
w := dom.GetWindow()
w.AddEventListener("keypress", false, func(ev dom.Event) {
// TODO: Not sure what to do here, come back
//ke := ev.(*dom.KeyboardEvent)
//responser.Type(rune(keyStates[Key(ke.KeyCode)]))
})
w.AddEventListener("keydown", false, func(ev dom.Event) {
ke := ev.(*dom.KeyboardEvent)
go func(i int) {
pollLock.Lock()
poll[i] = true
pollLock.Unlock()
}(ke.KeyCode)
})
w.AddEventListener("keyup", false, func(ev dom.Event) {
ke := ev.(*dom.KeyboardEvent)
go func(i int) {
pollLock.Lock()
poll[i] = false
pollLock.Unlock()
}(ke.KeyCode)
})
w.AddEventListener("mousemove", false, func(ev dom.Event) {
mm := ev.(*dom.MouseEvent)
Input.Mouse.X = float32(float64(mm.ClientX)) / opts.GlobalScale.X
Input.Mouse.Y = float32(float64(mm.ClientY)) / opts.GlobalScale.Y
//Mouse.Action = MOVE
})
w.AddEventListener("mousedown", false, func(ev dom.Event) {
mm := ev.(*dom.MouseEvent)
Input.Mouse.X = float32(float64(mm.ClientX)) / opts.GlobalScale.X
Input.Mouse.Y = float32(float64(mm.ClientY)) / opts.GlobalScale.Y
Input.Mouse.Action = Press
})
w.AddEventListener("mouseup", false, func(ev dom.Event) {
mm := ev.(*dom.MouseEvent)
Input.Mouse.X = float32(float64(mm.ClientX)) / opts.GlobalScale.X
Input.Mouse.Y = float32(float64(mm.ClientY)) / opts.GlobalScale.Y
Input.Mouse.Action = Release
})
}
// DestroyWindow handles destroying the window when done
func DestroyWindow() {}
// CursorPos returns the current cursor position
func CursorPos() (x, y float32) {
return Input.Mouse.X * opts.GlobalScale.X, Input.Mouse.Y * opts.GlobalScale.Y
}
// SetTitle changes the title of the page to the given string
func SetTitle(title string) {
document.SetTitle(title)
}
// WindowSize returns the width and height of the current window
func WindowSize() (w, h int) {
w = int(WindowWidth())
h = int(WindowHeight())
return
}
// WindowWidth returns the current window width
func WindowWidth() float32 {
return float32(dom.GetWindow().InnerWidth())
}
// WindowHeight returns the current window height
func WindowHeight() float32 {
return float32(dom.GetWindow().InnerHeight())
}
// CanvasWidth returns the current canvas width
func CanvasWidth() float32 {
flt, err := strconv.ParseFloat(document.Body().GetElementsByTagName("canvas")[0].GetAttribute("width"), 32)
if err != nil {
log.Println("[ERROR] [CanvasWidth]:", err)
}
return float32(flt)
}
// CanvasHeight returns the current canvas height
func CanvasHeight() float32 {
flt, err := strconv.ParseFloat(document.Body().GetElementsByTagName("canvas")[0].GetAttribute("height"), 32)
if err != nil {
log.Println("[ERROR] [CanvasHeight]:", err)
}
return float32(flt)
}
func CanvasScale() float32 {
return 1
}
func toPx(n int) string {
return strconv.FormatInt(int64(n), 10) + "px"
}
func rafPolyfill() {
window := js.Global
vendors := []string{"ms", "moz", "webkit", "o"}
if window.Get("requestAnimationFrame") == nil {
for i := 0; i < len(vendors) && window.Get("requestAnimationFrame") == nil; i++ {
vendor := vendors[i]
window.Set("requestAnimationFrame", window.Get(vendor+"RequestAnimationFrame"))
window.Set("cancelAnimationFrame", window.Get(vendor+"CancelAnimationFrame"))
if window.Get("cancelAnimationFrame") == nil {
window.Set("cancelAnimationFrame", window.Get(vendor+"CancelRequestAnimationFrame"))
}
}
}
lastTime := 0.0
if window.Get("requestAnimationFrame") == nil {
window.Set("requestAnimationFrame", func(callback func(float32)) int {
currTime := js.Global.Get("Date").New().Call("getTime").Float()
timeToCall := math.Max(0, 16-(currTime-lastTime))
id := window.Call("setTimeout", func() { callback(float32(currTime + timeToCall)) }, timeToCall)
lastTime = currTime + timeToCall
return id.Int()
})
}
if window.Get("cancelAnimationFrame") == nil {
window.Set("cancelAnimationFrame", func(id int) {
js.Global.Get("clearTimeout").Invoke(id)
})
}
}
// RunIteration runs one iteration per frame
func RunIteration() {
Time.Tick()
Input.update()
jsPollKeys()
currentUpdater.Update(Time.Delta())
Input.Mouse.Action = Neutral
// TODO: this may not work, and sky-rocket the FPS
// requestAnimationFrame(func(dt float32) {
// currentWorld.Update(Time.Delta())
// keysUpdate()
// if !headless {
// // TODO: does this require !headless?
// Mouse.ScrollX, Mouse.ScrollY = 0, 0
// }
// Time.Tick()
// })
}
// jsPollKeys polls the keys collected by the javascript callback
// this ensures the keys only get updated once per frame, since the
// callback has no information about the frames and is invoked several
// times between frames. This makes Input.Button.JustPressed and JustReleased
// able to return true properly.
func jsPollKeys() {
pollLock.Lock()
defer pollLock.Unlock()
for key, state := range poll {
Input.keys.Set(Key(key), state)
delete(poll, key)
}
}
func requestAnimationFrame(callback func(float32)) int {
//return dom.GetWindow().RequestAnimationFrame(callback)
return js.Global.Call("requestAnimationFrame", callback).Int()
}
func cancelAnimationFrame(id int) {
dom.GetWindow().CancelAnimationFrame(id)
}
// RunPreparation is called automatically when calling Open. It should only be called once.
func RunPreparation() {
Time = NewClock()
if !opts.HeadlessMode {
dom.GetWindow().AddEventListener("onbeforeunload", false, func(e dom.Event) {
dom.GetWindow().Alert("You're closing")
})
}
}
func runLoop(defaultScene Scene, headless bool) {
SetScene(defaultScene, false)
RunPreparation()
ticker := time.NewTicker(time.Duration(int(time.Second) / opts.FPSLimit))
// Start tick, minimize the delta
Time.Tick()
Outer:
for {
select {
case <-ticker.C:
if closeGame {
break Outer
}
RunIteration()
case <-resetLoopTicker:
ticker.Stop()
ticker = time.NewTicker(time.Duration(int(time.Second) / opts.FPSLimit))
}
}
ticker.Stop()
}
func openFile(url string) (io.ReadCloser, error) {
req := xhr.NewRequest("GET", url)
req.ResponseType = xhr.ArrayBuffer
if err := req.Send(""); err != nil {
return nil, err
}
if req.Status != http.StatusOK {
return nil, fmt.Errorf("unable to open resource (%s), expected HTTP status %d but got %d", url, http.StatusOK, req.Status)
}
buffer := bytes.NewBuffer(js.Global.Get("Uint8Array").New(req.Response).Interface().([]byte))
return noCloseReadCloser{buffer}, nil
}
type noCloseReadCloser struct {
r io.Reader
}
func (n noCloseReadCloser) Close() error { return nil }
func (n noCloseReadCloser) Read(p []byte) (int, error) {
return n.r.Read(p)
}
// SetCursor changes the cursor
func SetCursor(c Cursor) {
switch c {
case CursorNone:
document.Body().Style().Set("cursor", "default")
case CursorHand:
document.Body().Style().Set("cursor", "hand")
}
}
//SetCursorVisibility sets the visibility of the cursor.
//If true the cursor is visible, if false the cursor is not.
func SetCursorVisibility(visible bool) {
if visible {
document.Body().Style().Set("cursor", "default")
} else {
document.Body().Style().Set("cursor", "none")
}
}
// IsAndroidChrome tells if the browser is Chrome for android
func IsAndroidChrome() bool {
ua := js.Global.Get("navigator").Get("userAgent").String()
if !strings.Contains(ua, "Android") {
return false
}
if !strings.Contains(ua, "Chrome") {
return false
}
return true
}