Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Application.cpp
Go to the documentation of this file.
1
2
4
5#include "Headers/Kernel.h"
8
11
12namespace Divide {
13
16NO_DESTROY std::array<DisplayManager::OutputDisplayPropertiesContainer, DisplayManager::g_maxDisplayOutputs> DisplayManager::s_supportedDisplayModes;
17
19 : _mainLoopPaused{false}
20 , _mainLoopActive{false}
21 , _freezeRendering{false}
22{
23}
24
26{
27 DIVIDE_ASSERT( _kernel == nullptr );
28}
29
30ErrorCode Application::start(const string& entryPoint, const I32 argc, char** argv)
31{
32 assert( !entryPoint.empty() );
33 assert( _kernel == nullptr );
34
36 Console::printfn(LOCALE_STR("START_APPLICATION"));
37 Console::printfn(LOCALE_STR("START_APPLICATION_CMD_ARGUMENTS"));
38 if ( argc > 1 )
39 {
40 for (I32 i = 1; i < argc; ++i)
41 {
42 Console::printfn("{}", argv[i]);
43 }
44 }
45 else
46 {
47 Console::printfn(LOCALE_STR("START_APPLICATION_CMD_ARGUMENTS_NONE" ));
48 }
49
50 // Create a new kernel
51 _kernel = std::make_unique<Kernel>(argc, argv, *this);
52
53 _timer.reset();
54
55 // and load it via an XML file config
56 const ErrorCode err = Attorney::KernelApplication::initialize(_kernel.get(), entryPoint);
57
58 // failed to start, so cleanup
59 if (err != ErrorCode::NO_ERR)
60 {
62 }
63 else
64 {
66 Console::printfn(LOCALE_STR("START_MAIN_LOOP"));
68 mainLoopActive(true);
69 }
70
71 return err;
72}
73
75{
76 if ( state == Profiler::State::COUNT ) [[unlikely]]
77 {
78 return false;
79 }
80
81 PlatformContext& context = _kernel->platformContext();
82 static bool assertOnAPIError = context.config().debug.renderer.assertOnRenderAPIError;
83 static bool apiDebugging = context.config().debug.renderer.enableRenderAPIDebugging;
84
85 switch ( state )
86 {
88 {
91 } break;
93 {
94 context.config().debug.renderer.assertOnRenderAPIError = assertOnAPIError;
95 context.config().debug.renderer.enableRenderAPIDebugging = apiDebugging;
96 } break;
97
98 case Profiler::State::COUNT: break;
99 }
100
101
102 return true;
103}
104
106{
107 PlatformContext& context = _kernel->platformContext();
108 const Configuration& config = context.config();
109
110 return _windowManager.init( context,
111 api,
112 {-1, -1},
113 config.runtime.windowSize,
114 static_cast<WindowMode>(config.runtime.windowedMode),
115 config.runtime.targetDisplay );
116}
117
118void Application::stop( const AppStepResult stepResult )
119{
120 Console::printfn( LOCALE_STR( "STOP_APPLICATION" ) );
121
122 if ( _kernel == nullptr )
123 {
124 return;
125 }
126
128
130 _kernel.reset();
132
133 if ( stepResult == AppStepResult::RESTART_CLEAR_CACHE || stepResult == AppStepResult::STOP_CLEAR_CACHE )
134 {
135 if ( !deleteAllFiles( Paths::g_cacheLocation, nullptr, "keep") )
136 {
137 NOP();
138 }
139 }
140}
141
143{
145
146 if ( mainLoopActive() )
147 {
148 PROFILE_FRAME( "Main Thread" );
150 }
151 else
152 {
153 if ( RestartRequested() )
154 {
156 }
157 else if ( ShutdownRequested() )
158 {
160 }
161 else
162 {
163 result = AppStepResult::ERROR;
164 }
165
166 _clearCacheOnExit = false;
167
171 }
172
173 return result;
174}
175
176bool Application::onSDLEvent(const SDL_Event event) noexcept
177{
178 switch ( event.type )
179 {
180 case SDL_QUIT:
181 {
182 RequestShutdown( false );
183 } break;
184 case SDL_APP_TERMINATING:
185 {
186 Console::errorfn( LOCALE_STR( "ERROR_APPLICATION_SYSTEM_CLOSE_REQUEST" ) );
187 RequestShutdown( false );
188 } break;
189 case SDL_RENDER_TARGETS_RESET :
190 {
191 Console::warnfn( LOCALE_STR( "ERROR_APPLICATION_RENDER_TARGET_RESET" ) );
192 //RequestShutdown( false );
193 } break;
194 case SDL_RENDER_DEVICE_RESET :
195 {
196 Console::errorfn( LOCALE_STR("ERROR_APPLICATION_RENDER_DEVICE_RESET") );
197 RequestShutdown( false );
198 } break;
199 case SDL_APP_LOWMEMORY :
200 {
201 Console::errorfn( LOCALE_STR( "ERROR_APPLICATION_LOW_MEMORY" ) );
202 RequestShutdown( false );
203 } break;
204 case SDL_DROPFILE:
205 case SDL_DROPTEXT:
206 case SDL_DROPBEGIN:
207 case SDL_DROPCOMPLETE:
208 {
209 Console::warnfn( LOCALE_STR("WARN_APPLICATION_DRAG_DROP") );
210 } break;
211 case SDL_FINGERDOWN :
212 case SDL_FINGERUP :
213 case SDL_FINGERMOTION :
214 case SDL_DOLLARGESTURE :
215 case SDL_DOLLARRECORD :
216 case SDL_MULTIGESTURE :
217 {
218 Console::warnfn( LOCALE_STR( "WARN_APPLICATION_TOUCH_EVENT" ) );
219 } break;
220 default: break;
221 }
222
223 return ShutdownRequested();
224}
225
227{
229 return true;
230}
231
233{
235 return true;
236}
237
239{
240 s_activeDisplayCount = std::min( displayCount, g_maxDisplayOutputs );
241}
242
244{
245 DIVIDE_ASSERT( displayIndex < g_maxDisplayOutputs );
246 s_supportedDisplayModes[displayIndex].push_back( mode );
247}
248
250{
251 DIVIDE_ASSERT( displayIndex < g_maxDisplayOutputs );
252 return s_supportedDisplayModes[displayIndex];
253}
254
256{
258}
259
261{
262 return s_maxMSAASAmples;
263}
264
265void DisplayManager::MaxMSAASamples( const U8 maxSampleCount ) noexcept
266{
267 s_maxMSAASAmples = std::min( maxSampleCount, to_U8( 64u ) );
268}
269
271{
272 for ( auto& entries : s_supportedDisplayModes )
273 {
274 entries.clear();
275 }
276}
277
278}; //namespace Divide
#define LOCALE_STR(X)
Definition: Localization.h:91
#define DIVIDE_ASSERT(...)
#define NO_DESTROY
#define NOP()
#define PROFILE_FRAME(NAME)
Definition: Profiler.h:89
char * argv[]
Definition: main.cpp:8
bool mainLoopActive() const
Definition: Application.h:130
bool onSDLEvent(SDL_Event event) noexcept override
bool RestartRequested() const noexcept
Definition: Application.inl:72
std::unique_ptr< Kernel > _kernel
Definition: Application.h:148
void CancelShutdown() noexcept
Definition: Application.inl:51
void stop(const AppStepResult stepResult)
WindowManager & windowManager() noexcept
Definition: Application.inl:77
bool onResolutionChange(const SizeChangeParams &params) const
ErrorCode start(const string &entryPoint, I32 argc, char **argv)
Definition: Application.cpp:30
~Application() override
Definition: Application.cpp:25
bool ShutdownRequested() const noexcept
Definition: Application.inl:56
ErrorCode setRenderingAPI(const RenderAPI api)
WindowManager _windowManager
Definition: Application.h:146
bool onProfilerStateChanged(Profiler::State state)
Definition: Application.cpp:74
bool onWindowSizeChange(const SizeChangeParams &params) const
AppStepResult step()
void CancelRestart() noexcept
Definition: Application.inl:67
Application() noexcept
Definition: Application.cpp:18
static void onResolutionChange(Kernel *kernel, const SizeChangeParams &params)
Definition: Kernel.h:197
static ErrorCode initialize(Kernel *kernel, const string &entryPoint)
Definition: Kernel.h:185
static void onLoop(Kernel *kernel)
Definition: Kernel.h:205
static void shutdown(Kernel *kernel)
Definition: Kernel.h:189
static void onWindowSizeChange(Kernel *kernel, const SizeChangeParams &params)
Definition: Kernel.h:193
static void warmup(Kernel *kernel)
Definition: Kernel.h:201
Configuration & config() noexcept
ErrorCode init(PlatformContext &context, RenderAPI renderingAPI, vec2< I16 > initialPosition, vec2< U16 > initialSize, WindowMode windowMode, I32 targetDisplayIndex)
void hideAll() noexcept
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
AppStepResult
Definition: Application.h:61
int32_t I32
uint8_t U8
bool deleteAllFiles(const ResourcePath &filePath, const char *extension, const char *extensionToSkip)
constexpr U8 to_U8(const T value)
struct Divide::Configuration::Debug::Renderer renderer
struct Divide::Configuration::Runtime runtime
struct Divide::Configuration::Debug debug
static void ToggleFlag(const Flags flag, const bool state)
Definition: Console.h:135
static NO_INLINE void errorfn(const char *format, T &&... args)
static NO_INLINE void warnfn(const char *format, T &&... args)
static NO_INLINE void printfn(const char *format, T &&... args)
vector< OutputDisplayProperties > OutputDisplayPropertiesContainer
Definition: Application.h:178
static void SetActiveDisplayCount(const U8 displayCount)
static U8 ActiveDisplayCount() noexcept
static void RegisterDisplayMode(const U8 displayIndex, const OutputDisplayProperties &mode)
static U8 MaxMSAASamples() noexcept
static U8 s_activeDisplayCount
Definition: Application.h:192
static const OutputDisplayPropertiesContainer & GetDisplayModes(const size_t displayIndex) noexcept
static std::array< OutputDisplayPropertiesContainer, g_maxDisplayOutputs > s_supportedDisplayModes
Definition: Application.h:194
static constexpr U8 g_maxDisplayOutputs
Definition: Application.h:176
static U8 s_maxMSAASAmples
Definition: Application.h:193
static void Reset() noexcept