Created
November 23, 2017 15:36
-
-
Save jakelacey2012/c32af488449e0c37e0e600f298eb849a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
build:
go build
start:
make build && ./game