boo/lib/nx/ApplicationNX.cpp

102 lines
3.3 KiB
C++
Raw Normal View History

2018-10-07 02:49:22 +00:00
#include "boo/IApplication.hpp"
#include "logvisor/logvisor.hpp"
#include "nxstl/thread"
#include "nxstl/condition_variable"
#include "boo/graphicsdev/NX.hpp"
#include <limits.h>
#include <switch.h>
2018-12-08 05:17:51 +00:00
namespace boo {
2018-10-07 02:49:22 +00:00
static logvisor::Module Log("boo::NXApplication");
std::shared_ptr<IWindow> _WindowNXNew(std::string_view title, NXContext* nxCtx);
2018-12-08 05:17:51 +00:00
class ApplicationNX : public IApplication {
IApplicationCallback& m_callback;
const std::string m_uniqueName;
const std::string m_friendlyName;
const std::string m_pname;
const std::vector<std::string> m_args;
2018-10-07 02:49:22 +00:00
2018-12-08 05:17:51 +00:00
NXContext m_nxCtx;
2018-10-07 02:49:22 +00:00
void _deletedWindow(IWindow* window) override {}
2018-10-07 02:49:22 +00:00
public:
2018-12-08 05:17:51 +00:00
ApplicationNX(IApplicationCallback& callback, std::string_view uniqueName, std::string_view friendlyName,
std::string_view pname, const std::vector<std::string>& args, std::string_view gfxApi, uint32_t samples,
uint32_t anisotropy, bool deepColor, bool singleInstance)
: m_callback(callback), m_uniqueName(uniqueName), m_friendlyName(friendlyName), m_pname(pname), m_args(args) {}
EPlatformType getPlatformType() const override { return EPlatformType::NX; }
2018-12-08 05:17:51 +00:00
int run() override {
2018-12-08 05:17:51 +00:00
/* Spawn client thread */
int clientReturn = INT_MIN;
std::mutex initmt;
std::condition_variable initcv;
std::unique_lock<std::mutex> outerLk(initmt);
std::thread clientThread([&]() {
std::unique_lock<std::mutex> innerLk(initmt);
innerLk.unlock();
initcv.notify_one();
std::string thrName = std::string(getFriendlyName()) + " Client";
logvisor::RegisterThreadName(thrName.c_str());
clientReturn = m_callback.appMain(this);
});
initcv.wait(outerLk);
// Main graphics loop
while (clientReturn == INT_MIN && appletMainLoop()) {
// Get and process input
hidScanInput();
u32 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS)
break;
2018-10-07 02:49:22 +00:00
}
2018-12-08 05:17:51 +00:00
m_callback.appQuitting(this);
if (clientThread.joinable())
clientThread.join();
2018-10-07 02:49:22 +00:00
2018-12-08 05:17:51 +00:00
return 0;
}
2018-10-07 02:49:22 +00:00
std::string_view getUniqueName() const override { return m_uniqueName; }
2018-10-07 02:49:22 +00:00
std::string_view getFriendlyName() const override { return m_friendlyName; }
2018-10-07 02:49:22 +00:00
std::string_view getProcessName() const override { return m_pname; }
2018-12-08 05:17:51 +00:00
const std::vector<std::string>& getArgs() const override { return m_args; }
2018-12-08 05:17:51 +00:00
std::shared_ptr<IWindow> m_window;
std::shared_ptr<IWindow> newWindow(std::string_view title) override {
2018-12-08 05:17:51 +00:00
if (m_window)
2019-07-20 04:22:36 +00:00
Log.report(logvisor::Fatal, fmt("Only 1 window allowed on NX"));
2018-12-08 05:17:51 +00:00
m_window = _WindowNXNew(title, &m_nxCtx);
return m_window;
}
2018-10-07 02:49:22 +00:00
};
IApplication* APP = nullptr;
2018-12-08 05:17:51 +00:00
int ApplicationRun(IApplication::EPlatformType platform, IApplicationCallback& cb, SystemStringView uniqueName,
SystemStringView friendlyName, SystemStringView pname, const std::vector<SystemString>& args,
std::string_view gfxApi, uint32_t samples, uint32_t anisotropy, bool deepColor,
bool singleInstance) {
std::string thrName = std::string(friendlyName) + " Main Thread";
logvisor::RegisterThreadName(thrName.c_str());
if (APP)
return 1;
APP = new ApplicationNX(cb, uniqueName, friendlyName, pname, args, gfxApi, samples, anisotropy, deepColor,
singleInstance);
int ret = APP->run();
delete APP;
APP = nullptr;
return ret;
2018-10-07 02:49:22 +00:00
}
2018-12-08 05:17:51 +00:00
} // namespace boo