2021-05-24 05:06:51 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2021-05-25 02:02:57 +00:00
|
|
|
#include <numeric>
|
2021-05-24 05:06:51 +00:00
|
|
|
#include <hecl/Pipeline.hpp>
|
|
|
|
|
|
|
|
#include "boo/boo.hpp"
|
|
|
|
#include "logvisor/logvisor.hpp"
|
|
|
|
|
2021-05-24 21:25:31 +00:00
|
|
|
#include "ImGuiEngine.hpp"
|
2021-05-24 05:06:51 +00:00
|
|
|
#include "Runtime/Graphics/CGraphics.hpp"
|
|
|
|
#include "Runtime/MP1/MP1.hpp"
|
|
|
|
#include "amuse/BooBackend.hpp"
|
2021-05-24 21:25:31 +00:00
|
|
|
|
2021-05-24 05:06:51 +00:00
|
|
|
#include "../version.h"
|
|
|
|
|
2021-07-25 00:54:26 +00:00
|
|
|
//#include <fenv.h>
|
|
|
|
//#pragma STDC FENV_ACCESS ON
|
|
|
|
|
2021-05-24 21:25:31 +00:00
|
|
|
/* Static reference to dataspec additions
|
|
|
|
* (used by MSVC to definitively link DataSpecs) */
|
|
|
|
#include "DataSpecRegistry.hpp"
|
|
|
|
|
2021-05-24 05:06:51 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
|
|
|
static logvisor::Module AthenaLog("Athena");
|
|
|
|
static void AthenaExc(athena::error::Level level, const char* file, const char*, int line, fmt::string_view fmt,
|
|
|
|
fmt::format_args args) {
|
|
|
|
AthenaLog.vreport(logvisor::Level(level), fmt, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
void NanoSleep(const duration_t duration) {
|
|
|
|
std::this_thread::sleep_for(duration);
|
|
|
|
}
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WindowCallback : boo::IWindowCallback {
|
2021-05-24 21:25:31 +00:00
|
|
|
friend struct Application;
|
|
|
|
|
|
|
|
private:
|
2021-05-24 05:06:51 +00:00
|
|
|
bool m_fullscreenToggleRequested = false;
|
|
|
|
boo::SWindowRect m_lastRect;
|
|
|
|
bool m_rectDirty = false;
|
|
|
|
bool m_windowInvalid = false;
|
2021-05-24 21:25:31 +00:00
|
|
|
ImGuiWindowCallback m_imguiCallback;
|
2021-05-24 05:06:51 +00:00
|
|
|
|
|
|
|
void resized(const boo::SWindowRect& rect, bool sync) override {
|
|
|
|
m_lastRect = rect;
|
|
|
|
m_rectDirty = true;
|
2021-05-25 13:36:58 +00:00
|
|
|
m_imguiCallback.resized(rect, sync);
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mouseDown(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mods) override {
|
2021-05-24 21:25:31 +00:00
|
|
|
if (!ImGuiWindowCallback::m_mouseCaptured && g_mainMP1) {
|
2021-05-24 05:06:51 +00:00
|
|
|
if (MP1::CGameArchitectureSupport* as = g_mainMP1->GetArchSupport()) {
|
|
|
|
as->mouseDown(coord, button, mods);
|
|
|
|
}
|
|
|
|
}
|
2021-05-24 21:25:31 +00:00
|
|
|
m_imguiCallback.mouseDown(coord, button, mods);
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mouseUp(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mods) override {
|
|
|
|
if (g_mainMP1) {
|
|
|
|
if (MP1::CGameArchitectureSupport* as = g_mainMP1->GetArchSupport()) {
|
|
|
|
as->mouseUp(coord, button, mods);
|
|
|
|
}
|
|
|
|
}
|
2021-05-24 21:25:31 +00:00
|
|
|
m_imguiCallback.mouseUp(coord, button, mods);
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mouseMove(const boo::SWindowCoord& coord) override {
|
2021-05-24 21:25:31 +00:00
|
|
|
if (!ImGuiWindowCallback::m_mouseCaptured && g_mainMP1) {
|
2021-05-24 05:06:51 +00:00
|
|
|
if (MP1::CGameArchitectureSupport* as = g_mainMP1->GetArchSupport()) {
|
|
|
|
as->mouseMove(coord);
|
|
|
|
}
|
|
|
|
}
|
2021-05-24 21:25:31 +00:00
|
|
|
m_imguiCallback.mouseMove(coord);
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 21:25:31 +00:00
|
|
|
void mouseEnter(const boo::SWindowCoord& coord) override { m_imguiCallback.mouseEnter(coord); }
|
|
|
|
|
|
|
|
void mouseLeave(const boo::SWindowCoord& coord) override { m_imguiCallback.mouseLeave(coord); }
|
|
|
|
|
2021-05-24 05:06:51 +00:00
|
|
|
void scroll(const boo::SWindowCoord& coord, const boo::SScrollDelta& scroll) override {
|
2021-05-24 21:25:31 +00:00
|
|
|
if (!ImGuiWindowCallback::m_mouseCaptured && g_mainMP1) {
|
2021-05-24 05:06:51 +00:00
|
|
|
if (MP1::CGameArchitectureSupport* as = g_mainMP1->GetArchSupport()) {
|
|
|
|
as->scroll(coord, scroll);
|
|
|
|
}
|
|
|
|
}
|
2021-05-24 21:25:31 +00:00
|
|
|
m_imguiCallback.scroll(coord, scroll);
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void charKeyDown(unsigned long charCode, boo::EModifierKey mods, bool isRepeat) override {
|
2021-05-24 21:25:31 +00:00
|
|
|
if (!ImGuiWindowCallback::m_keyboardCaptured && g_mainMP1) {
|
2021-05-24 05:06:51 +00:00
|
|
|
if (MP1::CGameArchitectureSupport* as = g_mainMP1->GetArchSupport()) {
|
|
|
|
as->charKeyDown(charCode, mods, isRepeat);
|
|
|
|
}
|
|
|
|
}
|
2021-05-24 21:25:31 +00:00
|
|
|
m_imguiCallback.charKeyDown(charCode, mods, isRepeat);
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void charKeyUp(unsigned long charCode, boo::EModifierKey mods) override {
|
|
|
|
if (g_mainMP1) {
|
|
|
|
if (MP1::CGameArchitectureSupport* as = g_mainMP1->GetArchSupport()) {
|
|
|
|
as->charKeyUp(charCode, mods);
|
|
|
|
}
|
|
|
|
}
|
2021-05-24 21:25:31 +00:00
|
|
|
m_imguiCallback.charKeyUp(charCode, mods);
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void specialKeyDown(boo::ESpecialKey key, boo::EModifierKey mods, bool isRepeat) override {
|
2021-05-24 21:25:31 +00:00
|
|
|
if (!ImGuiWindowCallback::m_keyboardCaptured && g_mainMP1) {
|
2021-05-24 05:06:51 +00:00
|
|
|
if (MP1::CGameArchitectureSupport* as = g_mainMP1->GetArchSupport()) {
|
|
|
|
as->specialKeyDown(key, mods, isRepeat);
|
|
|
|
}
|
|
|
|
}
|
2021-05-29 06:04:02 +00:00
|
|
|
if (True(mods & boo::EModifierKey::Alt)) {
|
|
|
|
if (key == boo::ESpecialKey::Enter) {
|
|
|
|
m_fullscreenToggleRequested = true;
|
|
|
|
} else if (key == boo::ESpecialKey::F4) {
|
|
|
|
m_windowInvalid = true;
|
|
|
|
}
|
2021-05-24 21:25:31 +00:00
|
|
|
}
|
|
|
|
m_imguiCallback.specialKeyDown(key, mods, isRepeat);
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void specialKeyUp(boo::ESpecialKey key, boo::EModifierKey mods) override {
|
|
|
|
if (g_mainMP1) {
|
|
|
|
if (MP1::CGameArchitectureSupport* as = g_mainMP1->GetArchSupport()) {
|
|
|
|
as->specialKeyUp(key, mods);
|
|
|
|
}
|
|
|
|
}
|
2021-05-24 21:25:31 +00:00
|
|
|
m_imguiCallback.specialKeyUp(key, mods);
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void destroyed() override { m_windowInvalid = true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Application : boo::IApplicationCallback {
|
2021-05-24 21:25:31 +00:00
|
|
|
private:
|
2021-05-24 05:06:51 +00:00
|
|
|
std::shared_ptr<boo::IWindow> m_window;
|
|
|
|
WindowCallback m_windowCallback;
|
|
|
|
hecl::Runtime::FileStoreManager& m_fileMgr;
|
|
|
|
hecl::CVarManager& m_cvarManager;
|
|
|
|
hecl::CVarCommons& m_cvarCommons;
|
2021-05-30 19:03:35 +00:00
|
|
|
ImGuiConsole m_imGuiConsole;
|
|
|
|
std::string m_errorString;
|
2021-05-24 05:06:51 +00:00
|
|
|
|
|
|
|
boo::ObjToken<boo::ITextureR> m_renderTex;
|
2021-06-30 18:20:45 +00:00
|
|
|
std::string m_deferredProject;
|
2021-05-24 05:06:51 +00:00
|
|
|
std::unique_ptr<hecl::Database::Project> m_proj;
|
|
|
|
std::optional<amuse::BooBackendVoiceAllocator> m_amuseAllocWrapper;
|
|
|
|
std::unique_ptr<boo::IAudioVoiceEngine> m_voiceEngine;
|
|
|
|
std::unique_ptr<hecl::PipelineConverterBase> m_pipelineConv;
|
|
|
|
|
|
|
|
Limiter m_limiter{};
|
|
|
|
std::atomic_bool m_running = {true};
|
|
|
|
bool m_noShaderWarmup = false;
|
2021-05-25 02:02:57 +00:00
|
|
|
bool m_imGuiInitialized = false;
|
2021-05-24 05:06:51 +00:00
|
|
|
|
2021-05-24 21:25:31 +00:00
|
|
|
bool m_firstFrame = true;
|
|
|
|
using delta_clock = std::chrono::high_resolution_clock;
|
|
|
|
std::chrono::time_point<delta_clock> m_prevFrameTime;
|
|
|
|
|
2021-05-24 05:06:51 +00:00
|
|
|
public:
|
|
|
|
Application(hecl::Runtime::FileStoreManager& fileMgr, hecl::CVarManager& cvarMgr, hecl::CVarCommons& cvarCmns)
|
2021-05-30 19:03:35 +00:00
|
|
|
: m_fileMgr(fileMgr), m_cvarManager(cvarMgr), m_cvarCommons(cvarCmns), m_imGuiConsole(cvarMgr, cvarCmns) {}
|
2021-05-24 05:06:51 +00:00
|
|
|
|
|
|
|
int appMain(boo::IApplication* app) override {
|
|
|
|
initialize(app);
|
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
m_window = app->newWindow("Metaforce"sv);
|
2021-05-24 05:06:51 +00:00
|
|
|
if (!m_window) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
m_window->setCallback(&m_windowCallback);
|
|
|
|
m_window->showWindow();
|
|
|
|
|
2021-05-27 04:56:25 +00:00
|
|
|
boo::IGraphicsDataFactory* gfxF = m_window->getMainContextDataFactory();
|
2021-06-30 18:20:45 +00:00
|
|
|
m_window->setTitle(fmt::format(FMT_STRING("Metaforce {} [{}]"), METAFORCE_WC_DESCRIBE, gfxF->platformName()));
|
2021-05-27 04:56:25 +00:00
|
|
|
|
2021-05-24 05:06:51 +00:00
|
|
|
boo::SWindowRect rect = m_window->getWindowFrame();
|
2021-05-24 22:53:49 +00:00
|
|
|
m_windowCallback.m_lastRect = rect;
|
2021-05-24 21:25:31 +00:00
|
|
|
gfxF->commitTransaction([&](boo::IGraphicsDataFactory::Context& ctx) {
|
2021-05-24 05:06:51 +00:00
|
|
|
m_renderTex = ctx.newRenderTexture(rect.size[0], rect.size[1], boo::TextureClampMode::ClampToEdge, 3, 3);
|
|
|
|
return true;
|
|
|
|
} BooTrace);
|
|
|
|
|
2021-05-24 21:25:31 +00:00
|
|
|
m_pipelineConv = hecl::NewPipelineConverter(gfxF);
|
2021-05-24 05:06:51 +00:00
|
|
|
hecl::conv = m_pipelineConv.get();
|
|
|
|
|
|
|
|
m_voiceEngine = boo::NewAudioVoiceEngine();
|
|
|
|
m_voiceEngine->setVolume(0.7f);
|
|
|
|
m_amuseAllocWrapper.emplace(*m_voiceEngine);
|
|
|
|
|
|
|
|
hecl::ProjectPath projectPath;
|
|
|
|
for (const auto& arg : app->getArgs()) {
|
|
|
|
hecl::Sstat theStat;
|
2021-06-30 18:20:45 +00:00
|
|
|
if (!hecl::Stat((arg + "/out").c_str(), &theStat) && S_ISDIR(theStat.st_mode)) {
|
2021-05-24 05:06:51 +00:00
|
|
|
hecl::ProjectRootPath rootPath(arg);
|
|
|
|
hecl::Database::Project tmp(rootPath); // Force project creation
|
|
|
|
}
|
|
|
|
if (m_deferredProject.empty() && hecl::SearchForProject(arg))
|
|
|
|
m_deferredProject = arg;
|
2021-06-30 18:20:45 +00:00
|
|
|
if (arg == "--no-shader-warmup")
|
2021-05-24 05:06:51 +00:00
|
|
|
m_noShaderWarmup = true;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_deferredProject.empty()) {
|
|
|
|
/* Default behavior - search upwards for packaged project containing the program */
|
|
|
|
if (hecl::ProjectRootPath projRoot = hecl::SearchForProject(ExeDir)) {
|
2021-06-30 18:20:45 +00:00
|
|
|
std::string rootPath(projRoot.getAbsolutePath());
|
2021-05-24 05:06:51 +00:00
|
|
|
hecl::Sstat theStat;
|
2021-06-30 18:20:45 +00:00
|
|
|
if (hecl::Stat((rootPath + "/out/files/MP1/Metroid1.upak").c_str(), &theStat) == 0 && S_ISREG(theStat.st_mode))
|
|
|
|
m_deferredProject = rootPath + "/out";
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (m_running) {
|
|
|
|
onAppIdle();
|
|
|
|
}
|
|
|
|
|
2021-05-25 02:02:57 +00:00
|
|
|
if (m_imGuiInitialized) {
|
2021-05-30 19:03:35 +00:00
|
|
|
m_imGuiConsole.Shutdown();
|
2021-05-25 02:02:57 +00:00
|
|
|
ImGuiEngine::Shutdown();
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
2021-05-25 02:02:57 +00:00
|
|
|
if (g_mainMP1) {
|
2021-05-24 05:06:51 +00:00
|
|
|
g_mainMP1->Shutdown();
|
2021-05-25 02:02:57 +00:00
|
|
|
}
|
2021-05-24 05:06:51 +00:00
|
|
|
g_mainMP1.reset();
|
2021-05-30 20:59:19 +00:00
|
|
|
m_renderTex.reset();
|
|
|
|
m_pipelineConv.reset();
|
2021-05-25 02:02:57 +00:00
|
|
|
if (m_window) {
|
|
|
|
m_window->getCommandQueue()->stopRenderer();
|
|
|
|
}
|
2021-05-27 12:51:07 +00:00
|
|
|
m_cvarManager.serialize();
|
2021-05-24 05:06:51 +00:00
|
|
|
m_voiceEngine.reset();
|
|
|
|
m_amuseAllocWrapper.reset();
|
|
|
|
CDvdFile::Shutdown();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void initialize(boo::IApplication* app) {
|
|
|
|
zeus::detectCPU();
|
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
for (const auto& arg : app->getArgs()) {
|
|
|
|
if (arg.find("--verbosity=") == 0 || arg.find("-v=") == 0) {
|
|
|
|
hecl::VerbosityLevel = atoi(arg.substr(arg.find_last_of('=') + 1).c_str());
|
2021-05-24 05:06:51 +00:00
|
|
|
hecl::LogModule.report(logvisor::Info, FMT_STRING("Set verbosity level to {}"), hecl::VerbosityLevel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void onAppIdle() noexcept {
|
|
|
|
if (!m_deferredProject.empty()) {
|
2021-06-30 18:20:45 +00:00
|
|
|
std::string subPath;
|
2021-05-24 05:06:51 +00:00
|
|
|
hecl::ProjectRootPath projPath = hecl::SearchForProject(m_deferredProject, subPath);
|
2021-05-30 19:03:35 +00:00
|
|
|
if (projPath) {
|
|
|
|
m_proj = std::make_unique<hecl::Database::Project>(projPath);
|
|
|
|
m_deferredProject.clear();
|
2021-06-30 18:20:45 +00:00
|
|
|
hecl::ProjectPath projectPath{m_proj->getProjectWorkingPath(), "out/files/MP1"};
|
2021-05-30 19:03:35 +00:00
|
|
|
CDvdFile::Initialize(projectPath);
|
|
|
|
} else {
|
2021-06-30 18:20:45 +00:00
|
|
|
Log.report(logvisor::Error, FMT_STRING("Project doesn't exist at '{}'"), m_deferredProject);
|
|
|
|
m_errorString = fmt::format(FMT_STRING("Project not found at '{}'"), m_deferredProject);
|
2021-05-30 19:03:35 +00:00
|
|
|
m_deferredProject.clear();
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-30 19:03:35 +00:00
|
|
|
if (!m_proj && m_errorString.empty()) {
|
|
|
|
m_errorString = "Project directory not specified"s;
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_windowCallback.m_windowInvalid) {
|
|
|
|
m_running.store(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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("Wait for Retrace");
|
|
|
|
m_window->waitForRetrace();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
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-06-30 17:27:36 +00:00
|
|
|
CGraphics::SetCommitResourcesAsLazy(m_cvarCommons.m_lazyCommitResources->toBoolean());
|
2021-05-24 05:06:51 +00:00
|
|
|
|
2021-05-25 23:14:58 +00:00
|
|
|
boo::SWindowRect rect = m_windowCallback.m_lastRect;
|
|
|
|
rect.location = {0, 0};
|
2021-05-24 05:06:51 +00:00
|
|
|
boo::IGraphicsCommandQueue* gfxQ = m_window->getCommandQueue();
|
|
|
|
if (m_windowCallback.m_rectDirty) {
|
|
|
|
gfxQ->resizeRenderTexture(m_renderTex, rect.size[0], rect.size[1]);
|
2021-05-24 22:53:49 +00:00
|
|
|
CGraphics::SetViewportResolution({rect.size[0], rect.size[1]});
|
2021-05-24 05:06:51 +00:00
|
|
|
m_windowCallback.m_rectDirty = false;
|
2021-05-24 22:53:49 +00:00
|
|
|
} else if (m_firstFrame) {
|
|
|
|
CGraphics::SetViewportResolution({rect.size[0], rect.size[1]});
|
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
|
2021-05-24 05:06:51 +00:00
|
|
|
if (m_windowCallback.m_fullscreenToggleRequested) {
|
2021-06-06 20:59:53 +00:00
|
|
|
m_cvarCommons.m_fullscreen->fromBoolean(!m_cvarCommons.getFullscreen());
|
2021-05-24 05:06:51 +00:00
|
|
|
m_windowCallback.m_fullscreenToggleRequested = false;
|
|
|
|
}
|
|
|
|
|
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()) {
|
|
|
|
m_window->setFullscreen(m_cvarCommons.getFullscreen());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let CVarManager inform all CVar listeners of the CVar's state and clear all mdoified flags if necessary
|
|
|
|
m_cvarManager.proc();
|
|
|
|
|
2021-05-24 21:25:31 +00:00
|
|
|
boo::IGraphicsDataFactory* gfxF = m_window->getMainContextDataFactory();
|
2021-05-25 13:36:58 +00:00
|
|
|
float scale = std::floor(m_window->getVirtualPixelFactor() * 4.f) / 4.f;
|
2021-05-30 19:03:35 +00:00
|
|
|
if (!g_mainMP1 && m_proj) {
|
2021-05-24 21:25:31 +00:00
|
|
|
g_mainMP1.emplace(nullptr, nullptr, gfxF, gfxQ, m_renderTex.get());
|
2021-05-24 05:06:51 +00:00
|
|
|
g_mainMP1->Init(m_fileMgr, &m_cvarManager, m_window.get(), m_voiceEngine.get(), *m_amuseAllocWrapper);
|
|
|
|
if (!m_noShaderWarmup) {
|
|
|
|
g_mainMP1->WarmupShaders();
|
|
|
|
}
|
2021-05-30 19:03:35 +00:00
|
|
|
}
|
|
|
|
if (!m_imGuiInitialized) {
|
2021-06-30 18:20:45 +00:00
|
|
|
ImGuiEngine::Initialize(gfxF, m_window.get(), scale, m_fileMgr.getStoreRoot());
|
2021-05-25 02:02:57 +00:00
|
|
|
m_imGuiInitialized = true;
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 21:25:31 +00:00
|
|
|
float dt = 1 / 60.f;
|
|
|
|
float realDt = dt;
|
|
|
|
auto now = delta_clock::now();
|
|
|
|
if (m_firstFrame) {
|
|
|
|
m_firstFrame = false;
|
|
|
|
} else {
|
|
|
|
using delta_duration = std::chrono::duration<float, std::ratio<1>>;
|
|
|
|
realDt = std::chrono::duration_cast<delta_duration>(now - m_prevFrameTime).count();
|
|
|
|
if (m_cvarCommons.m_variableDt->toBoolean()) {
|
2021-07-03 05:23:29 +00:00
|
|
|
dt = std::min(realDt, 1 / 30.f);
|
2021-05-24 21:25:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m_prevFrameTime = now;
|
|
|
|
|
2021-05-24 22:53:49 +00:00
|
|
|
ImGuiEngine::Begin(realDt, scale);
|
2021-05-24 21:25:31 +00:00
|
|
|
|
2021-05-30 19:03:35 +00:00
|
|
|
if (g_mainMP1) {
|
|
|
|
m_imGuiConsole.PreUpdate();
|
|
|
|
if (g_mainMP1->Proc(dt)) {
|
|
|
|
m_running.store(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_imGuiConsole.PostUpdate();
|
|
|
|
} else {
|
|
|
|
m_imGuiConsole.ShowAboutWindow(false, m_errorString);
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 20:09:30 +00:00
|
|
|
{
|
|
|
|
OPTICK_EVENT("Flush");
|
|
|
|
CGraphics::SetCommitResourcesAsLazy(false);
|
|
|
|
}
|
|
|
|
|
2021-05-24 05:06:51 +00:00
|
|
|
{
|
|
|
|
OPTICK_EVENT("Draw");
|
2021-05-24 21:25:31 +00:00
|
|
|
gfxQ->setRenderTarget(m_renderTex);
|
2021-05-30 19:03:35 +00:00
|
|
|
gfxQ->clearTarget();
|
2021-05-24 22:53:49 +00:00
|
|
|
gfxQ->setViewport(rect);
|
|
|
|
gfxQ->setScissor(rect);
|
2021-05-24 21:25:31 +00:00
|
|
|
if (g_Renderer != nullptr) {
|
2021-05-24 05:06:51 +00:00
|
|
|
g_Renderer->BeginScene();
|
2021-05-24 21:25:31 +00:00
|
|
|
}
|
2021-05-30 19:03:35 +00:00
|
|
|
if (g_mainMP1) {
|
|
|
|
g_mainMP1->Draw();
|
|
|
|
}
|
2021-05-24 21:25:31 +00:00
|
|
|
if (g_Renderer != nullptr) {
|
2021-05-24 05:06:51 +00:00
|
|
|
g_Renderer->EndScene();
|
2021-05-24 21:25:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
OPTICK_EVENT("ImGui Draw");
|
|
|
|
ImGuiEngine::End();
|
|
|
|
ImGuiEngine::Draw(gfxQ);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
OPTICK_EVENT("Execute");
|
2021-05-24 05:06:51 +00:00
|
|
|
gfxQ->execute();
|
|
|
|
}
|
2021-05-24 21:25:31 +00:00
|
|
|
|
2021-05-24 05:06:51 +00:00
|
|
|
gfxQ->resolveDisplay(m_renderTex);
|
|
|
|
|
|
|
|
if (m_voiceEngine) {
|
|
|
|
m_voiceEngine->pumpAndMixVoices();
|
|
|
|
}
|
|
|
|
CBooModel::ClearModelUniformCounters();
|
|
|
|
CGraphics::TickRenderTimings();
|
|
|
|
++logvisor::FrameIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void appQuitting(boo::IApplication* /*unused*/) override { m_running.store(false); }
|
|
|
|
|
|
|
|
[[nodiscard]] std::string getGraphicsApi() const { return m_cvarCommons.getGraphicsApi(); }
|
|
|
|
|
|
|
|
[[nodiscard]] uint32_t getSamples() const { return m_cvarCommons.getSamples(); }
|
|
|
|
|
|
|
|
[[nodiscard]] uint32_t getAnisotropy() const { return m_cvarCommons.getAnisotropy(); }
|
|
|
|
|
|
|
|
[[nodiscard]] bool getDeepColor() const { return m_cvarCommons.getDeepColor(); }
|
|
|
|
|
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
|
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
static char CwdBuf[1024];
|
|
|
|
std::string ExeDir;
|
2021-05-24 05:06:51 +00:00
|
|
|
|
|
|
|
static void SetupBasics(bool logging) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
logvisor::RegisterStandardExceptions();
|
|
|
|
if (logging)
|
|
|
|
logvisor::RegisterConsoleLogger();
|
|
|
|
atSetExceptionHandler(AthenaExc);
|
|
|
|
|
|
|
|
#if SENTRY_ENABLED
|
2021-06-30 18:20:45 +00:00
|
|
|
hecl::Runtime::FileStoreManager fileMgr{"sentry-native-metaforce"};
|
|
|
|
std::string cacheDir{fileMgr.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) {
|
2021-05-24 05:06:51 +00:00
|
|
|
for (int i = 1; i < argc; ++i)
|
2021-06-30 18:20:45 +00:00
|
|
|
if (!hecl::StrNCmp(argv[i], "-l", 2))
|
2021-05-24 05:06:51 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !WINDOWS_STORE
|
2021-06-30 18:20:45 +00:00
|
|
|
int main(int argc, char** argv) {
|
2021-07-25 00:54:26 +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);
|
2021-06-30 18:20:45 +00:00
|
|
|
if (argc > 1 && !hecl::StrCmp(argv[1], "--dlpackage")) {
|
2021-05-24 05:06:51 +00:00
|
|
|
fmt::print(FMT_STRING("{}\n"), METAFORCE_DLPACKAGE);
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetupBasics(IsClientLoggingEnabled(argc, argv));
|
2021-06-30 18:20:45 +00:00
|
|
|
hecl::Runtime::FileStoreManager fileMgr{"metaforce"};
|
2021-05-24 05:06:51 +00:00
|
|
|
hecl::CVarManager cvarMgr{fileMgr};
|
|
|
|
hecl::CVarCommons cvarCmns{cvarMgr};
|
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
std::vector<std::string> args;
|
2021-05-24 05:06:51 +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
|
|
|
cvarMgr.parseCommandLine(args);
|
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
std::string logFile = cvarCmns.getLogFile();
|
|
|
|
std::string logFilePath;
|
2021-05-24 05:06:51 +00:00
|
|
|
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));
|
2021-06-30 18:20:45 +00:00
|
|
|
logFilePath = fmt::format(FMT_STRING("{}/{}-{}"), fileMgr.getStoreRoot(), buf, logFile);
|
2021-05-24 05:06:51 +00:00
|
|
|
logvisor::RegisterFileLogger(logFilePath.c_str());
|
|
|
|
}
|
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
if (char* cwd = hecl::Getcwd(CwdBuf, 1024)) {
|
2021-05-24 05:06:51 +00:00
|
|
|
if (hecl::PathRelative(argv[0]))
|
2021-06-30 18:20:45 +00:00
|
|
|
ExeDir = std::string(cwd) + '/';
|
|
|
|
std::string Argv0(argv[0]);
|
|
|
|
std::string::size_type lastIdx = Argv0.find_last_of("/\\");
|
|
|
|
if (lastIdx != std::string::npos)
|
2021-05-24 05:06:51 +00:00
|
|
|
ExeDir.insert(ExeDir.end(), Argv0.begin(), Argv0.begin() + lastIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle -j argument */
|
|
|
|
hecl::SetCpuCountOverride(argc, argv);
|
|
|
|
|
|
|
|
metaforce::Application appCb(fileMgr, cvarMgr, cvarCmns);
|
2021-06-30 18:20:45 +00:00
|
|
|
int ret = boo::ApplicationRun(boo::IApplication::EPlatformType::Auto, appCb, "metaforce", "Metaforce", argv[0], args,
|
|
|
|
appCb.getGraphicsApi(), appCb.getSamples(), appCb.getAnisotropy(), appCb.getDeepColor(),
|
|
|
|
false);
|
2021-05-24 05:06:51 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if WINDOWS_STORE
|
|
|
|
#include "boo/UWPViewProvider.hpp"
|
|
|
|
using namespace Windows::ApplicationModel::Core;
|
|
|
|
|
|
|
|
[Platform::MTAThread] int WINAPIV main(Platform::Array<Platform::String ^> ^ params) {
|
|
|
|
SetupBasics(false);
|
|
|
|
metaforce::Application appCb;
|
2021-06-30 18:20:45 +00:00
|
|
|
auto viewProvider = ref new boo::ViewProvider(appCb, "metaforce", "Metaforce", "metaforce", params, false);
|
2021-05-24 05:06:51 +00:00
|
|
|
CoreApplication::Run(viewProvider);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif _WIN32
|
2021-05-25 02:02:57 +00:00
|
|
|
#include <shellapi.h>
|
2021-06-30 18:20:45 +00:00
|
|
|
#include <nowide/args.hpp>
|
2021-05-25 02:02:57 +00:00
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int) {
|
2021-05-24 05:06:51 +00:00
|
|
|
int argc = 0;
|
2021-06-30 18:20:45 +00:00
|
|
|
char** argv = nullptr;
|
|
|
|
nowide::args _(argc, argv);
|
2021-05-25 02:02:57 +00:00
|
|
|
const DWORD outType = GetFileType(GetStdHandle(STD_ERROR_HANDLE));
|
2021-06-30 18:20:45 +00:00
|
|
|
if (IsClientLoggingEnabled(argc, argv) && outType == FILE_TYPE_UNKNOWN)
|
2021-05-24 05:06:51 +00:00
|
|
|
logvisor::CreateWin32Console();
|
2021-06-30 18:20:45 +00:00
|
|
|
return main(argc, argv);
|
2021-05-24 05:06:51 +00:00
|
|
|
}
|
|
|
|
#endif
|