SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_init.h
1#ifndef SDL3PP_INIT_H_
2#define SDL3PP_INIT_H_
3
4#include <atomic>
5#include <SDL3/SDL_init.h>
6#include "SDL3pp_callbackWrapper.h"
7#include "SDL3pp_error.h"
8#include "SDL3pp_events.h"
9#include "SDL3pp_log.h"
10#include "SDL3pp_strings.h"
11
12namespace SDL {
13
42
48
64
66 SDL_INIT_AUDIO;
67
71constexpr InitFlags INIT_VIDEO = SDL_INIT_VIDEO;
72
74 SDL_INIT_JOYSTICK;
75
76constexpr InitFlags INIT_HAPTIC = SDL_INIT_HAPTIC;
77
79 SDL_INIT_GAMEPAD;
80
81constexpr InitFlags INIT_EVENTS = SDL_INIT_EVENTS;
82
84 SDL_INIT_SENSOR;
85
87 SDL_INIT_CAMERA;
88
90
96
115using AppResult = SDL_AppResult;
116
118constexpr AppResult APP_CONTINUE = SDL_APP_CONTINUE;
119
121constexpr AppResult APP_SUCCESS = SDL_APP_SUCCESS;
122
124constexpr AppResult APP_FAILURE = SDL_APP_FAILURE;
125
127
133
150using AppInit_func = AppResult(SDLCALL*)(void** appstate,
151 int argc,
152 char* argv[]);
153
167using AppIterate_func = AppResult(SDLCALL*)(void* appstate);
168
183using AppEvent_func = AppResult(SDLCALL*)(void* appstate, Event* event);
184
197using AppQuit_func = void(SDLCALL*)(void* appstate, AppResult result);
198
200
255inline void Init(InitFlags flags) { CheckError(SDL_Init(flags)); }
256
273inline void InitSubSystem(InitFlags flags)
274{
275 CheckError(SDL_InitSubSystem(flags));
276}
277
293inline void QuitSubSystem(InitFlags flags) { SDL_QuitSubSystem(flags); }
294
309inline InitFlags WasInit(InitFlags flags) { return SDL_WasInit(flags); }
310
329inline void Quit() { SDL_Quit(); }
330
348inline bool IsMainThread() { return SDL_IsMainThread(); }
349
354
364using MainThreadCallback = void(SDLCALL*)(void* userdata);
365
378using MainThreadCB = std::function<void()>;
379
381
406 void* userdata,
407 bool wait_complete)
408{
409 CheckError(SDL_RunOnMainThread(callback, userdata, wait_complete));
410}
411
437inline void RunOnMainThread(MainThreadCB callback, bool wait_complete)
438{
439 using Wrapper = CallbackWrapper<MainThreadCB>;
440 void* wrapped = Wrapper::Wrap(std::move(callback));
441 RunOnMainThread(&Wrapper::CallOnce, wrapped, wait_complete);
442}
443
477inline void SetAppMetadata(StringParam appname,
478 StringParam appversion,
479 StringParam appidentifier)
480{
481 CheckError(SDL_SetAppMetadata(appname, appversion, appidentifier));
482}
483
543{
544 CheckError(SDL_SetAppMetadataProperty(name, value));
545}
546
554
555constexpr auto NAME_STRING = SDL_PROP_APP_METADATA_NAME_STRING;
556
557constexpr auto VERSION_STRING = SDL_PROP_APP_METADATA_VERSION_STRING;
558
559constexpr auto IDENTIFIER_STRING = SDL_PROP_APP_METADATA_IDENTIFIER_STRING;
560
561constexpr auto CREATOR_STRING = SDL_PROP_APP_METADATA_CREATOR_STRING;
562
563constexpr auto COPYRIGHT_STRING = SDL_PROP_APP_METADATA_COPYRIGHT_STRING;
564
565constexpr auto URL_STRING = SDL_PROP_APP_METADATA_URL_STRING;
566
567constexpr auto TYPE_STRING = SDL_PROP_APP_METADATA_TYPE_STRING;
568
569} // namespace prop::appMetaData
570
592inline const char* GetAppMetadataProperty(StringParam name)
593{
594 return SDL_GetAppMetadataProperty(name);
595}
596
597#ifndef SDL3PP_APPCLASS_LOG_PRIORITY
601#define SDL3PP_APPCLASS_LOG_PRIORITY LOG_PRIORITY_CRITICAL
602#endif // SDL3PP_APPCLASS_LOG_PRIORITY
603
607using AppArgs = std::span<char const* const>;
608
621template<class T>
622 requires std::convertible_to<AppArgs, T>
623inline AppResult DefaultCreateClass(T** state, AppArgs args)
624{
625 *state = new T{args};
626 return APP_CONTINUE;
627}
628
629template<class T>
630inline AppResult DefaultCreateClass(T** state, AppArgs)
631{
632 static_assert(std::is_default_constructible_v<T>);
633 *state = new T{};
634 return APP_CONTINUE;
635}
636
638
640template<class T>
641concept HasInitFunction = requires(T** state) {
642 { T::Init(state, AppArgs{}) } -> std::convertible_to<AppResult>;
643};
644
658template<class T>
659inline AppResult InitClass(T** state, AppArgs args)
660{
661 try {
662 return DefaultCreateClass(state, args);
663 } catch (std::exception& e) {
665 e.what());
666 } catch (...) {
667 }
668 return APP_FAILURE;
669}
670
671template<HasInitFunction T>
672inline AppResult InitClass(T** state, AppArgs args)
673{
674 *state = nullptr;
675 try {
676 AppResult result = T::Init(state, args);
677 if (*state == nullptr && result != APP_FAILURE) return APP_SUCCESS;
678 return result;
679 } catch (std::exception& e) {
681 e.what());
682 } catch (...) {
683 }
684 return APP_FAILURE;
685}
687
689template<class T>
690concept HasIterateFunction = requires(T* state) { state->Iterate(); };
691
699template<HasIterateFunction T>
700inline AppResult IterateClass(T* state)
701{
702 try {
703 return state->Iterate();
704 } catch (std::exception& e) {
706 e.what());
707 } catch (...) {
708 }
709 return APP_FAILURE;
710}
711
713template<class T>
715 requires(T* state, const SDL_Event& event) { state->Event(event); };
716
724template<class T>
725inline AppResult DefaultEventClass(T*, const SDL_Event& event)
726{
727 if (event.type == SDL_EVENT_QUIT) return APP_SUCCESS;
728 return APP_CONTINUE;
729}
730
740template<class T>
741inline AppResult EventClass(T* state, const SDL_Event& event)
742{
743 try {
744 return DefaultEventClass(state, event);
745 } catch (std::exception& e) {
747 e.what());
748 } catch (...) {
749 }
750 return APP_FAILURE;
751}
752
753template<HasEventFunction T>
754inline AppResult EventClass(T* state, const SDL_Event& event)
755{
756 try {
757 return state->Event(event);
758 } catch (std::exception& e) {
760 e.what());
761 } catch (...) {
762 }
763 return APP_FAILURE;
764}
765
767
774template<class T>
775inline void DefaultClassDestroy(T* state)
776{
777 delete state;
778}
779
781template<class T>
783 requires(T* state, AppResult result) { T::Quit(state, result); };
784
797template<class T>
798inline void QuitClass(T* state, AppResult)
799{
800 DefaultClassDestroy(state);
801}
802
803template<HasQuitFunction T>
804inline void QuitClass(T* state, AppResult result)
805{
806 T::Quit(state, result);
807}
809
811
812} // namespace SDL
813
814#endif /* SDL3PP_INIT_H_ */
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:43
Definition SDL3pp_init.h:714
Definition SDL3pp_init.h:641
Definition SDL3pp_init.h:690
Definition SDL3pp_init.h:782
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
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
void Init(InitFlags flags)
Initialize the SDL library.
Definition SDL3pp_init.h:255
void SetAppMetadata(StringParam appname, StringParam appversion, StringParam appidentifier)
Specify basic metadata about your app.
Definition SDL3pp_init.h:477
AppResult DefaultEventClass(T *, const SDL_Event &event)
Default handle by finishing if QUIT is requested.
Definition SDL3pp_init.h:725
AppResult IterateClass(T *state)
Iterate the state.
Definition SDL3pp_init.h:700
InitFlags WasInit(InitFlags flags)
Get a mask of the specified subsystems which are currently initialized.
Definition SDL3pp_init.h:309
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
void InitSubSystem(InitFlags flags)
Compatibility function to initialize the SDL library.
Definition SDL3pp_init.h:273
AppResult DefaultCreateClass(T **state, AppArgs args)
Allocate and initialize state with new.
Definition SDL3pp_init.h:623
std::span< char const *const > AppArgs
Represents application parameters.
Definition SDL3pp_init.h:607
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
std::function< void()> MainThreadCB
Callback run on the main thread.
Definition SDL3pp_init.h:378
const char * GetAppMetadataProperty(StringParam name)
Get metadata about your app.
Definition SDL3pp_init.h:592
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
void RunOnMainThread(MainThreadCallback callback, void *userdata, bool wait_complete)
Call a function on the main thread during event processing.
Definition SDL3pp_init.h:405
void DefaultClassDestroy(T *state)
Destroy state with delete;.
Definition SDL3pp_init.h:775
void SetAppMetadataProperty(StringParam name, StringParam value)
Specify metadata about your app through a set of properties.
Definition SDL3pp_init.h:542
#define SDL3PP_APPCLASS_LOG_PRIORITY
The default log priority for app class.
Definition SDL3pp_init.h:601
AppResult EventClass(T *state, const SDL_Event &event)
Iterate the state.
Definition SDL3pp_init.h:741
void Quit()
Clean up all initialized subsystems.
Definition SDL3pp_init.h:329
void QuitSubSystem(InitFlags flags)
Shut down specific SDL subsystems.
Definition SDL3pp_init.h:293
void(SDLCALL *)(void *userdata) MainThreadCallback
Callback run on the main thread.
Definition SDL3pp_init.h:364
AppResult InitClass(T **state, AppArgs args)
Init state with arguments.
Definition SDL3pp_init.h:659
bool IsMainThread()
Return whether this is the main thread.
Definition SDL3pp_init.h:348
void QuitClass(T *state, AppResult)
Destroy state with given result.
Definition SDL3pp_init.h:798
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:432
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition SDL3pp_stdinc.h:290
constexpr InitFlags INIT_SENSOR
INIT_SENSOR implies INIT_EVENTS
Definition SDL3pp_init.h:83
constexpr InitFlags INIT_JOYSTICK
INIT_JOYSTICK implies INIT_EVENTS
Definition SDL3pp_init.h:73
constexpr InitFlags INIT_VIDEO
INIT_VIDEO implies INIT_EVENTS, should be initialized on the main thread
Definition SDL3pp_init.h:71
constexpr InitFlags INIT_AUDIO
INIT_AUDIO implies INIT_EVENTS
Definition SDL3pp_init.h:65
constexpr InitFlags INIT_EVENTS
EVENTS.
Definition SDL3pp_init.h:81
constexpr InitFlags INIT_GAMEPAD
INIT_GAMEPAD implies INIT_JOYSTICK
Definition SDL3pp_init.h:78
Uint32 InitFlags
Initialization flags for Init and/or InitSubSystem.
Definition SDL3pp_init.h:63
constexpr InitFlags INIT_HAPTIC
HAPTIC.
Definition SDL3pp_init.h:76
constexpr InitFlags INIT_CAMERA
INIT_CAMERA implies INIT_EVENTS
Definition SDL3pp_init.h:86
Metadata property names for application.
Definition SDL3pp_init.h:553
Main include header for the SDL3pp library.
Definition SDL3pp_callbackWrapper.h:20