Init, errors & main callbacksSDL 3.2
SDL_Init
Starts the parts of SDL you ask for (video, audio, gamepads…). The first SDL call of a program.
Signature
bool SDL_Init(SDL_InitFlags flags);
How it works
SDL is split into subsystems. You start only the ones you use by combining flags with a bitwise or (|). Some flags bring others along: video, audio and joystick all start the event system too.
In SDL3 it returns a bool: true means success. (SDL2 returned 0 for success, so if (SDL_Init(...) < 0) from old tutorials is now wrong.)
Parameters
flagsSDL_InitFlags
The subsystems to start, combined with |.
Common values
- SDL_INIT_VIDEO
- Windows, renderer, OpenGL/Vulkan/GPU, keyboard and mouse. Start it on the main thread.
- SDL_INIT_AUDIO
- Audio playback and recording.
- SDL_INIT_GAMEPAD
- Gamepads with a standard layout (also starts SDL_INIT_JOYSTICK).
- SDL_INIT_JOYSTICK
- Raw joysticks: any axes and buttons, no standard names.
- SDL_INIT_HAPTIC
- Force feedback devices.
- SDL_INIT_EVENTS
- Only the event queue (started by most other flags anyway).
- SDL_INIT_SENSOR / SDL_INIT_CAMERA
- Accelerometer/gyroscope, and webcams.
Return value
true on success, false on failure. On failure, SDL_GetError says why.
Tips & pitfalls
There is no SDL_INIT_TIMER in SDL3: the clock (
SDL_GetTicks) always works.Pair it with one
SDL_Quitat the end of the program.
Example
example.cpp
1if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMEPAD)) {
2 SDL_Log("SDL_Init failed: %s", SDL_GetError());
3 return 1;
4}
5// ...
6SDL_Quit();