CreateWindow creates a window with the specified parameters
(title string, width, height int, fullscreen bool, msaa int)
| 35 | |
| 36 | // CreateWindow creates a window with the specified parameters |
| 37 | func CreateWindow(title string, width, height int, fullscreen bool, msaa int) { |
| 38 | rafPolyfill() |
| 39 | CurrentBackEnd = BackEndWeb |
| 40 | canvas = document.Call("createElement", "canvas") |
| 41 | |
| 42 | devicePixelRatio = js.Global().Get("devicePixelRatio").Float() |
| 43 | canvas.Set("width", int(float64(width)+0.5)) // Nearest non-negative int. |
| 44 | canvas.Set("height", int(float64(height)+0.5)) // Nearest non-negative int. |
| 45 | |
| 46 | if document.Get("body").IsNull() { |
| 47 | document.Set("body", document.Call("createElement", "body")) |
| 48 | } |
| 49 | body := document.Get("body") |
| 50 | |
| 51 | body.Get("style").Set("margin", "0") |
| 52 | body.Get("style").Set("padding", "0") |
| 53 | |
| 54 | canvas.Call("setAttribute", "tabindex", 1) |
| 55 | canvas.Get("style").Set("outline", "none") |
| 56 | body.Call("appendChild", canvas) |
| 57 | |
| 58 | document.Set("title", title) |
| 59 | |
| 60 | Gl, _ = gl.NewContext(canvas, nil) |
| 61 | |
| 62 | Gl.GetExtension("OES_texture_float") |
| 63 | |
| 64 | gameWidth = float32(width) |
| 65 | gameHeight = float32(height) |
| 66 | windowWidth = WindowWidth() |
| 67 | windowHeight = WindowHeight() |
| 68 | |
| 69 | ResizeXOffset = gameWidth - CanvasWidth() |
| 70 | ResizeYOffset = gameHeight - CanvasHeight() |
| 71 | |
| 72 | canvas.Call("addEventListener", "keypress", js.FuncOf(func(this js.Value, args []js.Value) interface{} { |
| 73 | // TODO: Not sure what to do here, come back |
| 74 | //ke := ev.(*dom.KeyboardEvent) |
| 75 | //responser.Type(rune(keyStates[Key(ke.KeyCode)])) |
| 76 | return nil |
| 77 | })) |
| 78 | |
| 79 | canvas.Call("addEventListener", "keydown", js.FuncOf(func(this js.Value, args []js.Value) interface{} { |
| 80 | event := args[0] |
| 81 | ke := event.Get("code") // TODO: preventdefault if it's a special key so it doesn't scroll when you press it! |
| 82 | if ke.Type() == js.TypeUndefined { |
| 83 | kc := event.Get("keyCode").Int() |
| 84 | go func(i int) { |
| 85 | pollLock.Lock() |
| 86 | poll[i] = true |
| 87 | pollLock.Unlock() |
| 88 | }(kc) |
| 89 | k := Key(kc) |
| 90 | if k == KeyArrowUp || k == KeyArrowDown || k == KeyArrowLeft || k == KeyArrowRight || k == KeyTab || k == KeyBackspace || k == KeySpace { |
| 91 | event.Call("preventDefault") |
| 92 | } |
| 93 | return nil |
| 94 | } |
no test coverage detected