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_log.h"
9#include "SDL3pp_strings.h"
10
11namespace SDL {
12
63
65 SDL_INIT_AUDIO;
66
70constexpr InitFlags INIT_VIDEO = SDL_INIT_VIDEO;
71
73 SDL_INIT_JOYSTICK;
74
75constexpr InitFlags INIT_HAPTIC = SDL_INIT_HAPTIC;
76
78 SDL_INIT_GAMEPAD;
79
80constexpr InitFlags INIT_EVENTS = SDL_INIT_EVENTS;
81
83 SDL_INIT_SENSOR;
84
86 SDL_INIT_CAMERA;
87
89
117using AppResult = SDL_AppResult;
118
120constexpr AppResult APP_CONTINUE = SDL_APP_CONTINUE;
121
123constexpr AppResult APP_SUCCESS = SDL_APP_SUCCESS;
124
126constexpr AppResult APP_FAILURE = SDL_APP_FAILURE;
127
129
153using AppInit_func = SDL_AppInit_func;
154
168using AppIterate_func = SDL_AppIterate_func;
169
184using AppEvent_func = SDL_AppEvent_func;
185
198using AppQuit_func = SDL_AppQuit_func;
199
201
256inline void Init(InitFlags flags) { CheckError(SDL_Init(flags)); }
257
272inline void InitSubSystem(InitFlags flags)
273{
274 CheckError(SDL_InitSubSystem(flags));
275}
276
290inline void QuitSubSystem(InitFlags flags) { SDL_QuitSubSystem(flags); }
291
304inline InitFlags WasInit(InitFlags flags) { return SDL_WasInit(flags); }
305
322inline void Quit() { SDL_Quit(); }
323
342inline bool IsMainThread() { return SDL_IsMainThread(); }
343
358using MainThreadCallback = SDL_MainThreadCallback;
359
371using MainThreadCB = std::function<void()>;
372
374
399 void* userdata,
400 bool wait_complete)
401{
402 CheckError(SDL_RunOnMainThread(callback, userdata, wait_complete));
403}
404
430inline void RunOnMainThread(MainThreadCB callback, bool wait_complete)
431{
432 using Wrapper = CallbackWrapper<MainThreadCB>;
433 void* wrapped = Wrapper::Wrap(std::move(callback));
434 RunOnMainThread(&Wrapper::CallOnce, wrapped, wait_complete);
435}
436
472inline void SetAppMetadata(StringParam appname,
473 StringParam appversion,
474 StringParam appidentifier)
475{
476 CheckError(SDL_SetAppMetadata(appname, appversion, appidentifier));
477}
478
540{
541 CheckError(SDL_SetAppMetadataProperty(name, value));
542}
543
544namespace prop::appMetaData {
545
546constexpr auto NAME_STRING = SDL_PROP_APP_METADATA_NAME_STRING;
547
548constexpr auto VERSION_STRING = SDL_PROP_APP_METADATA_VERSION_STRING;
549
550constexpr auto IDENTIFIER_STRING = SDL_PROP_APP_METADATA_IDENTIFIER_STRING;
551
552constexpr auto CREATOR_STRING = SDL_PROP_APP_METADATA_CREATOR_STRING;
553
554constexpr auto COPYRIGHT_STRING = SDL_PROP_APP_METADATA_COPYRIGHT_STRING;
555
556constexpr auto URL_STRING = SDL_PROP_APP_METADATA_URL_STRING;
557
558constexpr auto TYPE_STRING = SDL_PROP_APP_METADATA_TYPE_STRING;
559
560} // namespace prop::appMetaData
561
583inline const char* GetAppMetadataProperty(StringParam name)
584{
585 return SDL_GetAppMetadataProperty(name);
586}
587
588#ifndef SDL3PP_APPCLASS_LOG_PRIORITY
592#define SDL3PP_APPCLASS_LOG_PRIORITY LOG_PRIORITY_CRITICAL
593#endif // SDL3PP_APPCLASS_LOG_PRIORITY
594
598using AppArgs = std::span<char const* const>;
599
612template<class T>
613inline AppResult DefaultCreateClass(T** state, AppArgs args)
614{
615 static_assert(std::is_default_constructible_v<T>);
616 *state = new T{};
617 return APP_CONTINUE;
618}
619
620template<class T>
621 requires std::convertible_to<AppArgs, T>
622inline AppResult DefaultCreateClass(T** state, AppArgs args)
623{
624 *state = new T{args};
625 return APP_CONTINUE;
626}
628
630template<class T>
631concept HasInitFunction = requires(T** state) {
632 { T::Init(state, AppArgs{}) } -> std::convertible_to<AppResult>;
633};
634
648template<class T>
649inline AppResult InitClass(T** state, AppArgs args)
650{
651 try {
652 return DefaultCreateClass(state, args);
653 } catch (std::exception& e) {
655 e.what());
656 } catch (...) {
657 }
658 return APP_FAILURE;
659}
660
661template<HasInitFunction T>
662inline AppResult InitClass(T** state, AppArgs args)
663{
664 *state = nullptr;
665 try {
666 AppResult result = T::Init(state, args);
667 if (*state == nullptr && result != APP_FAILURE) return APP_SUCCESS;
668 return result;
669 } catch (std::exception& e) {
671 e.what());
672 } catch (...) {
673 }
674 return APP_FAILURE;
675}
677
679template<class T>
680concept HasIterateFunction = requires(T* state) { state->Iterate(); };
681
689template<HasIterateFunction T>
690inline AppResult IterateClass(T* state)
691{
692 try {
693 return state->Iterate();
694 } catch (std::exception& e) {
696 e.what());
697 } catch (...) {
698 }
699 return APP_FAILURE;
700}
701
703template<class T>
704concept HasEventFunction =
705 requires(T* state, const SDL_Event& event) { state->Event(event); };
706
715template<class T>
716inline AppResult DefaultEventClass(T* state, const SDL_Event& event)
717{
718 if (event.type == SDL_EVENT_QUIT) return APP_SUCCESS;
719 return APP_CONTINUE;
720}
721
731template<class T>
732inline AppResult EventClass(T* state, const SDL_Event& event)
733{
734 try {
735 return DefaultEventClass(state, event);
736 } catch (std::exception& e) {
738 e.what());
739 } catch (...) {
740 }
741 return APP_FAILURE;
742}
743
744template<HasEventFunction T>
745inline AppResult EventClass(T* state, const SDL_Event& event)
746{
747 try {
748 return state->Event(event);
749 } catch (std::exception& e) {
751 e.what());
752 } catch (...) {
753 }
754 return APP_FAILURE;
755}
756
758
765template<class T>
766inline void DefaultClassDestroy(T* state)
767{
768 delete state;
769}
770
772template<class T>
773concept HasQuitFunction =
774 requires(T* state, AppResult result) { T::Quit(state, result); };
775
788template<class T>
789inline void QuitClass(T* state, AppResult result)
790{
791 DefaultClassDestroy(state);
792}
793
794template<HasQuitFunction T>
795inline void QuitClass(T* state, AppResult result)
796{
797 T::Quit(state, result);
798}
800
802
803} // namespace SDL
804
805#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:198
void Init(InitFlags flags)
Initialize the SDL library.
Definition: SDL3pp_init.h:256
void SetAppMetadata(StringParam appname, StringParam appversion, StringParam appidentifier)
Specify basic metadata about your app.
Definition: SDL3pp_init.h:472
void QuitClass(T *state, AppResult result)
Destroy state with given result.
Definition: SDL3pp_init.h:789
AppResult IterateClass(T *state)
Iterate the state.
Definition: SDL3pp_init.h:690
InitFlags WasInit(InitFlags flags)
Get a mask of the specified subsystems which are currently initialized.
Definition: SDL3pp_init.h:304
std::function< void()> MainThreadCB
Callback run on the main thread.
Definition: SDL3pp_init.h:371
void InitSubSystem(InitFlags flags)
Compatibility function to initialize the SDL library.
Definition: SDL3pp_init.h:272
AppResult DefaultCreateClass(T **state, AppArgs args)
Allocate and initialize state with new.
Definition: SDL3pp_init.h:613
constexpr AppResult APP_FAILURE
Value that requests termination with error from the main callbacks.
Definition: SDL3pp_init.h:126
std::span< char const *const > AppArgs
Represents application parameters.
Definition: SDL3pp_init.h:598
constexpr AppResult APP_CONTINUE
Value that requests that the app continue from the main callbacks.
Definition: SDL3pp_init.h:120
SDL_AppIterate_func AppIterate_func
Function pointer typedef for SDL_AppIterate.
Definition: SDL3pp_init.h:168
const char * GetAppMetadataProperty(StringParam name)
Get metadata about your app.
Definition: SDL3pp_init.h:583
SDL_MainThreadCallback MainThreadCallback
Callback run on the main thread.
Definition: SDL3pp_init.h:358
SDL_AppResult AppResult
Return values for optional main callbacks.
Definition: SDL3pp_init.h:117
SDL_AppInit_func AppInit_func
Function pointer typedef for SDL_AppInit.
Definition: SDL3pp_init.h:153
void RunOnMainThread(MainThreadCallback callback, void *userdata, bool wait_complete)
Call a function on the main thread during event processing.
Definition: SDL3pp_init.h:398
AppResult DefaultEventClass(T *state, const SDL_Event &event)
Default handle by finishing if QUIT is requested.
Definition: SDL3pp_init.h:716
void DefaultClassDestroy(T *state)
Destroy state with delete;.
Definition: SDL3pp_init.h:766
void SetAppMetadataProperty(StringParam name, StringParam value)
Specify metadata about your app through a set of properties.
Definition: SDL3pp_init.h:539
#define SDL3PP_APPCLASS_LOG_PRIORITY
The default log priority for app class.
Definition: SDL3pp_init.h:592
AppResult EventClass(T *state, const SDL_Event &event)
Iterate the state.
Definition: SDL3pp_init.h:732
void Quit()
Clean up all initialized subsystems.
Definition: SDL3pp_init.h:322
void QuitSubSystem(InitFlags flags)
Shut down specific SDL subsystems.
Definition: SDL3pp_init.h:290
SDL_AppEvent_func AppEvent_func
Function pointer typedef for SDL_AppEvent.
Definition: SDL3pp_init.h:184
AppResult InitClass(T **state, AppArgs args)
Init state with arguments.
Definition: SDL3pp_init.h:649
bool IsMainThread()
Return whether this is the main thread.
Definition: SDL3pp_init.h:342
SDL_AppQuit_func AppQuit_func
Function pointer typedef for SDL_AppQuit.
Definition: SDL3pp_init.h:198
constexpr AppResult APP_SUCCESS
Value that requests termination with success from the main callbacks.
Definition: SDL3pp_init.h:123
void LogUnformatted(LogPriority priority, StringParam message) const
Log an unformatted message with the specified priority.
Definition: SDL3pp_log.h:216
constexpr LogCategory LOG_CATEGORY_APPLICATION
APPLICATION.
Definition: SDL3pp_log.h:441
Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:325
constexpr InitFlags INIT_SENSOR
INIT_SENSOR implies INIT_EVENTS
Definition: SDL3pp_init.h:82
Uint32 InitFlags
Initialization flags for Init and/or InitSubSystem.
Definition: SDL3pp_init.h:62
constexpr InitFlags INIT_JOYSTICK
INIT_JOYSTICK implies INIT_EVENTS
Definition: SDL3pp_init.h:72
constexpr InitFlags INIT_VIDEO
INIT_VIDEO implies INIT_EVENTS, should be initialized on the main thread
Definition: SDL3pp_init.h:70
constexpr InitFlags INIT_AUDIO
INIT_AUDIO implies INIT_EVENTS
Definition: SDL3pp_init.h:64
constexpr InitFlags INIT_EVENTS
EVENTS.
Definition: SDL3pp_init.h:80
constexpr InitFlags INIT_GAMEPAD
INIT_GAMEPAD implies INIT_JOYSTICK
Definition: SDL3pp_init.h:77
constexpr InitFlags INIT_HAPTIC
HAPTIC.
Definition: SDL3pp_init.h:75
constexpr InitFlags INIT_CAMERA
INIT_CAMERA implies INIT_EVENTS
Definition: SDL3pp_init.h:85
Main include header for the SDL3pp library.
Definition: SDL3pp_callbackWrapper.h:66