Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@jakelacey2012 jakelacey2012 commented Nov 23, 2017

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
You can’t perform that action at this time.