Skip to content

Instantly share code, notes, and snippets.

@jakelacey2012
Created November 23, 2017 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakelacey2012/c32af488449e0c37e0e600f298eb849a to your computer and use it in GitHub Desktop.
Save jakelacey2012/c32af488449e0c37e0e600f298eb849a to your computer and use it in GitHub Desktop.
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()
}
@jakelacey2012
Copy link
Author

build:
go build

start:
make build && ./game

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment