diff --git a/binding-sandbox/meson.build b/binding-sandbox/meson.build index c8774726..cb6d22c0 100644 --- a/binding-sandbox/meson.build +++ b/binding-sandbox/meson.build @@ -1,8 +1,8 @@ -global_sources += files([ +global_sources += files( 'binding-base.cpp', 'binding-util.cpp', 'module_rpg.cpp', 'sandbox.cpp', 'wasi.cpp', 'wasm-rt.cpp', -]) +) diff --git a/src/audio/alstream.cpp b/src/audio/alstream.cpp index 10600903..ede8272c 100644 --- a/src/audio/alstream.cpp +++ b/src/audio/alstream.cpp @@ -242,11 +242,7 @@ struct ALStreamOpenHandler : FileSystem::OpenHandler if (!strcmp(sig, "MThd")) { -#ifdef MKXPZ_RETRO - shState->midiState().initIfNeeded(); -#else shState->midiState().initIfNeeded(shState->config()); -#endif // MKXPZ_RETRO if (HAVE_FLUID) { diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index 17726d1d..97e78330 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -31,7 +31,9 @@ #include #include -#ifndef MKXPZ_RETRO +#ifdef MKXPZ_RETRO +# include "graphics.h" +#else # include "sdl-util.h" # include # include @@ -74,24 +76,16 @@ struct AudioPrivate MeWatchState state; } meWatch; -#ifdef MKXPZ_RETRO - AudioPrivate() -#else AudioPrivate(RGSSThreadData &rtData) -#endif // MKXPZ_RETRO : bgs(ALStream::Looped, "bgs"), me(ALStream::NotLooped, "me"), -#ifndef MKXPZ_RETRO se(rtData.config), +#ifndef MKXPZ_RETRO syncPoint(rtData.syncPoint), #endif // MKXPZ_RETRO volumeRatio(1) { -#ifdef MKXPZ_RETRO - for (int i = 0; i < 16; i++) { // TODO: read BGM track count from config -#else for (int i = 0; i < rtData.config.BGM.trackCount; i++) { -#endif // MKXPZ_RETRO std::string id = std::string("bgm" + std::to_string(i)); bgmTracks.push_back(new AudioStream(ALStream::Looped, id.c_str())); } @@ -123,9 +117,10 @@ struct AudioPrivate void meWatchProc() { -#ifdef MKXPZ_RETRO // TODO: use FPS - const float fadeOutStep = 1.f / (200 / 17); - const float fadeInStep = 1.f / (1000 / 17); +#ifdef MKXPZ_RETRO + const int fps = shState->graphics().getFrameRate(); + const float fadeOutStep = 5.f / fps; + const float fadeInStep = 1.f / fps; #else const float fadeOutStep = 1.f / (200 / AUDIO_SLEEP); const float fadeInStep = 1.f / (1000 / AUDIO_SLEEP); @@ -316,15 +311,9 @@ struct AudioPrivate #endif // MKXPZ_RETRO }; -#ifdef MKXPZ_RETRO -Audio::Audio() - : p(new AudioPrivate()) -{} -#else Audio::Audio(RGSSThreadData &rtData) : p(new AudioPrivate(rtData)) {} -#endif // MKXPZ_RETRO #ifdef MKXPZ_RETRO void Audio::render() { diff --git a/src/audio/audio.h b/src/audio/audio.h index 63786a59..8c6237e9 100644 --- a/src/audio/audio.h +++ b/src/audio/audio.h @@ -77,15 +77,13 @@ public: void reset(); -#ifdef MKXPZ_RETRO - Audio(); - ~Audio(); -#endif // MKXPZ_RETRO - -private: #ifndef MKXPZ_RETRO +private: +#endif // MKXPZ_RETRO Audio(RGSSThreadData &rtData); ~Audio(); +#ifdef MKXPZ_RETRO +private: #endif // MKXPZ_RETRO friend struct SharedStatePrivate; diff --git a/src/audio/sharedmidistate.h b/src/audio/sharedmidistate.h index c420c5cb..aac7bc5f 100644 --- a/src/audio/sharedmidistate.h +++ b/src/audio/sharedmidistate.h @@ -78,7 +78,7 @@ struct SharedMidiState } #ifdef MKXPZ_RETRO - void initIfNeeded() + void initIfNeeded(const Config &conf) { if (inited) return; @@ -93,8 +93,8 @@ struct SharedMidiState flSettings = fluid.new_settings(); fluid.settings_setnum(flSettings, "synth.gain", 1.0f); fluid.settings_setnum(flSettings, "synth.sample-rate", SYNTH_SAMPLERATE); - fluid.settings_setint(flSettings, "synth.chorus.active", 0); - fluid.settings_setint(flSettings, "synth.reverb.active", 0); + fluid.settings_setint(flSettings, "synth.chorus.active", conf.midi.chorus); + fluid.settings_setint(flSettings, "synth.reverb.active", conf.midi.reverb); extern const uint8_t mkxp_gmgsx_sf2[]; extern const size_t mkxp_gmgsx_sf2_len; diff --git a/src/audio/soundemitter.cpp b/src/audio/soundemitter.cpp index 67c187ad..4bedae81 100644 --- a/src/audio/soundemitter.cpp +++ b/src/audio/soundemitter.cpp @@ -94,17 +94,9 @@ arrayPushBack(std::vector &array, size_t size, size_t index) array[size-1] = v; } -#ifdef MKXPZ_RETRO -SoundEmitter::SoundEmitter() -#else SoundEmitter::SoundEmitter(const Config &conf) -#endif // MKXPZ_RETRO : bufferBytes(0), -#ifdef MKXPZ_RETRO - srcCount(6), // TODO: get from config -#else srcCount(conf.SE.sourceCount), -#endif // MKXPZ_RETRO alSrcs(srcCount), atchBufs(srcCount), srcPrio(srcCount) diff --git a/src/audio/soundemitter.h b/src/audio/soundemitter.h index dd6a64b3..0a834231 100644 --- a/src/audio/soundemitter.h +++ b/src/audio/soundemitter.h @@ -49,11 +49,7 @@ struct SoundEmitter /* Indices of sources, sorted by priority (lowest first) */ std::vector srcPrio; -#ifdef MKXPZ_RETRO - SoundEmitter(); -#else SoundEmitter(const Config &conf); -#endif // MKXPZ_RETRO ~SoundEmitter(); void play(const std::string &filename, diff --git a/src/config.cpp b/src/config.cpp index cdd7a295..e6376a38 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -6,7 +6,11 @@ // #include "config.h" -#include +#ifdef MKXPZ_RETRO +# include "core.h" +#else +# include +#endif // MKXPZ_RETRO #include #include @@ -28,7 +32,8 @@ namespace json = json5pp; -std::string prefPath(const char *org, const char *app) { +#ifndef MKXPZ_RETRO +static std::string prefPath(const char *org, const char *app) { char *path = SDL_GetPrefPath(org, app); if (!path) return std::string(""); @@ -36,8 +41,9 @@ std::string prefPath(const char *org, const char *app) { SDL_free(path); return ret; } +#endif // MKXPZ_RETRO -void fillStringVec(json::value &item, std::vector &vector) { +static void fillStringVec(json::value &item, std::vector &vector) { if (!item.is_array()) { if (item.is_string()) { vector.push_back(item.as_string()); @@ -53,7 +59,7 @@ void fillStringVec(json::value &item, std::vector &vector) { } } -bool copyObject(json::value &dest, json::value &src, const char *objectName = "") { +static bool copyObject(json::value &dest, json::value &src, const char *objectName = "") { assert(dest.is_object()); if (src.is_null()) return false; @@ -84,7 +90,8 @@ bool copyObject(json::value &dest, json::value &src, const char *objectName = "" return true; } -bool getEnvironmentBool(const char *env, bool defaultValue) { +#ifndef MKXPZ_RETRO +static bool getEnvironmentBool(const char *env, bool defaultValue) { const char *e = SDL_getenv(env); if (!e) return defaultValue; @@ -96,16 +103,40 @@ bool getEnvironmentBool(const char *env, bool defaultValue) { return defaultValue; } +#endif // MKXPZ_RETRO -json::value readConfFile(const char *path) { +static json::value readConfFile(const char *path) { json::value ret(0); - if (!mkxp_fs::fileExists(path)) { + +#ifdef MKXPZ_RETRO + FileSystem::File file(*mkxp_retro::fs, path, FileSystem::OpenMode::Read); + if (!file.is_open()) +#else + if (!mkxp_fs::fileExists(path)) +#endif // MKXPZ_RETRO + { return json::object({}); } try { +#ifdef MKXPZ_RETRO + std::vector buf(16); + size_t size = 0; + for (;;) { + PHYSFS_sint64 n = PHYSFS_readBytes(file.get(), buf.data() + size, buf.size() - size); + if (n <= 0) { + break; + } + size += n; + if (size >= buf.size()) { + buf.resize(buf.size() * 2); + } + } + std::string cfg(buf.begin(), buf.begin() + size); +#else std::string cfg = mkxp_fs::contentsOfFileAsString(path); +#endif // MKXPZ_RETRO ret = json::parse5(Encoding::convertString(cfg)); } catch (const std::exception &e) { @@ -121,7 +152,12 @@ json::value readConfFile(const char *path) { return ret; } -#define CONF_FILE "mkxp.json" +#define _CONF_FILE "mkxp.json" +#ifdef MKXPZ_RETRO +# define CONF_FILE "/mkxp-retro-game/" _CONF_FILE +#else +# define CONF_FILE _CONF_FILE +#endif // MKXPZ_RETRO Config::Config() {} @@ -254,17 +290,21 @@ try { exp } catch (...) {} SET_OPT(defScreenW, integer); SET_OPT(defScreenH, integer); +#ifndef MKXPZ_RETRO // Take a break real quick and witch to set game folder and read the game's ini if (!gameFolder.empty() && !mkxp_fs::setCurrentDirectory(gameFolder.c_str())) { throw Exception(Exception::MKXPError, "Unable to switch into gameFolder %s", gameFolder.c_str()); } +#endif // MKXPZ_RETRO readGameINI(); +#ifndef MKXPZ_RETRO // Now check for an extra mkxp.conf in the user's save directory and merge anything else from that userConfPath = mkxp_fs::normalizePath(std::string(customDataPath + "/" CONF_FILE).c_str(), 0, 1); json::value userConf = readConfFile(userConfPath.c_str()); copyObject(optsJ, userConf); +#endif // MKXPZ_RETRO // now RESUME @@ -342,8 +382,10 @@ try { exp } catch (...) {} SE.sourceCount = clamp(SE.sourceCount, 1, 64); BGM.trackCount = clamp(BGM.trackCount, 1, 16); +#ifndef MKXPZ_RETRO // Determine whether to open a console window on... Windows winConsole = getEnvironmentBool("MKXPZ_WINDOWS_CONSOLE", editor.debug); +#endif // MKXPZ_RETRO #ifdef __APPLE__ // Determine whether to use the Metal renderer on macOS @@ -351,11 +393,13 @@ try { exp } catch (...) {} preferMetalRenderer = isMetalSupported() && getEnvironmentBool("MKXPZ_MACOS_METAL", preferMetalRenderer); #endif +#ifndef MKXPZ_RETRO // Determine whether to allow manual selection of a game folder on startup // Only works on macOS atm, mainly used to test games located outside of the bundle. // The config is re-read after the window is already created, so some entries // may not take effect manualFolderSelect = getEnvironmentBool("MKXPZ_FOLDER_SELECT", false); +#endif // MKXPZ_RETRO raw = optsJ; } @@ -387,14 +431,29 @@ void Config::readGameINI() { return; } +#ifdef MKXPZ_RETRO + std::string iniFileName("/mkxp-retro-game/" + execName + ".ini"); + std::shared_ptr iniFile(new FileSystem::File(*mkxp_retro::fs, iniFileName.c_str(), FileSystem::OpenMode::Read)); +#else std::string iniFileName(execName + ".ini"); SDLRWStream iniFile(iniFileName.c_str(), "r"); +#endif // MKXPZ_RETRO bool convSuccess = false; +#ifdef MKXPZ_RETRO + if (iniFile->is_open()) +#else if (iniFile) +#endif // MKXPZ_RETRO { INIConfiguration ic; +#ifdef MKXPZ_RETRO + PHYSFSRWBuf buf(iniFile); + std::istream stream(&buf); + if (ic.load(stream)) +#else if (ic.load(iniFile.stream())) +#endif // MKXPZ_RETRO { GUARD(game.title = ic.getStringProperty("Game", "Title");); GUARD(game.scripts = ic.getStringProperty("Game", "Scripts");); @@ -429,7 +488,9 @@ void Config::readGameINI() { if (dataPathApp.empty()) dataPathApp = game.title; +#ifndef MKXPZ_RETRO customDataPath = mkxp_fs::normalizePath(prefPath(dataPathOrg.c_str(), dataPathApp.c_str()).c_str(), 0, 1); +#endif // MKXPZ_RETRO if (rgssVersion == 0) { /* Try to guess RGSS version based on Data/Scripts extension */ diff --git a/src/config.h b/src/config.h index b3e0cae3..bd627df6 100644 --- a/src/config.h +++ b/src/config.h @@ -35,7 +35,9 @@ struct Config { int rgssVersion; bool debugMode; +#ifndef MKXPZ_RETRO bool winConsole; +#endif // MKXPZ_RETRO bool preferMetalRenderer; bool displayFPS; bool printFPS; @@ -78,7 +80,9 @@ struct Config { } integerScaling; std::string gameFolder; +#ifndef MKXPZ_RETRO bool manualFolderSelect; +#endif // MKXPZ_RETRO bool anyAltToggleFS; bool enableReset; @@ -165,7 +169,9 @@ struct Config { std::string userConfPath; /* Internal */ +#ifndef MKXPZ_RETRO std::string customDataPath; +#endif // MKXPZ_RETRO Config(); diff --git a/src/core.cpp b/src/core.cpp index 0bbd5ed4..29fa2b59 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -37,6 +37,7 @@ #include "gl-fun.h" #include "glstate.h" #include "sharedmidistate.h" +#include "eventthread.h" using namespace mkxp_retro; using namespace mkxp_sandbox; @@ -59,6 +60,7 @@ static int16_t *sound_buf = NULL; static bool retro_framebuffer_supported; static bool shared_state_initialized; static PHYSFS_File *rgssad = NULL; +static retro_system_av_info av_info; namespace mkxp_retro { retro_log_printf_t log_printf; @@ -107,6 +109,8 @@ boost::optional mkxp_retro::sandbox; boost::optional