Skip to content

Commit

Permalink
Delete App::impl at exit so that assert doesn't trigger on next start…
Browse files Browse the repository at this point in the history
… on Android
  • Loading branch information
jhasse committed Feb 9, 2024
1 parent 81ba5ac commit 8f99617
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ App& App::instance() {
return *self;
}

void App::init(AppParameters params) {
Finally App::init(AppParameters params) {
assert(impl == nullptr);
impl = std::make_unique<App::Impl>(
App::Impl{ std::move(params.displayName), params.pixelArt, params.steamAppId });
return Finally{ [this]() { impl.reset(); } };
}

void App::atExit(std::function<void()> f) {
Expand Down Expand Up @@ -117,7 +118,7 @@ void App::updateProjectionMatrix() const {
namespace internal {

void mainLoop(AppParameters params) {
App::instance().init(params);
auto context = App::instance().init(params);
if (auto id = params.steamAppId) {
jngl::initSteam(*id);
}
Expand Down Expand Up @@ -160,6 +161,7 @@ void mainLoop(AppParameters params) {
}
setWork(params.start());
App::instance().mainLoop();
hideWindow();
}

} // namespace internal
Expand Down
6 changes: 4 additions & 2 deletions src/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
/// @file
#pragma once

#include "jngl/Finally.hpp"

#include <functional>
#include <memory>
#include <string>
Expand Down Expand Up @@ -45,8 +47,8 @@ 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);
/// Initializes Impl, the returned Finally will destroy it again
[[nodiscard]] Finally init(AppParameters);

// TODO for C++23: Change to std::move_only_function<void() noexcept>
void atExit(std::function<void()>);
Expand Down
8 changes: 5 additions & 3 deletions src/ios/JNGLView.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
#import <OpenGLES/ES3/glext.h>

namespace jngl {
struct AppParameters;
class WindowImpl;
}
struct AppParameters;
class WindowImpl;
class Finally;
} // namespace jngl

@interface JNGLView : UIView <UIKeyInput> {
EAGLContext* context;
Expand All @@ -18,6 +19,7 @@ namespace jngl {
jngl::WindowImpl* impl;
bool pause;
bool needToResetFrameLimiter;
jngl::Finally* cleanAppImpl;
}

- (instancetype)initWithFrame:(CGRect)frame withAppParameters:(const jngl::AppParameters&)params;
Expand Down
8 changes: 7 additions & 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);

jngl::App::instance().init(params);
cleanAppImpl = new jngl::Finally(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 Expand Up @@ -88,6 +88,12 @@ - (instancetype) initWithFrame: (CGRect)frame withAppParameters: (const jngl::Ap
return self;
}

- (void)dealloc {
delete cleanAppImpl; // it seems this never gets called? I guess we're not allowed to do much at
// exit on iOS
[super dealloc];
}

- (void) drawView: (CADisplayLink*) displayLink {
if (!pause) {
if (displayLink) {
Expand Down
3 changes: 1 addition & 2 deletions src/jngl/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
extern android_app* androidApp;
}
#define JNGL_MAIN_BEGIN void android_main(android_app* __androidApp) { \
jngl::androidApp = __androidApp; \
jngl::Finally _ZtzNg47T5XSjogv(jngl::hideWindow);
jngl::androidApp = __androidApp;
#define JNGL_MAIN_END }
#elif defined(__APPLE__) && TARGET_OS_IPHONE // iOS
#define JNGL_MAIN_BEGIN void shouldNotBeCalled() {
Expand Down

0 comments on commit 8f99617

Please sign in to comment.