From dadd5ebad045e75c3c3ff6ac4e3b3f65a6c30dfd Mon Sep 17 00:00:00 2001 From: giongto35 Date: Fri, 30 Aug 2019 02:47:08 +0800 Subject: [PATCH] Update NES emulator to follow correct order (#74) --- emulator/gameview.go | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/emulator/gameview.go b/emulator/gameview.go index b5688ea5..5a1f26a5 100644 --- a/emulator/gameview.go +++ b/emulator/gameview.go @@ -40,6 +40,19 @@ const ( AppAudio = 1 ) +var bindNESKeys = map[int]int{ + 0: nes.ButtonA, + 1: nes.ButtonB, + 2: -1, + 3: -1, + 4: nes.ButtonSelect, + 5: nes.ButtonStart, + 6: nes.ButtonUp, + 7: nes.ButtonDown, + 8: nes.ButtonLeft, + 9: nes.ButtonRight, +} + type GameView struct { console *nes.Console title string @@ -77,22 +90,19 @@ func NewGameView(console *nes.Console, title, saveFile string, imageChannel chan return gameview } -// ListenToInputChannel listen from input channel streamm, which is exposed to WebRTC session -//func (view *GameView) UpdateInput(keysInBinary int) { -//for i := 0; i < NumKeys*2; i++ { -//b := ((keysInBinary & 1) == 1) -//view.keyPressed[i] = (view.keyPressed[i] && b) || b -//keysInBinary = keysInBinary >> 1 -//} -//} - // ListenToInputChannel listen from input channel streamm, which is exposed to WebRTC session func (view *GameView) ListenToInputChannel() { for keysInBinary := range view.inputChannel { - for i := 0; i < NumKeys*2; i++ { - b := ((keysInBinary & 1) == 1) - view.keyPressed[i] = (view.keyPressed[i] && b) || b + for i := 0; i < NumKeys; i++ { + + key, ok := bindNESKeys[i] + isPressed := ((keysInBinary & 1) == 1) keysInBinary = keysInBinary >> 1 + if !ok || key == -1 { + continue + } + view.keyPressed[key] = (view.keyPressed[key] && isPressed) || isPressed + } } }