Skip to content

Commit

Permalink
Turn jngl::App back into some kind of singleton
Browse files Browse the repository at this point in the history
Otherwise isPixelArt crashes on emscripten since mainLoop() gets exited
there.
  • Loading branch information
jhasse committed Oct 6, 2023
1 parent fe7bcfb commit ed0405c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
22 changes: 14 additions & 8 deletions src/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "windowptr.hpp"

#include <cmath>
#include <memory>
#include <set>
#include <stdexcept>

Expand All @@ -32,15 +33,11 @@ struct App::Impl {
std::set<ShaderProgram*> 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() {
Expand All @@ -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>(
App::Impl{ std::move(params.displayName), params.pixelArt, params.steamAppId });
}

std::string App::getDisplayName() const {
return impl->displayName;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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*);

Expand Down
2 changes: 1 addition & 1 deletion src/ios/JNGLView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
3 changes: 0 additions & 3 deletions src/unittest/Fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

#include "Fixture.hpp"

#include "../App.hpp"

#include <boost/ut.hpp>
#include <cmath>
#include <jngl.hpp>
#include <jngl/AppParameters.hpp>

Fixture::Fixture(const double scaleFactor) {
jngl::setScaleFactor(scaleFactor);
static jngl::App app(jngl::AppParameters{});
jngl::showWindow("unit test", static_cast<int>(std::lround(320 * scaleFactor)),
static_cast<int>(std::lround(70 * scaleFactor)), false, { 32, 7 }, { 32, 7 });
reset();
Expand Down

0 comments on commit ed0405c

Please sign in to comment.