77#include " nbl/system/declarations.h"
88#include " nbl/system/definitions.h"
99
10+ #if defined(_NBL_PLATFORM_WINDOWS_)
11+ #include " nbl/system/CColoredStdoutLoggerWin32.h"
12+ #elif defined(_NBL_PLATFORM_ANDROID_)
13+ #include " nbl/system/CStdoutLoggerAndroid.h"
14+ #endif
15+ #include " nbl/system/CSystemAndroid.h"
16+ #include " nbl/system/CSystemLinux.h"
17+ #include " nbl/system/CSystemWin32.h"
1018
1119namespace nbl ::system
1220{
1321
14- class IApplicationFramework
22+ class IApplicationFramework : public core ::IReferenceCounted
1523{
1624 public:
25+ // this is safe to call multiple times
1726 static void GlobalsInit ()
1827 {
1928 #ifdef _NBL_PLATFORM_WINDOWS_
2029 #ifdef NBL_CPACK_PACKAGE_DXC_DLL_DIR
21- const HRESULT dxcLoad = CSystemWin32::delayLoadDLL (" dxcompiler.dll" , { system:: path (_DXC_DLL_).parent_path (), NBL_CPACK_PACKAGE_DXC_DLL_DIR });
30+ const HRESULT dxcLoad = CSystemWin32::delayLoadDLL (" dxcompiler.dll" , { path (_DXC_DLL_).parent_path (), NBL_CPACK_PACKAGE_DXC_DLL_DIR });
2231 #else
23- const HRESULT dxcLoad = CSystemWin32::delayLoadDLL (" dxcompiler.dll" , { system:: path (_DXC_DLL_).parent_path () });
32+ const HRESULT dxcLoad = CSystemWin32::delayLoadDLL (" dxcompiler.dll" , { path (_DXC_DLL_).parent_path () });
2433 #endif
2534
2635 // assert(SUCCEEDED(dxcLoad)); // no clue why this fails to find the dll
@@ -43,39 +52,63 @@ class IApplicationFramework
4352 #endif
4453 }
4554
55+ // we take the derived class as Curiously Recurring Template Parameter
56+ template <class CRTP > requires std::is_base_of_v<IApplicationFramework,CRTP>
57+ static int main (int argc, char ** argv)
58+ {
59+ path CWD = system::path (argv[0 ]).parent_path ().generic_string () + " /" ;
60+ auto app = core::make_smart_refctd_ptr<CRTP>(CWD/" ../../media/" ,CWD/" ../../tmp/" ,CWD/" ../" ,CWD);
61+ for (auto i=0 ; i<argc; i++)
62+ app->argv .emplace_back (argv[i]);
63+
64+ if (!app->onAppInitialized (nullptr ))
65+ return -1 ;
66+ while (app->keepRunning ())
67+ app->workLoopBody ();
68+ return app->onAppTerminated () ? 0 :(-2 );
69+ }
70+
71+ static nbl::core::smart_refctd_ptr<ISystem> createSystem ()
72+ {
73+ GlobalsInit ();
74+ #ifdef _NBL_PLATFORM_WINDOWS_
75+ return nbl::core::make_smart_refctd_ptr<CSystemWin32>();
76+ #elif defined(_NBL_PLATFORM_ANDROID_)
77+ return nullptr ;
78+ #endif
79+ return nullptr ;
80+ }
81+
4682 IApplicationFramework (
47- const system:: path& _localInputCWD,
48- const system:: path& _localOutputCWD,
49- const system:: path& _sharedInputCWD,
50- const system:: path& _sharedOutputCWD) :
83+ const path& _localInputCWD,
84+ const path& _localOutputCWD,
85+ const path& _sharedInputCWD,
86+ const path& _sharedOutputCWD) :
5187 localInputCWD (_localInputCWD), localOutputCWD(_localOutputCWD), sharedInputCWD(_sharedInputCWD), sharedOutputCWD(_sharedOutputCWD)
5288 {
5389 GlobalsInit ();
5490 }
5591
56- virtual void setSystem (core::smart_refctd_ptr<nbl::system::ISystem>&& system) = 0;
92+ // DEPRECATED
93+ virtual void setSystem (core::smart_refctd_ptr<ISystem>&& system) {}
5794
58- void onAppInitialized ()
59- {
60- return onAppInitialized_impl ();
61- }
62- void onAppTerminated ()
63- {
64- return onAppTerminated_impl ();
65- }
95+ // Some platforms are weird, and you can't really do anything with the system unless you have some magical object that gets passed to the platform's entry point.
96+ // Therefore the specific `CPLATFORMSystem` is uncreatable out of thin air so you need to take an outside provided one
97+ virtual bool onAppInitialized (core::smart_refctd_ptr<ISystem>&& system=nullptr ) {onAppInitialized_impl (std::move (system)); return true ;}
98+ virtual bool onAppTerminated () {return true ;}
6699
67100 virtual void workLoopBody () = 0;
68101 virtual bool keepRunning () = 0;
69102
70- // TODO: refactor/hide
71- std::vector<std::string> argv;
72-
73103 protected:
74104 ~IApplicationFramework () {}
75105
76- // TODO: why aren't these pure virtual, and why do we even need a `_impl()`suffix?
77- virtual void onAppInitialized_impl () {}
78- virtual void onAppTerminated_impl () {}
106+ // DEPRECATED
107+ virtual void onAppInitialized_impl (core::smart_refctd_ptr<ISystem>&& system) {assert (false );}
108+ virtual void onAppTerminated_impl () {assert (false );}
109+
110+ // for platforms with cmdline args
111+ core::vector<std::string> argv;
79112
80113 /*
81114 ****************** Current Working Directories ********************
@@ -90,24 +123,34 @@ class IApplicationFramework
90123
91124 To add files to your assets directory, create an "assets" directory in your app's source directory
92125 */
93- system:: path localInputCWD;
126+ path localInputCWD;
94127
95128 /*
96129 This is a CWD used to output app-local data e.g. screenshots
97130 */
98- system:: path localOutputCWD;
131+ path localOutputCWD;
99132
100133 /*
101134 The CWD for input data that can be shared among apps, like the "examples_tests/media" directory for Nabla examples
102135 */
103- system:: path sharedInputCWD;
136+ path sharedInputCWD;
104137
105138 /*
106139 This CWD is used to output data that can be shared between apps e.g. quantization cache
107140 */
108- system:: path sharedOutputCWD;
141+ path sharedOutputCWD;
109142};
110143
111144}
112145
146+ // get the correct entry point to declate
147+ #ifdef _NBL_PLATFORM_ANDROID_
148+ #include " nbl/system/CApplicationAndroid.h"
149+ #else
150+ #define NBL_MAIN_FUNC (AppClass ) int main (int argc, char ** argv) \
151+ {\
152+ return AppClass::main<AppClass>(argc,argv);\
153+ }
154+ #endif
155+
113156#endif
0 commit comments