Compare commits

...
Author SHA1 Message Date
Jean-André Santoni 7bc9c5e773 Forgot to add netplay.go 2020-05-04 17:19:57 +07:00
Jean-André Santoni 701a52803f Blocking netplay that works for local network 2020-05-04 16:33:39 +07:00
4 changed files with 102 additions and 4 deletions

No files matched your search

+3
View File
@@ -16,6 +16,7 @@ import (
"github.com/libretro/ludo/audio"
"github.com/libretro/ludo/input"
"github.com/libretro/ludo/libretro"
"github.com/libretro/ludo/netplay"
"github.com/libretro/ludo/options"
"github.com/libretro/ludo/patch"
"github.com/libretro/ludo/savefiles"
@@ -197,6 +198,8 @@ func LoadGame(gamePath string) error {
log.Println("[Core]: Game loaded: " + gamePath)
savefiles.LoadSRAM()
netplay.Init()
return nil
}
+54 -4
View File
@@ -4,8 +4,11 @@
package input
import (
"log"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/libretro/ludo/libretro"
"github.com/libretro/ludo/netplay"
ntf "github.com/libretro/ludo/notifications"
"github.com/libretro/ludo/video"
)
@@ -110,14 +113,61 @@ func pollJoypads(state inputstate) inputstate {
return state
}
// pollKeyboard processes keyboard keys
func pollKeyboard(state inputstate) inputstate {
func keyboardToPlayer(st inputstate, p int) inputstate {
for k, v := range keyBinds {
if vid.Window.GetKey(k) == glfw.Press {
state[0][v] = true
st[p][v] = true
}
}
return state
return st
}
func playerToNet(st inputstate, p int) {
netoutput := [20]byte{}
for i, b := range st[p] {
if b {
netoutput[i] = 1
}
}
if _, err := netplay.Conn.Write(netoutput[:]); err != nil {
log.Println(err)
}
}
func netToPlayer(st inputstate, p int) inputstate {
netinput := [20]byte{}
if _, err := netplay.Conn.Read(netinput[:]); err != nil {
log.Println(err)
}
for i, b := range netinput {
if b == 1 {
st[p][i] = true
}
}
return st
}
// pollKeyboard processes keyboard keys
func pollKeyboard(st inputstate) inputstate {
if netplay.Conn != nil { // Netplay mode
if netplay.Listen != "" { // Host mode
st = keyboardToPlayer(st, 0)
// Write
playerToNet(st, 0)
// Read
st = netToPlayer(st, 1)
} else if netplay.Join != "" { // Guest mode
st = keyboardToPlayer(st, 1)
// Write
playerToNet(st, 1)
// Read
st = netToPlayer(st, 0)
}
} else { // Non netplay mode
st = keyboardToPlayer(st, 0)
}
return st
}
// Compute the keys pressed or released during this frame
+3
View File
@@ -12,6 +12,7 @@ import (
"github.com/libretro/ludo/history"
"github.com/libretro/ludo/input"
"github.com/libretro/ludo/menu"
"github.com/libretro/ludo/netplay"
ntf "github.com/libretro/ludo/notifications"
"github.com/libretro/ludo/playlists"
"github.com/libretro/ludo/scanner"
@@ -72,6 +73,8 @@ func main() {
flag.StringVar(&state.Global.CorePath, "L", "", "Path to the libretro core")
flag.BoolVar(&state.Global.Verbose, "v", false, "Verbose logs")
flag.BoolVar(&state.Global.LudOS, "ludos", false, "Expose the features related to LudOS")
flag.StringVar(&netplay.Listen, "listen", "", "Example: :8080")
flag.StringVar(&netplay.Join, "join", "", "Peer IP and port to join")
flag.Parse()
args := flag.Args()
+42
View File
@@ -0,0 +1,42 @@
package netplay
import (
"net"
ntf "github.com/libretro/ludo/notifications"
)
// Listen is used by the netplay host, listening address and port
var Listen string
// Join is used by the netplay guest, address of the host
var Join string
// Conn is the connection between two players
var Conn net.Conn
// Init initialises a netplay session between two players
func Init() {
if Listen != "" { // Host mode
ln, err := net.Listen("tcp", Listen)
if err != nil {
ntf.DisplayAndLog(ntf.Error, "Netplay", err.Error())
return
}
Conn, err = ln.Accept()
if err != nil {
ntf.DisplayAndLog(ntf.Error, "Netplay", err.Error())
return
}
ntf.DisplayAndLog(ntf.Success, "Netplay", "Player #2 is connected.")
} else if Join != "" { // Guest mode
var err error
Conn, err = net.Dial("tcp", Join)
if err != nil {
ntf.DisplayAndLog(ntf.Error, "Netplay", err.Error())
return
}
ntf.DisplayAndLog(ntf.Success, "Netplay", "Connected.")
}
}