mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-04-17 09:33:51 +00:00
Add Windows cross-platform support alongside Linux
Replace POSIX-specific socket and process APIs with portable abstractions so the project builds on both Windows and Linux. - Add include/network/net_platform.hpp: Winsock2/POSIX socket abstraction (socket types, non-blocking, error handling, WSAStartup lifecycle) - Add include/platform/process.hpp: CreateProcess/fork+exec abstraction for spawning ffplay subprocesses - Update network module (tcp_socket, world_socket) to use portable socket helpers instead of raw POSIX calls - Update audio module (music_manager, footstep_manager, activity_sound_manager) to use portable process helpers instead of fork/exec/kill/waitpid - Replace hardcoded /tmp/ paths with std::filesystem::temp_directory_path() - Link ws2_32 and SDL2main on Windows in CMakeLists.txt
This commit is contained in:
parent
dd126c6e4b
commit
6bf3fa4ed4
14 changed files with 416 additions and 186 deletions
|
|
@ -1,13 +1,11 @@
|
|||
#include "audio/activity_sound_manager.hpp"
|
||||
#include "pipeline/asset_manager.hpp"
|
||||
#include "core/logger.hpp"
|
||||
#include "platform/process.hpp"
|
||||
#include <algorithm>
|
||||
#include <csignal>
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <cctype>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace wowee {
|
||||
namespace audio {
|
||||
|
|
@ -193,7 +191,7 @@ void ActivitySoundManager::rebuildHardLandClipsForProfile(const std::string& rac
|
|||
bool ActivitySoundManager::playOneShot(const std::vector<Sample>& clips, float volume, float pitchLo, float pitchHi) {
|
||||
if (clips.empty()) return false;
|
||||
reapProcesses();
|
||||
if (oneShotPid > 0) return false;
|
||||
if (oneShotPid != INVALID_PROCESS) return false;
|
||||
|
||||
std::uniform_int_distribution<size_t> clipDist(0, clips.size() - 1);
|
||||
const Sample& sample = clips[clipDist(rng)];
|
||||
|
|
@ -209,24 +207,16 @@ bool ActivitySoundManager::playOneShot(const std::vector<Sample>& clips, float v
|
|||
std::string filter = "asetrate=44100*" + std::to_string(pitch) +
|
||||
",aresample=44100,volume=" + std::to_string(volume);
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
setpgid(0, 0);
|
||||
FILE* outFile = freopen("/dev/null", "w", stdout);
|
||||
FILE* errFile = freopen("/dev/null", "w", stderr);
|
||||
(void)outFile; (void)errFile;
|
||||
execlp("ffplay", "ffplay", "-nodisp", "-autoexit", "-loglevel", "quiet",
|
||||
"-af", filter.c_str(), oneShotTempPath.c_str(), nullptr);
|
||||
_exit(1);
|
||||
} else if (pid > 0) {
|
||||
oneShotPid = pid;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
oneShotPid = platform::spawnProcess({
|
||||
"-nodisp", "-autoexit", "-loglevel", "quiet",
|
||||
"-af", filter, oneShotTempPath
|
||||
});
|
||||
|
||||
return oneShotPid != INVALID_PROCESS;
|
||||
}
|
||||
|
||||
void ActivitySoundManager::startSwimLoop() {
|
||||
if (swimLoopPid > 0 || swimLoopClips.empty()) return;
|
||||
if (swimLoopPid != INVALID_PROCESS || swimLoopClips.empty()) return;
|
||||
std::uniform_int_distribution<size_t> clipDist(0, swimLoopClips.size() - 1);
|
||||
const Sample& sample = swimLoopClips[clipDist(rng)];
|
||||
|
||||
|
|
@ -238,50 +228,26 @@ void ActivitySoundManager::startSwimLoop() {
|
|||
float volume = swimMoving ? 0.85f : 0.65f;
|
||||
std::string filter = "volume=" + std::to_string(volume);
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
setpgid(0, 0);
|
||||
FILE* outFile = freopen("/dev/null", "w", stdout);
|
||||
FILE* errFile = freopen("/dev/null", "w", stderr);
|
||||
(void)outFile; (void)errFile;
|
||||
execlp("ffplay", "ffplay", "-nodisp", "-autoexit", "-loop", "0", "-loglevel", "quiet",
|
||||
"-af", filter.c_str(), loopTempPath.c_str(), nullptr);
|
||||
_exit(1);
|
||||
} else if (pid > 0) {
|
||||
swimLoopPid = pid;
|
||||
}
|
||||
swimLoopPid = platform::spawnProcess({
|
||||
"-nodisp", "-autoexit", "-loop", "0", "-loglevel", "quiet",
|
||||
"-af", filter, loopTempPath
|
||||
});
|
||||
}
|
||||
|
||||
void ActivitySoundManager::stopSwimLoop() {
|
||||
if (swimLoopPid > 0) {
|
||||
kill(-swimLoopPid, SIGTERM);
|
||||
kill(swimLoopPid, SIGTERM);
|
||||
int status = 0;
|
||||
waitpid(swimLoopPid, &status, 0);
|
||||
swimLoopPid = -1;
|
||||
}
|
||||
platform::killProcess(swimLoopPid);
|
||||
}
|
||||
|
||||
void ActivitySoundManager::stopOneShot() {
|
||||
if (oneShotPid > 0) {
|
||||
kill(-oneShotPid, SIGTERM);
|
||||
kill(oneShotPid, SIGTERM);
|
||||
int status = 0;
|
||||
waitpid(oneShotPid, &status, 0);
|
||||
oneShotPid = -1;
|
||||
}
|
||||
platform::killProcess(oneShotPid);
|
||||
}
|
||||
|
||||
void ActivitySoundManager::reapProcesses() {
|
||||
if (oneShotPid > 0) {
|
||||
int status = 0;
|
||||
pid_t result = waitpid(oneShotPid, &status, WNOHANG);
|
||||
if (result == oneShotPid) oneShotPid = -1;
|
||||
if (oneShotPid != INVALID_PROCESS) {
|
||||
platform::isProcessRunning(oneShotPid);
|
||||
}
|
||||
if (swimLoopPid > 0) {
|
||||
int status = 0;
|
||||
pid_t result = waitpid(swimLoopPid, &status, WNOHANG);
|
||||
if (result == swimLoopPid) swimLoopPid = -1;
|
||||
if (swimLoopPid != INVALID_PROCESS) {
|
||||
platform::isProcessRunning(swimLoopPid);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,11 @@
|
|||
#include "audio/footstep_manager.hpp"
|
||||
#include "pipeline/asset_manager.hpp"
|
||||
#include "core/logger.hpp"
|
||||
#include "platform/process.hpp"
|
||||
#include <algorithm>
|
||||
#include <csignal>
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace wowee {
|
||||
namespace audio {
|
||||
|
|
@ -114,24 +112,14 @@ void FootstepManager::preloadSurface(FootstepSurface surface, const std::vector<
|
|||
}
|
||||
|
||||
void FootstepManager::stopCurrentProcess() {
|
||||
if (playerPid > 0) {
|
||||
kill(-playerPid, SIGTERM);
|
||||
kill(playerPid, SIGTERM);
|
||||
int status = 0;
|
||||
waitpid(playerPid, &status, 0);
|
||||
playerPid = -1;
|
||||
}
|
||||
platform::killProcess(playerPid);
|
||||
}
|
||||
|
||||
void FootstepManager::reapFinishedProcess() {
|
||||
if (playerPid <= 0) {
|
||||
if (playerPid == INVALID_PROCESS) {
|
||||
return;
|
||||
}
|
||||
int status = 0;
|
||||
pid_t result = waitpid(playerPid, &status, WNOHANG);
|
||||
if (result == playerPid) {
|
||||
playerPid = -1;
|
||||
}
|
||||
platform::isProcessRunning(playerPid);
|
||||
}
|
||||
|
||||
bool FootstepManager::playRandomStep(FootstepSurface surface, bool sprinting) {
|
||||
|
|
@ -153,7 +141,7 @@ bool FootstepManager::playRandomStep(FootstepSurface surface, bool sprinting) {
|
|||
}
|
||||
|
||||
// Keep one active step at a time to avoid ffplay process buildup.
|
||||
if (playerPid > 0) {
|
||||
if (playerPid != INVALID_PROCESS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -178,18 +166,12 @@ bool FootstepManager::playRandomStep(FootstepSurface surface, bool sprinting) {
|
|||
std::string filter = "asetrate=44100*" + std::to_string(pitch) +
|
||||
",aresample=44100,volume=" + std::to_string(volume);
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
setpgid(0, 0);
|
||||
FILE* outFile = freopen("/dev/null", "w", stdout);
|
||||
FILE* errFile = freopen("/dev/null", "w", stderr);
|
||||
(void)outFile;
|
||||
(void)errFile;
|
||||
execlp("ffplay", "ffplay", "-nodisp", "-autoexit", "-loglevel", "quiet",
|
||||
"-af", filter.c_str(), tempFilePath.c_str(), nullptr);
|
||||
_exit(1);
|
||||
} else if (pid > 0) {
|
||||
playerPid = pid;
|
||||
playerPid = platform::spawnProcess({
|
||||
"-nodisp", "-autoexit", "-loglevel", "quiet",
|
||||
"-af", filter, tempFilePath
|
||||
});
|
||||
|
||||
if (playerPid != INVALID_PROCESS) {
|
||||
lastPlayTime = now;
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +1,14 @@
|
|||
#include "audio/music_manager.hpp"
|
||||
#include "pipeline/asset_manager.hpp"
|
||||
#include "core/logger.hpp"
|
||||
#include "platform/process.hpp"
|
||||
#include <fstream>
|
||||
#include <cstdlib>
|
||||
#include <csignal>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace wowee {
|
||||
namespace audio {
|
||||
|
||||
MusicManager::MusicManager() {
|
||||
tempFilePath = "/tmp/wowee_music.mp3";
|
||||
tempFilePath = platform::getTempFilePath("wowee_music.mp3");
|
||||
}
|
||||
|
||||
MusicManager::~MusicManager() {
|
||||
|
|
@ -54,29 +51,24 @@ void MusicManager::playMusic(const std::string& mpqPath, bool loop) {
|
|||
out.close();
|
||||
|
||||
// Play with ffplay in background
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
// Child process — create new process group so we can kill all children
|
||||
setpgid(0, 0);
|
||||
// Redirect output to /dev/null
|
||||
freopen("/dev/null", "w", stdout);
|
||||
freopen("/dev/null", "w", stderr);
|
||||
std::vector<std::string> args;
|
||||
args.push_back("-nodisp");
|
||||
args.push_back("-autoexit");
|
||||
if (loop) {
|
||||
args.push_back("-loop");
|
||||
args.push_back("0");
|
||||
}
|
||||
args.push_back("-volume");
|
||||
args.push_back("30");
|
||||
args.push_back(tempFilePath);
|
||||
|
||||
if (loop) {
|
||||
execlp("ffplay", "ffplay", "-nodisp", "-autoexit", "-loop", "0",
|
||||
"-volume", "30", tempFilePath.c_str(), nullptr);
|
||||
} else {
|
||||
execlp("ffplay", "ffplay", "-nodisp", "-autoexit",
|
||||
"-volume", "30", tempFilePath.c_str(), nullptr);
|
||||
}
|
||||
_exit(1); // exec failed
|
||||
} else if (pid > 0) {
|
||||
playerPid = pid;
|
||||
playerPid = platform::spawnProcess(args);
|
||||
if (playerPid != INVALID_PROCESS) {
|
||||
playing = true;
|
||||
currentTrack = mpqPath;
|
||||
LOG_INFO("Music: Playing ", mpqPath);
|
||||
} else {
|
||||
LOG_ERROR("Music: fork() failed");
|
||||
LOG_ERROR("Music: Failed to spawn ffplay process");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -104,12 +96,8 @@ void MusicManager::crossfadeTo(const std::string& mpqPath, float fadeMs) {
|
|||
|
||||
void MusicManager::update(float deltaTime) {
|
||||
// Check if player process is still running
|
||||
if (playerPid > 0) {
|
||||
int status;
|
||||
pid_t result = waitpid(playerPid, &status, WNOHANG);
|
||||
if (result == playerPid) {
|
||||
// Process ended
|
||||
playerPid = -1;
|
||||
if (playerPid != INVALID_PROCESS) {
|
||||
if (!platform::isProcessRunning(playerPid)) {
|
||||
playing = false;
|
||||
}
|
||||
}
|
||||
|
|
@ -127,13 +115,8 @@ void MusicManager::update(float deltaTime) {
|
|||
}
|
||||
|
||||
void MusicManager::stopCurrentProcess() {
|
||||
if (playerPid > 0) {
|
||||
// Kill the entire process group (ffplay may spawn children)
|
||||
kill(-playerPid, SIGTERM);
|
||||
kill(playerPid, SIGTERM);
|
||||
int status;
|
||||
waitpid(playerPid, &status, 0);
|
||||
playerPid = -1;
|
||||
if (playerPid != INVALID_PROCESS) {
|
||||
platform::killProcess(playerPid);
|
||||
playing = false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue