SDL (Simple DirectMedia Layer)/Basics/Getting started

In this section, we will show you how to initialise and shutdown SDL subsystems.

You can download the source code of this section in this GitLab repository. All source code is stored in this group.

Linking SDL2 libraries
The basic functions required for SDL2 is either in the  library or   library depending on the operating system, hence the above source code uses

macro in Windows
When writing code for Windows machines, it is necessary to include

since SDL overwrites the  macro. According to SDL's [https://wiki.libsdl.org/FAQWindows#I_get_.22Undefined_reference_to_.27SDL_main.27.22_... FAQWindows]:

"You should be using instead of   even though you are creating a Windows application, because SDL provides a version of   which performs some SDL initialization before calling your main code. If for some reason you need to use, take a look at the SDL source code in   to see what kind of initialization you need to do in your   function so that SDL works properly."

Initialising SDL subsystems
When using a SDL subsystem, you must always initialise it first. In the following if-statement, we initialise the SDL video subsystem with the flag. If it succeeds, it returns, otherwise it returns a negative error code and will print information about the error using SDL_GetError through the   stream.

You can initialise multiple subsystems using the  operator:

All information about the  function subsystems, and their flags can be found in the SDL Wiki's SDL_Init page.

Shutting down SDL subsystems
To shut down the SDL subsystems, you must execute

However, this will only shut down the main subsystems; shutting down the TTF subsystem requires a different function

|.