|
SDL3pp
A slim C++ wrapper for SDL3
|
All SDL programs need to initialize the library before starting to work with it. More...
Topics | |
| Initialization flags | |
Namespaces | |
| namespace | SDL::prop |
| Constants for Properties keys. | |
| namespace | SDL::prop::appMetaData |
| Metadata property names for application. | |
Macros | |
| #define | SDL3PP_APPCLASS_LOG_PRIORITY SDL::LOG_PRIORITY_CRITICAL |
| The default log priority for app class. | |
Functions | |
| void | SDL::Init (InitFlags flags) |
| Initialize the SDL library. | |
| void | SDL::InitSubSystem (InitFlags flags) |
| Compatibility function to initialize the SDL library. | |
| void | SDL::QuitSubSystem (InitFlags flags) |
| Shut down specific SDL subsystems. | |
| InitFlags | SDL::WasInit (InitFlags flags) |
| Get a mask of the specified subsystems which are currently initialized. | |
| void | SDL::Quit () |
| Clean up all initialized subsystems. | |
| bool | SDL::IsMainThread () |
| Return whether this is the main thread. | |
| void | SDL::RunOnMainThread (MainThreadCallback callback, void *userdata, bool wait_complete) |
| Call a function on the main thread during event processing. | |
| void | SDL::RunOnMainThread (MainThreadCB callback, bool wait_complete) |
| Call a function on the main thread during event processing. | |
| void | SDL::SetAppMetadata (StringParam appname, StringParam appversion, StringParam appidentifier) |
| Specify basic metadata about your app. | |
| void | SDL::SetAppMetadataProperty (StringParam name, StringParam value) |
| Specify metadata about your app through a set of properties. | |
| const char * | SDL::GetAppMetadataProperty (StringParam name) |
| Get metadata about your app. | |
AppResult | |
App result for Main callback | |
| using | SDL::AppResult = SDL_AppResult |
| Return values for optional main callbacks. | |
| constexpr AppResult | SDL::APP_CONTINUE = SDL_APP_CONTINUE |
| Value that requests that the app continue from the main callbacks. | |
| constexpr AppResult | SDL::APP_SUCCESS = SDL_APP_SUCCESS |
| Value that requests termination with success from the main callbacks. | |
| constexpr AppResult | SDL::APP_FAILURE = SDL_APP_FAILURE |
| Value that requests termination with error from the main callbacks. | |
Callbacks for EnterAppMainCallbacks() | |
| using | SDL::AppInit_func |
| Function pointer typedef for SDL_AppInit. | |
| using | SDL::AppIterate_func = AppResult(SDLCALL*)(void* appstate) |
| Function pointer typedef for SDL_AppIterate. | |
| using | SDL::AppEvent_func = AppResult(SDLCALL*)(void* appstate, Event* event) |
| Function pointer typedef for SDL_AppEvent. | |
| using | SDL::AppQuit_func = void(SDLCALL*)(void* appstate, AppResult result) |
| Function pointer typedef for SDL_AppQuit. | |
Callbacks for RunOnMainThread() | |
| using | SDL::MainThreadCallback = void(SDLCALL*)(void* userdata) |
| Callback run on the main thread. | |
| using | SDL::MainThreadCB = std::function<void()> |
| Callback run on the main thread. | |
All SDL programs need to initialize the library before starting to work with it.
Almost everything can simply call Init() near startup, with a handful of flags to specify subsystems to touch. These are here to make sure SDL does not even attempt to touch low-level pieces of the operating system that you don't intend to use. For example, you might be using SDL for video and input but chose an external library for audio, and in this case you would just need to leave off the INIT_AUDIO flag to make sure that external library has complete control.
Most apps, when terminating, should call Quit(). This will clean up (nearly) everything that SDL might have allocated, and crucially, it'll make sure that the display's resolution is back to what the user expects if you had previously changed it for your game.
SDL3 apps are strongly encouraged to call SetAppMetadata() at startup to fill in details about the program. This is completely optional, but it helps in small ways (we can provide an About dialog box for the macOS menu, we can name the app in the system's audio mixer, etc). Those that want to provide a lot of information should look at the more-detailed SetAppMetadataProperty().
| using SDL::AppEvent_func = AppResult(SDLCALL*)(void* appstate, Event* event) |
Function pointer typedef for SDL_AppEvent.
These are used by EnterAppMainCallbacks. This mechanism operates behind the scenes for apps using the optional main callbacks. Apps that want to use this should just implement SDL_AppEvent directly.
| appstate | an optional pointer, provided by the app in SDL_AppInit. |
| event | the new event for the app to examine. |
| using SDL::AppInit_func |
Function pointer typedef for SDL_AppInit.
These are used by EnterAppMainCallbacks. This mechanism operates behind the scenes for apps using the optional main callbacks. Apps that want to use this should just implement SDL_AppInit directly.
| appstate | a place where the app can optionally store a pointer for future use. |
| argc | the standard ANSI C main's argc; number of elements in argv. |
| argv | the standard ANSI C main's argv; array of command line arguments. |
| using SDL::AppIterate_func = AppResult(SDLCALL*)(void* appstate) |
Function pointer typedef for SDL_AppIterate.
These are used by EnterAppMainCallbacks. This mechanism operates behind the scenes for apps using the optional main callbacks. Apps that want to use this should just implement SDL_AppIterate directly.
| appstate | an optional pointer, provided by the app in SDL_AppInit. |
| using SDL::AppQuit_func = void(SDLCALL*)(void* appstate, AppResult result) |
Function pointer typedef for SDL_AppQuit.
These are used by EnterAppMainCallbacks. This mechanism operates behind the scenes for apps using the optional main callbacks. Apps that want to use this should just implement SDL_AppEvent directly.
| appstate | an optional pointer, provided by the app in SDL_AppInit. |
| result | the result code that terminated the app (success or failure). |
| using SDL::AppResult = SDL_AppResult |
Return values for optional main callbacks.
Returning APP_SUCCESS or APP_FAILURE from SDL_AppInit, SDL_AppEvent, or SDL_AppIterate will terminate the program and report success/failure to the operating system. What that means is platform-dependent. On Unix, for example, on success, the process error code will be zero, and on failure it will be 1. This interface doesn't allow you to return specific exit codes, just whether there was an error generally or not.
Returning APP_CONTINUE from these functions will let the app continue to run.
See Main callbacks in SDL3 for complete details.
| using SDL::MainThreadCallback = void(SDLCALL*)(void* userdata) |
Callback run on the main thread.
| userdata | an app-controlled pointer that is passed to the callback. |
| using SDL::MainThreadCB = std::function<void()> |
Callback run on the main thread.
|
inline |
Get metadata about your app.
This returns metadata previously set using SetAppMetadata() or SetAppMetadataProperty(). See SetAppMetadataProperty() for the list of available properties and their meanings.
| name | the name of the metadata property to get. |
|
inline |
Initialize the SDL library.
Init() simply forwards to calling InitSubSystem(). Therefore, the two may be used interchangeably. Though for readability of your code InitSubSystem() might be preferred.
The file I/O (for example: IOFromFile) and threading (CreateThread) subsystems are initialized by default. Message boxes (ShowSimpleMessageBox) also attempt to work without initializing the video subsystem, in hopes of being useful in showing an error dialog when Init fails. You must specifically initialize other subsystems if you use them in your application.
Logging (such as Log) works without initialization, too.
flags may be any of the following OR'd together:
Subsystem initialization is ref-counted, you must call QuitSubSystem() for each InitSubSystem() to correctly shutdown a subsystem manually (or call Quit() to force shutdown). If a subsystem is already loaded then this call will increase the ref-count and return.
Consider reporting some basic metadata about your application before calling Init, using either SetAppMetadata() or SetAppMetadataProperty().
| flags | subsystem initialization flags. |
| Error | on failure. |
|
inline |
Compatibility function to initialize the SDL library.
This function and Init() are interchangeable.
| flags | any of the flags used by Init(); see Init for details. |
| Error | on failure. |
|
inline |
Return whether this is the main thread.
On Apple platforms, the main thread is the thread that runs your program's main() entry point. On other platforms, the main thread is the one that calls Init(INIT_VIDEO), which should usually be the one that runs your program's main() entry point. If you are using the main callbacks, SDL_AppInit(), SDL_AppIterate(), and SDL_AppQuit() are all called on the main thread.
|
inline |
Clean up all initialized subsystems.
You should call this function even if you have already shutdown each initialized subsystem with QuitSubSystem(). It is safe to call this function even in the case of errors in initialization.
You can use this function with atexit() to ensure that it is run when your application is shutdown, but it is not wise to do this from a library or other dynamically loaded code.
|
inline |
Shut down specific SDL subsystems.
You still need to call Quit() even if you close all open subsystems with QuitSubSystem().
| flags | any of the flags used by Init(); see Init for details. |
|
inline |
Call a function on the main thread during event processing.
If this is called on the main thread, the callback is executed immediately. If this is called on another thread, this callback is queued for execution on the main thread during event processing.
Be careful of deadlocks when using this functionality. You should not have the main thread wait for the current thread while this function is being called with wait_complete true.
| callback | the callback to call on the main thread. |
| userdata | a pointer that is passed to callback. |
| wait_complete | true to wait for the callback to complete, false to return immediately. |
| Error | on failure. |
|
inline |
Call a function on the main thread during event processing.
If this is called on the main thread, the callback is executed immediately. If this is called on another thread, this callback is queued for execution on the main thread during event processing.
Be careful of deadlocks when using this functionality. You should not have the main thread wait for the current thread while this function is being called with wait_complete true.
| callback | the callback to call on the main thread. |
| wait_complete | true to wait for the callback to complete, false to return immediately. |
| Error | on failure. |
|
inline |
Specify basic metadata about your app.
You can optionally provide metadata about your app to SDL. This is not required, but strongly encouraged.
There are several locations where SDL can make use of metadata (an "About" box in the macOS menu bar, the name of the app can be shown on some audio mixers, etc). Any piece of metadata can be left as nullptr, if a specific detail doesn't make sense for the app.
This function should be called as early as possible, before Init. Multiple calls to this function are allowed, but various state might not change once it has been set up with a previous call to this function.
Passing a nullptr removes any previous metadata.
This is a simplified interface for the most important information. You can supply significantly more detailed metadata with SetAppMetadataProperty().
| appname | The name of the application ("My Game 2: Bad Guy's Revenge!"). |
| appversion | The version of the application ("1.0.0beta5" or a git hash, or whatever makes sense). |
| appidentifier | A unique string in reverse-domain format that identifies this app ("com.example.mygame2"). |
| Error | on failure. |
|
inline |
Specify metadata about your app through a set of properties.
You can optionally provide metadata about your app to SDL. This is not required, but strongly encouraged.
There are several locations where SDL can make use of metadata (an "About" box in the macOS menu bar, the name of the app can be shown on some audio mixers, etc). Any piece of metadata can be left out, if a specific detail doesn't make sense for the app.
This function should be called as early as possible, before Init. Multiple calls to this function are allowed, but various state might not change once it has been set up with a previous call to this function.
Once set, this metadata can be read using GetAppMetadataProperty().
These are the supported properties:
| name | the name of the metadata property to set. |
| value | the value of the property, or nullptr to remove that property. |
| Error | on failure. |
Get a mask of the specified subsystems which are currently initialized.
| flags | any of the flags used by Init(); see Init for details. |