2021-05-24 05:06:51 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2021-05-25 02:02:57 +00:00
|
|
|
#include <numeric>
|
2022-02-01 00:06:54 +00:00
|
|
|
#include <iostream>
|
2021-05-24 05:06:51 +00:00
|
|
|
|
2022-05-04 00:27:19 +00:00
|
|
|
#include "ImGuiEngine.hpp"
|
|
|
|
#include "Runtime/Graphics/CGraphics.hpp"
|
|
|
|
#include "Runtime/MP1/MP1.hpp"
|
|
|
|
#include "Runtime/ConsoleVariables/FileStoreManager.hpp"
|
|
|
|
#include "Runtime/ConsoleVariables/CVarManager.hpp"
|
2022-07-29 20:46:51 +00:00
|
|
|
#include "Runtime/CInfiniteLoopDetector.hpp"
|
2022-05-04 00:27:19 +00:00
|
|
|
#include "amuse/BooBackend.hpp"
|
|
|
|
|
|
|
|
#include "logvisor/logvisor.hpp"
|
|
|
|
|
2022-03-12 19:13:01 +00:00
|
|
|
#ifdef _WIN32
|
2022-02-22 05:53:57 +00:00
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#endif
|
|
|
|
#ifndef NOMINMAX
|
|
|
|
#define NOMINMAX
|
|
|
|
#endif
|
|
|
|
#include <Windows.h>
|
2022-06-02 05:54:22 +00:00
|
|
|
#include <shellapi.h>
|
2022-02-22 05:53:57 +00:00
|
|
|
#endif
|
|
|
|
|
2021-05-24 05:06:51 +00:00
|
|
|
#include "../version.h"
|
|
|
|
|
2022-07-29 20:46:51 +00:00
|
|
|
// #include <fenv.h>
|
|
|
|
// #pragma STDC FENV_ACCESS ON
|
2022-02-21 02:28:07 +00:00
|
|
|
|
2022-07-29 20:16:55 +00:00
|
|
|
#include <aurora/event.h>
|
|
|
|
#include <aurora/main.h>
|
2022-07-29 20:46:51 +00:00
|
|
|
#include <dolphin/vi.h>
|
2022-08-09 06:11:23 +00:00
|
|
|
#include <SDL_messagebox.h>
|
2022-02-01 00:06:54 +00:00
|
|
|
|
2021-05-24 05:06:51 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
|
|
|
class Limiter {
|
2021-05-30 19:03:35 +00:00
|
|
|
using delta_clock = std::chrono::high_resolution_clock;
|
|
|
|
using duration_t = std::chrono::nanoseconds;
|
2021-05-24 05:06:51 +00:00
|
|
|
|
|
|
|
public:
|
2021-05-30 19:03:35 +00:00
|
|
|
void Reset() { m_oldTime = delta_clock::now(); }
|
|
|
|
|
|
|
|
void Sleep(duration_t targetFrameTime) {
|
|
|
|
if (targetFrameTime.count() == 0) {
|
2021-05-24 05:06:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto start = delta_clock::now();
|
2021-05-30 19:03:35 +00:00
|
|
|
duration_t adjustedSleepTime = SleepTime(targetFrameTime);
|
|
|
|
if (adjustedSleepTime.count() > 0) {
|
2021-06-29 04:12:41 +00:00
|
|
|
NanoSleep(adjustedSleepTime);
|
2021-05-30 19:03:35 +00:00
|
|
|
duration_t overslept = TimeSince(start) - adjustedSleepTime;
|
|
|
|
if (overslept < duration_t{targetFrameTime}) {
|
2021-05-24 05:06:51 +00:00
|
|
|
m_overheadTimes[m_overheadTimeIdx] = overslept;
|
|
|
|
m_overheadTimeIdx = (m_overheadTimeIdx + 1) % m_overheadTimes.size();
|
|
|
|
}
|
|
|
|
}
|
2021-05-30 19:03:35 +00:00
|
|
|
Reset();
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-30 19:03:35 +00:00
|
|
|
duration_t SleepTime(duration_t targetFrameTime) {
|
|
|
|
const auto sleepTime = duration_t{targetFrameTime} - TimeSince(m_oldTime);
|
|
|
|
m_overhead = std::accumulate(m_overheadTimes.begin(), m_overheadTimes.end(), duration_t{}) / m_overheadTimes.size();
|
2021-05-24 05:06:51 +00:00
|
|
|
if (sleepTime > m_overhead) {
|
|
|
|
return sleepTime - m_overhead;
|
|
|
|
}
|
2021-05-30 19:03:35 +00:00
|
|
|
return duration_t{0};
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
delta_clock::time_point m_oldTime;
|
2021-05-30 19:03:35 +00:00
|
|
|
std::array<duration_t, 4> m_overheadTimes{};
|
2021-05-24 05:06:51 +00:00
|
|
|
size_t m_overheadTimeIdx = 0;
|
2021-05-30 19:03:35 +00:00
|
|
|
duration_t m_overhead = duration_t{0};
|
2021-05-24 05:06:51 +00:00
|
|
|
|
2021-05-30 19:03:35 +00:00
|
|
|
duration_t TimeSince(delta_clock::time_point start) {
|
|
|
|
return std::chrono::duration_cast<duration_t>(delta_clock::now() - start);
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
2021-06-29 04:12:41 +00:00
|
|
|
|
|
|
|
#if _WIN32
|
|
|
|
bool m_initialized;
|
|
|
|
double m_countPerNs;
|
|
|
|
|
|
|
|
void NanoSleep(const duration_t duration) {
|
|
|
|
if (!m_initialized) {
|
|
|
|
LARGE_INTEGER freq;
|
|
|
|
QueryPerformanceFrequency(&freq);
|
|
|
|
m_countPerNs = static_cast<double>(freq.QuadPart) / 1000000000.0;
|
|
|
|
m_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
|
|
|
|
auto tickCount = static_cast<LONGLONG>(static_cast<double>(duration.count()) * m_countPerNs);
|
|
|
|
LARGE_INTEGER count;
|
|
|
|
QueryPerformanceCounter(&count);
|
|
|
|
if (ms > 10) {
|
|
|
|
// Adjust for Sleep overhead
|
|
|
|
::Sleep(ms - 10);
|
|
|
|
}
|
|
|
|
auto end = count.QuadPart + tickCount;
|
|
|
|
do {
|
|
|
|
QueryPerformanceCounter(&count);
|
|
|
|
} while (count.QuadPart < end);
|
|
|
|
}
|
|
|
|
#else
|
2022-02-07 10:45:56 +00:00
|
|
|
void NanoSleep(const duration_t duration) { std::this_thread::sleep_for(duration); }
|
2021-06-29 04:12:41 +00:00
|
|
|
#endif
|
2021-05-24 05:06:51 +00:00
|
|
|
};
|
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
extern std::string ExeDir;
|
2021-05-24 05:06:51 +00:00
|
|
|
|
|
|
|
namespace metaforce {
|
|
|
|
static logvisor::Module Log{"Metaforce"};
|
|
|
|
|
|
|
|
std::optional<MP1::CMain> g_mainMP1;
|
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
static std::string CPUFeatureString(const zeus::CPUInfo& cpuInf) {
|
|
|
|
std::string features;
|
2021-05-24 05:06:51 +00:00
|
|
|
#if defined(__x86_64__) || defined(_M_X64)
|
2021-06-30 18:20:45 +00:00
|
|
|
auto AddFeature = [&features](const char* str) {
|
2021-05-24 05:06:51 +00:00
|
|
|
if (!features.empty())
|
2021-06-30 18:20:45 +00:00
|
|
|
features += ", ";
|
2021-05-24 05:06:51 +00:00
|
|
|
features += str;
|
|
|
|
};
|
|
|
|
if (cpuInf.AESNI)
|
2021-06-30 18:20:45 +00:00
|
|
|
AddFeature("AES-NI");
|
2021-05-24 05:06:51 +00:00
|
|
|
if (cpuInf.SSE1)
|
2021-06-30 18:20:45 +00:00
|
|
|
AddFeature("SSE");
|
2021-05-24 05:06:51 +00:00
|
|
|
if (cpuInf.SSE2)
|
2021-06-30 18:20:45 +00:00
|
|
|
AddFeature("SSE2");
|
2021-05-24 05:06:51 +00:00
|
|
|
if (cpuInf.SSE3)
|
2021-06-30 18:20:45 +00:00
|
|
|
AddFeature("SSE3");
|
2021-05-24 05:06:51 +00:00
|
|
|
if (cpuInf.SSSE3)
|
2021-06-30 18:20:45 +00:00
|
|
|
AddFeature("SSSE3");
|
2021-05-24 05:06:51 +00:00
|
|
|
if (cpuInf.SSE4a)
|
2021-06-30 18:20:45 +00:00
|
|
|
AddFeature("SSE4a");
|
2021-05-24 05:06:51 +00:00
|
|
|
if (cpuInf.SSE41)
|
2021-06-30 18:20:45 +00:00
|
|
|
AddFeature("SSE4.1");
|
2021-05-24 05:06:51 +00:00
|
|
|
if (cpuInf.SSE42)
|
2021-06-30 18:20:45 +00:00
|
|
|
AddFeature("SSE4.2");
|
2021-05-24 05:06:51 +00:00
|
|
|
if (cpuInf.AVX)
|
2021-06-30 18:20:45 +00:00
|
|
|
AddFeature("AVX");
|
2021-05-24 05:06:51 +00:00
|
|
|
if (cpuInf.AVX2)
|
2021-06-30 18:20:45 +00:00
|
|
|
AddFeature("AVX2");
|
2021-05-24 05:06:51 +00:00
|
|
|
#endif
|
|
|
|
return features;
|
|
|
|
}
|
|
|
|
|
2022-07-29 20:16:55 +00:00
|
|
|
struct Application {
|
2021-05-24 21:25:31 +00:00
|
|
|
private:
|
2022-07-29 20:16:55 +00:00
|
|
|
int m_argc;
|
|
|
|
char** m_argv;
|
2022-02-21 02:28:07 +00:00
|
|
|
FileStoreManager& m_fileMgr;
|
|
|
|
CVarManager& m_cvarManager;
|
|
|
|
CVarCommons& m_cvarCommons;
|
2021-05-30 19:03:35 +00:00
|
|
|
ImGuiConsole m_imGuiConsole;
|
2021-05-24 05:06:51 +00:00
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
std::string m_deferredProject;
|
2022-02-01 00:06:54 +00:00
|
|
|
bool m_projectInitialized = false;
|
2021-05-24 05:06:51 +00:00
|
|
|
std::optional<amuse::BooBackendVoiceAllocator> m_amuseAllocWrapper;
|
|
|
|
std::unique_ptr<boo::IAudioVoiceEngine> m_voiceEngine;
|
|
|
|
|
|
|
|
Limiter m_limiter{};
|
|
|
|
|
2021-05-24 21:25:31 +00:00
|
|
|
bool m_firstFrame = true;
|
2022-02-13 21:32:52 +00:00
|
|
|
bool m_fullscreenToggleRequested = false;
|
|
|
|
bool m_quitRequested = false;
|
2022-08-09 22:29:12 +00:00
|
|
|
bool m_lAltHeld = false;
|
2021-05-24 21:25:31 +00:00
|
|
|
using delta_clock = std::chrono::high_resolution_clock;
|
2022-06-12 21:09:47 +00:00
|
|
|
delta_clock::time_point m_prevFrameTime;
|
2021-05-24 21:25:31 +00:00
|
|
|
|
2022-02-23 05:28:21 +00:00
|
|
|
std::vector<u32> m_deferredControllers; // used to capture controllers added before CInputGenerator
|
|
|
|
// is built, i.e during initialization
|
2022-02-09 08:54:53 +00:00
|
|
|
|
2021-05-24 05:06:51 +00:00
|
|
|
public:
|
2022-07-29 20:16:55 +00:00
|
|
|
Application(int argc, char** argv, FileStoreManager& fileMgr, CVarManager& cvarMgr, CVarCommons& cvarCmns)
|
|
|
|
: m_argc(argc)
|
|
|
|
, m_argv(argv)
|
|
|
|
, m_fileMgr(fileMgr)
|
|
|
|
, m_cvarManager(cvarMgr)
|
|
|
|
, m_cvarCommons(cvarCmns)
|
|
|
|
, m_imGuiConsole(cvarMgr, cvarCmns) {}
|
|
|
|
|
2022-07-29 20:46:51 +00:00
|
|
|
void onAppLaunched(const AuroraInfo& info) noexcept {
|
2022-02-01 00:06:54 +00:00
|
|
|
initialize();
|
2021-05-24 05:06:51 +00:00
|
|
|
|
2022-08-09 22:28:42 +00:00
|
|
|
VISetWindowTitle(
|
|
|
|
fmt::format(FMT_STRING("Metaforce {} [{}]"), METAFORCE_WC_DESCRIBE, backend_name(info.backend)).c_str());
|
2021-05-24 05:06:51 +00:00
|
|
|
|
2022-02-01 00:06:54 +00:00
|
|
|
m_voiceEngine = boo::NewAudioVoiceEngine("metaforce", "Metaforce");
|
2021-05-24 05:06:51 +00:00
|
|
|
m_voiceEngine->setVolume(0.7f);
|
|
|
|
m_amuseAllocWrapper.emplace(*m_voiceEngine);
|
|
|
|
|
2022-05-27 19:52:16 +00:00
|
|
|
#if TARGET_OS_IOS || TARGET_OS_TV
|
|
|
|
m_deferredProject = std::string{m_fileMgr.getStoreRoot()} + "game.iso";
|
|
|
|
#else
|
2022-07-29 20:16:55 +00:00
|
|
|
for (int i = 1; i < m_argc; ++i) {
|
|
|
|
std::string arg = m_argv[i];
|
2022-02-01 00:06:54 +00:00
|
|
|
if (m_deferredProject.empty() && !arg.starts_with('-') && !arg.starts_with('+'))
|
2021-05-24 05:06:51 +00:00
|
|
|
m_deferredProject = arg;
|
2021-06-30 18:20:45 +00:00
|
|
|
else if (arg == "--no-sound")
|
2021-05-24 05:06:51 +00:00
|
|
|
m_voiceEngine->setVolume(0.f);
|
|
|
|
}
|
2022-05-27 19:52:16 +00:00
|
|
|
#endif
|
2022-06-01 00:48:25 +00:00
|
|
|
|
|
|
|
m_voiceEngine->startPump();
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 00:06:54 +00:00
|
|
|
void initialize() {
|
2021-05-24 05:06:51 +00:00
|
|
|
zeus::detectCPU();
|
|
|
|
|
|
|
|
const zeus::CPUInfo& cpuInf = zeus::cpuFeatures();
|
|
|
|
Log.report(logvisor::Info, FMT_STRING("CPU Name: {}"), cpuInf.cpuBrand);
|
|
|
|
Log.report(logvisor::Info, FMT_STRING("CPU Vendor: {}"), cpuInf.cpuVendor);
|
2021-06-30 18:20:45 +00:00
|
|
|
Log.report(logvisor::Info, FMT_STRING("CPU Features: {}"), CPUFeatureString(cpuInf));
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 20:46:51 +00:00
|
|
|
void onSdlEvent(const SDL_Event& event) noexcept {
|
|
|
|
switch (event.type) {
|
|
|
|
case SDL_KEYDOWN:
|
2022-08-09 22:29:12 +00:00
|
|
|
m_lAltHeld = event.key.keysym.sym == SDLK_LALT;
|
2022-07-29 20:46:51 +00:00
|
|
|
// Toggle fullscreen on ALT+ENTER
|
|
|
|
if (event.key.keysym.sym == SDLK_RETURN && (event.key.keysym.mod & KMOD_ALT) != 0u && event.key.repeat == 0u) {
|
|
|
|
m_cvarCommons.m_fullscreen->fromBoolean(!m_cvarCommons.m_fullscreen->toBoolean());
|
|
|
|
}
|
2022-08-09 22:29:12 +00:00
|
|
|
break;
|
|
|
|
case SDL_KEYUP:
|
|
|
|
if (m_lAltHeld && event.key.keysym.sym == SDLK_LALT) {
|
|
|
|
m_imGuiConsole.ToggleVisible();
|
|
|
|
m_lAltHeld = false;
|
|
|
|
}
|
2022-07-29 20:46:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-29 20:16:55 +00:00
|
|
|
bool onAppIdle(float realDt) noexcept {
|
2022-03-04 09:46:33 +00:00
|
|
|
#ifdef NDEBUG
|
|
|
|
/* Ping the watchdog to let it know we're still alive */
|
|
|
|
CInfiniteLoopDetector::UpdateWatchDog(std::chrono::system_clock::now());
|
|
|
|
#endif
|
2022-02-09 08:54:53 +00:00
|
|
|
|
2022-02-01 00:06:54 +00:00
|
|
|
if (!m_projectInitialized && !m_deferredProject.empty()) {
|
2022-08-28 18:06:58 +00:00
|
|
|
Log.report(logvisor::Info, FMT_STRING("Loading game from '{}'"), m_deferredProject);
|
2022-02-01 00:06:54 +00:00
|
|
|
if (CDvdFile::Initialize(m_deferredProject)) {
|
|
|
|
m_projectInitialized = true;
|
2022-06-13 05:18:30 +00:00
|
|
|
m_cvarCommons.m_lastDiscPath->fromLiteral(m_deferredProject);
|
2021-05-30 19:03:35 +00:00
|
|
|
} else {
|
2022-02-23 05:28:21 +00:00
|
|
|
Log.report(logvisor::Error, FMT_STRING("Failed to open disc image '{}'"), m_deferredProject);
|
2022-06-12 21:09:47 +00:00
|
|
|
m_imGuiConsole.m_errorString = fmt::format(FMT_STRING("Failed to open disc image '{}'"), m_deferredProject);
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
2022-06-12 21:09:47 +00:00
|
|
|
m_deferredProject.clear();
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 21:54:31 +00:00
|
|
|
const auto targetFrameTime = getTargetFrameTime();
|
|
|
|
bool skipRetrace = false;
|
|
|
|
if (g_ResFactory != nullptr) {
|
|
|
|
OPTICK_EVENT("Async Load Resources");
|
|
|
|
const auto idleTime = m_limiter.SleepTime(targetFrameTime);
|
|
|
|
skipRetrace = g_ResFactory->AsyncIdle(idleTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skipRetrace) {
|
|
|
|
// We stopped loading resources to catch the next frame
|
|
|
|
m_limiter.Reset();
|
|
|
|
} else {
|
|
|
|
// No more to load, and we're under frame time
|
|
|
|
{
|
|
|
|
OPTICK_EVENT("Sleep");
|
|
|
|
m_limiter.Sleep(targetFrameTime);
|
|
|
|
}
|
2021-06-28 22:10:54 +00:00
|
|
|
}
|
2021-06-30 21:54:31 +00:00
|
|
|
|
2021-06-28 22:10:54 +00:00
|
|
|
OPTICK_FRAME("MainThread");
|
2021-05-24 05:06:51 +00:00
|
|
|
|
2021-06-06 20:59:53 +00:00
|
|
|
// Check if fullscreen has been toggled, if so set the fullscreen cvar accordingly
|
2022-02-13 21:32:52 +00:00
|
|
|
if (m_fullscreenToggleRequested) {
|
2021-06-06 20:59:53 +00:00
|
|
|
m_cvarCommons.m_fullscreen->fromBoolean(!m_cvarCommons.getFullscreen());
|
2022-02-13 21:32:52 +00:00
|
|
|
m_fullscreenToggleRequested = false;
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 20:59:53 +00:00
|
|
|
// Check if the user has modified the fullscreen CVar, if so set fullscreen state accordingly
|
|
|
|
if (m_cvarCommons.m_fullscreen->isModified()) {
|
2022-07-29 20:46:51 +00:00
|
|
|
VISetWindowFullscreen(m_cvarCommons.getFullscreen());
|
2021-06-06 20:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Let CVarManager inform all CVar listeners of the CVar's state and clear all mdoified flags if necessary
|
|
|
|
m_cvarManager.proc();
|
|
|
|
|
2022-02-01 00:06:54 +00:00
|
|
|
if (!g_mainMP1 && m_projectInitialized) {
|
|
|
|
g_mainMP1.emplace(nullptr, nullptr);
|
2022-07-29 20:16:55 +00:00
|
|
|
auto result =
|
|
|
|
g_mainMP1->Init(m_argc, m_argv, m_fileMgr, &m_cvarManager, m_voiceEngine.get(), *m_amuseAllocWrapper);
|
2022-06-12 21:09:47 +00:00
|
|
|
if (!result.empty()) {
|
|
|
|
Log.report(logvisor::Error, FMT_STRING("{}"), result);
|
|
|
|
m_imGuiConsole.m_errorString = result;
|
|
|
|
g_mainMP1.reset();
|
|
|
|
CDvdFile::Shutdown();
|
|
|
|
m_projectInitialized = false;
|
2022-06-13 05:18:30 +00:00
|
|
|
m_cvarCommons.m_lastDiscPath->fromLiteral(""sv);
|
2022-06-12 21:09:47 +00:00
|
|
|
}
|
2021-05-30 19:03:35 +00:00
|
|
|
}
|
2021-05-24 05:06:51 +00:00
|
|
|
|
2021-05-24 21:25:31 +00:00
|
|
|
float dt = 1 / 60.f;
|
2022-02-01 00:06:54 +00:00
|
|
|
if (m_cvarCommons.m_variableDt->toBoolean()) {
|
|
|
|
dt = std::min(realDt, 1 / 30.f);
|
2021-05-24 21:25:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 21:09:47 +00:00
|
|
|
m_imGuiConsole.PreUpdate();
|
2021-05-30 19:03:35 +00:00
|
|
|
if (g_mainMP1) {
|
2022-06-01 00:48:25 +00:00
|
|
|
if (m_voiceEngine) {
|
|
|
|
m_voiceEngine->lockPump();
|
|
|
|
}
|
2021-05-30 19:03:35 +00:00
|
|
|
if (g_mainMP1->Proc(dt)) {
|
2022-02-01 00:06:54 +00:00
|
|
|
return false;
|
2021-05-30 19:03:35 +00:00
|
|
|
}
|
2022-06-01 00:48:25 +00:00
|
|
|
if (m_voiceEngine) {
|
|
|
|
m_voiceEngine->unlockPump();
|
|
|
|
}
|
2022-06-12 21:09:47 +00:00
|
|
|
}
|
|
|
|
m_imGuiConsole.PostUpdate();
|
|
|
|
if (!g_mainMP1 && m_imGuiConsole.m_gameDiscSelected) {
|
|
|
|
std::optional<std::string> result;
|
|
|
|
m_imGuiConsole.m_gameDiscSelected.swap(result);
|
|
|
|
m_deferredProject = std::move(*result);
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 21:09:47 +00:00
|
|
|
if (m_quitRequested || m_imGuiConsole.m_quitRequested || m_cvarManager.restartRequired()) {
|
2022-02-13 21:32:52 +00:00
|
|
|
if (g_mainMP1) {
|
|
|
|
g_mainMP1->Quit();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-02-01 00:06:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-29 20:16:55 +00:00
|
|
|
void onAppDraw() noexcept {
|
2022-02-01 00:06:54 +00:00
|
|
|
OPTICK_EVENT("Draw");
|
|
|
|
if (g_Renderer != nullptr) {
|
|
|
|
g_Renderer->BeginScene();
|
2021-05-30 19:03:35 +00:00
|
|
|
if (g_mainMP1) {
|
|
|
|
g_mainMP1->Draw();
|
|
|
|
}
|
2022-02-01 00:06:54 +00:00
|
|
|
g_Renderer->EndScene();
|
2021-05-24 21:25:31 +00:00
|
|
|
}
|
2022-02-01 00:06:54 +00:00
|
|
|
}
|
2021-05-24 21:25:31 +00:00
|
|
|
|
2022-07-29 20:16:55 +00:00
|
|
|
void onAppPostDraw() noexcept {
|
2022-02-01 00:06:54 +00:00
|
|
|
OPTICK_EVENT("PostDraw");
|
2021-05-24 05:06:51 +00:00
|
|
|
if (m_voiceEngine) {
|
|
|
|
m_voiceEngine->pumpAndMixVoices();
|
|
|
|
}
|
2022-08-03 22:19:11 +00:00
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
CDvdFile::DoWork();
|
|
|
|
#endif
|
2021-05-24 05:06:51 +00:00
|
|
|
CGraphics::TickRenderTimings();
|
|
|
|
++logvisor::FrameIndex;
|
|
|
|
}
|
|
|
|
|
2022-07-29 20:16:55 +00:00
|
|
|
void onAppWindowResized(const AuroraWindowSize& size) noexcept {
|
2022-02-26 20:38:08 +00:00
|
|
|
CGraphics::SetViewportResolution({static_cast<s32>(size.fb_width), static_cast<s32>(size.fb_height)});
|
2022-02-06 22:53:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 20:16:55 +00:00
|
|
|
void onAppDisplayScaleChanged(float scale) noexcept { ImGuiEngine_Initialize(scale); }
|
2022-02-08 01:48:08 +00:00
|
|
|
|
2022-07-29 20:16:55 +00:00
|
|
|
void onControllerAdded(uint32_t which) noexcept { m_imGuiConsole.ControllerAdded(which); }
|
2022-02-08 01:48:08 +00:00
|
|
|
|
2022-07-29 20:16:55 +00:00
|
|
|
void onControllerRemoved(uint32_t which) noexcept { m_imGuiConsole.ControllerRemoved(which); }
|
2022-02-09 08:54:53 +00:00
|
|
|
|
2022-07-29 20:16:55 +00:00
|
|
|
void onAppExiting() noexcept {
|
2022-02-01 00:06:54 +00:00
|
|
|
m_imGuiConsole.Shutdown();
|
2022-06-01 00:48:25 +00:00
|
|
|
if (m_voiceEngine) {
|
2022-06-02 05:52:58 +00:00
|
|
|
m_voiceEngine->unlockPump();
|
2022-06-01 00:48:25 +00:00
|
|
|
m_voiceEngine->stopPump();
|
|
|
|
}
|
2022-02-01 00:06:54 +00:00
|
|
|
if (g_mainMP1) {
|
|
|
|
g_mainMP1->Shutdown();
|
|
|
|
}
|
|
|
|
g_mainMP1.reset();
|
|
|
|
m_cvarManager.serialize();
|
|
|
|
m_amuseAllocWrapper.reset();
|
2022-06-01 00:48:25 +00:00
|
|
|
m_voiceEngine.reset();
|
2022-02-01 00:06:54 +00:00
|
|
|
CDvdFile::Shutdown();
|
|
|
|
}
|
2021-05-24 05:06:51 +00:00
|
|
|
|
2022-07-29 20:16:55 +00:00
|
|
|
void onImGuiInit(float scale) noexcept { ImGuiEngine_Initialize(scale); }
|
2022-02-16 05:21:24 +00:00
|
|
|
|
2022-07-29 20:16:55 +00:00
|
|
|
void onImGuiAddTextures() noexcept { ImGuiEngine_AddTextures(); }
|
2021-05-24 05:06:51 +00:00
|
|
|
|
2021-05-30 19:03:35 +00:00
|
|
|
[[nodiscard]] std::chrono::nanoseconds getTargetFrameTime() const {
|
|
|
|
if (m_cvarCommons.getVariableFrameTime()) {
|
|
|
|
return std::chrono::nanoseconds{0};
|
|
|
|
}
|
|
|
|
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::seconds{1}) / 60;
|
2021-05-24 21:25:31 +00:00
|
|
|
}
|
2021-05-24 05:06:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace metaforce
|
|
|
|
|
2022-06-12 21:09:47 +00:00
|
|
|
static void SetupBasics() {
|
2021-05-24 05:06:51 +00:00
|
|
|
auto result = zeus::validateCPU();
|
|
|
|
if (!result.first) {
|
|
|
|
#if _WIN32 && !WINDOWS_STORE
|
2021-06-30 18:20:45 +00:00
|
|
|
std::string msg =
|
|
|
|
fmt::format(FMT_STRING("ERROR: This build of Metaforce requires the following CPU features:\n{}\n"),
|
2021-05-24 05:06:51 +00:00
|
|
|
metaforce::CPUFeatureString(result.second));
|
2021-06-30 18:20:45 +00:00
|
|
|
MessageBoxW(nullptr, nowide::widen(msg).c_str(), L"CPU error", MB_OK | MB_ICONERROR);
|
2021-05-24 05:06:51 +00:00
|
|
|
#else
|
|
|
|
fmt::print(stderr, FMT_STRING("ERROR: This build of Metaforce requires the following CPU features:\n{}\n"),
|
|
|
|
metaforce::CPUFeatureString(result.second));
|
|
|
|
#endif
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if SENTRY_ENABLED
|
2022-06-13 07:55:12 +00:00
|
|
|
std::string cacheDir{metaforce::FileStoreManager::instance()->getStoreRoot()};
|
2021-05-24 05:06:51 +00:00
|
|
|
logvisor::RegisterSentry("metaforce", METAFORCE_WC_DESCRIBE, cacheDir.c_str());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
static bool IsClientLoggingEnabled(int argc, char** argv) {
|
2022-08-03 22:19:11 +00:00
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
return true;
|
|
|
|
#else
|
2022-02-21 02:28:07 +00:00
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
if (!strncmp(argv[i], "-l", 2)) {
|
2021-05-24 05:06:51 +00:00
|
|
|
return true;
|
2022-02-21 02:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-24 05:06:51 +00:00
|
|
|
return false;
|
2022-08-03 22:19:11 +00:00
|
|
|
#endif
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 20:16:55 +00:00
|
|
|
static std::unique_ptr<metaforce::Application> g_app;
|
2022-08-09 06:11:23 +00:00
|
|
|
static SDL_Window* g_window;
|
2022-07-29 20:16:55 +00:00
|
|
|
static bool g_paused;
|
|
|
|
|
|
|
|
static void aurora_log_callback(AuroraLogLevel level, const char* message, unsigned int len) {
|
|
|
|
logvisor::Level severity = logvisor::Fatal;
|
|
|
|
switch (level) {
|
|
|
|
case LOG_DEBUG:
|
|
|
|
case LOG_INFO:
|
|
|
|
severity = logvisor::Info;
|
|
|
|
break;
|
|
|
|
case LOG_WARNING:
|
|
|
|
severity = logvisor::Warning;
|
|
|
|
break;
|
|
|
|
case LOG_ERROR:
|
|
|
|
severity = logvisor::Error;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2022-08-09 06:11:23 +00:00
|
|
|
if (level == LOG_FATAL) {
|
|
|
|
auto msg = fmt::format(FMT_STRING("Metaforce encountered an internal error:\n\n{}"), message);
|
|
|
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Metaforce", msg.c_str(), g_window);
|
|
|
|
}
|
2022-07-29 20:16:55 +00:00
|
|
|
metaforce::Log.report(severity, FMT_STRING("{}"), message);
|
2022-06-13 05:33:41 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 20:16:55 +00:00
|
|
|
static void aurora_imgui_init_callback(const AuroraWindowSize* size) { g_app->onImGuiInit(size->scale); }
|
|
|
|
|
2021-05-24 05:06:51 +00:00
|
|
|
#if !WINDOWS_STORE
|
2021-06-30 18:20:45 +00:00
|
|
|
int main(int argc, char** argv) {
|
2022-02-20 03:22:03 +00:00
|
|
|
// TODO: This seems to fix a lot of weird issues with rounding
|
|
|
|
// but breaks animations, need to research why this is the case
|
|
|
|
// for now it's disabled
|
|
|
|
// fesetround(FE_TOWARDZERO);
|
2022-02-21 02:28:07 +00:00
|
|
|
if (argc > 1 && !strcmp(argv[1], "--dlpackage")) {
|
2021-05-24 05:06:51 +00:00
|
|
|
fmt::print(FMT_STRING("{}\n"), METAFORCE_DLPACKAGE);
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
|
2022-02-28 01:35:13 +00:00
|
|
|
metaforce::FileStoreManager fileMgr{"AxioDL", "metaforce"};
|
2022-06-13 07:55:12 +00:00
|
|
|
SetupBasics();
|
2021-05-24 05:06:51 +00:00
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
std::vector<std::string> args;
|
2022-06-12 21:09:47 +00:00
|
|
|
for (int i = 1; i < argc; ++i) {
|
2021-06-30 18:20:45 +00:00
|
|
|
args.emplace_back(argv[i]);
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 20:16:55 +00:00
|
|
|
auto icon = metaforce::GetIcon();
|
|
|
|
|
2022-06-13 05:46:01 +00:00
|
|
|
// FIXME: logvisor needs to copy this
|
|
|
|
std::string logFilePath;
|
|
|
|
|
2022-06-12 21:09:47 +00:00
|
|
|
bool restart = false;
|
|
|
|
do {
|
|
|
|
metaforce::CVarManager cvarMgr{fileMgr};
|
|
|
|
metaforce::CVarCommons cvarCmns{cvarMgr};
|
2022-06-13 05:33:41 +00:00
|
|
|
|
2022-06-12 21:09:47 +00:00
|
|
|
if (!restart) {
|
|
|
|
cvarMgr.parseCommandLine(args);
|
|
|
|
|
|
|
|
// TODO add clear loggers func to logvisor so we can recreate loggers on restart
|
2022-06-13 05:33:41 +00:00
|
|
|
bool logging = IsClientLoggingEnabled(argc, argv);
|
|
|
|
#if _WIN32
|
|
|
|
if (logging && GetFileType(GetStdHandle(STD_ERROR_HANDLE)) == FILE_TYPE_UNKNOWN) {
|
|
|
|
logvisor::CreateWin32Console();
|
|
|
|
}
|
|
|
|
#endif
|
2022-06-12 21:09:47 +00:00
|
|
|
logvisor::RegisterStandardExceptions();
|
2022-06-13 05:33:41 +00:00
|
|
|
if (logging) {
|
2022-06-12 21:09:47 +00:00
|
|
|
logvisor::RegisterConsoleLogger();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string logFile = cvarCmns.getLogFile();
|
|
|
|
if (!logFile.empty()) {
|
|
|
|
std::time_t time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
|
|
|
|
char buf[100];
|
|
|
|
std::strftime(buf, 100, "%Y-%m-%d_%H-%M-%S", std::localtime(&time));
|
|
|
|
logFilePath = fmt::format(FMT_STRING("{}/{}-{}"), fileMgr.getStoreRoot(), buf, logFile);
|
|
|
|
logvisor::RegisterFileLogger(logFilePath.c_str());
|
|
|
|
}
|
|
|
|
}
|
2022-06-13 05:33:41 +00:00
|
|
|
|
2022-07-29 20:16:55 +00:00
|
|
|
g_app = std::make_unique<metaforce::Application>(argc, argv, fileMgr, cvarMgr, cvarCmns);
|
|
|
|
std::string configPath{fileMgr.getStoreRoot()};
|
|
|
|
const AuroraConfig config{
|
|
|
|
.appName = "Metaforce",
|
|
|
|
.configPath = configPath.c_str(),
|
2022-08-09 22:28:42 +00:00
|
|
|
.desiredBackend = metaforce::backend_from_string(cvarCmns.getGraphicsApi()),
|
2022-07-29 20:16:55 +00:00
|
|
|
.msaa = cvarCmns.getSamples(),
|
|
|
|
.maxTextureAnisotropy = static_cast<uint16_t>(cvarCmns.getAnisotropy()),
|
|
|
|
.startFullscreen = cvarCmns.getFullscreen(),
|
|
|
|
.iconRGBA8 = icon.data.get(),
|
|
|
|
.iconWidth = icon.width,
|
|
|
|
.iconHeight = icon.height,
|
|
|
|
.logCallback = aurora_log_callback,
|
|
|
|
.imGuiInitCallback = aurora_imgui_init_callback,
|
2022-06-12 21:09:47 +00:00
|
|
|
};
|
2022-07-29 20:16:55 +00:00
|
|
|
const auto info = aurora_initialize(argc, argv, &config);
|
2022-08-09 06:11:23 +00:00
|
|
|
g_window = info.window;
|
2022-07-29 20:16:55 +00:00
|
|
|
g_app->onImGuiAddTextures();
|
2022-07-29 20:46:51 +00:00
|
|
|
g_app->onAppLaunched(info);
|
2022-07-29 20:16:55 +00:00
|
|
|
g_app->onAppWindowResized(info.windowSize);
|
2022-08-09 22:28:42 +00:00
|
|
|
while (!cvarMgr.restartRequired()) {
|
2022-07-29 20:16:55 +00:00
|
|
|
const auto* event = aurora_update();
|
|
|
|
bool exiting = false;
|
|
|
|
while (event != nullptr && event->type != AURORA_NONE) {
|
|
|
|
switch (event->type) {
|
|
|
|
case AURORA_EXIT:
|
|
|
|
exiting = true;
|
|
|
|
break;
|
2022-07-29 20:46:51 +00:00
|
|
|
case AURORA_SDL_EVENT:
|
|
|
|
g_app->onSdlEvent(event->sdl);
|
|
|
|
break;
|
2022-07-29 20:16:55 +00:00
|
|
|
case AURORA_WINDOW_RESIZED:
|
|
|
|
g_app->onAppWindowResized(event->windowSize);
|
|
|
|
break;
|
|
|
|
case AURORA_CONTROLLER_ADDED:
|
|
|
|
g_app->onControllerAdded(event->controller);
|
|
|
|
break;
|
|
|
|
case AURORA_CONTROLLER_REMOVED:
|
|
|
|
g_app->onControllerRemoved(event->controller);
|
|
|
|
break;
|
|
|
|
case AURORA_PAUSED:
|
|
|
|
g_paused = true;
|
|
|
|
break;
|
|
|
|
case AURORA_UNPAUSED:
|
|
|
|
g_paused = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (exiting) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++event;
|
|
|
|
}
|
|
|
|
if (exiting) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (g_paused) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
g_app->onAppIdle(1.f / 60.f /* TODO */);
|
|
|
|
aurora_begin_frame();
|
|
|
|
g_app->onAppDraw();
|
|
|
|
aurora_end_frame();
|
|
|
|
g_app->onAppPostDraw();
|
|
|
|
}
|
|
|
|
g_app->onAppExiting();
|
|
|
|
aurora_shutdown();
|
|
|
|
g_app.reset();
|
|
|
|
|
2022-06-12 21:09:47 +00:00
|
|
|
restart = cvarMgr.restartRequired();
|
|
|
|
} while (restart);
|
2022-02-01 00:06:54 +00:00
|
|
|
return 0;
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
#endif
|