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
114using AppResult = SDL_AppResult;
115
117constexpr AppResult APP_CONTINUE = SDL_APP_CONTINUE;
118
120constexpr AppResult APP_SUCCESS = SDL_APP_SUCCESS;
121
123constexpr AppResult APP_FAILURE = SDL_APP_FAILURE;
124
126
149using AppInit_func = SDL_AppInit_func;
150
164using AppIterate_func = SDL_AppIterate_func;
165
180using AppEvent_func = SDL_AppEvent_func;
181
194using AppQuit_func = SDL_AppQuit_func;
195
197
250inline void Init(InitFlags flags) { CheckError(SDL_Init(flags)); }
251
266inline void InitSubSystem(InitFlags flags)
267{
268 CheckError(SDL_InitSubSystem(flags));
269}
270
284inline void QuitSubSystem(InitFlags flags) { SDL_QuitSubSystem(flags); }
285
298inline InitFlags WasInit(InitFlags flags) { return SDL_WasInit(flags); }
299
316inline void Quit() { SDL_Quit(); }
317
335inline bool IsMainThread() { return SDL_IsMainThread(); }
336
351using MainThreadCallback = SDL_MainThreadCallback;
352
365using MainThreadCB = std::function<void()>;
366
368
393 void* userdata,
394 bool wait_complete)
395{
396 CheckError(SDL_RunOnMainThread(callback, userdata, wait_complete));
397}
398
424inline void RunOnMainThread(MainThreadCB callback, bool wait_complete)
425{
426 using Wrapper = CallbackWrapper<MainThreadCB>;
427 void* wrapped = Wrapper::Wrap(std::move(callback));
428 RunOnMainThread(&Wrapper::CallOnce, wrapped, wait_complete);
429}
430
464inline void SetAppMetadata(StringParam appname,
465 StringParam appversion,
466 StringParam appidentifier)
467{
468 CheckError(SDL_SetAppMetadata(appname, appversion, appidentifier));
469}
470
531{
532 CheckError(SDL_SetAppMetadataProperty(name, value));
533}
534
535namespace prop::appMetaData {
536
537constexpr auto NAME_STRING = SDL_PROP_APP_METADATA_NAME_STRING;
538
539constexpr auto VERSION_STRING = SDL_PROP_APP_METADATA_VERSION_STRING;
540
541constexpr auto IDENTIFIER_STRING = SDL_PROP_APP_METADATA_IDENTIFIER_STRING;
542
543constexpr auto CREATOR_STRING = SDL_PROP_APP_METADATA_CREATOR_STRING;
544
545constexpr auto COPYRIGHT_STRING = SDL_PROP_APP_METADATA_COPYRIGHT_STRING;
546
547constexpr auto URL_STRING = SDL_PROP_APP_METADATA_URL_STRING;
548
549constexpr auto TYPE_STRING = SDL_PROP_APP_METADATA_TYPE_STRING;
550
551} // namespace prop::appMetaData
552
574inline const char* GetAppMetadataProperty(StringParam name)
575{
576 return SDL_GetAppMetadataProperty(name);
577}
578
579#ifndef SDL3PP_APPCLASS_LOG_PRIORITY
583#define SDL3PP_APPCLASS_LOG_PRIORITY LOG_PRIORITY_CRITICAL
584#endif // SDL3PP_APPCLASS_LOG_PRIORITY
585
589using AppArgs = std::span<char const* const>;
590
603template<class T>
604inline AppResult DefaultCreateClass(T** state, AppArgs args)
605{
606 static_assert(std::is_default_constructible_v<T>);
607 *state = new T{};
608 return APP_CONTINUE;
609}
610
611template<class T>
612 requires std::convertible_to<AppArgs, T>
613inline AppResult DefaultCreateClass(T** state, AppArgs args)
614{
615 *state = new T{args};
616 return APP_CONTINUE;
617}
619
621template<class T>
622concept HasInitFunction = requires(T** state) {
623 { T::Init(state, AppArgs{}) } -> std::convertible_to<AppResult>;
624};
625
639template<class T>
640inline AppResult InitClass(T** state, AppArgs args)
641{
642 try {
643 return DefaultCreateClass(state, args);
644 } catch (std::exception& e) {
646 e.what());
647 } catch (...) {
648 }
649 return APP_FAILURE;
650}
651
652template<HasInitFunction T>
653inline AppResult InitClass(T** state, AppArgs args)
654{
655 *state = nullptr;
656 try {
657 AppResult result = T::Init(state, args);
658 if (*state == nullptr && result != APP_FAILURE) return APP_SUCCESS;
659 return result;
660 } catch (std::exception& e) {
662 e.what());
663 } catch (...) {
664 }
665 return APP_FAILURE;
666}
668
670template<class T>
671concept HasIterateFunction = requires(T* state) { state->Iterate(); };
672
680template<HasIterateFunction T>
681inline AppResult IterateClass(T* state)
682{
683 try {
684 return state->Iterate();
685 } catch (std::exception& e) {
687 e.what());
688 } catch (...) {
689 }
690 return APP_FAILURE;
691}
692
694template<class T>
695concept HasEventFunction =
696 requires(T* state, const SDL_Event& event) { state->Event(event); };
697
706template<class T>
707inline AppResult DefaultEventClass(T* state, const SDL_Event& event)
708{
709 if (event.type == SDL_EVENT_QUIT) return APP_SUCCESS;
710 return APP_CONTINUE;
711}
712
722template<class T>
723inline AppResult EventClass(T* state, const SDL_Event& event)
724{
725 try {
726 return DefaultEventClass(state, event);
727 } catch (std::exception& e) {
729 e.what());
730 } catch (...) {
731 }
732 return APP_FAILURE;
733}
734
735template<HasEventFunction T>
736inline AppResult EventClass(T* state, const SDL_Event& event)
737{
738 try {
739 return state->Event(event);
740 } catch (std::exception& e) {
742 e.what());
743 } catch (...) {
744 }
745 return APP_FAILURE;
746}
747
749
756template<class T>
757inline void DefaultClassDestroy(T* state)
758{
759 delete state;
760}
761
763template<class T>
764concept HasQuitFunction =
765 requires(T* state, AppResult result) { T::Quit(state, result); };
766
779template<class T>
780inline void QuitClass(T* state, AppResult result)
781{
782 DefaultClassDestroy(state);
783}
784
785template<HasQuitFunction T>
786inline void QuitClass(T* state, AppResult result)
787{
788 T::Quit(state, result);
789}
791
793
794} // namespace SDL
795
796#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
void Init(InitFlags flags)
Initialize the SDL library.
Definition: SDL3pp_init.h:250
void SetAppMetadata(StringParam appname, StringParam appversion, StringParam appidentifier)
Specify basic metadata about your app.
Definition: SDL3pp_init.h:464
void QuitClass(T *state, AppResult result)
Destroy state with given result.
Definition: SDL3pp_init.h:780
AppResult IterateClass(T *state)
Iterate the state.
Definition: SDL3pp_init.h:681
InitFlags WasInit(InitFlags flags)
Get a mask of the specified subsystems which are currently initialized.
Definition: SDL3pp_init.h:298
std::function< void()> MainThreadCB
Callback run on the main thread.
Definition: SDL3pp_init.h:365
void InitSubSystem(InitFlags flags)
Compatibility function to initialize the SDL library.
Definition: SDL3pp_init.h:266
AppResult DefaultCreateClass(T **state, AppArgs args)
Allocate and initialize state with new.
Definition: SDL3pp_init.h:604
constexpr AppResult APP_FAILURE
Value that requests termination with error from the main callbacks.
Definition: SDL3pp_init.h:123
std::span< char const *const > AppArgs
Represents application parameters.
Definition: SDL3pp_init.h:589
constexpr AppResult APP_CONTINUE
Value that requests that the app continue from the main callbacks.
Definition: SDL3pp_init.h:117
SDL_AppIterate_func AppIterate_func
Function pointer typedef for SDL_AppIterate.
Definition: SDL3pp_init.h:164
const char * GetAppMetadataProperty(StringParam name)
Get metadata about your app.
Definition: SDL3pp_init.h:574
SDL_MainThreadCallback MainThreadCallback
Callback run on the main thread.
Definition: SDL3pp_init.h:351
SDL_AppResult AppResult
Return values for optional main callbacks.
Definition: SDL3pp_init.h:114
SDL_AppInit_func AppInit_func
Function pointer typedef for SDL_AppInit.
Definition: SDL3pp_init.h:149
void RunOnMainThread(MainThreadCallback callback, void *userdata, bool wait_complete)
Call a function on the main thread during event processing.
Definition: SDL3pp_init.h:392
AppResult DefaultEventClass(T *state, const SDL_Event &event)
Default handle by finishing if QUIT is requested.
Definition: SDL3pp_init.h:707
void DefaultClassDestroy(T *state)
Destroy state with delete;.
Definition: SDL3pp_init.h:757
void SetAppMetadataProperty(StringParam name, StringParam value)
Specify metadata about your app through a set of properties.
Definition: SDL3pp_init.h:530
#define SDL3PP_APPCLASS_LOG_PRIORITY
The default log priority for app class.
Definition: SDL3pp_init.h:583
AppResult EventClass(T *state, const SDL_Event &event)
Iterate the state.
Definition: SDL3pp_init.h:723
void Quit()
Clean up all initialized subsystems.
Definition: SDL3pp_init.h:316
void QuitSubSystem(InitFlags flags)
Shut down specific SDL subsystems.
Definition: SDL3pp_init.h:284
SDL_AppEvent_func AppEvent_func
Function pointer typedef for SDL_AppEvent.
Definition: SDL3pp_init.h:180
AppResult InitClass(T **state, AppArgs args)
Init state with arguments.
Definition: SDL3pp_init.h:640
bool IsMainThread()
Return whether this is the main thread.
Definition: SDL3pp_init.h:335
SDL_AppQuit_func AppQuit_func
Function pointer typedef for SDL_AppQuit.
Definition: SDL3pp_init.h:194
constexpr AppResult APP_SUCCESS
Value that requests termination with success from the main callbacks.
Definition: SDL3pp_init.h:120
void LogUnformatted(LogPriority priority, StringParam message) const
Log an unformatted message with the specified priority.
Definition: SDL3pp_log.h:207
constexpr LogCategory LOG_CATEGORY_APPLICATION
APPLICATION.
Definition: SDL3pp_log.h:432
::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: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