Permalink
Browse files

Move controller input to it's own thread (again)

  • Loading branch information...
1 parent 3782877 commit 79812b3623c232f37554854315e1830d065472d7 @scottferg committed Oct 30, 2012
Showing with 116 additions and 112 deletions.
  1. +94 −0 controller.go
  2. +5 −2 machine.go
  3. +3 −3 ppu.go
  4. +14 −107 video.go
View
@@ -4,6 +4,10 @@ import (
"github.com/scottferg/Go-SDL/sdl"
)
+var (
+ joy []*sdl.Joystick
+)
+
const (
JoypadButtonA = 1
JoypadButtonB = 2
@@ -161,3 +165,93 @@ func (c *Controller) Init() {
c.ButtonState[i] = 0x40
}
}
+
+func ReadInput(r chan [2]int) {
+ for {
+ select {
+ case ev := <-sdl.Events:
+ switch e := ev.(type) {
+ case sdl.ResizeEvent:
+ r <- [2]int{int(e.W), int(e.H)}
+ case sdl.QuitEvent:
+ running = false
+ case sdl.JoyAxisEvent:
+ joy := int(e.Which)
+
+ if joy > 0 {
+ joy = 1
+ }
+
+ switch e.Value {
+ // Same values for left/right
+ case JoypadAxisUp:
+ fallthrough
+ case JoypadAxisDown:
+ pads[joy].AxisDown(int(e.Axis), int(e.Value))
+ default:
+ pads[joy].AxisUp(int(e.Axis), int(e.Value))
+ }
+ case sdl.JoyButtonEvent:
+ j := int(e.Which)
+
+ if j > 0 {
+ j = 1
+ }
+
+ switch joy[int(e.Which)].GetButton(int(e.Button)) {
+ case 1:
+ pads[j].ButtonDown(int(e.Button))
+ case 0:
+ pads[j].ButtonUp(int(e.Button))
+ }
+ case sdl.KeyboardEvent:
+ switch e.Keysym.Sym {
+ case sdl.K_ESCAPE:
+ running = false
+ case sdl.K_r:
+ // Trigger reset interrupt
+ if e.Type == sdl.KEYDOWN {
+ cpu.RequestInterrupt(InterruptReset)
+ }
+ case sdl.K_l:
+ if e.Type == sdl.KEYDOWN {
+ // Trigger reset interrupt
+ LoadState()
+ }
+ case sdl.K_s:
+ if e.Type == sdl.KEYDOWN {
+ // Trigger reset interrupt
+ SaveState()
+ }
+ case sdl.K_o:
+ if e.Type == sdl.KEYDOWN {
+ ppu.OverscanEnabled = !ppu.OverscanEnabled
+ }
+ case sdl.K_1:
+ if e.Type == sdl.KEYDOWN {
+ r <- [2]int{256, 240}
+ }
+ case sdl.K_2:
+ if e.Type == sdl.KEYDOWN {
+ r <- [2]int{512, 480}
+ }
+ case sdl.K_3:
+ if e.Type == sdl.KEYDOWN {
+ r <- [2]int{768, 720}
+ }
+ case sdl.K_4:
+ if e.Type == sdl.KEYDOWN {
+ r <- [2]int{1024, 960}
+ }
+ }
+
+ switch e.Type {
+ case sdl.KEYDOWN:
+ pads[0].KeyDown(e)
+ case sdl.KEYUP:
+ pads[0].KeyUp(e)
+ }
+ }
+ }
+ }
+}
View
@@ -205,7 +205,7 @@ func main() {
Ram.Init()
cpu.Init()
apu.Init()
- v, d := ppu.Init()
+ v := ppu.Init()
if contents, err := ioutil.ReadFile(os.Args[len(os.Args)-1]); err == nil {
@@ -243,9 +243,12 @@ func main() {
}
}()
+ r := video.Init(v, gamename)
+
+ go ReadInput(r)
+
// This needs to happen on the main thread for OSX
runtime.LockOSThread()
- video.Init(v, d, gamename)
defer video.Close()
video.Render()
View
6 ppu.go
@@ -84,7 +84,7 @@ type Ppu struct {
OverscanEnabled bool
}
-func (p *Ppu) Init() (chan []uint32, chan []uint32) {
+func (p *Ppu) Init() chan []uint32 {
p.WriteLatch = true
p.Output = make(chan []uint32)
@@ -112,7 +112,7 @@ func (p *Ppu) Init() (chan []uint32, chan []uint32) {
p.Palettebuffer = make([]Pixel, 0xF000)
p.Framebuffer = make([]uint32, 0xEFE0)
- return p.Output, nil
+ return p.Output
}
func (p *Ppu) PpuRegRead(a int) (Word, error) {
@@ -239,7 +239,7 @@ func (p *Ppu) Step() {
}
} else if p.Cycle == 260 {
if p.SpritePatternAddress == 0x1 && p.BackgroundPatternAddress == 0x0 {
- rom.Hook()
+ rom.Hook()
}
}
case p.Scanline == -1:
View
121 video.go
@@ -2,26 +2,25 @@ package main
import (
"fmt"
+ "github.com/banthar/gl"
"github.com/scottferg/Go-SDL/gfx"
"github.com/scottferg/Go-SDL/sdl"
- "github.com/banthar/gl"
"log"
"math"
)
type Video struct {
tick <-chan []uint32
- debug <-chan []uint32
+ resize chan [2]int
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) {
+func (v *Video) Init(t <-chan []uint32, n string) chan [2]int {
v.tick = t
- v.debug = d
+ v.resize = make(chan [2]int)
if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
log.Fatal(sdl.GetError())
@@ -47,22 +46,24 @@ func (v *Video) Init(t <-chan []uint32, d <-chan []uint32, n string) {
v.fpsmanager = gfx.NewFramerate()
v.fpsmanager.SetFramerate(70)
- v.joy = make([]*sdl.Joystick, sdl.NumJoysticks())
+ joy = make([]*sdl.Joystick, sdl.NumJoysticks())
for i := 0; i < sdl.NumJoysticks(); i++ {
- v.joy[i] = sdl.JoystickOpen(i)
+ joy[i] = sdl.JoystickOpen(i)
fmt.Println("-----------------")
- if v.joy[i] != nil {
+ if joy[i] != nil {
fmt.Printf("Joystick %d\n", i)
fmt.Println(" Name: ", sdl.JoystickName(0))
- fmt.Println(" Number of Axes: ", v.joy[i].NumAxes())
- fmt.Println(" Number of Buttons: ", v.joy[i].NumButtons())
- fmt.Println(" Number of Balls: ", v.joy[i].NumBalls())
+ fmt.Println(" Number of Axes: ", joy[i].NumAxes())
+ fmt.Println(" Number of Buttons: ", joy[i].NumButtons())
+ fmt.Println(" Number of Balls: ", joy[i].NumBalls())
} else {
fmt.Println(" Couldn't open Joystick!")
}
}
+
+ return v.resize
}
func (v *Video) ResizeEvent(w, h int) {
@@ -117,102 +118,8 @@ func quit_event() int {
func (v *Video) Render() {
for running {
select {
- case ev := <-sdl.Events:
- // TODO: Should see if there's a way to do this
- // from another goroutine. Had to move it here for
- // the ResizeEvent
- switch e := ev.(type) {
- case sdl.ResizeEvent:
- v.ResizeEvent(int(e.W), int(e.H))
- case sdl.QuitEvent:
- running = false
- case sdl.JoyAxisEvent:
- joy := int(e.Which)
-
- if joy > 0 {
- joy = 1
- }
-
- switch e.Value {
- // Same values for left/right
- case JoypadAxisUp:
- fallthrough
- case JoypadAxisDown:
- pads[joy].AxisDown(int(e.Axis), int(e.Value))
- default:
- pads[joy].AxisUp(int(e.Axis), int(e.Value))
- }
- case sdl.JoyButtonEvent:
- joy := int(e.Which)
-
- if joy > 0 {
- joy = 1
- }
-
- switch v.joy[int(e.Which)].GetButton(int(e.Button)) {
- case 1:
- pads[joy].ButtonDown(int(e.Button))
- case 0:
- pads[joy].ButtonUp(int(e.Button))
- }
- case sdl.KeyboardEvent:
- switch e.Keysym.Sym {
- case sdl.K_ESCAPE:
- running = false
- case sdl.K_r:
- // Trigger reset interrupt
- if e.Type == sdl.KEYDOWN {
- cpu.RequestInterrupt(InterruptReset)
- }
- case sdl.K_l:
- if e.Type == sdl.KEYDOWN {
- // Trigger reset interrupt
- LoadState()
- }
- case sdl.K_s:
- if e.Type == sdl.KEYDOWN {
- // Trigger reset interrupt
- SaveState()
- }
- case sdl.K_o:
- if e.Type == sdl.KEYDOWN {
- ppu.OverscanEnabled = !ppu.OverscanEnabled
- }
- case sdl.K_1:
- if e.Type == sdl.KEYDOWN {
- v.ResizeEvent(256, 240)
- }
- case sdl.K_2:
- if e.Type == sdl.KEYDOWN {
- v.ResizeEvent(512, 480)
- }
- case sdl.K_3:
- if e.Type == sdl.KEYDOWN {
- v.ResizeEvent(768, 720)
- }
- case sdl.K_4:
- 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 {
- case sdl.KEYDOWN:
- pads[0].KeyDown(e)
- case sdl.KEYUP:
- pads[0].KeyUp(e)
- }
- }
+ case dimensions := <-v.resize:
+ v.ResizeEvent(dimensions[0], dimensions[1])
case val := <-v.tick:
slice := make([]uint8, len(val)*3)
for i := 0; i < len(val); i = i + 1 {

0 comments on commit 79812b3

Please sign in to comment.