First Steps Feature Image SDL3

First Steps with SDL3


Let’s jump right into our first SDL3 application. Every SDL3 application needs to include the SDL3 unit (obviously), because it is that unit which provides all the SDL3 functions and types we use for development.

program FirstSteps;

uses SDL3;

begin

  //initilization of video subsystem
  if not SDL_Init(SDL_INIT_VIDEO) then Exit;

    {your SDL2 application/game}
    SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 'First Steps', 'It works!', nil);

  //shutting down video subsystem
  SDL_Quit;

end.

Initialization and Quitting of SDL3

The first function we use is called SDL_Init(subsystem flags). It is used to initialize subsystems of SDL3 and must be called always before using any SDL3 features. These subsystems are:

  • SDL_INIT_AUDIO
  • SDL_INIT_VIDEO
  • SDL_INIT_JOYSTICK
  • SDL_INIT_HAPTIC
  • SDL_INIT_GAMEPAD
  • SDL_INIT_EVENTS
  • SDL_INIT_SENSOR
  • SDL_INIT_CAMERA

The names are self-explanatory, so if you need video functionality you set SDL_INIT_VIDEO. If you need audio functionality, you set SDL_INIT_AUDIO. You can combine subsystems by using the logical or. For video and audio functionality you use SDL_INIT_VIDEO or SDL_INIT_AUDIO.

By the way, with the exception of the haptic flag, all flags do imply the event system, so you don’t need to load it.

You may have wondered why the SDL_Init function is buried in the if-statement. Most functions in SDL3 return a boolean return value which indicates if its execution was successful. This allows for efficient error checking as seen here. If the SDL_Init is returning true the applications proceeds, if not it would exit immediately.

When quitting your application, use the SDL_Quit function.

A simple message box, because we can!

If you see a message box like this…

… then everything works alright. Otherwise, something is is wrong. Most probably the SDL3 library hasn’t been found.

And here you see one great benefit of SDL3. The message box is generated by exactly one simple call to the SDL_ShowSimpleMessageBox(message box flags, title, message, window) function. To realize the same, not to mention on different platforms in a platform-independent way, is whole other question and takes much greater effort.

SDL3 simplifies your (game) development endeavor by a giant margin in many respects.

Previous Chapter | Next Chapter

Leave a Reply

Your email address will not be published. Required fields are marked *