Skip to content

Commit 80a7383

Browse files
committed
Removed individual macros and created SUPERNOVA_INIT
1 parent 42d6b04 commit 80a7383

10 files changed

Lines changed: 46 additions & 52 deletions

File tree

engine/core/Engine.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ FunctionSubscribe<void(int,bool,int)> Engine::onKeyDown;
9393
FunctionSubscribe<void(int,bool,int)> Engine::onKeyUp;
9494
FunctionSubscribe<void(wchar_t)> Engine::onCharInput;
9595

96+
FunctionSubscribe<void()>& Engine::getOnInit() {
97+
// This static variable is guaranteed to be created the first time this function is called
98+
static FunctionSubscribe<void()> onInit;
99+
return onInit;
100+
}
96101

97102
void Engine::setScene(Scene* scene){
98103
if (asyncThread)
@@ -554,12 +559,14 @@ void Engine::calculateCanvas(){
554559
}
555560
}
556561

557-
void Engine::systemInit(int argc, char* argv[]){
562+
void Engine::systemInit(int argc, char* argv[], System* system){
558563
#ifndef NO_THREAD_SUPPORT
559564
size_t maxThreads = std::max(1u, std::thread::hardware_concurrency() - 1);
560565
ThreadPoolManager::initialize(maxThreads);
561566
#endif
562567

568+
System::setSystemInstance(system);
569+
563570
Engine::setCanvasSize(1000,480);
564571

565572
lastTime = 0;
@@ -580,7 +587,7 @@ void Engine::systemInit(int argc, char* argv[]){
580587
LuaBinding::init();
581588
#endif
582589
#ifndef NO_CPP_INIT
583-
init();
590+
getOnInit().call();
584591
#endif
585592
}
586593

engine/core/Engine.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,16 @@
6868
#include <atomic>
6969
#include <unordered_set>
7070

71-
void init();
71+
#define SUPERNOVA_INIT \
72+
void init(); \
73+
namespace { \
74+
struct InitRegistrar { \
75+
InitRegistrar() { \
76+
Supernova::Engine::getOnInit().add("user_init", &init); \
77+
} \
78+
}; \
79+
static InitRegistrar _supernova_init_registrar; \
80+
}
7281

7382
namespace Supernova {
7483

@@ -267,7 +276,7 @@ namespace Supernova {
267276
static void clearAllSubscriptions(bool includeLifecycle);
268277

269278
//-----Supernova API functions-----
270-
static void systemInit(int argc, char* argv[]);
279+
static void systemInit(int argc, char* argv[], System* system);
271280
static void systemViewLoaded();
272281
static void systemViewChanged();
273282
static void systemDraw();
@@ -295,6 +304,10 @@ namespace Supernova {
295304
static void systemCharInput(wchar_t codepoint);
296305

297306
//-----Supernova user events-----
307+
// Safe accessor for the Init event
308+
static FunctionSubscribe<void()>& getOnInit();
309+
310+
// Existing events (keep these as they are safe for runtime usage)
298311
static FunctionSubscribe<void()> onViewLoaded;
299312
static FunctionSubscribe<void()> onViewChanged;
300313
static FunctionSubscribe<void()> onViewDestroyed;

engine/core/System.cpp

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,22 @@
1010

1111
using namespace Supernova;
1212

13-
System* System::external = nullptr;
13+
System* System::systemInstance = nullptr;
1414

1515
#define USERSETTINGS_ROOT "userSettings"
1616
#define USERSETTINGS_XML_FILE "data://UserSettings.xml"
1717

18-
#ifdef SUPERNOVA_ANDROID
19-
#include "SupernovaAndroid.h"
20-
#endif
21-
#ifdef SUPERNOVA_IOS
22-
#include "SupernovaIOS.h"
23-
#endif
24-
#ifdef SUPERNOVA_WEB
25-
#include "SupernovaWeb.h"
26-
#endif
27-
#ifdef SUPERNOVA_SOKOL
28-
#include "SupernovaSokol.h"
29-
#endif
30-
#ifdef SUPERNOVA_GLFW
31-
#include "SupernovaGLFW.h"
32-
#endif
33-
#ifdef SUPERNOVA_APPLE
34-
#include "SupernovaApple.h"
35-
#endif
36-
3718
System& System::instance(){
38-
#ifdef SUPERNOVA_ANDROID
39-
static System* instance = new SupernovaAndroid();
40-
#elif defined(SUPERNOVA_IOS)
41-
static System* instance = new SupernovaIOS();
42-
#elif defined(SUPERNOVA_WEB)
43-
static System* instance = new SupernovaWeb();
44-
#elif defined(SUPERNOVA_SOKOL)
45-
static System* instance = new SupernovaSokol();
46-
#elif defined(SUPERNOVA_GLFW)
47-
static System* instance = new SupernovaGLFW();
48-
#elif defined(SUPERNOVA_APPLE)
49-
static System* instance = new SupernovaApple();
50-
#else
51-
static System* instance = external;
52-
#endif
19+
if (!systemInstance) {
20+
Log::error("System instance not set. Engine must call System::setSystemInstance() during initialization.");
21+
abort();
22+
}
5323

54-
return *instance;
24+
return *systemInstance;
5525
}
5626

57-
void System::setExternalSystem(System* system){
58-
external = system;
27+
void System::setSystemInstance(System* system){
28+
systemInstance = system;
5929
}
6030

6131
sg_environment System::getSokolEnvironment(){

engine/core/System.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "sokol_gfx.h"
1919

2020
namespace Supernova {
21+
class Engine;
22+
2123
enum class AdMobRating{
2224
General,
2325
ParentalGuidance,
@@ -41,7 +43,12 @@ namespace Supernova {
4143
class SUPERNOVA_API System {
4244
private:
4345

44-
static System* external;
46+
friend class Engine;
47+
48+
static System* systemInstance;
49+
50+
// Only Engine is allowed to inject an external System implementation.
51+
static void setSystemInstance(System* system);
4552

4653
protected:
4754

@@ -54,8 +61,6 @@ namespace Supernova {
5461

5562
virtual ~System() {}
5663

57-
static void setExternalSystem(System* system);
58-
5964
// *******
6065
// Used for user and Lua
6166
// *******

platform/android/NativeEngine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ NativeEngine::NativeEngine(struct android_app *app) {
110110

111111
setupJNI();
112112

113-
Supernova::Engine::systemInit(0, nullptr);
113+
Supernova::Engine::systemInit(0, nullptr, new SupernovaAndroid());
114114
}
115115

116116
NativeEngine::~NativeEngine() {

platform/apple/Renderer.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ -(char**)getArray:(NSArray*)a_array
4343

4444
- (nonnull instancetype)initWithMetalKitView:(nonnull MTKView *)view withArgs:(NSArray*)args;
4545
{
46-
Supernova::Engine::systemInit((int)[args count], [self getArray:args]);
46+
Supernova::Engine::systemInit((int)[args count], [self getArray:args], new SupernovaApple());
4747

4848
self = [super init];
4949
if(self)

platform/emscripten/SupernovaWeb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ int SupernovaWeb::init(int argc, char **argv){
147147
});
148148
);
149149

150-
Supernova::Engine::systemInit(argc, argv);
150+
Supernova::Engine::systemInit(argc, argv, new SupernovaWeb());
151151

152152
sampleCount = (antiAlias) ? 4 : 1;
153153

platform/glfw/SupernovaGLFW.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int SupernovaGLFW::init(int argc, char **argv){
3333

3434
sampleCount = 1;
3535

36-
Supernova::Engine::systemInit(argc, argv);
36+
Supernova::Engine::systemInit(argc, argv, new SupernovaGLFW());
3737

3838
/* create window and GL context via GLFW */
3939
glfwInit();

platform/sokol/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ sapp_desc sokol_main(int argc, char* argv[]) {
8787
app_desc.sample_count = 4;
8888
app_desc.window_title = "Supernova";
8989

90-
Supernova::Engine::systemInit(argc, argv);
90+
Supernova::Engine::systemInit(argc, argv, new SupernovaSokol());
9191

9292
return app_desc;
9393
}

project/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ using namespace Supernova;
66
Scene scene;
77
Image image(&scene);
88

9-
void init(){
10-
9+
SUPERNOVA_INIT void init(){
1110
image.setAnchorPreset(AnchorPreset::CENTER);
1211
image.setTexture("supernova.png");
1312

0 commit comments

Comments
 (0)