mirror of
https://github.com/bloeys/nmage.git
synced 2025-12-29 13:28:20 +00:00
Keyboard and mouse input system
This commit is contained in:
47
input/input.go
Executable file
47
input/input.go
Executable file
@ -0,0 +1,47 @@
|
|||||||
|
package input
|
||||||
|
|
||||||
|
import "github.com/veandco/go-sdl2/sdl"
|
||||||
|
|
||||||
|
type InputKey int
|
||||||
|
|
||||||
|
var (
|
||||||
|
anyKeyDown bool
|
||||||
|
anyMouseBtnDown bool
|
||||||
|
)
|
||||||
|
|
||||||
|
//EventLoopStarted should be called just before processing SDL events
|
||||||
|
func EventLoopStarted() {
|
||||||
|
|
||||||
|
anyKeyDown = false
|
||||||
|
anyMouseBtnDown = false
|
||||||
|
|
||||||
|
//Clear XThisFrame which is needed because a repeat event needs multiple frames to happen
|
||||||
|
for _, v := range mouseBtns {
|
||||||
|
|
||||||
|
v.isDoubleClick = false
|
||||||
|
v.pressedThisFrame = false
|
||||||
|
v.releasedThisFrame = false
|
||||||
|
|
||||||
|
if v.state == sdl.PRESSED {
|
||||||
|
anyMouseBtnDown = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range keyboardKeys {
|
||||||
|
|
||||||
|
v.pressedThisFrame = false
|
||||||
|
v.releasedThisFrame = false
|
||||||
|
|
||||||
|
if v.state == sdl.PRESSED {
|
||||||
|
anyKeyDown = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func AnyKeyDown() bool {
|
||||||
|
return anyKeyDown
|
||||||
|
}
|
||||||
|
|
||||||
|
func AnyMouseBtnDown() bool {
|
||||||
|
return anyMouseBtnDown
|
||||||
|
}
|
||||||
67
input/keyboard.go
Executable file
67
input/keyboard.go
Executable file
@ -0,0 +1,67 @@
|
|||||||
|
package input
|
||||||
|
|
||||||
|
import "github.com/veandco/go-sdl2/sdl"
|
||||||
|
|
||||||
|
type keyState struct {
|
||||||
|
key sdl.Keycode
|
||||||
|
state byte
|
||||||
|
pressedThisFrame bool
|
||||||
|
releasedThisFrame bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
keyboardKeys = make(map[sdl.Keycode]*keyState)
|
||||||
|
)
|
||||||
|
|
||||||
|
func HandleKeyboardEvent(e *sdl.KeyboardEvent) {
|
||||||
|
|
||||||
|
ks := keyboardKeys[e.Keysym.Sym]
|
||||||
|
if ks == nil {
|
||||||
|
ks = &keyState{key: e.Keysym.Sym}
|
||||||
|
keyboardKeys[e.Keysym.Sym] = ks
|
||||||
|
}
|
||||||
|
|
||||||
|
ks.state = e.State
|
||||||
|
ks.pressedThisFrame = e.Repeat == 0 && e.State == sdl.PRESSED
|
||||||
|
ks.releasedThisFrame = e.Repeat == 0 && e.State == sdl.RELEASED
|
||||||
|
}
|
||||||
|
|
||||||
|
func KeyClicked(kc sdl.Keycode) bool {
|
||||||
|
|
||||||
|
key := keyboardKeys[kc]
|
||||||
|
if key == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return key.pressedThisFrame
|
||||||
|
}
|
||||||
|
|
||||||
|
func KeyReleased(kc sdl.Keycode) bool {
|
||||||
|
|
||||||
|
key := keyboardKeys[kc]
|
||||||
|
if key == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return key.releasedThisFrame
|
||||||
|
}
|
||||||
|
|
||||||
|
func KeyDown(kc sdl.Keycode) bool {
|
||||||
|
|
||||||
|
key := keyboardKeys[kc]
|
||||||
|
if key == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return key.state == sdl.PRESSED
|
||||||
|
}
|
||||||
|
|
||||||
|
func KeyUp(kc sdl.Keycode) bool {
|
||||||
|
|
||||||
|
key := keyboardKeys[kc]
|
||||||
|
if key == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return key.state == sdl.RELEASED
|
||||||
|
}
|
||||||
79
input/mouse.go
Executable file
79
input/mouse.go
Executable file
@ -0,0 +1,79 @@
|
|||||||
|
package input
|
||||||
|
|
||||||
|
import "github.com/veandco/go-sdl2/sdl"
|
||||||
|
|
||||||
|
type mouseBtnState struct {
|
||||||
|
button byte
|
||||||
|
state byte
|
||||||
|
isDoubleClick bool
|
||||||
|
pressedThisFrame bool
|
||||||
|
releasedThisFrame bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
mouseBtns = make(map[byte]*mouseBtnState)
|
||||||
|
)
|
||||||
|
|
||||||
|
func HandleMouseBtnEvent(e *sdl.MouseButtonEvent) {
|
||||||
|
|
||||||
|
mb := mouseBtns[e.Button]
|
||||||
|
if mb == nil {
|
||||||
|
mb = &mouseBtnState{button: e.Button}
|
||||||
|
mouseBtns[e.Button] = mb
|
||||||
|
}
|
||||||
|
|
||||||
|
mb.state = e.State
|
||||||
|
mb.isDoubleClick = e.Clicks > 1 && e.State == sdl.PRESSED
|
||||||
|
mb.pressedThisFrame = e.State == sdl.PRESSED
|
||||||
|
mb.releasedThisFrame = e.State == sdl.RELEASED
|
||||||
|
}
|
||||||
|
|
||||||
|
func MouseClicked(mouseBtn byte) bool {
|
||||||
|
|
||||||
|
mb := mouseBtns[mouseBtn]
|
||||||
|
if mb == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return mb.pressedThisFrame
|
||||||
|
}
|
||||||
|
|
||||||
|
func MouseDoubleClicked(mouseBtn byte) bool {
|
||||||
|
|
||||||
|
mb := mouseBtns[mouseBtn]
|
||||||
|
if mb == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return mb.isDoubleClick
|
||||||
|
}
|
||||||
|
|
||||||
|
func MouseReleased(mouseBtn byte) bool {
|
||||||
|
|
||||||
|
mb := mouseBtns[mouseBtn]
|
||||||
|
if mb == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return mb.releasedThisFrame
|
||||||
|
}
|
||||||
|
|
||||||
|
func MouseDown(mouseBtn byte) bool {
|
||||||
|
|
||||||
|
mb := mouseBtns[mouseBtn]
|
||||||
|
if mb == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return mb.state == sdl.PRESSED
|
||||||
|
}
|
||||||
|
|
||||||
|
func MouseUp(mouseBtn byte) bool {
|
||||||
|
|
||||||
|
mb := mouseBtns[mouseBtn]
|
||||||
|
if mb == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return mb.state == sdl.RELEASED
|
||||||
|
}
|
||||||
14
main.go
14
main.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/bloeys/go-sdl-engine/input"
|
||||||
"github.com/bloeys/go-sdl-engine/timing"
|
"github.com/bloeys/go-sdl-engine/timing"
|
||||||
"github.com/go-gl/gl/v4.6-compatibility/gl"
|
"github.com/go-gl/gl/v4.6-compatibility/gl"
|
||||||
"github.com/veandco/go-sdl2/sdl"
|
"github.com/veandco/go-sdl2/sdl"
|
||||||
@ -105,6 +106,7 @@ func gameLoop() {
|
|||||||
timing.FrameStarted()
|
timing.FrameStarted()
|
||||||
|
|
||||||
handleEvents()
|
handleEvents()
|
||||||
|
update()
|
||||||
draw()
|
draw()
|
||||||
|
|
||||||
window.GLSwap()
|
window.GLSwap()
|
||||||
@ -116,10 +118,18 @@ func gameLoop() {
|
|||||||
|
|
||||||
func handleEvents() {
|
func handleEvents() {
|
||||||
|
|
||||||
|
input.EventLoopStarted()
|
||||||
|
|
||||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||||
|
|
||||||
switch e := event.(type) {
|
switch e := event.(type) {
|
||||||
|
|
||||||
|
case *sdl.KeyboardEvent:
|
||||||
|
input.HandleKeyboardEvent(e)
|
||||||
|
|
||||||
|
case *sdl.MouseButtonEvent:
|
||||||
|
input.HandleMouseBtnEvent(e)
|
||||||
|
|
||||||
case *sdl.QuitEvent:
|
case *sdl.QuitEvent:
|
||||||
println("Quit at ", e.Timestamp)
|
println("Quit at ", e.Timestamp)
|
||||||
isRunning = false
|
isRunning = false
|
||||||
@ -127,6 +137,10 @@ func handleEvents() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func update() {
|
||||||
|
println(input.AnyKeyDown(), ";", input.AnyMouseBtnDown())
|
||||||
|
}
|
||||||
|
|
||||||
func draw() {
|
func draw() {
|
||||||
//Clear screen and depth buffers
|
//Clear screen and depth buffers
|
||||||
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
|
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
|
||||||
|
|||||||
Reference in New Issue
Block a user