diff --git a/emulator/director.go b/emulator/director.go index 6eba51a5..9c911e54 100644 --- a/emulator/director.go +++ b/emulator/director.go @@ -9,21 +9,23 @@ import ( // "github.com/gordonklaus/portaudio" ) +// Director is the nes emulator type Director struct { // audio *Audio view *GameView timestamp float64 - imageChannel chan *image.RGBA - audioChannel chan float32 - inputChannel chan int + imageChannel chan<- *image.RGBA + audioChannel chan<- float32 + inputChannel <-chan int Done chan struct{} roomID string } -const FPS = 60 +const fps = 60 -func NewDirector(roomID string, imageChannel chan *image.RGBA, audioChannel chan float32, inputChannel chan int) *Director { +// NewDirector returns a new director +func NewDirector(roomID string, imageChannel chan<- *image.RGBA, audioChannel chan<- float32, inputChannel <-chan int) *Director { // TODO: return image channel from where it write director := Director{} director.Done = make(chan struct{}) @@ -34,6 +36,7 @@ func NewDirector(roomID string, imageChannel chan *image.RGBA, audioChannel chan return &director } +// SetView ... func (d *Director) SetView(view *GameView) { if d.view != nil { d.view.Exit() @@ -49,6 +52,7 @@ func (d *Director) SetView(view *GameView) { //d.view.UpdateInput(input) //} +// Step ... func (d *Director) Step() { timestamp := float64(time.Now().Nanosecond()) / float64(time.Second) dt := timestamp - d.timestamp @@ -58,6 +62,7 @@ func (d *Director) Step() { } } +// Start ... func (d *Director) Start(paths []string) { // portaudio.Initialize() // defer portaudio.Terminate() @@ -73,8 +78,9 @@ func (d *Director) Start(paths []string) { d.Run() } +// Run ... func (d *Director) Run() { - c := time.Tick(time.Second / FPS) + c := time.Tick(time.Second / fps) L: for range c { // for { @@ -96,6 +102,7 @@ L: log.Println("Closed Director") } +// PalyGame starts a game given a rom path func (d *Director) PlayGame(path string) { console, err := nes.NewConsole(path) if err != nil { diff --git a/emulator/gameview.go b/emulator/gameview.go index 5a4c270f..0be6c22b 100644 --- a/emulator/gameview.go +++ b/emulator/gameview.go @@ -49,9 +49,9 @@ type GameView struct { savingJob *job loadingJob *job - imageChannel chan *image.RGBA - audioChannel chan float32 - inputChannel chan int + imageChannel chan<- *image.RGBA + audioChannel chan<- float32 + inputChannel <-chan int } type job struct { @@ -59,7 +59,7 @@ type job struct { extraFunc func() error } -func NewGameView(console *nes.Console, title, saveFile string, imageChannel chan *image.RGBA, audioChannel chan float32, inputChannel chan int) *GameView { +func NewGameView(console *nes.Console, title, saveFile string, imageChannel chan<- *image.RGBA, audioChannel chan<- float32, inputChannel <-chan int) *GameView { gameview := &GameView{ console: console, title: title, diff --git a/emulator/nes/apu.go b/emulator/nes/apu.go index 33b10578..8a1a2aff 100644 --- a/emulator/nes/apu.go +++ b/emulator/nes/apu.go @@ -45,7 +45,7 @@ func init() { type APU struct { console *Console - channel chan float32 + channel chan<- float32 sampleRate float64 pulse1 Pulse pulse2 Pulse diff --git a/emulator/nes/console.go b/emulator/nes/console.go index 2dbd0d11..d884a865 100644 --- a/emulator/nes/console.go +++ b/emulator/nes/console.go @@ -89,7 +89,7 @@ func (console *Console) SetButtons2(buttons [8]bool) { console.Controller2.SetButtons(buttons) } -func (console *Console) SetAudioChannel(channel chan float32) { +func (console *Console) SetAudioChannel(channel chan<- float32) { console.APU.channel = channel } diff --git a/worker/room/room.go b/worker/room/room.go index 7047078a..8e855a17 100644 --- a/worker/room/room.go +++ b/worker/room/room.go @@ -19,12 +19,12 @@ import ( type Room struct { ID string - // imageChannel is image stream from director - imageChannel chan *image.RGBA - // audioChannel is audio stream from director - audioChannel chan float32 - // inputChannel is input stream from websocket to room - inputChannel chan int + // imageChannel is image stream received from director + imageChannel <-chan *image.RGBA + // audioChannel is audio stream received from director + audioChannel <-chan float32 + // inputChannel is input stream from websocket send to room + inputChannel chan<- int // State of room IsRunning bool // Done channel is to fire exit event when room is closed