Skip to content

Commit

Permalink
Improve input, autodetect SBS mode
Browse files Browse the repository at this point in the history
  • Loading branch information
donomii committed Apr 11, 2023
1 parent d362b6f commit 804db77
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 73 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Easy 3D camera management
Description
===========

SceneCamera manages the camera view for 3D applications. It provides the V, in the GL trinity MVP. And if needed, the P as well.
SceneCamera manages the camera for 3D applications. It provides the V, in the GL trinity MVP. And if needed, the P as well.

It comes with 3 convenient modes, museum mode, first person mode, and RTS (Real Time Strategy) mode.

Expand Down Expand Up @@ -104,6 +104,5 @@ An example program is included in the examples directory. It can be run with

## Known issues

RTS mode is not complete, it does not rotate correctly.

It's not fast, but it's fast enough to handle movement in a game.
197 changes: 150 additions & 47 deletions example/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"log"

"sync"
"github.com/go-gl/glfw/v3.2/glfw"
)

Expand All @@ -12,7 +12,7 @@ var MouseWheelValue float32
func handleMouseMove(w *glfw.Window, xpos float64, ypos float64) {
//log.Printf("Mouse moved: %v,%v", xpos, ypos)
//diff := xpos - oldXpos

}
func handleMouseButton(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
log.Printf("Got mouse button %v,%v,%v", button, mod, action)
Expand All @@ -26,9 +26,11 @@ func handleMouseWheel(w *glfw.Window, xoff float64, yoff float64) {
//handleKey(w, key, scancode, action, mods)
}

var keyLatch = NewGenericMap[glfw.Key,bool]()

func handleKey(win *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {

if mods == 2 && (action == 1 || action == 2) && key != 341 { //action 1 is press, 2 is repeat
if mods == 2 && (action == 1 || action == 2) && key != 341 { //action 1 is press, 2 is repeat
mask := ^byte(64 + 128)
log.Printf("key mask: %#b", mask)
val := byte(key)
Expand All @@ -38,50 +40,151 @@ func handleKey(win *glfw.Window, key glfw.Key, scancode int, action glfw.Action,

}

if (action == 1 || action == 2) && mods == 0 { //FIXME use latch
log.Printf("Acting on key %c,%v", key, key)
switch key {
case 65:
//Left
camera.Move(2,0.1)
case 68:
//Right
camera.Move(3,0.1)
case 87:
//Up
camera.Move(0,0.2)
case 83:
//Down
camera.Move(1,0.2)
case 81:
//Q
camera.Move(8,0.1)
case 69:
//E
camera.Move(9,0.1)
// X
case 88:
camera.Move(4,0.1)
// space bar
case 32:
camera.Move(5,0.1)
// R
case 82:
camera.Move(6,0.1)
// F
case 70:
camera.Move(7,0.1)




case 256:
log.Println("Quitting")
shutdown()
win.SetShouldClose(true)

}
camera.Dump()
if (action == 1 || action == 2) && mods == 0 { //FIXME use latch
keyLatch.Set(key, true)
}

if action == 0 && mods == 0 {
keyLatch.Set(key,false)
}


camera.Dump()

}


func MoveStep(win *glfw.Window, size float32) {

//Left
if keyLatch.Get(65) {
camera.Move(2, 0.2*size)
}
//Right
if keyLatch.Get(68) {
camera.Move(3, 0.2*size)
}
//Up
if keyLatch.Get(87) {
camera.Move(0, 0.2*size)
}
//Down
if keyLatch.Get(83) {
camera.Move(1, 0.2*size)
}
//Q
if keyLatch.Get(81) {
camera.Move(8, 0.1*size)
}
//E
if keyLatch.Get(69) {
camera.Move(9, 0.1*size)

}
// X
if keyLatch.Get(88) {
camera.Move(4, 0.1*size)
}
// space bar
if keyLatch.Get(32) {
camera.Move(5, 0.1*size)
}
// R
if keyLatch.Get(82) {
camera.Move(6, 0.1*size)
}
// F
if keyLatch.Get(70){
camera.Move(7, 0.1*size)
}
// Esc
if keyLatch .Get(256) {
log.Println("Quitting")
shutdown()
win.SetShouldClose(true)
}
}

// Make a generic hashmap for key presses

type GenericMap[K comparable, V any] struct {
M map[K]V
Mutex sync.Mutex
}

func (m *GenericMap[K, V]) Get(key K) V {
m.Mutex.Lock()
defer m.Mutex.Unlock()
return m.M[key]
}

func (m *GenericMap[K, V]) Load(key K) (V, bool) {
m.Mutex.Lock()
defer m.Mutex.Unlock()
v, ok := m.M[key]
return v, ok
}

func (m *GenericMap[K, V]) Set(key K, value V) {
m.Mutex.Lock()
defer m.Mutex.Unlock()
m.M[key] = value
}

func (m *GenericMap[K, V]) Delete(key K) {
m.Mutex.Lock()
defer m.Mutex.Unlock()
delete(m.M, key)
}

func (m *GenericMap[K, V]) Has(key K) bool {
m.Mutex.Lock()
defer m.Mutex.Unlock()
_, ok := m.M[key]
return ok
}

func (m *GenericMap[K, V]) Keys() []K {
m.Mutex.Lock()
defer m.Mutex.Unlock()
keys := make([]K, 0, len(m.M))
for k := range m.M {
keys = append(keys, k)
}
return keys
}

func (m *GenericMap[K, V]) Values() []V {
m.Mutex.Lock()
defer m.Mutex.Unlock()
values := make([]V, 0, len(m.M))
for _, v := range m.M {
values = append(values, v)
}
return values
}

func (m *GenericMap[K, V]) Len() int {
m.Mutex.Lock()
defer m.Mutex.Unlock()
return len(m.M)
}

func (m *GenericMap[K, V]) Clear() {
m.Mutex.Lock()
defer m.Mutex.Unlock()
m.M = make(map[K]V)
}

func (m *GenericMap[K, V]) Lock() {
m.Mutex.Lock()
}

func (m *GenericMap[K, V]) Unlock() {
m.Mutex.Unlock()
}

func NewGenericMap[K comparable, V any]() *GenericMap[K, V] {
return &GenericMap[K, V]{M: make(map[K]V)}
}

Loading

0 comments on commit 804db77

Please sign in to comment.