front page placeholder1 placeholder2

10/05/2026: Introduction to SDL

(this article will only reference the latest iteration of SDL, which is SDL3.)
(this article also assumes the reader has basic c++ knowledge.)

with my own words, SDL is a cross platform interface for creating windows, receiving&handling user inputs (mouse, keyboards, touchscreen), and interacting with graphics apis (basically providing a way for a graphic api to display image on the window). it supports opengl, vulkan, directx, and metal. even old/ancient versions of directx can be used, thought sdl does not officially support it.

SDL has been available ever since the days of half-life 1, 1998, and it continues to be used frequently in engines, such as goldsrc, source engine, GZDoom, unreal engine(ever since version 1), godot, and even cryengine ! it is written almost entirely in c. not only can it be used in c++, but thanks to the community, there are existing extensions for other languages! some of which are:

* C#(SDL3-CS);
* Java(lwjgl3);
* Python(PySDL3);
* Rust;
* Pascal;
* and others (see wiki.libsdl.org/SDL3/LanguageBindings).

; though in this article, i'll only cover usage of it with C++.

jumping into how to use SDL, the first thing one should do is initialize the library. that can be done with a single function: SDL_Init(x), with x being a bitflag of one or more flags, the following being the options:

* SDL_INIT_AUDIO - initializes the SDL audio library, not necessary if you plan to use an external library like OpenAL or FMOD.

* SDL_INIT_VIDEO - initializes the SDL video library, should always be used, unless you dont want to display anything to the monitor..?
* SDL_INIT_JOYSTICK - initializes the library used for processing input of joystick devices.

* SDL_INIT_HAPTIC - initializes the library used for processing input of haptic devices. (haptic devices are devices meant to output physical feedback to the user, like vibrations)

* SDL_INIT_GAMEPAD - initializes the library used for processing input of gamepad devices. if used, automatically initializes the joystick library too (SDL_INIT_JOYSTICK).

* SDL_INIT_EVENTS - initializes the library used for processing window events. almost all the other options automatically initalize this library, with the exception of gamepad and haptic library.

* SDL_INIT_SENSOR - initializes the library used for processing sensors. (sensors such as gyros and accelerometers.)

* SDL_INIT_CAMERA - initializes the library used for processing cameras. (webcams for example.)

for this article/tutorial, we'll only initialize the video library.

so, we've initialized the sdl library, how do we create a window? believe it or not, this can also be done with a single function: SDL_CreateWindow(x), the following are the arguments of this function, in order:

* title: the name of the window.
* w: width of the window, in pixels
* h: height of the window, in pixels
* flags: the flags of the window, the following being the main options:

* SDL_WINDOW_FULLSCREEN;
* SDL_WINDOW_BORDERLESS;
* SDL_WINDOW_RESIZEABLE;
* SDL_WINDOW_ALWAYS_ON_TOP;
* SDL_WINDOW_OPENGL;
* SDL_WINDOW_VULKAN;
* SDL_WINDOW_METAL;
* ..and more. see SDL_video.h.

the options for flags are pretty self explanatory from the name, so i think no further explanation is necessary

with this function, you can already make a window pop up on your screen ! pretty easy, but how do we display anything on it ? for that we'll need to select our graphics api. SDL already has a builtin way to talk to the gpu, it is just a wrapper for existing graphics apis of course, but for simplicity, we'll use our old and dear friend OpenGL.

to initialize opengl for a SDL window, you first need to define attributes of the GL context to create, which you can do with SDL_GL_SetAttribute. you can take a look at SDL_video.h for all the options, its redundant to explain all of them here. then you simply use SDL_GL_CreateContext(x) to create a opengl context, with x being the handle to the SDL window you just made.

after initializing the opengl context, all left to do is render and display the image ! the rendering part ill explain later, this isnt a article about opengl anyways. to display the image, you require just a single function as well ! which is SDL_GL_SwapWindow(x), with x being the handle to the sdl window. why the name "swap" though? because simply put, we are swapping the displayed image with another one, think of it like a simple paper: in one side of the paper, you draw something and then display it. then, while people observe what you've drawn, you draw on the back of the paper and then display it, and so on so on.

to clean everthing up, aka shutdown, you need to first destroy the opengl context with SDL_GL_DestroyContext(x), with x being the handle to the context you received from SDL_GL_CreateContext, and afterwards destroy the window with SDL_DestroyWindow(x), with x being the handle to the SDL window.

and thats it, those are the basics for using the sdl library. the library is quite easy to learn and use, so i suggest you experiment and learn with it.