Getting started with SDL

Screenshot of Tutorial 4

SDL (Simple DirectMedia Layer) is a C library for writing games. This blog post should help you get started with developing SDL games.

If you prefer to use Visual Studio, this article will be irrevelant to you. Follow the steps at lazyfoo.net instead. I'd suggest that you make use of property sheets. That way you'll need to do the configuration only once.

Setting up your development environment

  1. Setup clang (or gcc if you don't like friendly error messages :p), make, etc.
  2. Install and setup your code editor (VS Code, Atom, etc.)
  3. Setup your Version Control Software (Git, mercurial, etc.)
  4. Download SDL2 and update your compiler's include/library paths.
  5. Test build some sample SDL code.

I've tested this on a few environments, here are the important steps:

OSX 10.11 - El Capitan

  1. Install the command-line developer tools, if you haven't already, using xcode-select --install and follow the steps.
  2. Install homebrew.
  3. Install SDL2 development libraries with brew install sdl2 --universal.

Ubuntu 16.04 - Xenial Xerus

  1. Install clang or g++ using sudo apt-get install clang or sudo apt-get install build-essentials
  2. Install SDL2 development libraries with sudo apt-get install libsdl2-dev

Windows

Sorry, but I just haven't been able to make the time for this. If you're on Windows 10, you should be able to use the Ubuntu 16.04 steps on Windows Subsystem for Linux (WSL).

Testing the environment

To build a program you'll need to run: clang++ filename.cpp -lSDL2 -g or g++ filename.cpp -w -lSDL2. You can then run the output with ./a.out.
While including SDL, make sure the include statement is #include <SDL2/SDL.h>.

If you're on windows, or you can't get it to work with the above steps for any reason I'd suggest you check this out at lazyfoo.net or leave a comment below. That site also has instructions on setting up IDEs.
BTW, if you're on Windows I've got a question for you: Whyyy?
I've always wanted to do that :p

To make sure your setup is working properly, clone this repository and build one of its subfolders with:

# Clone the repository
git clone https://github.com/gldraphael/LazyFoo.git
# Go to the 4th tutorial ...
cd LazyFoo/4
# ... and build it
make
# Run the executable
./a.out

The Makefile uses clang. If you want to use g++ instead, run g++ main.cpp -w -lSDL2 instead of make.

Learning SDL

There are many sites that can help you learn SDL. Just make sure the tutorials you're referring to use SDL2. SDL2 has significant changes compared to SDL 1.2 and its predecessors.

Three sites I'd recommend:

  1. LazyFoo
  2. SDL Tutorials
  3. Official SDL Documentation

When I started learning SDL in 2011, I was just starting out with C++ too, and I could follow nothing but LazyFoo back then. I still recommend LazyFoo, especially if you're new to programming.