Permalink
Browse files

Terrible hard-coded joystick support

  • Loading branch information...
1 parent e8677d5 commit bce922af0a9ffe81c8fa6632d2320ba933761a7a @scottferg committed Oct 13, 2012
Showing with 135 additions and 0 deletions.
  1. +87 −0 controller.go
  2. +48 −0 video.go
View
@@ -4,10 +4,81 @@ import (
"github.com/0xe2-0x9a-0x9b/Go-SDL/sdl"
)
+const (
+ JoypadButtonA = 1
+ JoypadButtonB = 2
+ JoypadButtonStart = 9
+ JoypadButtonSelect = 8
+ JoypadAxisUp = -32768
+ JoypadAxisDown = 32767
+ JoypadAxisLeft = -32768
+ JoypadAxisRight = 32767
+)
+
type Controller struct {
ButtonState [8]Word
StrobeState int
LastWrite Word
+ LastYAxis int
+ LastXAxis int
+}
+
+func (c *Controller) SetJoypadAxisState(a, d int, v Word) {
+ resetAxis := func(d int) {
+ switch d {
+ case 0:
+ if c.LastYAxis != -1 {
+ c.ButtonState[c.LastYAxis] = 0x40
+ }
+ case 1:
+ if c.LastXAxis != -1 {
+ c.ButtonState[c.LastXAxis] = 0x40
+ }
+ }
+ }
+
+ if a == 4 {
+ switch d {
+ case JoypadAxisUp: // Up
+ resetAxis(0)
+ c.ButtonState[4] = v
+ c.LastYAxis = 4
+ case JoypadAxisDown: // Down
+ resetAxis(0)
+ c.ButtonState[5] = v
+ c.LastYAxis = 5
+ default:
+ resetAxis(0)
+ c.LastYAxis = -1
+ }
+ } else if a == 3 {
+ switch d {
+ case JoypadAxisLeft: // Left
+ resetAxis(1)
+ c.ButtonState[6] = v
+ c.LastXAxis = 6
+ case JoypadAxisRight: // Right
+ resetAxis(1)
+ c.ButtonState[7] = v
+ c.LastXAxis = 7
+ default:
+ resetAxis(1)
+ c.LastXAxis = -1
+ }
+ }
+}
+
+func (c *Controller) SetJoypadButtonState(k int, v Word) {
+ switch k {
+ case JoypadButtonA: // A
+ c.ButtonState[0] = v
+ case JoypadButtonB: // B
+ c.ButtonState[1] = v
+ case JoypadButtonSelect: // Select
+ c.ButtonState[2] = v
+ case JoypadButtonStart: // Start
+ c.ButtonState[3] = v
+ }
}
func (c *Controller) SetButtonState(k sdl.KeyboardEvent, v Word) {
@@ -31,6 +102,22 @@ func (c *Controller) SetButtonState(k sdl.KeyboardEvent, v Word) {
}
}
+func (c *Controller) AxisDown(a, d int) {
+ c.SetJoypadAxisState(a, d, 0x41)
+}
+
+func (c *Controller) AxisUp(a, d int) {
+ c.SetJoypadAxisState(a, d, 0x40)
+}
+
+func (c *Controller) ButtonDown(b int) {
+ c.SetJoypadButtonState(b, 0x41)
+}
+
+func (c *Controller) ButtonUp(b int) {
+ c.SetJoypadButtonState(b, 0x40)
+}
+
func (c *Controller) KeyDown(e sdl.KeyboardEvent) {
c.SetButtonState(e, 0x41)
}
View
@@ -15,6 +15,8 @@ type Video struct {
fpsmanager *gfx.FPSmanager
screen *sdl.Surface
tex gl.Texture
+ joy *sdl.Joystick
+ Fullscreen bool
}
func (v *Video) Init(t <-chan []uint32, d <-chan []uint32, n string) {
@@ -44,13 +46,32 @@ func (v *Video) Init(t <-chan []uint32, d <-chan []uint32, n string) {
v.fpsmanager = gfx.NewFramerate()
v.fpsmanager.SetFramerate(70)
+
+ for i := 0; i < sdl.NumJoysticks(); i++ {
+ v.joy = sdl.JoystickOpen(i)
+
+ if v.joy != nil {
+ fmt.Printf("Joystick %d\n", i)
+ fmt.Println(" Name: ", sdl.JoystickName(0))
+ fmt.Println(" Number of Axes: ", v.joy.NumAxes())
+ fmt.Println(" Number of Buttons: ", v.joy.NumButtons())
+ fmt.Println(" Number of Balls: ", v.joy.NumBalls())
+ } else {
+ fmt.Println(" Couldn't open Joystick!")
+ }
+ }
}
func (v *Video) ResizeEvent(w, h int) {
v.screen = sdl.SetVideoMode(w, h, 32, sdl.OPENGL|sdl.RESIZABLE)
v.Reshape(w, h)
}
+func (v *Video) FullscreenEvent() {
+ v.screen = sdl.SetVideoMode(1440, 900, 32, sdl.OPENGL|sdl.FULLSCREEN)
+ v.Reshape(1440, 900)
+}
+
func (v *Video) Reshape(width int, height int) {
x_offset := 0
y_offset := 0
@@ -102,6 +123,23 @@ func (v *Video) Render() {
v.ResizeEvent(int(e.W), int(e.H))
case sdl.QuitEvent:
running = false
+ case sdl.JoyAxisEvent:
+ switch e.Value {
+ // Same values for left/right
+ case JoypadAxisUp:
+ fallthrough
+ case JoypadAxisDown:
+ controller.AxisDown(int(e.Axis), int(e.Value))
+ default:
+ controller.AxisUp(int(e.Axis), int(e.Value))
+ }
+ case sdl.JoyButtonEvent:
+ switch v.joy.GetButton(int(e.Button)) {
+ case 1:
+ controller.ButtonDown(int(e.Button))
+ case 0:
+ controller.ButtonUp(int(e.Button))
+ }
case sdl.KeyboardEvent:
switch e.Keysym.Sym {
case sdl.K_ESCAPE:
@@ -141,6 +179,16 @@ func (v *Video) Render() {
if e.Type == sdl.KEYDOWN {
v.ResizeEvent(1024, 960)
}
+ case sdl.K_f:
+ if e.Type == sdl.KEYDOWN {
+ if v.Fullscreen {
+ v.ResizeEvent(512, 480)
+ } else {
+ v.FullscreenEvent()
+ }
+
+ v.Fullscreen = !v.Fullscreen
+ }
}
switch e.Type {

0 comments on commit bce922a

Please sign in to comment.