SDL 3.0
SDL_init.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/**
23 * # CategoryInit
24 *
25 * All SDL programs need to initialize the library before starting to work
26 * with it.
27 *
28 * Almost everything can simply call SDL_Init() near startup, with a handful
29 * of flags to specify subsystems to touch. These are here to make sure SDL
30 * does not even attempt to touch low-level pieces of the operating system
31 * that you don't intend to use. For example, you might be using SDL for video
32 * and input but chose an external library for audio, and in this case you
33 * would just need to leave off the `SDL_INIT_AUDIO` flag to make sure that
34 * external library has complete control.
35 *
36 * Most apps, when terminating, should call SDL_Quit(). This will clean up
37 * (nearly) everything that SDL might have allocated, and crucially, it'll
38 * make sure that the display's resolution is back to what the user expects if
39 * you had previously changed it for your game.
40 *
41 * SDL3 apps are strongly encouraged to call SDL_SetAppMetadata() at startup
42 * to fill in details about the program. This is completely optional, but it
43 * helps in small ways (we can provide an About dialog box for the macOS menu,
44 * we can name the app in the system's audio mixer, etc). Those that want to
45 * provide a _lot_ of information should look at the more-detailed
46 * SDL_SetAppMetadataProperty().
47 */
48
49#ifndef SDL_init_h_
50#define SDL_init_h_
51
52#include <SDL3/SDL_stdinc.h>
53#include <SDL3/SDL_error.h>
54#include <SDL3/SDL_events.h>
55
56#include <SDL3/SDL_begin_code.h>
57/* Set up for C function definitions, even when using C++ */
58#ifdef __cplusplus
59extern "C" {
60#endif
61
62/* As of version 0.5, SDL is loaded dynamically into the application */
63
64/**
65 * Initialization flags for SDL_Init and/or SDL_InitSubSystem
66 *
67 * These are the flags which may be passed to SDL_Init(). You should specify
68 * the subsystems which you will be using in your application.
69 *
70 * \since This datatype is available since SDL 3.1.3.
71 *
72 * \sa SDL_Init
73 * \sa SDL_Quit
74 * \sa SDL_InitSubSystem
75 * \sa SDL_QuitSubSystem
76 * \sa SDL_WasInit
77 */
79
80#define SDL_INIT_AUDIO 0x00000010u /**< `SDL_INIT_AUDIO` implies `SDL_INIT_EVENTS` */
81#define SDL_INIT_VIDEO 0x00000020u /**< `SDL_INIT_VIDEO` implies `SDL_INIT_EVENTS` */
82#define SDL_INIT_JOYSTICK 0x00000200u /**< `SDL_INIT_JOYSTICK` implies `SDL_INIT_EVENTS`, should be initialized on the same thread as SDL_INIT_VIDEO on Windows if you don't set SDL_HINT_JOYSTICK_THREAD */
83#define SDL_INIT_HAPTIC 0x00001000u
84#define SDL_INIT_GAMEPAD 0x00002000u /**< `SDL_INIT_GAMEPAD` implies `SDL_INIT_JOYSTICK` */
85#define SDL_INIT_EVENTS 0x00004000u
86#define SDL_INIT_SENSOR 0x00008000u /**< `SDL_INIT_SENSOR` implies `SDL_INIT_EVENTS` */
87#define SDL_INIT_CAMERA 0x00010000u /**< `SDL_INIT_CAMERA` implies `SDL_INIT_EVENTS` */
88
89/**
90 * Return values for optional main callbacks.
91 *
92 * Returning SDL_APP_SUCCESS or SDL_APP_FAILURE from SDL_AppInit,
93 * SDL_AppEvent, or SDL_AppIterate will terminate the program and report
94 * success/failure to the operating system. What that means is
95 * platform-dependent. On Unix, for example, on success, the process error
96 * code will be zero, and on failure it will be 1. This interface doesn't
97 * allow you to return specific exit codes, just whether there was an error
98 * generally or not.
99 *
100 * Returning SDL_APP_CONTINUE from these functions will let the app continue
101 * to run.
102 *
103 * See
104 * [Main callbacks in SDL3](https://wiki.libsdl.org/SDL3/README/main-functions#main-callbacks-in-sdl3)
105 * for complete details.
106 *
107 * \since This enum is available since SDL 3.1.3.
108 */
109typedef enum SDL_AppResult
110{
111 SDL_APP_CONTINUE, /**< Value that requests that the app continue from the main callbacks. */
112 SDL_APP_SUCCESS, /**< Value that requests termination with success from the main callbacks. */
113 SDL_APP_FAILURE /**< Value that requests termination with error from the main callbacks. */
115
116typedef SDL_AppResult (SDLCALL *SDL_AppInit_func)(void **appstate, int argc, char *argv[]);
117typedef SDL_AppResult (SDLCALL *SDL_AppIterate_func)(void *appstate);
118typedef SDL_AppResult (SDLCALL *SDL_AppEvent_func)(void *appstate, SDL_Event *event);
119typedef void (SDLCALL *SDL_AppQuit_func)(void *appstate, SDL_AppResult result);
120
121/**
122 * Initialize the SDL library.
123 *
124 * SDL_Init() simply forwards to calling SDL_InitSubSystem(). Therefore, the
125 * two may be used interchangeably. Though for readability of your code
126 * SDL_InitSubSystem() might be preferred.
127 *
128 * The file I/O (for example: SDL_IOFromFile) and threading (SDL_CreateThread)
129 * subsystems are initialized by default. Message boxes
130 * (SDL_ShowSimpleMessageBox) also attempt to work without initializing the
131 * video subsystem, in hopes of being useful in showing an error dialog when
132 * SDL_Init fails. You must specifically initialize other subsystems if you
133 * use them in your application.
134 *
135 * Logging (such as SDL_Log) works without initialization, too.
136 *
137 * `flags` may be any of the following OR'd together:
138 *
139 * - `SDL_INIT_AUDIO`: audio subsystem; automatically initializes the events
140 * subsystem
141 * - `SDL_INIT_VIDEO`: video subsystem; automatically initializes the events
142 * subsystem
143 * - `SDL_INIT_JOYSTICK`: joystick subsystem; automatically initializes the
144 * events subsystem
145 * - `SDL_INIT_HAPTIC`: haptic (force feedback) subsystem
146 * - `SDL_INIT_GAMEPAD`: gamepad subsystem; automatically initializes the
147 * joystick subsystem
148 * - `SDL_INIT_EVENTS`: events subsystem
149 * - `SDL_INIT_SENSOR`: sensor subsystem; automatically initializes the events
150 * subsystem
151 * - `SDL_INIT_CAMERA`: camera subsystem; automatically initializes the events
152 * subsystem
153 *
154 * Subsystem initialization is ref-counted, you must call SDL_QuitSubSystem()
155 * for each SDL_InitSubSystem() to correctly shutdown a subsystem manually (or
156 * call SDL_Quit() to force shutdown). If a subsystem is already loaded then
157 * this call will increase the ref-count and return.
158 *
159 * Consider reporting some basic metadata about your application before
160 * calling SDL_Init, using either SDL_SetAppMetadata() or
161 * SDL_SetAppMetadataProperty().
162 *
163 * \param flags subsystem initialization flags.
164 * \returns true on success or false on failure; call SDL_GetError() for more
165 * information.
166 *
167 * \since This function is available since SDL 3.1.3.
168 *
169 * \sa SDL_SetAppMetadata
170 * \sa SDL_SetAppMetadataProperty
171 * \sa SDL_InitSubSystem
172 * \sa SDL_Quit
173 * \sa SDL_SetMainReady
174 * \sa SDL_WasInit
175 */
176extern SDL_DECLSPEC bool SDLCALL SDL_Init(SDL_InitFlags flags);
177
178/**
179 * Compatibility function to initialize the SDL library.
180 *
181 * This function and SDL_Init() are interchangeable.
182 *
183 * \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
184 * \returns true on success or false on failure; call SDL_GetError() for more
185 * information.
186 *
187 * \since This function is available since SDL 3.1.3.
188 *
189 * \sa SDL_Init
190 * \sa SDL_Quit
191 * \sa SDL_QuitSubSystem
192 */
193extern SDL_DECLSPEC bool SDLCALL SDL_InitSubSystem(SDL_InitFlags flags);
194
195/**
196 * Shut down specific SDL subsystems.
197 *
198 * You still need to call SDL_Quit() even if you close all open subsystems
199 * with SDL_QuitSubSystem().
200 *
201 * \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
202 *
203 * \since This function is available since SDL 3.1.3.
204 *
205 * \sa SDL_InitSubSystem
206 * \sa SDL_Quit
207 */
208extern SDL_DECLSPEC void SDLCALL SDL_QuitSubSystem(SDL_InitFlags flags);
209
210/**
211 * Get a mask of the specified subsystems which are currently initialized.
212 *
213 * \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
214 * \returns a mask of all initialized subsystems if `flags` is 0, otherwise it
215 * returns the initialization status of the specified subsystems.
216 *
217 * \since This function is available since SDL 3.1.3.
218 *
219 * \sa SDL_Init
220 * \sa SDL_InitSubSystem
221 */
222extern SDL_DECLSPEC SDL_InitFlags SDLCALL SDL_WasInit(SDL_InitFlags flags);
223
224/**
225 * Clean up all initialized subsystems.
226 *
227 * You should call this function even if you have already shutdown each
228 * initialized subsystem with SDL_QuitSubSystem(). It is safe to call this
229 * function even in the case of errors in initialization.
230 *
231 * You can use this function with atexit() to ensure that it is run when your
232 * application is shutdown, but it is not wise to do this from a library or
233 * other dynamically loaded code.
234 *
235 * \since This function is available since SDL 3.1.3.
236 *
237 * \sa SDL_Init
238 * \sa SDL_QuitSubSystem
239 */
240extern SDL_DECLSPEC void SDLCALL SDL_Quit(void);
241
242/**
243 * Specify basic metadata about your app.
244 *
245 * You can optionally provide metadata about your app to SDL. This is not
246 * required, but strongly encouraged.
247 *
248 * There are several locations where SDL can make use of metadata (an "About"
249 * box in the macOS menu bar, the name of the app can be shown on some audio
250 * mixers, etc). Any piece of metadata can be left as NULL, if a specific
251 * detail doesn't make sense for the app.
252 *
253 * This function should be called as early as possible, before SDL_Init.
254 * Multiple calls to this function are allowed, but various state might not
255 * change once it has been set up with a previous call to this function.
256 *
257 * Passing a NULL removes any previous metadata.
258 *
259 * This is a simplified interface for the most important information. You can
260 * supply significantly more detailed metadata with
261 * SDL_SetAppMetadataProperty().
262 *
263 * \param appname The name of the application ("My Game 2: Bad Guy's
264 * Revenge!").
265 * \param appversion The version of the application ("1.0.0beta5" or a git
266 * hash, or whatever makes sense).
267 * \param appidentifier A unique string in reverse-domain format that
268 * identifies this app ("com.example.mygame2").
269 * \returns true on success or false on failure; call SDL_GetError() for more
270 * information.
271 *
272 * \threadsafety It is safe to call this function from any thread.
273 *
274 * \since This function is available since SDL 3.1.3.
275 *
276 * \sa SDL_SetAppMetadataProperty
277 */
278extern SDL_DECLSPEC bool SDLCALL SDL_SetAppMetadata(const char *appname, const char *appversion, const char *appidentifier);
279
280/**
281 * Specify metadata about your app through a set of properties.
282 *
283 * You can optionally provide metadata about your app to SDL. This is not
284 * required, but strongly encouraged.
285 *
286 * There are several locations where SDL can make use of metadata (an "About"
287 * box in the macOS menu bar, the name of the app can be shown on some audio
288 * mixers, etc). Any piece of metadata can be left out, if a specific detail
289 * doesn't make sense for the app.
290 *
291 * This function should be called as early as possible, before SDL_Init.
292 * Multiple calls to this function are allowed, but various state might not
293 * change once it has been set up with a previous call to this function.
294 *
295 * Once set, this metadata can be read using SDL_GetMetadataProperty().
296 *
297 * These are the supported properties:
298 *
299 * - `SDL_PROP_APP_METADATA_NAME_STRING`: The human-readable name of the
300 * application, like "My Game 2: Bad Guy's Revenge!". This will show up
301 * anywhere the OS shows the name of the application separately from window
302 * titles, such as volume control applets, etc. This defaults to "SDL
303 * Application".
304 * - `SDL_PROP_APP_METADATA_VERSION_STRING`: The version of the app that is
305 * running; there are no rules on format, so "1.0.3beta2" and "April 22nd,
306 * 2024" and a git hash are all valid options. This has no default.
307 * - `SDL_PROP_APP_METADATA_IDENTIFIER_STRING`: A unique string that
308 * identifies this app. This must be in reverse-domain format, like
309 * "com.example.mygame2". This string is used by desktop compositors to
310 * identify and group windows together, as well as match applications with
311 * associated desktop settings and icons. If you plan to package your
312 * application in a container such as Flatpak, the app ID should match the
313 * name of your Flatpak container as well. This has no default.
314 * - `SDL_PROP_APP_METADATA_CREATOR_STRING`: The human-readable name of the
315 * creator/developer/maker of this app, like "MojoWorkshop, LLC"
316 * - `SDL_PROP_APP_METADATA_COPYRIGHT_STRING`: The human-readable copyright
317 * notice, like "Copyright (c) 2024 MojoWorkshop, LLC" or whatnot. Keep this
318 * to one line, don't paste a copy of a whole software license in here. This
319 * has no default.
320 * - `SDL_PROP_APP_METADATA_URL_STRING`: A URL to the app on the web. Maybe a
321 * product page, or a storefront, or even a GitHub repository, for user's
322 * further information This has no default.
323 * - `SDL_PROP_APP_METADATA_TYPE_STRING`: The type of application this is.
324 * Currently this string can be "game" for a video game, "mediaplayer" for a
325 * media player, or generically "application" if nothing else applies.
326 * Future versions of SDL might add new types. This defaults to
327 * "application".
328 *
329 * \param name the name of the metadata property to set.
330 * \param value the value of the property, or NULL to remove that property.
331 * \returns true on success or false on failure; call SDL_GetError() for more
332 * information.
333 *
334 * \threadsafety It is safe to call this function from any thread.
335 *
336 * \since This function is available since SDL 3.1.3.
337 *
338 * \sa SDL_GetAppMetadataProperty
339 * \sa SDL_SetAppMetadata
340 */
341extern SDL_DECLSPEC bool SDLCALL SDL_SetAppMetadataProperty(const char *name, const char *value);
342
343#define SDL_PROP_APP_METADATA_NAME_STRING "SDL.app.metadata.name"
344#define SDL_PROP_APP_METADATA_VERSION_STRING "SDL.app.metadata.version"
345#define SDL_PROP_APP_METADATA_IDENTIFIER_STRING "SDL.app.metadata.identifier"
346#define SDL_PROP_APP_METADATA_CREATOR_STRING "SDL.app.metadata.creator"
347#define SDL_PROP_APP_METADATA_COPYRIGHT_STRING "SDL.app.metadata.copyright"
348#define SDL_PROP_APP_METADATA_URL_STRING "SDL.app.metadata.url"
349#define SDL_PROP_APP_METADATA_TYPE_STRING "SDL.app.metadata.type"
350
351/**
352 * Get metadata about your app.
353 *
354 * This returns metadata previously set using SDL_SetAppMetadata() or
355 * SDL_SetAppMetadataProperty(). See SDL_SetAppMetadataProperty() for the list
356 * of available properties and their meanings.
357 *
358 * \param name the name of the metadata property to get.
359 * \returns the current value of the metadata property, or the default if it
360 * is not set, NULL for properties with no default.
361 *
362 * \threadsafety It is safe to call this function from any thread, although
363 * the string returned is not protected and could potentially be
364 * freed if you call SDL_SetAppMetadataProperty() to set that
365 * property from another thread.
366 *
367 * \since This function is available since SDL 3.1.3.
368 *
369 * \sa SDL_SetAppMetadata
370 * \sa SDL_SetAppMetadataProperty
371 */
372extern SDL_DECLSPEC const char * SDLCALL SDL_GetAppMetadataProperty(const char *name);
373
374/* Ends C function definitions when using C++ */
375#ifdef __cplusplus
376}
377#endif
378#include <SDL3/SDL_close_code.h>
379
380#endif /* SDL_init_h_ */
SDL_AppResult(* SDL_AppEvent_func)(void *appstate, SDL_Event *event)
Definition: SDL_init.h:118
Uint32 SDL_InitFlags
Definition: SDL_init.h:78
bool SDL_SetAppMetadataProperty(const char *name, const char *value)
void SDL_QuitSubSystem(SDL_InitFlags flags)
SDL_InitFlags SDL_WasInit(SDL_InitFlags flags)
SDL_AppResult(* SDL_AppInit_func)(void **appstate, int argc, char *argv[])
Definition: SDL_init.h:116
const char * SDL_GetAppMetadataProperty(const char *name)
bool SDL_InitSubSystem(SDL_InitFlags flags)
void SDL_Quit(void)
SDL_AppResult
Definition: SDL_init.h:110
@ SDL_APP_CONTINUE
Definition: SDL_init.h:111
@ SDL_APP_SUCCESS
Definition: SDL_init.h:112
@ SDL_APP_FAILURE
Definition: SDL_init.h:113
bool SDL_Init(SDL_InitFlags flags)
bool SDL_SetAppMetadata(const char *appname, const char *appversion, const char *appidentifier)
void(* SDL_AppQuit_func)(void *appstate, SDL_AppResult result)
Definition: SDL_init.h:119
SDL_AppResult(* SDL_AppIterate_func)(void *appstate)
Definition: SDL_init.h:117
uint32_t Uint32
Definition: SDL_stdinc.h:370