Guest User

Untitled

a guest
Nov 25th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "runtime"
  6.  
  7. "github.com/go-gl/gl/v2.1/gl"
  8. "github.com/go-gl/glfw/v3.2/glfw"
  9. )
  10.  
  11. // create on key thing which would be used to mutate the state
  12.  
  13. func onKey(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
  14. fmt.Println(key) // this would mutate state telling us what keys are being pressed
  15. fmt.Println(action)
  16. if key == glfw.KeyEscape && action == glfw.Press {
  17. w.SetShouldClose(true)
  18. }
  19. }
  20.  
  21. func loop(w *glfw.Window) bool {
  22. if w.ShouldClose() {
  23. return true
  24. }
  25.  
  26. // gl.ClearColor(2, 0.5, 2.0, 1.0)
  27.  
  28. gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
  29. w.SwapBuffers()
  30. glfw.PollEvents()
  31.  
  32. // logic get new state
  33. // do drawing
  34.  
  35. return loop(w)
  36. }
  37.  
  38. func openWindow() {
  39.  
  40. runtime.LockOSThread()
  41.  
  42. if err := glfw.Init(); err != nil {
  43. panic(fmt.Errorf("could not initialize glfw: %v", err))
  44. }
  45.  
  46. glfw.WindowHint(glfw.ContextVersionMajor, 4)
  47. glfw.WindowHint(glfw.ContextVersionMinor, 1)
  48. glfw.WindowHint(glfw.Resizable, glfw.True)
  49. glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
  50. glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
  51.  
  52. win, err := glfw.CreateWindow(800, 600, "Hello world", nil, nil)
  53.  
  54. win.SetKeyCallback(onKey)
  55.  
  56. if err != nil {
  57. panic(fmt.Errorf("could not create opengl renderer: %v", err))
  58. }
  59.  
  60. win.MakeContextCurrent()
  61.  
  62. if err := gl.Init(); err != nil {
  63. panic(err)
  64. }
  65.  
  66. // recursive function which takes old state creates new and loops until w *glfw.Window is not true
  67.  
  68. loop(win) // we would also pass through the initial state here to
  69.  
  70. }
  71.  
  72. func main() {
  73. openWindow()
  74. }
Add Comment
Please, Sign In to add comment