diff --git a/main.go b/main.go
index 90ca0ed2..52b1cb5c 100644
--- a/main.go
+++ b/main.go
@@ -63,8 +63,8 @@ var rooms map[string]*Room
func init() {
}
-func startGame(path string, imageChannel chan *image.RGBA, inputChannel chan int) {
- ui.Run([]string{path}, imageChannel, inputChannel)
+func startGame(path string, roomID string, imageChannel chan *image.RGBA, inputChannel chan int) {
+ ui.Run([]string{path}, roomID, imageChannel, inputChannel)
}
func main() {
@@ -118,7 +118,7 @@ func initRoom(roomID, gameName string) string {
rtcSessions: []*webrtc.WebRTC{},
}
go fanoutScreen(imageChannel, roomID)
- go startGame("games/"+gameName, imageChannel, inputChannel)
+ go startGame("games/"+gameName, roomID, imageChannel, inputChannel)
return roomID
}
@@ -255,7 +255,7 @@ func postSession(w http.ResponseWriter, r *http.Request) {
rtcSessions: []*webrtc.WebRTC{},
}
go fanoutScreen(imageChannel, roomID)
- go startGame("games/"+postPacket.Game, imageChannel, inputChannel)
+ go startGame("games/"+postPacket.Game, roomID, imageChannel, inputChannel)
// fanin input channel
// fanout output channel
} else {
diff --git a/static/index_ws.html b/static/index_ws.html
index e7527605..af57ae65 100644
--- a/static/index_ws.html
+++ b/static/index_ws.html
@@ -36,6 +36,8 @@ Room ID:
X to sprint (B)
C is start button
V is select button
+ S to save
+ L to load
@@ -200,6 +202,8 @@ keyMap = {
88: "b",
67: "start",
86: "select",
+ 83: "save",
+ 76: "load",
}
INPUT_FPS = 100;
@@ -243,7 +247,7 @@ function sendInput() {
if (stateUnchange || unchangePacket > 0) {
st = "";
- ["a", "b", "select", "start", "up", "down", "left", "right"].forEach(elem => {
+ ["a", "b", "select", "start", "up", "down", "left", "right", "save", "load"].forEach(elem => {
st += keyState[elem]?1:0;
});
ss = parseInt(st, 2);
diff --git a/ui/director.go b/ui/director.go
index 0fb8ad7b..197dc0b0 100644
--- a/ui/director.go
+++ b/ui/director.go
@@ -20,14 +20,16 @@ type Director struct {
timestamp float64
imageChannel chan *image.RGBA
inputChannel chan int
+ roomID string
}
-func NewDirector(imageChannel chan *image.RGBA, inputChannel chan int) *Director {
+func NewDirector(roomID string, imageChannel chan *image.RGBA, inputChannel chan int) *Director {
// func NewDirector(audio *Audio, imageChannel chan *image.RGBA, inputChannel chan int) *Director {
director := Director{}
// director.audio = audio
director.imageChannel = imageChannel
director.inputChannel = inputChannel
+ director.roomID = roomID
return &director
}
@@ -70,7 +72,7 @@ func (d *Director) Run() {
}
func (d *Director) PlayGame(path string) {
- hash, err := hashFile(path)
+ hash, err := hashFile(path, d.roomID)
if err != nil {
log.Fatalln(err)
}
diff --git a/ui/gameview.go b/ui/gameview.go
index 7ef60b12..75d86867 100644
--- a/ui/gameview.go
+++ b/ui/gameview.go
@@ -20,14 +20,14 @@ type GameView struct {
record bool
frames []image.Image
- keyPressed [8]bool
+ keyPressed [10]bool
imageChannel chan *image.RGBA
inputChannel chan int
}
func NewGameView(director *Director, console *nes.Console, title, hash string, imageChannel chan *image.RGBA, inputChannel chan int) View {
- gameview := &GameView{director, console, title, hash, false, nil, [8]bool{false}, imageChannel, inputChannel}
+ gameview := &GameView{director, console, title, hash, false, nil, [10]bool{false}, imageChannel, inputChannel}
go gameview.ListenToInputChannel()
return gameview
}
@@ -35,7 +35,8 @@ func NewGameView(director *Director, console *nes.Console, title, hash string, i
func (view *GameView) ListenToInputChannel() {
for {
key := <-view.inputChannel
- s := fmt.Sprintf("%.8b", key)
+ s := fmt.Sprintf("%.10b", key)
+ fmt.Println(s)
for i := 0; i < len(s); i++ {
if s[i] == '1' {
view.keyPressed[i] = true
@@ -107,5 +108,17 @@ func (view *GameView) updateControllers() {
// buttons[nes.ButtonStart] = view.keyPressed[13]
// buttons[nes.ButtonSelect] = view.keyPressed[16]
// view.console.Controller1.SetButtons(buttons)
- view.console.Controller1.SetButtons(view.keyPressed)
+ var gameKeys [8]bool
+ copy(gameKeys[:], view.keyPressed[:8])
+
+ if view.keyPressed[8] {
+ fmt.Println("saving", view.hash)
+ view.console.SaveState(savePath(view.hash))
+ }
+ if view.keyPressed[9] {
+ fmt.Println("loading", view.hash)
+ view.console.LoadState(savePath(view.hash))
+ }
+
+ view.console.Controller1.SetButtons(gameKeys)
}
diff --git a/ui/run.go b/ui/run.go
index 65381393..642fc457 100644
--- a/ui/run.go
+++ b/ui/run.go
@@ -20,7 +20,7 @@ func init() {
runtime.LockOSThread()
}
-func Run(paths []string, imageChannel chan *image.RGBA, inputChannel chan int) {
+func Run(paths []string, roomID string, imageChannel chan *image.RGBA, inputChannel chan int) {
// TODO: stream audio
// initialize audio
// portaudio.Initialize()
@@ -33,7 +33,7 @@ func Run(paths []string, imageChannel chan *image.RGBA, inputChannel chan int) {
// defer audio.Stop()
// run director
- director := NewDirector(imageChannel, inputChannel)
+ director := NewDirector(roomID, imageChannel, inputChannel)
// director := NewDirector(audio, imageChannel, inputChannel)
director.Start(paths)
}
diff --git a/ui/util.go b/ui/util.go
index 22a78186..931ce8d8 100644
--- a/ui/util.go
+++ b/ui/util.go
@@ -45,13 +45,15 @@ func combineButtons(a, b [8]bool) [8]bool {
return result
}
-func hashFile(path string) (string, error) {
+// hashFile : signature of a room, maybe not need path
+func hashFile(path string, roomID string) (string, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
- return fmt.Sprintf("%x", md5.Sum(data)), nil
+ return fmt.Sprintf("%x", md5.Sum(append(data, []byte(roomID)...))), nil
}
+
func copyImage(src image.Image) *image.RGBA {
dst := image.NewRGBA(src.Bounds())
draw.Draw(dst, dst.Rect, src, image.ZP, draw.Src)