mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-23 18:17:32 +00:00
Fix data race
This commit is contained in:
parent
51c46e90e8
commit
4e7e44fcd9
3 changed files with 28 additions and 14 deletions
|
|
@ -44,6 +44,10 @@ func (d *Director) SetView(view *GameView) {
|
|||
d.timestamp = float64(time.Now().Nanosecond()) / float64(time.Second)
|
||||
}
|
||||
|
||||
func (d *Director) UpdateInput(input int) {
|
||||
d.view.UpdateInput(input)
|
||||
}
|
||||
|
||||
func (d *Director) Step() {
|
||||
timestamp := float64(time.Now().Nanosecond()) / float64(time.Second)
|
||||
dt := timestamp - d.timestamp
|
||||
|
|
@ -68,6 +72,8 @@ L:
|
|||
|
||||
select {
|
||||
// if there is event from close channel => the game is ended
|
||||
case input := <-d.inputChannel:
|
||||
d.UpdateInput(input)
|
||||
case <-d.Done:
|
||||
break L
|
||||
default:
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package ui
|
|||
|
||||
import (
|
||||
"image"
|
||||
|
||||
"github.com/giongto35/cloud-game/nes"
|
||||
)
|
||||
|
||||
|
|
@ -29,9 +30,9 @@ const (
|
|||
const NumKeys = 8
|
||||
|
||||
type GameView struct {
|
||||
console *nes.Console
|
||||
title string
|
||||
hash string
|
||||
console *nes.Console
|
||||
title string
|
||||
hash string
|
||||
|
||||
// equivalent to the list key pressed const above
|
||||
keyPressed [NumKeys * 2]bool
|
||||
|
|
@ -53,22 +54,30 @@ func NewGameView(console *nes.Console, title, hash string, imageChannel chan *im
|
|||
inputChannel: inputChannel,
|
||||
}
|
||||
|
||||
go gameview.ListenToInputChannel()
|
||||
return gameview
|
||||
}
|
||||
|
||||
// ListenToInputChannel listen from input channel streamm, which is exposed to WebRTC session
|
||||
func (view *GameView) ListenToInputChannel() {
|
||||
for {
|
||||
keysInBinary := <-view.inputChannel
|
||||
for i := 0; i < NumKeys*2; i++ {
|
||||
b := ((keysInBinary & 1) == 1)
|
||||
view.keyPressed[i] = (view.keyPressed[i] && b) || b
|
||||
keysInBinary = keysInBinary >> 1
|
||||
}
|
||||
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 := <-view.inputChannel
|
||||
//for i := 0; i < NumKeys*2; i++ {
|
||||
//b := ((keysInBinary & 1) == 1)
|
||||
//view.keyPressed[i] = (view.keyPressed[i] && b) || b
|
||||
//keysInBinary = keysInBinary >> 1
|
||||
//}
|
||||
//}
|
||||
//}
|
||||
|
||||
// Enter enter the game view.
|
||||
func (view *GameView) Enter() {
|
||||
// Always reset game
|
||||
|
|
@ -152,5 +161,4 @@ func (view *GameView) updateControllers() {
|
|||
view.console.Controller1.SetButtons(player1Keys)
|
||||
view.console.Controller2.SetButtons(player2Keys)
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
2
ws.go
2
ws.go
|
|
@ -88,7 +88,7 @@ func (c *Client) receive(id string, f func(response WSPacket) (request WSPacket)
|
|||
if err != nil {
|
||||
log.Println("[!] json marshal error:", err)
|
||||
}
|
||||
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
//c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
c.sendLock.Lock()
|
||||
c.conn.WriteMessage(websocket.TextMessage, resp)
|
||||
c.sendLock.Unlock()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue