tofu

Making something with OpenGL in Go
Log | Files | Refs

commit fc6eaa5e3954f98dbe2255dbce581d24a3371d74
parent 58dfa4db77cc69654cded4da4554c84fee8f4c2a
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Wed, 16 Oct 2024 21:14:24 +0900

complete hello-window

Diffstat:
Mcmd/sample/main.go | 40+++++++++++++++++++++++++++++++++++++---
1 file changed, 37 insertions(+), 3 deletions(-)

diff --git a/cmd/sample/main.go b/cmd/sample/main.go @@ -1,27 +1,61 @@ package main import ( + "log" + "math" "runtime" + + "github.com/go-gl/gl/v3.3-core/gl" "github.com/go-gl/glfw/v3.3/glfw" ) +const ( + winW, winH = 800, 600 +) + func init() { runtime.LockOSThread() } +func framebufferSizeCallback(w *glfw.Window, width int, height int) { + if (width > math.MaxInt32 || height > math.MaxInt32) { + log.Fatal("framebufferSizeCallback: integer overflow") + } + gl.Viewport(0, 0, int32(width), int32(height)) +} + +func processInput(w *glfw.Window) { + if w.GetKey(glfw.KeyQ) == glfw.Press { + w.SetShouldClose(true); + } +} + func main() { err := glfw.Init() if err != nil { - panic(err) + log.Fatalf("init glfw: %v", err) } defer glfw.Terminate() + glfw.WindowHint(glfw.ContextVersionMajor, 3) + glfw.WindowHint(glfw.ContextVersionMinor, 3) + glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) - window, err := glfw.CreateWindow(640, 480, "Testing", nil, nil) + window, err := glfw.CreateWindow(winW, winH, "Testing", nil, nil) if err != nil { - panic(err) + log.Fatalf("create window: %v", err) } window.MakeContextCurrent() + if err = gl.Init(); err != nil { + log.Fatalf("init gl: %v", err) + } + gl.Viewport(0, 0, winW, winH) + + window.SetFramebufferSizeCallback(framebufferSizeCallback) + for !window.ShouldClose() { + processInput(window) + gl.ClearColor(0.2, 0.3, 0.3, 1.0) + gl.Clear(gl.COLOR_BUFFER_BIT) window.SwapBuffers() glfw.PollEvents() }