added an example for using GLFW

This commit is contained in:
Thomas Friedel 2018-04-30 12:09:57 +02:00
parent 6b36b47b0c
commit 35f6c274a9

74
examples/glfw/glfw.go Normal file
View file

@ -0,0 +1,74 @@
package main
import (
"log"
"runtime"
"github.com/go-gl/gl/v3.2-core/gl"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/tfriedel6/canvas"
"github.com/tfriedel6/canvas/goglimpl"
)
func main() {
runtime.LockOSThread()
// init SDL
err := glfw.Init()
if err != nil {
log.Fatalf("Error initializing GLFW: %v", err)
}
defer glfw.Terminate()
// the stencil size setting is required for the canvas to work
glfw.WindowHint(glfw.StencilBits, 8)
glfw.WindowHint(glfw.DepthBits, 0)
// create window
window, err := glfw.CreateWindow(1280, 720, "GLFW Test", nil, nil)
if err != nil {
log.Fatalf("Error creating window: %v", err)
}
window.MakeContextCurrent()
// init GL
err = gl.Init()
if err != nil {
log.Fatalf("Error initializing GL: %v", err)
}
// set vsync on, enable multisample (if available)
glfw.SwapInterval(1)
gl.Enable(gl.MULTISAMPLE)
// load canvas GL assets
err = canvas.LoadGL(goglimpl.GLImpl{})
if err != nil {
log.Fatalf("Error loading canvas GL assets: %v", err)
}
// initialize canvas with zero size, since size is set in main loop
cv := canvas.New(0, 0, 0, 0)
for !window.ShouldClose() {
window.MakeContextCurrent()
glfw.PollEvents()
// set canvas size
ww, wh := window.GetSize()
cv.SetSize(ww, wh)
// call the run function to do all the drawing
run(cv, float64(ww), float64(wh))
// swap back and front buffer
window.SwapBuffers()
}
}
func run(cv *canvas.Canvas, w, h float64) {
cv.SetFillStyle("#000")
cv.FillRect(0, 0, w, h)
cv.SetFillStyle("#00F")
cv.FillRect(w*0.25, h*0.25, w*0.5, h*0.5)
}