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
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
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
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
253inline void Init(InitFlags flags) { CheckError(SDL_Init(flags)); }
254
269inline void InitSubSystem(InitFlags flags)
270{
271 CheckError(SDL_InitSubSystem(flags));
272}
273
287inline void QuitSubSystem(InitFlags flags) { SDL_QuitSubSystem(flags); }
288
301inline InitFlags WasInit(InitFlags flags) { return SDL_WasInit(flags); }
302
319inline void Quit() { SDL_Quit(); }
320
338inline bool IsMainThread() { return SDL_IsMainThread(); }
339
354using MainThreadCallback = void(SDLCALL*)(void* userdata);
355
368using MainThreadCB = std::function<void()>;
369
371
396 void* userdata,
397 bool wait_complete)
398{
399 CheckError(SDL_RunOnMainThread(callback, userdata, wait_complete));
400}
401
427inline void RunOnMainThread(MainThreadCB callback, bool wait_complete)
428{
429 using Wrapper = CallbackWrapper<MainThreadCB>;
430 void* wrapped = Wrapper::Wrap(std::move(callback));
431 RunOnMainThread(&Wrapper::CallOnce, wrapped, wait_complete);
432}
433
467inline void SetAppMetadata(StringParam appname,
468 StringParam appversion,
469 StringParam appidentifier)
470{
471 CheckError(SDL_SetAppMetadata(appname, appversion, appidentifier));
472}
473
534{
535 CheckError(SDL_SetAppMetadataProperty(name, value));
536}
537
538namespace prop::appMetaData {
539
540constexpr auto NAME_STRING = SDL_PROP_APP_METADATA_NAME_STRING;
541
542constexpr auto VERSION_STRING = SDL_PROP_APP_METADATA_VERSION_STRING;
543
544constexpr auto IDENTIFIER_STRING = SDL_PROP_APP_METADATA_IDENTIFIER_STRING;
545
546constexpr auto CREATOR_STRING = SDL_PROP_APP_METADATA_CREATOR_STRING;
547
548constexpr auto COPYRIGHT_STRING = SDL_PROP_APP_METADATA_COPYRIGHT_STRING;
549
550constexpr auto URL_STRING = SDL_PROP_APP_METADATA_URL_STRING;
551
552constexpr auto TYPE_STRING = SDL_PROP_APP_METADATA_TYPE_STRING;
553
554} // namespace prop::appMetaData
555
577inline const char* GetAppMetadataProperty(StringParam name)
578{
579 return SDL_GetAppMetadataProperty(name);
580}
581
582#ifndef SDL3PP_APPCLASS_LOG_PRIORITY
586#define SDL3PP_APPCLASS_LOG_PRIORITY LOG_PRIORITY_CRITICAL
587#endif // SDL3PP_APPCLASS_LOG_PRIORITY
588
592using AppArgs = std::span<char const* const>;
593
606template<class T>
607inline AppResult DefaultCreateClass(T** state, AppArgs args)
608{
609 static_assert(std::is_default_constructible_v<T>);
610 *state = new T{};
611 return APP_CONTINUE;
612}
613
614template<class T>
615 requires std::convertible_to<AppArgs, T>
616inline AppResult DefaultCreateClass(T** state, AppArgs args)
617{
618 *state = new T{args};
619 return APP_CONTINUE;
620}
622
624template<class T>
625concept HasInitFunction = requires(T** state) {
626 { T::Init(state, AppArgs{}) } -> std::convertible_to<AppResult>;
627};
628
642template<class T>
643inline AppResult InitClass(T** state, AppArgs args)
644{
645 try {
646 return DefaultCreateClass(state, args);
647 } catch (std::exception& e) {
649 e.what());
650 } catch (...) {
651 }
652 return APP_FAILURE;
653}
654
655template<HasInitFunction T>
656inline AppResult InitClass(T** state, AppArgs args)
657{
658 *state = nullptr;
659 try {
660 AppResult result = T::Init(state, args);
661 if (*state == nullptr && result != APP_FAILURE) return APP_SUCCESS;
662 return result;
663 } catch (std::exception& e) {
665 e.what());
666 } catch (...) {
667 }
668 return APP_FAILURE;
669}
671
673template<class T>
674concept HasIterateFunction = requires(T* state) { state->Iterate(); };
675
683template<HasIterateFunction T>
684inline AppResult IterateClass(T* state)
685{
686 try {
687 return state->Iterate();
688 } catch (std::exception& e) {
690 e.what());
691 } catch (...) {
692 }
693 return APP_FAILURE;
694}
695
697template<class T>
698concept HasEventFunction =
699 requires(T* state, const SDL_Event& event) { state->Event(event); };
700
709template<class T>
710inline AppResult DefaultEventClass(T* state, const SDL_Event& event)
711{
712 if (event.type == SDL_EVENT_QUIT) return APP_SUCCESS;
713 return APP_CONTINUE;
714}
715
725template<class T>
726inline AppResult EventClass(T* state, const SDL_Event& event)
727{
728 try {
729 return DefaultEventClass(state, event);
730 } catch (std::exception& e) {
732 e.what());
733 } catch (...) {
734 }
735 return APP_FAILURE;
736}
737
738template<HasEventFunction T>
739inline AppResult EventClass(T* state, const SDL_Event& event)
740{
741 try {
742 return state->Event(event);
743 } catch (std::exception& e) {
745 e.what());
746 } catch (...) {
747 }
748 return APP_FAILURE;
749}
750
752
759template<class T>
760inline void DefaultClassDestroy(T* state)
761{
762 delete state;
763}
764
766template<class T>
767concept HasQuitFunction =
768 requires(T* state, AppResult result) { T::Quit(state, result); };
769
782template<class T>
783inline void QuitClass(T* state, AppResult result)
784{
785 DefaultClassDestroy(state);
786}
787
788template<HasQuitFunction T>
789inline void QuitClass(T* state, AppResult result)
790{
791 T::Quit(state, result);
792}
794
796
797} // namespace SDL
798
799#endif /* SDL3PP_INIT_H_ */
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
SDL_Event Event
The structure for all events in SDL.
Definition: SDL3pp_events.h:798
void Init(InitFlags flags)
Initialize the SDL library.
Definition: SDL3pp_init.h:253
void SetAppMetadata(StringParam appname, StringParam appversion, StringParam appidentifier)
Specify basic metadata about your app.
Definition: SDL3pp_init.h:467
void QuitClass(T *state, AppResult result)
Destroy state with given result.
Definition: SDL3pp_init.h:783
AppResult(SDLCALL *)(void **appstate, int argc, char *argv[]) AppInit_func
Function pointer typedef for SDL_AppInit.
Definition: SDL3pp_init.h:152
AppResult IterateClass(T *state)
Iterate the state.
Definition: SDL3pp_init.h:684
InitFlags WasInit(InitFlags flags)
Get a mask of the specified subsystems which are currently initialized.
Definition: SDL3pp_init.h:301
std::function< void()> MainThreadCB
Callback run on the main thread.
Definition: SDL3pp_init.h:368
void InitSubSystem(InitFlags flags)
Compatibility function to initialize the SDL library.
Definition: SDL3pp_init.h:269
AppResult DefaultCreateClass(T **state, AppArgs args)
Allocate and initialize state with new.
Definition: SDL3pp_init.h:607
constexpr AppResult APP_FAILURE
Value that requests termination with error from the main callbacks.
Definition: SDL3pp_init.h:124
std::span< char const *const > AppArgs
Represents application parameters.
Definition: SDL3pp_init.h:592
constexpr AppResult APP_CONTINUE
Value that requests that the app continue from the main callbacks.
Definition: SDL3pp_init.h:118
const char * GetAppMetadataProperty(StringParam name)
Get metadata about your app.
Definition: SDL3pp_init.h:577
SDL_AppResult AppResult
Return values for optional main callbacks.
Definition: SDL3pp_init.h:115
void(SDLCALL *)(void *userdata) MainThreadCallback
Callback run on the main thread.
Definition: SDL3pp_init.h:354
void RunOnMainThread(MainThreadCallback callback, void *userdata, bool wait_complete)
Call a function on the main thread during event processing.
Definition: SDL3pp_init.h:395
AppResult DefaultEventClass(T *state, const SDL_Event &event)
Default handle by finishing if QUIT is requested.
Definition: SDL3pp_init.h:710
void DefaultClassDestroy(T *state)
Destroy state with delete;.
Definition: SDL3pp_init.h:760
AppResult(SDLCALL *)(void *appstate, Event *event) AppEvent_func
Function pointer typedef for SDL_AppEvent.
Definition: SDL3pp_init.h:183
void SetAppMetadataProperty(StringParam name, StringParam value)
Specify metadata about your app through a set of properties.
Definition: SDL3pp_init.h:533
#define SDL3PP_APPCLASS_LOG_PRIORITY
The default log priority for app class.
Definition: SDL3pp_init.h:586
AppResult EventClass(T *state, const SDL_Event &event)
Iterate the state.
Definition: SDL3pp_init.h:726
void Quit()
Clean up all initialized subsystems.
Definition: SDL3pp_init.h:319
void QuitSubSystem(InitFlags flags)
Shut down specific SDL subsystems.
Definition: SDL3pp_init.h:287
AppResult InitClass(T **state, AppArgs args)
Init state with arguments.
Definition: SDL3pp_init.h:643
void(SDLCALL *)(void *appstate, AppResult result) AppQuit_func
Function pointer typedef for SDL_AppQuit.
Definition: SDL3pp_init.h:197
bool IsMainThread()
Return whether this is the main thread.
Definition: SDL3pp_init.h:338
constexpr AppResult APP_SUCCESS
Value that requests termination with success from the main callbacks.
Definition: SDL3pp_init.h:121
AppResult(SDLCALL *)(void *appstate) AppIterate_func
Function pointer typedef for SDL_AppIterate.
Definition: SDL3pp_init.h:167
void LogUnformatted(LogPriority priority, StringParam message) const
Log an unformatted message with the specified priority.
Definition: SDL3pp_log.h:208
constexpr LogCategory LOG_CATEGORY_APPLICATION
APPLICATION.
Definition: SDL3pp_log.h:433
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:341
constexpr InitFlags INIT_SENSOR
INIT_SENSOR implies INIT_EVENTS
Definition: SDL3pp_init.h:83
Uint32 InitFlags
Initialization flags for Init and/or InitSubSystem.
Definition: SDL3pp_init.h:63
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
constexpr InitFlags INIT_HAPTIC
HAPTIC.
Definition: SDL3pp_init.h:76
constexpr InitFlags INIT_CAMERA
INIT_CAMERA implies INIT_EVENTS
Definition: SDL3pp_init.h:86
Main include header for the SDL3pp library.
Definition: SDL3pp_callbackWrapper.h:20