glfw.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package window
  5. import (
  6. "github.com/g3n/engine/core"
  7. "github.com/go-gl/glfw/v3.2/glfw"
  8. )
  9. type GLFW struct {
  10. core.Dispatcher
  11. win *glfw.Window
  12. keyEv KeyEvent
  13. charEv CharEvent
  14. mouseEv MouseEvent
  15. posEv PosEvent
  16. sizeEv SizeEvent
  17. cursorEv CursorEvent
  18. scrollEv ScrollEvent
  19. arrowCursor *glfw.Cursor
  20. ibeamCursor *glfw.Cursor
  21. crosshairCursor *glfw.Cursor
  22. handCursor *glfw.Cursor
  23. hresizeCursor *glfw.Cursor
  24. vresizeCursor *glfw.Cursor
  25. }
  26. // Global GLFW initialization flag
  27. // is initialized when the first window is created
  28. var initialized bool = false
  29. func newGLFW(width, height int, title string, full bool) (*GLFW, error) {
  30. // Initialize GLFW once before the first window is created
  31. if !initialized {
  32. err := glfw.Init()
  33. if err != nil {
  34. return nil, err
  35. }
  36. // Sets window hints
  37. glfw.WindowHint(glfw.ContextVersionMajor, 3)
  38. glfw.WindowHint(glfw.ContextVersionMinor, 3)
  39. glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
  40. glfw.WindowHint(glfw.Samples, 8)
  41. initialized = true
  42. }
  43. // If full screen requested, get primary monitor and screen size
  44. var mon *glfw.Monitor
  45. if full {
  46. mon = glfw.GetPrimaryMonitor()
  47. vmode := mon.GetVideoMode()
  48. width = vmode.Width
  49. height = vmode.Height
  50. }
  51. // Creates window and sets it as the current context
  52. win, err := glfw.CreateWindow(width, height, title, mon, nil)
  53. if err != nil {
  54. return nil, err
  55. }
  56. win.MakeContextCurrent()
  57. // Create wrapper window with dispacher
  58. w := new(GLFW)
  59. w.win = win
  60. w.Dispatcher.Initialize()
  61. // Set key callback to dispatch event
  62. win.SetKeyCallback(func(x *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
  63. w.keyEv.W = w
  64. w.keyEv.Keycode = Key(key)
  65. w.keyEv.Scancode = scancode
  66. w.keyEv.Action = Action(action)
  67. w.keyEv.Mods = ModifierKey(mods)
  68. if action == glfw.Press {
  69. w.Dispatch(OnKeyDown, &w.keyEv)
  70. return
  71. }
  72. if action == glfw.Release {
  73. w.Dispatch(OnKeyUp, &w.keyEv)
  74. return
  75. }
  76. })
  77. // Set char callback
  78. win.SetCharModsCallback(func(x *glfw.Window, char rune, mods glfw.ModifierKey) {
  79. w.charEv.W = w
  80. w.charEv.Char = char
  81. w.charEv.Mods = ModifierKey(mods)
  82. w.Dispatch(OnChar, &w.charEv)
  83. })
  84. // Set mouse button callback to dispatch event
  85. win.SetMouseButtonCallback(func(x *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
  86. xpos, ypos := x.GetCursorPos()
  87. w.mouseEv.W = w
  88. w.mouseEv.Button = MouseButton(button)
  89. w.mouseEv.Action = Action(action)
  90. w.mouseEv.Mods = ModifierKey(mods)
  91. w.mouseEv.Xpos = float32(xpos)
  92. w.mouseEv.Ypos = float32(ypos)
  93. if action == glfw.Press {
  94. w.Dispatch(OnMouseDown, &w.mouseEv)
  95. return
  96. }
  97. if action == glfw.Release {
  98. w.Dispatch(OnMouseUp, &w.mouseEv)
  99. return
  100. }
  101. })
  102. // Set window size callback to dispatch event
  103. win.SetSizeCallback(func(x *glfw.Window, width int, height int) {
  104. w.sizeEv.W = w
  105. w.sizeEv.Width = width
  106. w.sizeEv.Height = height
  107. w.Dispatch(OnWindowSize, &w.sizeEv)
  108. })
  109. // Set window position event callback to dispatch event
  110. win.SetPosCallback(func(x *glfw.Window, xpos int, ypos int) {
  111. w.posEv.W = w
  112. w.posEv.Xpos = xpos
  113. w.posEv.Ypos = ypos
  114. w.Dispatch(OnWindowPos, &w.posEv)
  115. })
  116. // Set window cursor position event callback to dispatch event
  117. win.SetCursorPosCallback(func(x *glfw.Window, xpos float64, ypos float64) {
  118. w.cursorEv.W = w
  119. w.cursorEv.Xpos = float32(xpos)
  120. w.cursorEv.Ypos = float32(ypos)
  121. w.Dispatch(OnCursor, &w.cursorEv)
  122. })
  123. // Set mouse wheel scroll event callback to dispatch event
  124. win.SetScrollCallback(func(x *glfw.Window, xoff float64, yoff float64) {
  125. w.scrollEv.W = w
  126. w.scrollEv.Xoffset = float32(xoff)
  127. w.scrollEv.Yoffset = float32(yoff)
  128. w.Dispatch(OnScroll, &w.scrollEv)
  129. })
  130. // Preallocate standard cursors
  131. w.arrowCursor = glfw.CreateStandardCursor(int(glfw.ArrowCursor))
  132. w.ibeamCursor = glfw.CreateStandardCursor(int(glfw.IBeamCursor))
  133. w.crosshairCursor = glfw.CreateStandardCursor(int(glfw.CrosshairCursor))
  134. w.handCursor = glfw.CreateStandardCursor(int(glfw.HandCursor))
  135. w.hresizeCursor = glfw.CreateStandardCursor(int(glfw.HResizeCursor))
  136. w.vresizeCursor = glfw.CreateStandardCursor(int(glfw.VResizeCursor))
  137. return w, nil
  138. }
  139. func (w *GLFW) SwapInterval(interval int) {
  140. glfw.SwapInterval(interval)
  141. }
  142. func (w *GLFW) MakeContextCurrent() {
  143. w.win.MakeContextCurrent()
  144. }
  145. func (w *GLFW) GetSize() (width int, height int) {
  146. return w.win.GetSize()
  147. }
  148. func (w *GLFW) SetSize(width int, height int) {
  149. w.win.SetSize(width, height)
  150. }
  151. func (w *GLFW) GetPos() (xpos, ypos int) {
  152. return w.win.GetPos()
  153. }
  154. func (w *GLFW) SetPos(xpos, ypos int) {
  155. w.win.SetPos(xpos, ypos)
  156. }
  157. func (w *GLFW) SetTitle(title string) {
  158. w.win.SetTitle(title)
  159. }
  160. func (w *GLFW) SetStandardCursor(cursor StandardCursor) {
  161. switch cursor {
  162. case ArrowCursor:
  163. w.win.SetCursor(w.arrowCursor)
  164. case IBeamCursor:
  165. w.win.SetCursor(w.ibeamCursor)
  166. case CrosshairCursor:
  167. w.win.SetCursor(w.crosshairCursor)
  168. case HandCursor:
  169. w.win.SetCursor(w.handCursor)
  170. case HResizeCursor:
  171. w.win.SetCursor(w.hresizeCursor)
  172. case VResizeCursor:
  173. w.win.SetCursor(w.vresizeCursor)
  174. default:
  175. panic("Invalid cursor")
  176. }
  177. }
  178. func (w *GLFW) ShouldClose() bool {
  179. return w.win.ShouldClose()
  180. }
  181. func (w *GLFW) SetShouldClose(v bool) {
  182. w.win.SetShouldClose(v)
  183. }
  184. func (w *GLFW) SwapBuffers() {
  185. w.win.SwapBuffers()
  186. }
  187. func (w *GLFW) PollEvents() {
  188. glfw.PollEvents()
  189. }
  190. func (w *GLFW) GetTime() float64 {
  191. return glfw.GetTime()
  192. }