Introduction
This tutorial will teach you how to program your own games using C++ and 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 SDL2.
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?
SDL is a cross-platform multimedia development library. You can use SDL to access your keyboard, for graphics, play sounds and for communication over Internet. SDl2 can also be used alongside with SDL2. You can also, for instance, use SDL2 to create the window and for window and OpenGL for rendering ( I'll probably go into that in a later guide. )
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.
Installation
Installing SDL2 can be a bit tricky, especially on some operating systems where you might have to compile it yourself ( though this is becoming more and more rare. )
Linux
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 :
Installing SDL2 on Linux
Arch
Probably the easiest. Simply use pacman :
sudo pacman -S sdl2
And that's all, SDL2 is installed now.Fedora
Just as easy as Arch :
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! )Other distros?
If you are using other distributions, there are three things you can try.
- Simply use your package manager and see if it has SDL2, libsdl2 or lSDL2 or 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 install
with the command for install packages on your distribution.
Windows
Note: the names and menus might mentioned in this guide might be slightly wrong since I don't have access to a Windows machine at the moment.
Installing on Windows is actually a bit more elaborative than installing on Linux. The upside is that you can set it up in the same way on any version of Linux! This guide assumes you have any version of VisualStudio installed.
1. Download SDL2 development libraries
Go to the SDL2 download page.
And download
SDL2-devel-2.x.x-VC.zip (Visual C++ 32/64-bit)
Once you have downloaded the .zip file, you can unzip it anywhere you like. 2a. Moving development files
1
The .lib and .h files are used during linking and compiling. This means that VisualStudio needs to know where you have placed them. I suggest you copy the files from where you have unzipped them and move them to a place you will remember. For instance
for .lib files.C:/Libraries/lib
/
SDL2/
And
for header files.C:/Libraries/include
/
SDL2/
Now that you have the development files in a secure location, it's time to move on to the next step.
2b Specifying development file locations in VisualStudio
As I said earlier, VisualStudio needs to know where you .lib and .h files are.
Open
Tools -> Options -> Projects and Settings -> VC++ Directories -> Include Directories
Under here, add the folder containing the folder you copied your project to. I.e C:/Libraries/include/
and not C:/Libraries/include/
SDL2/
Under
Include directories
you should seeLibrary Directories
Here you need to add the library (.lib ) files. This time though, you add the directory where the .lib files are. So in my example, it'll be
C:/Libraries/lib
/
SDL2/
And finally, navigate to
Input
and add SDL2.lb
and SDL2main.lib
Now you should be able to build SDL2 applications! But we're not quite done yet.
3 Copying .dll files
The .dll files are needed to run SDL2 applications. When it comes to placing them you have two options :
- In the project directory ( same folder as your .exe file. ) This means you have to copy them every time you create a new project.
- In your Windows system directories.
- In x86 this directory is C:/Windows/system32/
- In x64 this directory is C:/Windows/SysWOW64/ though you might have to place them in System32/ as well.
And you're done. You can now create your own SDL2 applications in VisualStudio.
Mac
This will be the shortest guide and it will only cover
homebrew
since I don't have the access to any computer running OS X.In terminal simply type
brew install SDL2
Note: Since I haven't actually tested this, I can't guarantee that it will work.
Testing it
Now we come to the fun part, we get to actually use it and run or SDL2 application. 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
lSDL2
to your compilation string. To compile main.cpp you can doclang++ main.cpp -lSDL2 -o SDLTest
If you are using GCC, the compilation string is
g++ main.cpp -lSDL2 -o SDLTest
Compiling on Linux and Mac without terminal
If you're not using the terminal, you need to set up your IDE to use the SDL2 libraries. This should be a simple process, check the documentation for you IDE to 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
#include
If you get linker errors, make sure you have added the folder with the SDL2.lib file.
If you get runtime errors, make sure you have all the .dll files in an appropriate directory.
Sample code
To test it, simply replace the content with your main.cpp with the following code snippet. If it displays a window, SDL2 is 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 have any issues/questions. I always appreciate getting comments.
For a full list of my tutorials / posts, click here.
When I try to run the example code I get the following error message:
SvarSlettundefined reference to `SDL_CreateWindow'
To run in linux:
Slettrun this first: $ g++ test.cpp -lSDL2 -o SDLTest
then this: $ ./SDLTest
yo error y los cambie nullptr por NULL... y funciono.
SvarSlettnose porque escribio nullptr?
Muy buen tutorial, gracias
SvarSlett