Expand description
§sdl3-main
This crate provides tools for using SDL 3’s main and callback APIs, and for interfacing with the main thread of the process.
§SDL main
To provide your own main but call it through SDL, use the main attribute macro.
See the documentation for that for more information.
§Callback API
To use the SDL callback API, you can use either the app_impl attribute macro,
or all four of the app_init, app_iterate, app_event and app_quit
attribute macros. Don’t use the main attribute macro in this mode.
See the documentation for more information.
§Main thread helpers
Some SDL functions have to be called on the main thread of the process. This crate provides some helper types and functions to alleviate this.
MainThreadToken: Zero-sized token that can only exist on the main thread.MainThreadData: Wrapper for data that can move between threads but that should only be accessed on the main thread.run_sync_on_main_thread(): Run a callback on the main thread, synchronously.run_async_on_main_thread(): Run a callback on the main thread, asynchronously.
§Features
| Feature | Description |
|---|---|
alloc | Enable features that require allocation (enabled by default) |
std | Enable features that require the standard library (enabled by default) |
nightly | Enable the ? operator to convert Result::Err and Option::None to AppResult*::Failure |
use-parking-lot-v0-12 | Support parking_lot 0.12 locks in app state accessors |
§Recent changes
-
0.6.1:
- Make
MainThreadToken::init()safe - Fix undefined reference to
main_with_resultwhen using themainmacro in some cases
- Make
-
0.6.0:
- Update sdl3-sys to 0.6.0
MainThreadToken::init()is no longer necessary in most cases- Pass arguments to main on
std(!stdalready did) - Add
MainThreadData::assert_new/get/get_mut - Log error when converting to
AppResult* - MSRV 1.85
See ChangeLog.md for older changes
Modules§
Structs§
- Main
Thread Data - Data that can only be accessed from the main thread. Accessors take a
MainThreadToken. - Main
Thread Token - Zero sized token that can only exist on the main thread.
Enums§
- AppResult
- This is the Rust enum equivalent to
SDL_AppResult. - AppResult
With State - An
AppResultwith an app state, for returning from the function tagged withapp_init.
Traits§
- Into
AppResult - This trait is used for converting a type into an
SDL_AppResultorAppResult.
Functions§
- run_
async_ on_ main_ thread - Schedule a callback to run on the main thread and immediately return without waiting for it.
- run_
sync_ on_ main_ thread - Run a callback on the main thread and wait for it to complete before returning.
Attribute Macros§
- app_
event - The function tagged with
app_eventis called by SDL when an event is delivered. This may get called on the main thread or on another thread. - app_
impl - This attribute macro can be applied to an
implblock for a type to assign the associated functions or methods namedapp_init,app_iterate,app_eventandapp_quitto the respective sdl main callbacks, as if the corresponding attribute macros were used. All four must be defined in a single impl block, butapp_quitis optional and will be defined as an empty function if omitted. - app_
init - The function tagged with
app_initis called by SDL at the start of the program on the main thread. - app_
iterate - The function tagged with
app_iterateis called continuously by SDL on the main thread while the app is running. - app_
quit - The function tagged with
app_quitis called by SDL on the main thread when the app quits. - main
- Use this attribute macro if you want to provide your own main, but run it through SDL.