SDL2 Tutorials

Welcome!

The goal of the following tutorials is to provide you with an introduction to SDL2 in C++. It’s assumed throughout the series that you have some familiarity with C++ and are comfortable with functions, classes and memory management. If you find yourself having trouble understanding the code in the tutorials feel free to comment on the lesson and/or grab a good C++ book from this excellent list on StackOverflow.

The full source and assets for the tutorials can be found on Github. It may be helpful to check against this code to find errors, but it’s important to not copy directly as it will take away from the learning experience. If you ever have questions about the meaning or functionality of SDL features head over to the SDL documentation wiki and/or comment on the lesson page. You can also get help with SDL in the SDL Forums.

Tutorials

Postscript 1: Easy Cleanup

Fri Aug 1, 2014

In this quick postscript we’ll look into a simple way to clean up our various SDL resources with variadic templates and template specialization. This will let us clean up all our resources with a single simple call: cleanup(texA, texB, renderer, window) instead of calling all the corresponding SDL_Destroy/Free* functions, saving ourselves a lot of typing.

We’ll do this by creating a variadic function cleanup that will take the list of SDL resources to be free’d and then define specializations of it for each resource we’ll be passing, eg. for SDL_Window, SDL_Renderer, SDL_Texture and so on.

Continue

Postscript 0: Properly Finding Resource Paths

Sun Mar 16, 2014

In this short postscript we’ll learn how to make use of SDL_GetBasePath to properly resolve the path to our resource directory where we’ll be storing all the assets needed for each lesson. This approach lets us avoid issues with relative paths since it doesn’t depend on where the program working directory is set when it’s run. This functionality was introduced in SDL 2.0.1 so if you haven’t updated to the latest SDL be sure to grab that before getting started.

Continue

Lesson 0: CMake

Thu Mar 6, 2014

CMake is really useful for building the lessons since it lets us generate make files or project files for just about any platform and IDE. It also helps with resolving dependencies (such as SDL2), platform specific configurations and much much more. If you’re unfamiliar with CMake there’s a nice introduction available on their site to help you get started.

Continue

Lesson 6: True Type Fonts with SDL_ttf

Wed Dec 18, 2013

In this lesson we’ll see how to perform basic True Type font rendering with the SDL_ttf extension library. Setting up the library is identical to what we did in Lesson 3 for SDL_image, but just replace “image” with “ttf” (Windows users should also copy the included freetype dll over). So download SDL_ttf, take a peek at the documentation, and let’s get started!

Continue

Lesson 5: Clipping Sprite Sheets

Tue Aug 27, 2013

It’s common in sprite based games to use a larger image file containing many smaller images, such as the tiles for a tileset, instead of having a separate image file for each tile. This type of image is known as a sprite sheet and is very handy to work with since we don’t need to change which texture we’re drawing each time but rather just which subsection of the texture.

Continue

Lesson 4: Handling Events

Tue Aug 20, 2013

In this lesson we’ll learn the basics of reading user input with SDL, in this simple example we’ll interpret any input as the user wanting to quit our application. To read events SDL provides the SDL_Event union and functions to get events from the queue such as SDL_PollEvent. The code for this lesson is built off of the lesson 3 code, if you need that code to start from grab it on Github and let’s get started!

Continue

Lesson 3: SDL Extension Libraries

Sun Aug 18, 2013

Up until now we’ve only been using BMP images as they’re the only type supported by the base SDL library, but being restricted to using BMP images isn’t that great. Fortunately there are a set of SDL extension libraries that add useful features to SDL, such as support for a wide variety of image types through SDL_image. The other available libraries are SDL_ttf which provides TTF rendering support, SDL_net which provides low level networking and SDL_mixer which provides multi-channel audio playback.

Continue

Lesson 1: Hello World

Sat Aug 17, 2013

In this lesson we’ll learn how to open a window, create a rendering context and draw an image we’ve loaded to the screen. Grab the BMP we’ll be drawing below and save it somewhere in your project and let’s get started!

Continue

Lesson 2: Don't Put Everything in Main

Sat Aug 17, 2013

In this lesson we’ll begin organizing our texture loading and rendering code from the previous lesson by moving them out of main and placing them into some useful functions. We’ll also write a simple generic SDL error logger and learn how images are positioned and scaled when rendering with SDL.

Continue

Lesson 0: Linux Command Line

Thu Aug 15, 2013

To build the projects on Linux we’ll be using a simple makefile that will setup the include and library dependencies for us. The makefile assumes that your SDL libraries are installed under /usr/local/lib and the headers are under /usr/local/include. These are the install locations if you built the project through cmake, some more detail on building from source can be found here. If you’ve installed it through your package manager or placed the libraries and headers elsewhere you may need to change these paths to match your installation. You can also check the output of sdl2-config with the --cflags and --libs switches to locate your install, assuming you haven’t moved it.

If you’re unfamiliar with Makefiles a basic introduction can be found here.

Continue