commit eb86b4639c4b06a71b404489bff845b687b26b36
parent de0d6cb74f630c090bd547e1dd7506bfbdef1e53
Author: Matsuda Kenji <info@mtkn.jp>
Date: Mon, 11 Nov 2024 14:24:38 +0900
implement key
Diffstat:
M | cmd/sample/main.go | | | 53 | +++++++++++++++++++++++++++++------------------------ |
M | tofu.go | | | 53 | ++++++++++++++++++++++++++++++++++++++++++++++++----- |
2 files changed, 77 insertions(+), 29 deletions(-)
diff --git a/cmd/sample/main.go b/cmd/sample/main.go
@@ -23,54 +23,52 @@ func init() {
runtime.LockOSThread()
}
-func keyboardCallback(w *glfw.Window) {
+func processInput(app *App) {
const speed = 0.1
- if w.GetKey(glfw.KeyQ) == glfw.Press {
- w.SetShouldClose(true)
+ if tofu.IsKeyPressed(tofu.KeyQ) {
+ app.termination = true
+ return
}
- if w.GetKey(glfw.KeyUp) == glfw.Press {
+ if tofu.IsKeyPressed(tofu.KeyUp) {
alpha += 0.01
if alpha > 1.0 {
alpha = 1.0
}
}
- if w.GetKey(glfw.KeyDown) == glfw.Press {
+ if tofu.IsKeyPressed(tofu.KeyDown) {
alpha -= 0.01
if alpha < 0 {
alpha = 0
}
}
- if w.GetKey(glfw.KeyW) == glfw.Press {
+ if tofu.IsKeyPressed(tofu.KeyW) {
camera.Move(0, 0, -speed)
}
- if w.GetKey(glfw.KeyS) == glfw.Press {
+ if tofu.IsKeyPressed(tofu.KeyS) {
camera.Move(0, 0, speed)
}
- if w.GetKey(glfw.KeyA) == glfw.Press {
+ if tofu.IsKeyPressed(tofu.KeyA) {
camera.Move(-speed, 0, 0)
}
- if w.GetKey(glfw.KeyD) == glfw.Press {
+ if tofu.IsKeyPressed(tofu.KeyD) {
camera.Move(speed, 0, 0)
}
- if w.GetKey(glfw.KeySpace) == glfw.Press {
+ if tofu.IsKeyPressed(tofu.KeySpace) {
camera.Move(0, speed, 0)
}
- if w.GetKey(glfw.KeyLeftShift) == glfw.Press {
+ if tofu.IsKeyPressed(tofu.KeyLeftShift) {
camera.Move(0, -speed, 0)
}
}
-var (
- cursorX, cursorY float32
- mouseCallbackFirstTime = true
-)
-func watchCursor(app App) {
+func watchCursor(app *App) {
const sensitivity = 0.005
+ firstTime := true
for cur := range app.cursorChan {
- if app.cursorFirstTime {
+ if firstTime {
app.cur = cur
- app.cursorFirstTime = false
+ firstTime = false
continue
}
dx, dy := float32(app.cur.X-cur.X), float32(app.cur.Y-cur.Y)
@@ -144,13 +142,22 @@ type App struct {
lightProgram *tofu.Program
lightCol tofu.Vec3
objCol tofu.Vec3
+
cursorChan chan tofu.Cursor
cur tofu.Cursor
- cursorFirstTime bool
+
+ keyChan chan struct{}
+
camera *tofu.Camera
+
+ termination bool
}
-func (app App) Update() error {
+func (app *App) Update() error {
+ processInput(app)
+ if app.termination {
+ return tofu.Termination
+ }
lightModel := tofu.Rotate(float32(glfw.GetTime()), tofu.Vec3{0, 1.41421356 / 2, 1.41421356 / 2}).
Mul(tofu.Translate(tofu.Vec3{3, 0, 0})).
Mul(tofu.Scale(0.1))
@@ -181,13 +188,13 @@ func (app App) Update() error {
return nil
}
-func (app App) CursorChan() chan<- tofu.Cursor {
+func (app *App) CursorChan() chan<- tofu.Cursor {
return app.cursorChan
}
func main() {
var err error
- app := App{cursorFirstTime: true}
+ app := &App{}
app.lightCol = tofu.Vec3{1, 1, 1}
app.objCol = tofu.Vec3{1.0, 0.5, 0.32}
app.cursorChan = make(chan tofu.Cursor)
@@ -218,8 +225,6 @@ func main() {
camera.MoveTo(tofu.Vec3{0, 0, 3})
camera.LookAt(tofu.Vec3{0, 0, 0})
- tofu.SetKeyCallback(keyboardCallback)
-
if err := tofu.Run(app); err != nil {
log.Fatal(err)
}
diff --git a/tofu.go b/tofu.go
@@ -1,6 +1,7 @@
package tofu
import (
+ "errors"
"fmt"
"log"
"unsafe"
@@ -57,10 +58,12 @@ func Run(app App) error {
defer close(c)
}
for !window.ShouldClose() {
- keyCallback(window)
gl.ClearColor(0.1, 0.2, 0.2, 1.0)
- gl.Clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT)
- app.Update()
+ gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
+ err := app.Update()
+ if err == Termination {
+ break
+ }
window.SwapBuffers()
glfw.PollEvents()
}
@@ -92,4 +95,45 @@ func SetKeyCallback(f func(*glfw.Window)) {
func SetWindowSize(w, h int) {
winW, winH = w, h
-}
-\ No newline at end of file
+}
+
+type Key int
+
+const (
+ KeyA Key = Key(glfw.KeyA)
+ KeyB = Key(glfw.KeyB)
+ KeyC = Key(glfw.KeyC)
+ KeyD = Key(glfw.KeyD)
+ KeyE = Key(glfw.KeyE)
+ KeyF = Key(glfw.KeyF)
+ KeyG = Key(glfw.KeyG)
+ KeyH = Key(glfw.KeyH)
+ KeyI = Key(glfw.KeyI)
+ KeyJ = Key(glfw.KeyJ)
+ KeyK = Key(glfw.KeyK)
+ KeyL = Key(glfw.KeyL)
+ KeyM = Key(glfw.KeyM)
+ KeyN = Key(glfw.KeyN)
+ KeyO = Key(glfw.KeyO)
+ KeyP = Key(glfw.KeyP)
+ KeyQ = Key(glfw.KeyQ)
+ KeyR = Key(glfw.KeyR)
+ KeyS = Key(glfw.KeyS)
+ KeyT = Key(glfw.KeyT)
+ KeyU = Key(glfw.KeyU)
+ KeyV = Key(glfw.KeyV)
+ KeyW = Key(glfw.KeyW)
+ KeyX = Key(glfw.KeyX)
+ KeyY = Key(glfw.KeyY)
+ KeyZ = Key(glfw.KeyZ)
+ KeySpace = Key(glfw.KeySpace)
+ KeyLeftShift = Key(glfw.KeyLeftShift)
+ KeyUp = Key(glfw.KeyUp)
+ KeyDown = Key(glfw.KeyDown)
+)
+
+var Termination error = errors.New("Termination")
+
+func IsKeyPressed(key Key) bool {
+ return window.GetKey(glfw.Key(key)) == glfw.Press
+}