SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_main.h
1#ifndef SDL3PP_MAIN_H_
2#define SDL3PP_MAIN_H_
3
4#ifdef SDL3PP_MAIN_HANDLED
5#define SDL_MAIN_HANDLED
6#endif // SDL3PP_MAIN_HANDLED
7
8#ifdef SDL3PP_MAIN_USE_CALLBACKS
9#define SDL_MAIN_USE_CALLBACKS
10#endif // SDL3PP_MAIN_USE_CALLBACKS
11
12#ifdef SDL3PP_MAIN_USE_CLASS_CALLBACKS
13#define SDL_MAIN_USE_CALLBACKS
14#endif // SDL3PP_MAIN_USE_CLASS_CALLBACKS
15
16#include <SDL3/SDL_main.h>
17#include "SDL3pp.h"
18
19namespace SDL {
20
53
54#ifdef SDL3PP_DOC
55
70#define SDL3PP_MAIN_HANDLED SDL_MAIN_HANDLED
71
92#define SDL3PP_MAIN_USE_CALLBACKS SDL_MAIN_USE_CALLBACKS
93
109#define SDL3PP_MAIN_USE_CLASS_CALLBACKS 1
110
111#endif // SDL3PP_DOC
112
123using main_func = int(SDLCALL*)(int argc, char* argv[]);
124
139inline void SetMainReady() { SDL_SetMainReady(); }
140
171inline int RunApp(int argc,
172 char* argv[],
173 main_func mainFunction,
174 void* reserved)
175{
176 return SDL_RunApp(argc, argv, mainFunction, reserved);
177}
178
204inline int EnterAppMainCallbacks(int argc,
205 char* argv[],
206 AppInit_func appinit,
207 AppIterate_func appiter,
208 AppEvent_func appevent,
209 AppQuit_func appquit)
210{
211 return SDL_EnterAppMainCallbacks(
212 argc, argv, appinit, appiter, appevent, appquit);
213}
214
215#if defined(SDL_PLATFORM_WINDOWS)
216
243inline bool RegisterApp(StringParam name, Uint32 style, void* hInst)
244{
245 return SDL_RegisterApp(name, style, hInst);
246}
247
265inline void UnregisterApp() { SDL_UnregisterApp(); }
266
267#endif /* defined(SDL_PLATFORM_WINDOWS) */
268
293inline void GDKSuspendComplete() { SDL_GDKSuspendComplete(); }
294
306{
307 virtual ~AppInterface() = default;
308
317
329
340 virtual SDL::AppResult Event(const SDL::Event& ev)
341 {
342 return ev.type == SDL_EVENT_QUIT ? SDL::APP_SUCCESS : SDL::APP_CONTINUE;
343 }
344
352 virtual void quit(SDL::AppResult result) {}
353};
354
355} // namespace SDL
356
403extern "C" SDLMAIN_DECLSPEC SDL::AppInterface* SDLCALL
404SDL_AppCreate(int argc, char* argv[]);
405
407namespace SDL {
408
409#ifdef SDL3PP_MAIN_USE_CLASS_CALLBACKS
410
412extern "C" SDLMAIN_DECLSPEC SDL::AppResult SDL_AppInit(void** appstate,
413 int argc,
414 char* argv[])
415{
416 try {
417 auto app = SDL_AppCreate(argc, argv);
418 if (!app) return APP_SUCCESS;
419 *appstate = app;
420 app->Init();
421 return APP_CONTINUE;
422 } catch (std::exception& e) {
424 e.what());
425 } catch (...) {
426 LOG_CATEGORY_APPLICATION.LogUnformatted(
427 SDL3PP_APPCLASS_LOG_PRIORITY, "Critical error during app initialization");
428 }
429 return APP_FAILURE;
430}
431
433extern "C" SDLMAIN_DECLSPEC SDL::AppResult SDL_AppIterate(void* appstate)
434{
435 try {
436 return static_cast<AppInterface*>(appstate)->Iterate();
437 } catch (std::exception& e) {
439 e.what());
440 } catch (...) {
441 LOG_CATEGORY_APPLICATION.LogUnformatted(
442 SDL3PP_APPCLASS_LOG_PRIORITY, "Critical error during app iteration");
443 }
444 return APP_FAILURE;
445}
446
448extern "C" SDLMAIN_DECLSPEC SDL::AppResult SDL_AppEvent(void* appstate,
449 SDL::Event* event)
450{
451 try {
452 return static_cast<AppInterface*>(appstate)->Event(*event);
453 } catch (std::exception& e) {
455 e.what());
456 } catch (...) {
457 LOG_CATEGORY_APPLICATION.LogUnformatted(
458 SDL3PP_APPCLASS_LOG_PRIORITY, "Critical error during app event handling");
459 }
460 return APP_FAILURE;
461}
462
464extern "C" SDLMAIN_DECLSPEC void SDL_AppQuit(void* appstate,
465 SDL::AppResult result)
466{
467 try {
468 auto app = static_cast<AppInterface*>(appstate);
469 if (!app) return;
470 app->quit(result);
471 delete app;
472 } catch (std::exception& e) {
474 e.what());
475 } catch (...) {
477 "Critical error during app exit");
478 }
479}
480
485#define SDL3PP_DEFINE_CALLBACKS(CLASS) \
486 extern "C" SDLMAIN_DECLSPEC SDL::AppInterface* SDLCALL SDL_AppCreate( \
487 int, char*[]) \
488 { \
489 return new CLASS(); \
490 }
491
500#define SDL3PP_DEFINE_CLASS_CALLBACKS( \
501 CLASS, INIT_FLAGS, APPNAME, APPVERSION, APPIDENTIFIER) \
502 extern "C" SDLMAIN_DECLSPEC SDL::AppInterface* SDLCALL SDL_AppCreate( \
503 int, char*[]) \
504 { \
505 SDL::SetAppMetadata(APPNAME, APPVERSION, APPIDENTIFIER); \
506 SDL::Init(INIT_FLAGS); \
507 return new CLASS(); \
508 }
509
510#else // SDL3PP_MAIN_USE_CLASS_CALLBACKS
511
516#define SDL3PP_DEFINE_CALLBACKS(CLASS) \
517 static_assert(SDL::HasIterateFunction<CLASS>, "Main class not compatible"); \
518 inline SDL::AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) \
519 { \
520 return SDL::InitClass(reinterpret_cast<CLASS**>(appstate), \
521 SDL::AppArgs{argv, size_t(argc)}); \
522 } \
523 inline SDL::AppResult SDL_AppIterate(void* appstate) \
524 { \
525 return SDL::IterateClass(static_cast<CLASS*>(appstate)); \
526 } \
527 inline SDL::AppResult SDL_AppEvent(void* appstate, SDL::Event* event) \
528 { \
529 return SDL::EventClass(static_cast<CLASS*>(appstate), *event); \
530 } \
531 inline void SDL_AppQuit(void* appstate, SDL::AppResult result) \
532 { \
533 SDL::QuitClass(static_cast<CLASS*>(appstate), result); \
534 }
535
536#endif // SDL3PP_MAIN_USE_CLASS_CALLBACKS
537
539
540} // namespace SDL
541
542#endif /* SDL3PP_MAIN_H_ */
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:43
SDL_Event Event
The structure for all events in SDL.
Definition SDL3pp_events.h:844
AppResult(SDLCALL *)(void **appstate, int argc, char *argv[]) AppInit_func
Function pointer typedef for SDL_AppInit.
Definition SDL3pp_init.h:150
SDL_AppResult AppResult
Return values for optional main callbacks.
Definition SDL3pp_init.h:115
void(SDLCALL *)(void *appstate, AppResult result) AppQuit_func
Function pointer typedef for SDL_AppQuit.
Definition SDL3pp_init.h:197
constexpr AppResult APP_FAILURE
Value that requests termination with error from the main callbacks.
Definition SDL3pp_init.h:124
constexpr AppResult APP_CONTINUE
Value that requests that the app continue from the main callbacks.
Definition SDL3pp_init.h:118
AppResult(SDLCALL *)(void *appstate, Event *event) AppEvent_func
Function pointer typedef for SDL_AppEvent.
Definition SDL3pp_init.h:183
AppResult(SDLCALL *)(void *appstate) AppIterate_func
Function pointer typedef for SDL_AppIterate.
Definition SDL3pp_init.h:167
#define SDL3PP_APPCLASS_LOG_PRIORITY
The default log priority for app class.
Definition SDL3pp_init.h:608
constexpr AppResult APP_SUCCESS
Value that requests termination with success from the main callbacks.
Definition SDL3pp_init.h:121
constexpr LogCategory LOG_CATEGORY_APPLICATION
APPLICATION.
Definition SDL3pp_log.h:438
virtual SDL::AppResult Iterate()
Called to iterate the app.
Definition SDL3pp_main.h:328
int RunApp(int argc, char *argv[], main_func mainFunction, void *reserved)
Initializes and launches an SDL application, by doing platform-specific initialization before calling...
Definition SDL3pp_main.h:171
void SetMainReady()
Circumvent failure of Init() when not using SDL_main() as an entry point.
Definition SDL3pp_main.h:139
int EnterAppMainCallbacks(int argc, char *argv[], AppInit_func appinit, AppIterate_func appiter, AppEvent_func appevent, AppQuit_func appquit)
An entry point for SDL's use in SDL3PP_MAIN_USE_CALLBACKS.
Definition SDL3pp_main.h:204
void UnregisterApp()
Deregister the win32 window class from an RegisterApp call.
Definition SDL3pp_main.h:265
virtual SDL::AppResult Init()
Called to initialize the app.
Definition SDL3pp_main.h:316
virtual SDL::AppResult Event(const SDL::Event &ev)
Called to handle an event.
Definition SDL3pp_main.h:340
int(SDLCALL *)(int argc, char *argv[]) main_func
The prototype for the application's main() function.
Definition SDL3pp_main.h:123
virtual void quit(SDL::AppResult result)
Called to quit the app.
Definition SDL3pp_main.h:352
void GDKSuspendComplete()
Callback from the application to let the suspend continue.
Definition SDL3pp_main.h:293
SDLMAIN_DECLSPEC SDL::AppInterface *SDLCALL SDL_AppCreate(int argc, char *argv[])
Prototype for the application's main class creation function.
bool RegisterApp(StringParam name, Uint32 style, void *hInst)
Register a win32 window class for SDL's use.
Definition SDL3pp_main.h:243
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition SDL3pp_stdinc.h:296
Main include header for the SDL3pp library.
Base class for SDL3PP_MAIN_USE_CLASS_CALLBACKS main callback classes.
Definition SDL3pp_main.h:306