From 35f6c274a9aa2e3c04d421c85cfe10e676df0515 Mon Sep 17 00:00:00 2001 From: Thomas Friedel Date: Mon, 30 Apr 2018 12:09:57 +0200 Subject: [PATCH] added an example for using GLFW --- examples/glfw/glfw.go | 74 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 examples/glfw/glfw.go diff --git a/examples/glfw/glfw.go b/examples/glfw/glfw.go new file mode 100644 index 0000000..77988b8 --- /dev/null +++ b/examples/glfw/glfw.go @@ -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) +}