DEV Community

Noah11012
Noah11012

Posted on • Updated on

Using SDL2 in C (and with C++, too)

What is this?

I'm asking the same question right now to myself. This is the first article I've written on Dev.to so I'm new to this and I'm not sure how this series will be structured. Of course, I could preplan this before I made this series, however, I want to start writing articles and start getting a feel for this.

But back to the question.

What is this?

This is a series that teaches using SDL2 in C, and perhaps in C++ also. This series isn't designed to make you understand everything about SDL2. Instead, it will (hopefully) teach you enough where you can start writing applications/games in SDL2.

What is SDL2 and should I care?

For starters, SDL is an acronym for Simple Directmedia Layer and is a cross-platform library designed to give low-level access to audio, keyword, mouse, graphics and more with a simple to use C API.

Why should you care? Well, because many applications such as playback software, emulators, and popular games from Valve use SDL2. SDL2 depending on your platform uses OpenGL or Direct3D. Instead of using Direct3D or OpenGL directly you can have a nice layer atop to help avoid touching Direct3D or OpenGL.

Getting started

Windows

For Windows users, go to the SDL2 website and head to the download page. Under Development Libraries choose SDL2-devel-[VERSION NUMBER]-VC.zip if you're using Visual Studio which I assume most C/C++ developers on Windows use. If you use MinGW, choose the one underneath for VS.

Unzip and you will see several folders with the names docs, include, and lib. include contains the header files which we will be needing soon. Create a simple C++ VS project and copy/move the include directory in your project folder. Same thing with lib.

Now in lib there exists two folders: x64 and x86.

In x86 contains the 32-bit version of the library whereas in x64 contains the 64-bit version. The one you want to use depends on what you're targeting for. If unsure, use the 32-bit version.

Unfortunately, this is as far as I can go. I quit using Windows a while back and have been using Linux ever since. You'll have to configure Visual Studio or whatever IDE you are using to search for the include directory and search for the lib/x64 or lib/x86 directory depending on what architecture you are targeting and linking against the libraries within.

Linux

For Ubuntu and its derivatives can use the following command to install the development version of SDL2:

sudo apt-get install libsdl2-dev

For Manjaro users and other similar Linux distributions like Antegros can use the following command:

sudo pacman -S sdl2

What will be next?

In the next part, we'll get a window up and running and get an image loaded onto the screen.

Top comments (0)