• 2D shoot 'em up
SDL2 turn-based strategy tutorial
Water Closet ported to PlayStation Vita
The Legend of Edgar 1.35
SDL2 Rogue tutorial
— 2D Shoot 'Em Up Tutorial —
This first tutorial will explain how to open a window in SDL2. Unpack the code and then type make to build. Once compiling is finished type ./shooter01 to run the code.
A 1280 x 720 window will open, coloured a light shade of blue. Close the window by clicking on the window's close button. As already said: simple.
Inspecting the code
There are several files involved in this example:
common.h - contains common headers.
We'll start by looking at defs.h.
The definitions of the screen resolution we want to use. This will also be our window size. Looking at structs.h we'll find a single object definition:
The App struct will hold references to our renderer and window that we'll set up next. So, onto initSDL in init.c:
We start by calling SDL_Init, passing over SDL_INIT_VIDEO to initialize SDL2's video subsystem. We next create our window using SDL_CreateWindow. SDL_WINDOWPOS_UNDEFINED tells SDL to let the OS position the window wherever it likes. Our width and height are defined by SCREEN_WIDTH and SCREEN_HEIGHT (found in defs.h). With this done, we next create the main renderer by calling SDL_CreateRenderer, supplying the window we just created and the render flags that we want to use (in this case SDL_RENDERER_ACCELERATED). The -1 tells SDL to use the first graphics acceleration device it finds. Unless you're trying to do something very specific, you'll want to pass -1 to this call. For both the creation of the window and renderer we'll check to see if they were successful and exit with an error message if not.
Let's now look at some basic input handling. Looking at input.c we'll find a single function: doInput.
This function simply reads all the recent inputs (keyboard, mouse, etc) from SDL's queue until there are none left. We'll only handle one right now - qutting. This event is sent when we close the SDL window by clicking on its close gadget.
Now let's look at the drawing code. Again, draw.c is very straightforward. It has just two functions:
These functions deal with preparing to render: prepareScene, and displaying it: presentScene. In prepareScene, we set the colour of SDL renderer using SDL_SetRenderDrawColor. This function takes five parameters: the pointer to the renderer that we created in init.c and the RGBA of the colour that we want to use. These values go from 0 to 255, with 0 being darkest and 255 being full. The final parameter is the alpha value. 0 is fully transparent whereas 255 is opaque. SDL_RenderPresent takes just one parameter - once again the renderer that we are using.
Finally, let's look at main.c. This is where we'll find our main function and main program loop. As ever, there's not much to it:
We first clear our App object and then call initSDL. We'll set an atexit to cleanup everything (see init.c) and then enter our main loop. For each frame we'll call prepareScene to setup rendering, collect and process the user input with doInput, display the scene with presentScene and then finally call SDL_Delay with a value of 16 (milliseconds). This wait serves to limit our loop to around 62 frames per second and also prevent the application from running at full tilt and consuming far too much CPU time.
And there you have it - a simple application for opening a window in SDL.
The source code for all parts of this tutorial (including assets) is available for purchase:
It is also available as part of the SDL2 tutorial bundle:
If you do not wish to create an itch.io account, you can also purchase the tutorial bundle using PayPal, and then download the tutorials directly from the main tutorials page.