This tutorial will teach you how to program your own games using
SDL2! This first part will just teach you how to set up
SDL2. Don’t worry, it’s very easy. And when you’re done, you can jump ahead to part 2 where we will get something on screen.
This series is a little fast-paced and is mostly aimed at people who knows the basics of programming in
C++, but I will try to explain everything. The series will also explain the various types and functions in
SDL2, what they do, and what they are for. By the end of the series, you will have a good understanding of
During the series we will be making several small games, but I recommend that you play around with the code and have fun. The best way of learning is to experiment.
So what is SDL2?
SDL2is a cross-platform multimedia development library. You can use
SDL2to access your keyboard, for graphics, play sounds and for communication over Internet. SDl2 can also be used alongside with OpenGL.
SDL2can create the window for
OpenGLand be used for text rendering, multiplayer and image loading.
SDL2 brings on a set of changes from
SDL1.2. First of all,
SDL2 has support for hardware acceleration, which means it’ll be very fast compared to
SDL1.2. It also has an improved rendering structure with an object representing the window and an object for dealing with the rendering. I will cover these in more detail in the next post. For now, let’s just install it.
SDL2can be a bit tricky, especially on some operating systems where you might have to compile it yourself ( though this is becoming more rare. )
Linux comes in all shapes and sizes. Below are guides on how to install on most distributions of Linux.
Debian ( Ubuntu, Debian, Mint )
Depnding on your distro and version, you might be able to install SDL2 using the package manager. I.e :
sudo apt-get install libsdl2
If your package manager does have
SDL2. it will be installed and you’re done. If not, you’ll have to compile
SDL2 yourself Don’t worry, it’s easy. Just follow this excellent guide :
Probably the easiest. Simply use
sudo pacman -S sdl2
And that’s all, SDL2 is installed now.
Just as easy as
sudo yum install sdl2
Note: Since I’m not running Fedora, I haven’t been able to test this. But it should work, and if it doesn’t, feel free to post a comment ( you’ll be the first, you lucky bastard! )
If you are using other distributions, there are three things you can try.
- Simply use your package manager and see if it has
lSDL2or something like that.
- Try Google! Yes, I know it’s kinda obvious, but chances are someone else has had the same issue, it’s worth a shot.
- If 1 and 2 doesn’t work, you could still try the guide for Debian. You will have to switch
sudo apt-get installwith the command for install packages on your distribution.
Windowsis a littel bit more complicated, so I made a seperate post for it. You can find it here.
This will be the shortest guide and it will only cover
homebrewsince I don’t have the access to any computer running
In terminal simply type
brew install SDL2
Note: Since I haven’t actually tested this, I can’t guarantee that it will work.
Now we come to the fun part, we get to actually use it and run or
SDL2application. Our end result isn’t terribly exiting this time, it just creates an empty window with a red background. But things will get better, I promise!
Compiling on Linux and Mac using terminal
To compile on
Linux, simply add
lSDL2to your compilation string. To compile
main.cppyou can do
clang++ main.cpp -lSDL2 -o SDLTest
If you are using
GCC, the compilation string is
g++ main.cpp -lSDL2 -o SDLTest
Mac without terminal
If you’re not using the terminal, you need to set up your
IDEto use the
SDL2libraries. This should be a simple process, check the documentation for you
IDEto find exactly how to do this. I do recommend using the terminal, though. It’s much simpler.
Compiling on Windows in VisualStudio
If you have followed my guide, you shouldn’t have to do anything in order to compile.
If you get any compile errors you need to check your include directory. Remember that you should add the folder that has the
SDL2 folder, but not the
SDL2 folder itself. This is because the example code uses
If you get linker errors, make sure you have added the folder with the
If you get runtime errors, make sure you have all the
.dll files in an appropriate directory.
To test it, simply replace the content with your main.cpp with the following code snippet. If it displays a window,
SDL2is working properly on your window. Don’t worry about the code just now, I’ll explain it in the next part.
Feel free to comment if you have anything to say or ask questions if anything is unclear. I always appreciate getting comments.You can also email me : firstname.lastname@example.org