From ed0405c83af131d6e7166f44534ac477c816fe6f Mon Sep 17 00:00:00 2001 From: Jan Niklas Hasse Date: Fri, 6 Oct 2023 21:40:44 +0200 Subject: [PATCH] Turn jngl::App back into some kind of singleton Otherwise isPixelArt crashes on emscripten since mainLoop() gets exited there. --- src/App.cpp | 22 ++++++++++++++-------- src/App.hpp | 8 +++++--- src/ios/JNGLView.mm | 2 +- src/unittest/Fixture.cpp | 3 --- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/App.cpp b/src/App.cpp index 6bbc4bad5..64f7ab6fa 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -12,6 +12,7 @@ #include "windowptr.hpp" #include +#include #include #include @@ -32,15 +33,11 @@ struct App::Impl { std::set shaderPrograms{}; }; -App::App(AppParameters params) -: impl(new Impl{ std::move(params.displayName), params.pixelArt, params.steamAppId }) { +App::App() { if (self) { throw std::runtime_error("You may only create one instance of jngl::App."); } self = this; - if (auto id = impl->steamAppId) { - jngl::initSteam(*id); - } } App::~App() { @@ -49,11 +46,16 @@ App::~App() { App& App::instance() { if (!self) { - self = new App({}); + self = new App; } return *self; } +void App::init(AppParameters params) { + impl = std::make_unique( + App::Impl{ std::move(params.displayName), params.pixelArt, params.steamAppId }); +} + std::string App::getDisplayName() const { return impl->displayName; } @@ -100,7 +102,11 @@ void App::updateProjectionMatrix() const { namespace internal { void mainLoop(AppParameters params) { - App app(params); + App::instance().init(params); + if (auto id = params.steamAppId) { + jngl::initSteam(*id); + } + bool fullscreen = false; #if (!defined(__EMSCRIPTEN__) && defined(NDEBUG)) || defined(__ANDROID__) fullscreen = true; @@ -141,7 +147,7 @@ void mainLoop(AppParameters params) { fullscreen, params.minAspectRatio ? *params.minAspectRatio : minAspectRatio, params.maxAspectRatio ? *params.maxAspectRatio : maxAspectRatio); setWork(params.start()); - app.mainLoop(); + App::instance().mainLoop(); } } // namespace internal diff --git a/src/App.hpp b/src/App.hpp index 50ac7c9a2..27568c281 100644 --- a/src/App.hpp +++ b/src/App.hpp @@ -26,9 +26,6 @@ class ShaderProgram; /// \endcode class App { public: - /// Do not call this constructor yourself, it gets called by JNGL_MAIN_BEGIN - explicit App(AppParameters); - ~App(); App(const App&) = delete; App& operator=(const App&) = delete; @@ -58,7 +55,12 @@ class App { /// Internal function used by JNGL when the Window is resized void updateProjectionMatrix() const; + /// Do not call this function yourself, it gets called by JNGL_MAIN_BEGIN + void init(AppParameters); + private: + App(); + void registerShaderProgram(ShaderProgram*); void unregisterShaderProgram(ShaderProgram*); diff --git a/src/ios/JNGLView.mm b/src/ios/JNGLView.mm index 517a8c929..a03d682ca 100644 --- a/src/ios/JNGLView.mm +++ b/src/ios/JNGLView.mm @@ -60,7 +60,7 @@ - (instancetype) initWithFrame: (CGRect)frame withAppParameters: (const jngl::Ap glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width); glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height); - jnglApp.reset(new jngl::App(params)); + jngl::App::instance().init(params); jngl::showWindow("", width, height, true, params.minAspectRatio ? *params.minAspectRatio : std::make_pair(1, 3), params.maxAspectRatio ? *params.maxAspectRatio : std::make_pair(3, 1)); diff --git a/src/unittest/Fixture.cpp b/src/unittest/Fixture.cpp index 81dad1c3f..902d89426 100644 --- a/src/unittest/Fixture.cpp +++ b/src/unittest/Fixture.cpp @@ -3,8 +3,6 @@ #include "Fixture.hpp" -#include "../App.hpp" - #include #include #include @@ -12,7 +10,6 @@ Fixture::Fixture(const double scaleFactor) { jngl::setScaleFactor(scaleFactor); - static jngl::App app(jngl::AppParameters{}); jngl::showWindow("unit test", static_cast(std::lround(320 * scaleFactor)), static_cast(std::lround(70 * scaleFactor)), false, { 32, 7 }, { 32, 7 }); reset();