SDL 2.0
SDL.c File Reference
#include "./SDL_internal.h"
#include "SDL.h"
#include "SDL_bits.h"
#include "SDL_revision.h"
#include "SDL_assert_c.h"
#include "events/SDL_events_c.h"
#include "haptic/SDL_haptic_c.h"
#include "joystick/SDL_joystick_c.h"
#include "sensor/SDL_sensor_c.h"
#include "timer/SDL_timer_c.h"
+ Include dependency graph for SDL.c:

Go to the source code of this file.

Functions

static void SDL_PrivateSubsystemRefCountIncr (Uint32 subsystem)
 
static void SDL_PrivateSubsystemRefCountDecr (Uint32 subsystem)
 
static SDL_bool SDL_PrivateShouldInitSubsystem (Uint32 subsystem)
 
static SDL_bool SDL_PrivateShouldQuitSubsystem (Uint32 subsystem)
 
void SDL_SetMainReady (void)
 
int SDL_InitSubSystem (Uint32 flags)
 
int SDL_Init (Uint32 flags)
 
void SDL_QuitSubSystem (Uint32 flags)
 
Uint32 SDL_WasInit (Uint32 flags)
 
void SDL_Quit (void)
 
void SDL_GetVersion (SDL_version *ver)
 Get the version of SDL that is linked against your program. More...
 
const char * SDL_GetRevision (void)
 Get the code revision of SDL that is linked against your program. More...
 
int SDL_GetRevisionNumber (void)
 Get the revision number of SDL that is linked against your program. More...
 
const char * SDL_GetPlatform ()
 Gets the name of the platform. More...
 
SDL_bool SDL_IsTablet ()
 Return true if the current device is a tablet. More...
 

Variables

static SDL_bool SDL_MainIsReady = SDL_TRUE
 
static SDL_bool SDL_bInMainQuit = SDL_FALSE
 
static Uint8 SDL_SubsystemRefCount [32]
 

Function Documentation

◆ SDL_GetPlatform()

const char * SDL_GetPlatform ( void  )

Gets the name of the platform.

Definition at line 423 of file SDL.c.

424{
425#if __AIX__
426 return "AIX";
427#elif __ANDROID__
428 return "Android";
429#elif __BSDI__
430 return "BSDI";
431#elif __DREAMCAST__
432 return "Dreamcast";
433#elif __EMSCRIPTEN__
434 return "Emscripten";
435#elif __FREEBSD__
436 return "FreeBSD";
437#elif __HAIKU__
438 return "Haiku";
439#elif __HPUX__
440 return "HP-UX";
441#elif __IRIX__
442 return "Irix";
443#elif __LINUX__
444 return "Linux";
445#elif __MINT__
446 return "Atari MiNT";
447#elif __MACOS__
448 return "MacOS Classic";
449#elif __MACOSX__
450 return "Mac OS X";
451#elif __NACL__
452 return "NaCl";
453#elif __NETBSD__
454 return "NetBSD";
455#elif __OPENBSD__
456 return "OpenBSD";
457#elif __OS2__
458 return "OS/2";
459#elif __OSF__
460 return "OSF/1";
461#elif __QNXNTO__
462 return "QNX Neutrino";
463#elif __RISCOS__
464 return "RISC OS";
465#elif __SOLARIS__
466 return "Solaris";
467#elif __WIN32__
468 return "Windows";
469#elif __WINRT__
470 return "WinRT";
471#elif __TVOS__
472 return "tvOS";
473#elif __IPHONEOS__
474 return "iOS";
475#elif __PSP__
476 return "PlayStation Portable";
477#else
478 return "Unknown (see SDL_platform.h)";
479#endif
480}

◆ SDL_GetRevision()

const char * SDL_GetRevision ( void  )

Get the code revision of SDL that is linked against your program.

Returns an arbitrary string (a hash value) uniquely identifying the exact revision of the SDL library in use, and is only useful in comparing against other revisions. It is NOT an incrementing number.

Definition at line 409 of file SDL.c.

410{
411 return SDL_REVISION;
412}
#define SDL_REVISION
Definition: SDL_revision.h:1

References SDL_REVISION.

◆ SDL_GetRevisionNumber()

int SDL_GetRevisionNumber ( void  )

Get the revision number of SDL that is linked against your program.

Returns a number uniquely identifying the exact revision of the SDL library in use. It is an incrementing number based on commits to hg.libsdl.org.

Definition at line 416 of file SDL.c.

417{
418 return SDL_REVISION_NUMBER;
419}
#define SDL_REVISION_NUMBER
Definition: SDL_revision.h:2

References SDL_REVISION_NUMBER.

◆ SDL_GetVersion()

void SDL_GetVersion ( SDL_version ver)

Get the version of SDL that is linked against your program.

If you are linking to SDL dynamically, then it is possible that the current version will be different than the version you compiled against. This function returns the current version, while SDL_VERSION() is a macro that tells you what version you compiled with.

SDL_version compiled;
SDL_version linked;
SDL_VERSION(&compiled);
SDL_GetVersion(&linked);
printf("We compiled against SDL version %d.%d.%d ...\n",
compiled.major, compiled.minor, compiled.patch);
printf("But we linked against SDL version %d.%d.%d.\n",
linked.major, linked.minor, linked.patch);
#define SDL_GetVersion
#define SDL_VERSION(x)
Macro to determine SDL version program was compiled against.
Definition: SDL_version.h:79
Information the version of SDL in use.
Definition: SDL_version.h:52
Uint8 minor
Definition: SDL_version.h:54
Uint8 patch
Definition: SDL_version.h:55
Uint8 major
Definition: SDL_version.h:53

This function may be called safely at any time, even before SDL_Init().

See also
SDL_VERSION

Definition at line 402 of file SDL.c.

403{
404 SDL_VERSION(ver);
405}

References SDL_VERSION.

◆ SDL_Init()

int SDL_Init ( Uint32  flags)

This function initializes the subsystems specified by flags

Definition at line 253 of file SDL.c.

254{
255 return SDL_InitSubSystem(flags);
256}
int SDL_InitSubSystem(Uint32 flags)
Definition: SDL.c:106
GLbitfield flags

References SDL_InitSubSystem().

◆ SDL_InitSubSystem()

int SDL_InitSubSystem ( Uint32  flags)

This function initializes specific SDL subsystems

Subsystem initialization is ref-counted, you must call SDL_QuitSubSystem() for each SDL_InitSubSystem() to correctly shutdown a subsystem manually (or call SDL_Quit() to force shutdown). If a subsystem is already loaded then this call will increase the ref-count and return.

Definition at line 106 of file SDL.c.

107{
108 if (!SDL_MainIsReady) {
109 SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");
110 return -1;
111 }
112
113 /* Clear the error message */
115
117 /* game controller implies joystick */
119 }
120
122 /* video or joystick implies events */
124 }
125
126#if SDL_VIDEO_DRIVER_WINDOWS
128 if (SDL_HelperWindowCreate() < 0) {
129 return -1;
130 }
131 }
132#endif
133
134#if !SDL_TIMERS_DISABLED
136#endif
137
138 /* Initialize the event subsystem */
139 if ((flags & SDL_INIT_EVENTS)) {
140#if !SDL_EVENTS_DISABLED
142 if (SDL_EventsInit() < 0) {
143 return (-1);
144 }
145 }
147#else
148 return SDL_SetError("SDL not built with events support");
149#endif
150 }
151
152 /* Initialize the timer subsystem */
153 if ((flags & SDL_INIT_TIMER)){
154#if !SDL_TIMERS_DISABLED
156 if (SDL_TimerInit() < 0) {
157 return (-1);
158 }
159 }
161#else
162 return SDL_SetError("SDL not built with timer support");
163#endif
164 }
165
166 /* Initialize the video subsystem */
167 if ((flags & SDL_INIT_VIDEO)){
168#if !SDL_VIDEO_DISABLED
170 if (SDL_VideoInit(NULL) < 0) {
171 return (-1);
172 }
173 }
175#else
176 return SDL_SetError("SDL not built with video support");
177#endif
178 }
179
180 /* Initialize the audio subsystem */
181 if ((flags & SDL_INIT_AUDIO)){
182#if !SDL_AUDIO_DISABLED
184 if (SDL_AudioInit(NULL) < 0) {
185 return (-1);
186 }
187 }
189#else
190 return SDL_SetError("SDL not built with audio support");
191#endif
192 }
193
194 /* Initialize the joystick subsystem */
195 if ((flags & SDL_INIT_JOYSTICK)){
196#if !SDL_JOYSTICK_DISABLED
198 if (SDL_JoystickInit() < 0) {
199 return (-1);
200 }
201 }
203#else
204 return SDL_SetError("SDL not built with joystick support");
205#endif
206 }
207
209#if !SDL_JOYSTICK_DISABLED
211 if (SDL_GameControllerInit() < 0) {
212 return (-1);
213 }
214 }
216#else
217 return SDL_SetError("SDL not built with joystick support");
218#endif
219 }
220
221 /* Initialize the haptic subsystem */
222 if ((flags & SDL_INIT_HAPTIC)){
223#if !SDL_HAPTIC_DISABLED
225 if (SDL_HapticInit() < 0) {
226 return (-1);
227 }
228 }
230#else
231 return SDL_SetError("SDL not built with haptic (force feedback) support");
232#endif
233 }
234
235 /* Initialize the sensor subsystem */
236 if ((flags & SDL_INIT_SENSOR)){
237#if !SDL_SENSOR_DISABLED
239 if (SDL_SensorInit() < 0) {
240 return (-1);
241 }
242 }
244#else
245 return SDL_SetError("SDL not built with sensor support");
246#endif
247 }
248
249 return (0);
250}
static void SDL_PrivateSubsystemRefCountIncr(Uint32 subsystem)
Definition: SDL.c:59
static SDL_bool SDL_MainIsReady
Definition: SDL.c:52
static SDL_bool SDL_PrivateShouldInitSubsystem(Uint32 subsystem)
Definition: SDL.c:78
#define SDL_INIT_SENSOR
Definition: SDL.h:84
#define SDL_INIT_TIMER
Definition: SDL.h:77
#define SDL_INIT_AUDIO
Definition: SDL.h:78
#define SDL_INIT_GAMECONTROLLER
Definition: SDL.h:82
#define SDL_INIT_JOYSTICK
Definition: SDL.h:80
#define SDL_INIT_EVENTS
Definition: SDL.h:83
#define SDL_INIT_HAPTIC
Definition: SDL.h:81
#define SDL_INIT_VIDEO
Definition: SDL.h:79
#define SDL_SetError
#define SDL_AudioInit
#define SDL_VideoInit
#define SDL_ClearError
int SDL_EventsInit(void)
Definition: SDL_events.c:1002
int SDL_GameControllerInit(void)
int SDL_HapticInit(void)
Definition: SDL_haptic.c:39
int SDL_JoystickInit(void)
Definition: SDL_joystick.c:114
int SDL_SensorInit(void)
Definition: SDL_sensor.c:69
int SDL_TimerInit(void)
Definition: SDL_timer.c:207
void SDL_TicksInit(void)
#define NULL
Definition: begin_code.h:167

References NULL, SDL_AudioInit, SDL_ClearError, SDL_EventsInit(), SDL_GameControllerInit(), SDL_HapticInit(), SDL_INIT_AUDIO, SDL_INIT_EVENTS, SDL_INIT_GAMECONTROLLER, SDL_INIT_HAPTIC, SDL_INIT_JOYSTICK, SDL_INIT_SENSOR, SDL_INIT_TIMER, SDL_INIT_VIDEO, SDL_JoystickInit(), SDL_MainIsReady, SDL_PrivateShouldInitSubsystem(), SDL_PrivateSubsystemRefCountIncr(), SDL_SensorInit(), SDL_SetError, SDL_TicksInit(), SDL_TimerInit(), and SDL_VideoInit.

Referenced by SDL_Init().

◆ SDL_IsTablet()

SDL_bool SDL_IsTablet ( void  )

Return true if the current device is a tablet.

Definition at line 483 of file SDL.c.

484{
485#if __ANDROID__
486 extern SDL_bool SDL_IsAndroidTablet(void);
487 return SDL_IsAndroidTablet();
488#elif __IPHONEOS__
489 extern SDL_bool SDL_IsIPad(void);
490 return SDL_IsIPad();
491#else
492 return SDL_FALSE;
493#endif
494}
SDL_bool SDL_IsAndroidTablet(void)
SDL_bool
Definition: SDL_stdinc.h:162
@ SDL_FALSE
Definition: SDL_stdinc.h:163

References SDL_FALSE, and SDL_IsAndroidTablet().

◆ SDL_PrivateShouldInitSubsystem()

static SDL_bool SDL_PrivateShouldInitSubsystem ( Uint32  subsystem)
static

Definition at line 78 of file SDL.c.

79{
80 int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
81 SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255);
82 return (SDL_SubsystemRefCount[subsystem_index] == 0) ? SDL_TRUE : SDL_FALSE;
83}
static Uint8 SDL_SubsystemRefCount[32]
Definition: SDL.c:55
#define SDL_assert(condition)
Definition: SDL_assert.h:169
SDL_FORCE_INLINE int SDL_MostSignificantBitIndex32(Uint32 x)
Definition: SDL_bits.h:61
@ SDL_TRUE
Definition: SDL_stdinc.h:164

References SDL_assert, SDL_FALSE, SDL_MostSignificantBitIndex32(), SDL_SubsystemRefCount, and SDL_TRUE.

Referenced by SDL_InitSubSystem().

◆ SDL_PrivateShouldQuitSubsystem()

static SDL_bool SDL_PrivateShouldQuitSubsystem ( Uint32  subsystem)
static

Definition at line 87 of file SDL.c.

87 {
88 int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
89 if (SDL_SubsystemRefCount[subsystem_index] == 0) {
90 return SDL_FALSE;
91 }
92
93 /* If we're in SDL_Quit, we shut down every subsystem, even if refcount
94 * isn't zero.
95 */
96 return (SDL_SubsystemRefCount[subsystem_index] == 1 || SDL_bInMainQuit) ? SDL_TRUE : SDL_FALSE;
97}
static SDL_bool SDL_bInMainQuit
Definition: SDL.c:54

References SDL_bInMainQuit, SDL_FALSE, SDL_MostSignificantBitIndex32(), SDL_SubsystemRefCount, and SDL_TRUE.

Referenced by SDL_QuitSubSystem().

◆ SDL_PrivateSubsystemRefCountDecr()

static void SDL_PrivateSubsystemRefCountDecr ( Uint32  subsystem)
static

Definition at line 68 of file SDL.c.

69{
70 int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
71 if (SDL_SubsystemRefCount[subsystem_index] > 0) {
72 --SDL_SubsystemRefCount[subsystem_index];
73 }
74}

References SDL_MostSignificantBitIndex32(), and SDL_SubsystemRefCount.

Referenced by SDL_QuitSubSystem().

◆ SDL_PrivateSubsystemRefCountIncr()

static void SDL_PrivateSubsystemRefCountIncr ( Uint32  subsystem)
static

Definition at line 59 of file SDL.c.

60{
61 int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
62 SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255);
63 ++SDL_SubsystemRefCount[subsystem_index];
64}

References SDL_assert, SDL_MostSignificantBitIndex32(), and SDL_SubsystemRefCount.

Referenced by SDL_InitSubSystem().

◆ SDL_Quit()

void SDL_Quit ( void  )

This function cleans up all initialized subsystems. You should call it upon all exit conditions.

Definition at line 374 of file SDL.c.

375{
377
378 /* Quit all subsystems */
379#if SDL_VIDEO_DRIVER_WINDOWS
380 SDL_HelperWindowDestroy();
381#endif
383
384#if !SDL_TIMERS_DISABLED
386#endif
387
391
392 /* Now that every subsystem has been quit, we reset the subsystem refcount
393 * and the list of initialized subsystems.
394 */
396
398}
void SDL_QuitSubSystem(Uint32 flags)
Definition: SDL.c:259
#define SDL_INIT_EVERYTHING
Definition: SDL.h:86
void SDL_AssertionsQuit(void)
Definition: SDL_assert.c:404
#define SDL_memset
#define SDL_LogResetPriorities
#define SDL_ClearHints
void SDL_TicksQuit(void)

References SDL_AssertionsQuit(), SDL_bInMainQuit, SDL_ClearHints, SDL_FALSE, SDL_INIT_EVERYTHING, SDL_LogResetPriorities, SDL_memset, SDL_QuitSubSystem(), SDL_SubsystemRefCount, SDL_TicksQuit(), and SDL_TRUE.

◆ SDL_QuitSubSystem()

void SDL_QuitSubSystem ( Uint32  flags)

This function cleans up specific SDL subsystems

Definition at line 259 of file SDL.c.

260{
261 /* Shut down requested initialized subsystems */
262#if !SDL_SENSOR_DISABLED
263 if ((flags & SDL_INIT_SENSOR)) {
266 }
268 }
269#endif
270
271#if !SDL_JOYSTICK_DISABLED
273 /* game controller implies joystick */
275
278 }
280 }
281
282 if ((flags & SDL_INIT_JOYSTICK)) {
283 /* joystick implies events */
285
288 }
290 }
291#endif
292
293#if !SDL_HAPTIC_DISABLED
294 if ((flags & SDL_INIT_HAPTIC)) {
297 }
299 }
300#endif
301
302#if !SDL_AUDIO_DISABLED
303 if ((flags & SDL_INIT_AUDIO)) {
306 }
308 }
309#endif
310
311#if !SDL_VIDEO_DISABLED
312 if ((flags & SDL_INIT_VIDEO)) {
313 /* video implies events */
315
318 }
320 }
321#endif
322
323#if !SDL_TIMERS_DISABLED
324 if ((flags & SDL_INIT_TIMER)) {
327 }
329 }
330#endif
331
332#if !SDL_EVENTS_DISABLED
333 if ((flags & SDL_INIT_EVENTS)) {
336 }
338 }
339#endif
340}
static SDL_bool SDL_PrivateShouldQuitSubsystem(Uint32 subsystem)
Definition: SDL.c:87
static void SDL_PrivateSubsystemRefCountDecr(Uint32 subsystem)
Definition: SDL.c:68
#define SDL_AudioQuit
#define SDL_VideoQuit
void SDL_EventsQuit(void)
Definition: SDL_events.c:1016
void SDL_GameControllerQuit(void)
void SDL_HapticQuit(void)
Definition: SDL_haptic.c:394
void SDL_JoystickQuit(void)
Definition: SDL_joystick.c:697
void SDL_SensorQuit(void)
Definition: SDL_sensor.c:440
void SDL_TimerQuit(void)
Definition: SDL_timer.c:239

References SDL_AudioQuit, SDL_EventsQuit(), SDL_GameControllerQuit(), SDL_HapticQuit(), SDL_INIT_AUDIO, SDL_INIT_EVENTS, SDL_INIT_GAMECONTROLLER, SDL_INIT_HAPTIC, SDL_INIT_JOYSTICK, SDL_INIT_SENSOR, SDL_INIT_TIMER, SDL_INIT_VIDEO, SDL_JoystickQuit(), SDL_PrivateShouldQuitSubsystem(), SDL_PrivateSubsystemRefCountDecr(), SDL_SensorQuit(), SDL_TimerQuit(), and SDL_VideoQuit.

Referenced by SDL_Quit().

◆ SDL_SetMainReady()

void SDL_SetMainReady ( void  )

This is called by the real SDL main function to let the rest of the library know that initialization was done properly.

Calling this yourself without knowing what you're doing can cause crashes and hard to diagnose problems with your application.

Definition at line 100 of file SDL.c.

101{
103}

References SDL_MainIsReady, and SDL_TRUE.

◆ SDL_WasInit()

Uint32 SDL_WasInit ( Uint32  flags)

This function returns a mask of the specified subsystems which have previously been initialized.

If flags is 0, it returns a mask of all initialized subsystems.

Definition at line 343 of file SDL.c.

344{
345 int i;
346 int num_subsystems = SDL_arraysize(SDL_SubsystemRefCount);
347 Uint32 initialized = 0;
348
349 /* Fast path for checking one flag */
351 int subsystem_index = SDL_MostSignificantBitIndex32(flags);
352 return SDL_SubsystemRefCount[subsystem_index] ? flags : 0;
353 }
354
355 if (!flags) {
357 }
358
359 num_subsystems = SDL_min(num_subsystems, SDL_MostSignificantBitIndex32(flags) + 1);
360
361 /* Iterate over each bit in flags, and check the matching subsystem. */
362 for (i = 0; i < num_subsystems; ++i) {
363 if ((flags & 1) && SDL_SubsystemRefCount[i] > 0) {
364 initialized |= (1 << i);
365 }
366
367 flags >>= 1;
368 }
369
370 return initialized;
371}
SDL_FORCE_INLINE SDL_bool SDL_HasExactlyOneBitSet32(Uint32 x)
Definition: SDL_bits.h:105
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:115
uint32_t Uint32
Definition: SDL_stdinc.h:203
#define SDL_min(x, y)
Definition: SDL_stdinc.h:406
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50

References i, SDL_arraysize, SDL_HasExactlyOneBitSet32(), SDL_INIT_EVERYTHING, SDL_min, SDL_MostSignificantBitIndex32(), and SDL_SubsystemRefCount.

Variable Documentation

◆ SDL_bInMainQuit

SDL_bool SDL_bInMainQuit = SDL_FALSE
static

Definition at line 54 of file SDL.c.

Referenced by SDL_PrivateShouldQuitSubsystem(), and SDL_Quit().

◆ SDL_MainIsReady

SDL_bool SDL_MainIsReady = SDL_TRUE
static

Definition at line 52 of file SDL.c.

Referenced by SDL_InitSubSystem(), and SDL_SetMainReady().

◆ SDL_SubsystemRefCount