Compare commits

...
7 Commits
5 changed files with 173 additions and 13 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
}
+38
View File
@@ -0,0 +1,38 @@
package delay
import (
"log"
"github.com/libretro/ludo/netplay"
)
var LocalQueue chan [20]bool
var RemoteQueue chan [20]bool
var Count uint64
var Delay uint64 = 5
func init() {
LocalQueue = make(chan [20]bool, 60)
RemoteQueue = make(chan [20]bool, 60)
}
func ReceiveInputs() {
for {
netinput := [20]byte{}
if _, err := netplay.Conn.Read(netinput[:]); err != nil {
log.Fatalln(err)
}
Count++
playerInput := [20]bool{}
for i, b := range netinput {
if b == 1 {
playerInput[i] = true
}
}
RemoteQueue <- playerInput
}
}
+74 -7
View File
@@ -4,8 +4,12 @@
package input
import (
"log"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/libretro/ludo/delay"
"github.com/libretro/ludo/libretro"
"github.com/libretro/ludo/netplay"
ntf "github.com/libretro/ludo/notifications"
"github.com/libretro/ludo/video"
)
@@ -110,14 +114,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
@@ -133,15 +184,31 @@ func getPressedReleased(new inputstate, old inputstate) (inputstate, inputstate)
// Poll calculates the input state. It is meant to be called for each frame.
func Poll() {
NewState = reset(NewState)
NewState = pollJoypads(NewState)
NewState = pollKeyboard(NewState)
if netplay.Listen != "" { // Host mode
NewState[0] = <-delay.LocalQueue
NewState[1] = <-delay.RemoteQueue
} else if netplay.Join != "" { // Guest mode
NewState[1] = <-delay.LocalQueue
NewState[0] = <-delay.RemoteQueue
}
// NewState = pollJoypads(NewState)
// NewState = pollKeyboard(NewState)
Pressed, Released = getPressedReleased(NewState, OldState)
// Store the old input state for comparisions
OldState = NewState
}
func Reset() {
NewState = reset(NewState)
}
func EnQueue() {
st := pollKeyboard(NewState)
delay.LocalQueue <- st[0]
playerToNet(st, 0)
}
// State is a callback passed to core.SetInputState
// It returns 1 if the button corresponding to the parameters is pressed
func State(port uint, device uint32, index uint, id uint) int16 {
+16 -6
View File
@@ -9,9 +9,11 @@ import (
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/libretro/ludo/audio"
"github.com/libretro/ludo/core"
"github.com/libretro/ludo/delay"
"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"
@@ -35,13 +37,17 @@ func runLoop(vid *video.Video, m *menu.Menu) {
ntf.Process(dt)
vid.ResizeViewport()
if !state.Global.MenuActive {
input.Reset()
if state.Global.CoreRunning {
state.Global.Core.Run()
if state.Global.Core.FrameTimeCallback != nil {
state.Global.Core.FrameTimeCallback.Callback(state.Global.Core.FrameTimeCallback.Reference)
}
if state.Global.Core.AudioCallback != nil {
state.Global.Core.AudioCallback.Callback()
input.EnQueue()
if delay.Count > delay.Delay {
state.Global.Core.Run()
if state.Global.Core.FrameTimeCallback != nil {
state.Global.Core.FrameTimeCallback.Callback(state.Global.Core.FrameTimeCallback.Reference)
}
if state.Global.Core.AudioCallback != nil {
state.Global.Core.AudioCallback.Callback()
}
}
}
vid.Render()
@@ -72,6 +78,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()
@@ -117,6 +125,8 @@ func main() {
ntf.DisplayAndLog(ntf.Error, "Menu", err.Error())
} else {
m.WarpToQuickMenu()
go delay.ReceiveInputs()
}
}
+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.")
}
}