Skip to content

Commit

Permalink
Split up code into modules, add actions file
Browse files Browse the repository at this point in the history
  • Loading branch information
donomii committed Apr 13, 2023
1 parent 804db77 commit 667a8d2
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 7 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/github-actions-demo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: GitHub Actions Demo
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
on: [push]
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
- name: Check out repository code
uses: actions/checkout@v3
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: List files in the repository
run: |
ls ${{ github.workspace }}
- run: echo "🍏 This job's status is ${{ job.status }}."
Binary file added example/grass.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion example/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"log"
"sync"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/go-gl/glfw/v3.3/glfw"
)

var oldXpos float64
Expand Down
24 changes: 18 additions & 6 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"runtime/debug"
"sort"
"time"
joystick "../joystick"
messages "../messages"

"github.com/donomii/goof"

Expand All @@ -28,12 +30,12 @@ import (

Cameras "github.com/donomii/sceneCamera"
"github.com/go-gl/gl/v3.2-core/gl"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/go-gl/glfw/v3.3/glfw"

_ "embed"
)

//go:embed logo.png
//go:embed grass.png
var logo_bytes []byte

var MainWin *glfw.Window
Expand Down Expand Up @@ -135,7 +137,7 @@ func main() {
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
win, err := glfw.CreateWindow(winWidth, winHeight, "Grafana", nil, nil)
win, err := glfw.CreateWindow(winWidth, winHeight, "Demo", nil, nil)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -184,11 +186,12 @@ func main() {
projectionUniform := gl.GetUniformLocation(state.Program, gl.Str("projection\x00"))
gl.UniformMatrix4fv(projectionUniform, 1, false, &projection[0])

/*
//Setup the camera
camera := mgl32.LookAtV(mgl32.Vec3{3, 3, 3}, mgl32.Vec3{0, 0, 0}, mgl32.Vec3{0, 1, 0})
cameraUniform := gl.GetUniformLocation(state.Program, gl.Str("camera\x00"))
gl.UniformMatrix4fv(cameraUniform, 1, false, &camera[0])

*/
//Setup the cube
model := mgl32.Ident4()
state.ModelUniform = gl.GetUniformLocation(state.Program, gl.Str("model\x00"))
Expand All @@ -210,7 +213,7 @@ func main() {
//Load textures into texture bank

state.TextureBank = make([]uint32, 2)
for i, textureFile := range []string{"logo.png", "tree.jpg"} {
for i, textureFile := range []string{"grass.png", "tree.jpg"} {
log.Printf("Loading texture %v", textureFile)
//Load an image from a file
data, _ := ioutil.ReadFile(textureFile)
Expand Down Expand Up @@ -272,8 +275,17 @@ func main() {
time.Sleep(25 * time.Millisecond)
}
}()
joystick.Setup_joystick()
messages.Register("JoystickY", "JoystickY", func(name , id string, args interface{}) {
amount := args.(float64)
camera.Move(0, float32(amount))
})
messages.Register("JoystickX", "JoystickX", func(name , id string, args interface{}) {
amount := args.(float64)
camera.Move(2, float32(amount))
})
for !win.ShouldClose() {

joystick.DoJoystick()
mode := glfw.GetPrimaryMonitor().GetVideoMode()
screenW, screenH := mode.Width, mode.Height
if screenW >= screenH*2-1 {
Expand Down
125 changes: 125 additions & 0 deletions joystick/joystick.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package joystick

import (
messages "../messages"
"fmt"
"github.com/donomii/goof"
"github.com/go-gl/glfw/v3.3/glfw"
)

// Global flag to switch on steam deck features, such as correct button mapping
var steamDeck = false

// Default to a games controller
var joystick_type = "F310"
var latches = make(map[int]bool)
var joystickHistory [][]float64
var joystickHistoryIndex int

func Setup_joystick() {

//Attempt to detect steam deck
if goof.Exists("/sys/devices/virtual/dmi/id/board_vendor") {

if goof.FileContains("/sys/devices/virtual/dmi/id/board_vendor", "Valve") ||
goof.FileContains("/sys/devices/virtual/dmi/id/board_vendor", "valve") {
steamDeck = true
joystick_type = "steamdeck"
}
}

}

func outOfDeadZone(deadZone, xdeflect, ydeflect float64) bool {

return xdeflect > deadZone || xdeflect < -deadZone || ydeflect > deadZone || ydeflect < -deadZone
}
func DoJoystick() {
deadZone := 0.2
joy := glfw.Joystick(0)
if joy.Present() {

//fmt.Println("Joystick 1 present. Axes: ", joy.GetAxes(), " Buttons: ", joy.GetButtons(), " Hats: ", joy.GetHats(), "Latches: ", latches)

//Fetch button states
buttons := joy.GetButtons()

//Check for button presses
for i := 0; i < len(buttons); i++ {

butt := buttons[i]

//If the button is available

//If the button is pressed, and it wasn't pressed last time
if butt == glfw.Press {

if !latches[i] {
latches[i] = true
messages.SendMessage("Button", i)

}
} else {
//Button is released, so reset the latch
latches[i] = false
}

}
if joystick_type == "steamdeck" {
for i := 0; i < len(joy.GetAxes()); i = i + 3 {
var ydeflect float64 = 0
var trigger float64 = 0
xdeflect := float64(joy.GetAxes()[i])
if i+1 < len(joy.GetAxes()) {
ydeflect = float64(joy.GetAxes()[i+1])
}
if i+2 < len(joy.GetAxes()) {
trigger = float64(joy.GetAxes()[i+2])
}
if outOfDeadZone(deadZone, xdeflect, ydeflect) {
messages.SendMessage("JoystickY", ydeflect/-100)
messages.SendMessage("JoystickX", xdeflect/-100)
messages.SendMessage("JoystickTrigger", trigger)
}
/*
if xdeflect > deadZone || xdeflect < -deadZone || ydeflect > deadZone || ydeflect < -deadZone {
b.X = b.X + (xdeflect)*10.0
b.Y = b.Y + (ydeflect)*10.0
}
*/

}

//fmt.Printf("Axes: %+v, hats: %+v, buttons: %+v\n", joy.GetAxes(), joy.GetButtons(), joy.GetHats())
}

if joystick_type == "F310" {
for i := 0; i < len(joy.GetAxes()); i = i + 2 {
var ydeflect float64 = 0
var trigger float64 = 0
xdeflect := float64(joy.GetAxes()[i])
if i+1 < len(joy.GetAxes()) {
ydeflect = float64(joy.GetAxes()[i+1])
}
if i+2 < len(joy.GetAxes()) {
trigger = float64(joy.GetAxes()[i+2])
}
if outOfDeadZone(deadZone, xdeflect, ydeflect) {
messages.SendMessage("JoystickY", ydeflect/-100)
messages.SendMessage("JoystickX", xdeflect/-100)
messages.SendMessage("JoystickTrigger", trigger)
}

/*
if xdeflect > deadZone || xdeflect < -deadZone || ydeflect > deadZone || ydeflect < -deadZone {
b.X = b.X + (xdeflect)*10.0
b.Y = b.Y + (ydeflect)*10.0
}
*/

}
}
}
}
61 changes: 61 additions & 0 deletions messages/messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package messages

import (
"github.com/cornelk/hashmap"
)

//The message registry allows communication between modules. It supports dynamic and changing message handles, and is a solution to the common problems with import cycles and dependencies that other systems have.

//The use is simple. You register a handler for a message, and then send the message. The handler will be called with the message name, an id, and the arguments. You provide the id when you register. The id is used to identify the handler, so you can remove it later. Registering another handler with the same id will replace the old handler.

// The message registry is thread-safe, and can be used from multiple goroutines. However the handler will run in the same routine as the caller, so be careful about calling graphics functions from the handler. Many graphics libraries, such as OpenGL, are not thread-safe.

//The message system is designed for notifications of events, less for passing large amounts of data, or for large numbers of events, or for complex execution flow. It works well for simple things like "the user has clicked on this button", or "the user has pressed this key", or "the user has moved the mouse".

//Messages are never queued, they are delivered immediately.

var MessageRegistry *hashmap.Map[string, *hashmap.Map[string, func(name , id string, args interface{})]] = hashmap.New[string, *hashmap.Map[string, func(name , id string, args interface{})]]()

//Register your message handler with the message system. Name is the lookup name for the message, id is a free text field
//that you can use to identify your handler, and handler is the function that will be called when the message is sent.
//
//There can be as many handlers as you want for a message. The id is used to identify the handler, so you can remove it later.
//Registering another handler with the same id will replace the old handler.
func Register( name string, id string, handler func(name , id string, args interface{})) {
key_name := name
ids,_ := MessageRegistry.Get(key_name)
if ids == nil {
ids = hashmap.New[string, func(name , id string, args interface{})]()
MessageRegistry.Set(key_name, ids)
}
f,_ := ids.Get(id)
if f == nil {
ids := hashmap.New[string, func(name , id string, args interface{})]()
MessageRegistry.Set(key_name, ids)
}
ids.Set(id, handler)
MessageRegistry.Set(key_name, ids)
}

//Delete a handler. The name and id must be the same as the ones used to register the handler.
func Unregister(name string, id string) {
key_name := name
ids,_ := MessageRegistry.Get(key_name)
ids.Del(id)
}

//Send a message. The name is the lookup name for the message, and args is the data to be sent to the handler. Sendmessage calls the handler immediately (i.e. synchronously)
func SendMessage( name string, args interface{}) {

//log.Printf("SendMessage: %s, %v", name, args)
ids, _ := MessageRegistry.Get(name)
if ids == nil {
//TODO some kind of warning that you are sending messages to a non existant message handler?
return
}
ids.Range(func(key string, handler func(name , id string, args interface{})) bool {
handler(name, key, args)
return true
})

}

0 comments on commit 667a8d2

Please sign in to comment.