glfw.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. if action == glfw.Repeat {
  77. w.Dispatch(OnKeyRepeat, &w.keyEv)
  78. return
  79. }
  80. })
  81. // Set char callback
  82. win.SetCharModsCallback(func(x *glfw.Window, char rune, mods glfw.ModifierKey) {
  83. w.charEv.W = w
  84. w.charEv.Char = char
  85. w.charEv.Mods = ModifierKey(mods)
  86. w.Dispatch(OnChar, &w.charEv)
  87. })
  88. // Set mouse button callback to dispatch event
  89. win.SetMouseButtonCallback(func(x *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
  90. xpos, ypos := x.GetCursorPos()
  91. w.mouseEv.W = w
  92. w.mouseEv.Button = MouseButton(button)
  93. w.mouseEv.Action = Action(action)
  94. w.mouseEv.Mods = ModifierKey(mods)
  95. w.mouseEv.Xpos = float32(xpos)
  96. w.mouseEv.Ypos = float32(ypos)
  97. if action == glfw.Press {
  98. w.Dispatch(OnMouseDown, &w.mouseEv)
  99. return
  100. }
  101. if action == glfw.Release {
  102. w.Dispatch(OnMouseUp, &w.mouseEv)
  103. return
  104. }
  105. })
  106. // Set window size callback to dispatch event
  107. win.SetSizeCallback(func(x *glfw.Window, width int, height int) {
  108. w.sizeEv.W = w
  109. w.sizeEv.Width = width
  110. w.sizeEv.Height = height
  111. w.Dispatch(OnWindowSize, &w.sizeEv)
  112. })
  113. // Set window position event callback to dispatch event
  114. win.SetPosCallback(func(x *glfw.Window, xpos int, ypos int) {
  115. w.posEv.W = w
  116. w.posEv.Xpos = xpos
  117. w.posEv.Ypos = ypos
  118. w.Dispatch(OnWindowPos, &w.posEv)
  119. })
  120. // Set window cursor position event callback to dispatch event
  121. win.SetCursorPosCallback(func(x *glfw.Window, xpos float64, ypos float64) {
  122. w.cursorEv.W = w
  123. w.cursorEv.Xpos = float32(xpos)
  124. w.cursorEv.Ypos = float32(ypos)
  125. w.Dispatch(OnCursor, &w.cursorEv)
  126. })
  127. // Set mouse wheel scroll event callback to dispatch event
  128. win.SetScrollCallback(func(x *glfw.Window, xoff float64, yoff float64) {
  129. w.scrollEv.W = w
  130. w.scrollEv.Xoffset = float32(xoff)
  131. w.scrollEv.Yoffset = float32(yoff)
  132. w.Dispatch(OnScroll, &w.scrollEv)
  133. })
  134. // Preallocate standard cursors
  135. w.arrowCursor = glfw.CreateStandardCursor(int(glfw.ArrowCursor))
  136. w.ibeamCursor = glfw.CreateStandardCursor(int(glfw.IBeamCursor))
  137. w.crosshairCursor = glfw.CreateStandardCursor(int(glfw.CrosshairCursor))
  138. w.handCursor = glfw.CreateStandardCursor(int(glfw.HandCursor))
  139. w.hresizeCursor = glfw.CreateStandardCursor(int(glfw.HResizeCursor))
  140. w.vresizeCursor = glfw.CreateStandardCursor(int(glfw.VResizeCursor))
  141. return w, nil
  142. }
  143. // GetScreenResolution returns the resolution of the primary screen in pixels.
  144. // The parameter is currently ignored
  145. func (w *GLFW) GetScreenResolution(p interface{}) (width, height int) {
  146. mon := glfw.GetPrimaryMonitor()
  147. vmode := mon.GetVideoMode()
  148. return vmode.Width, vmode.Height
  149. }
  150. func (w *GLFW) SwapInterval(interval int) {
  151. glfw.SwapInterval(interval)
  152. }
  153. func (w *GLFW) MakeContextCurrent() {
  154. w.win.MakeContextCurrent()
  155. }
  156. func (w *GLFW) GetSize() (width int, height int) {
  157. return w.win.GetSize()
  158. }
  159. func (w *GLFW) SetSize(width int, height int) {
  160. w.win.SetSize(width, height)
  161. }
  162. func (w *GLFW) GetPos() (xpos, ypos int) {
  163. return w.win.GetPos()
  164. }
  165. func (w *GLFW) SetPos(xpos, ypos int) {
  166. w.win.SetPos(xpos, ypos)
  167. }
  168. func (w *GLFW) SetTitle(title string) {
  169. w.win.SetTitle(title)
  170. }
  171. func (w *GLFW) SetStandardCursor(cursor StandardCursor) {
  172. switch cursor {
  173. case ArrowCursor:
  174. w.win.SetCursor(w.arrowCursor)
  175. case IBeamCursor:
  176. w.win.SetCursor(w.ibeamCursor)
  177. case CrosshairCursor:
  178. w.win.SetCursor(w.crosshairCursor)
  179. case HandCursor:
  180. w.win.SetCursor(w.handCursor)
  181. case HResizeCursor:
  182. w.win.SetCursor(w.hresizeCursor)
  183. case VResizeCursor:
  184. w.win.SetCursor(w.vresizeCursor)
  185. default:
  186. panic("Invalid cursor")
  187. }
  188. }
  189. func (w *GLFW) ShouldClose() bool {
  190. return w.win.ShouldClose()
  191. }
  192. func (w *GLFW) SetShouldClose(v bool) {
  193. w.win.SetShouldClose(v)
  194. }
  195. func (w *GLFW) SwapBuffers() {
  196. w.win.SwapBuffers()
  197. }
  198. func (w *GLFW) PollEvents() {
  199. glfw.PollEvents()
  200. }
  201. func (w *GLFW) GetTime() float64 {
  202. return glfw.GetTime()
  203. }