Add channel direction

This commit is contained in:
giongto35 2019-05-20 01:46:58 +08:00
parent b894375f99
commit 4e7d274ce1
5 changed files with 25 additions and 18 deletions

View file

@ -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 {

View file

@ -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,

View file

@ -45,7 +45,7 @@ func init() {
type APU struct {
console *Console
channel chan float32
channel chan<- float32
sampleRate float64
pulse1 Pulse
pulse2 Pulse

View file

@ -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
}

View file

@ -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