Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "runtime"
- "github.com/go-gl/gl/v2.1/gl"
- "github.com/go-gl/glfw/v3.2/glfw"
- )
- // create on key thing which would be used to mutate the state
- func onKey(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
- fmt.Println(key) // this would mutate state telling us what keys are being pressed
- fmt.Println(action)
- if key == glfw.KeyEscape && action == glfw.Press {
- w.SetShouldClose(true)
- }
- }
- func loop(w *glfw.Window) bool {
- if w.ShouldClose() {
- return true
- }
- // gl.ClearColor(2, 0.5, 2.0, 1.0)
- gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
- w.SwapBuffers()
- glfw.PollEvents()
- // logic get new state
- // do drawing
- return loop(w)
- }
- func openWindow() {
- runtime.LockOSThread()
- if err := glfw.Init(); err != nil {
- panic(fmt.Errorf("could not initialize glfw: %v", err))
- }
- glfw.WindowHint(glfw.ContextVersionMajor, 4)
- glfw.WindowHint(glfw.ContextVersionMinor, 1)
- glfw.WindowHint(glfw.Resizable, glfw.True)
- glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
- glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
- win, err := glfw.CreateWindow(800, 600, "Hello world", nil, nil)
- win.SetKeyCallback(onKey)
- if err != nil {
- panic(fmt.Errorf("could not create opengl renderer: %v", err))
- }
- win.MakeContextCurrent()
- if err := gl.Init(); err != nil {
- panic(err)
- }
- // recursive function which takes old state creates new and loops until w *glfw.Window is not true
- loop(win) // we would also pass through the initial state here to
- }
- func main() {
- openWindow()
- }
Add Comment
Please, Sign In to add comment