Game Development with SDL2 Part 1: Creating a Window

Guest Posts Bilal Cheema

Author: Bilal Cheema

This is the first post of the series.

In this post, we are going to discuss all about game development in C++ along with a very popular multimedia library called Simple DirectMedia Layer or SDL (version 2.0+). I am assuming that you know the basics of C++ and a bit about linking libraries. If you don't know about C++, its syntax and all such things, you should know C++ first to step ahead. My recommendation for learning C++ is to follow a free course offered by John Purcell here.

Setting up SDL

If you already know C++, then you may want to set up SDL2. I am not going to tell you the whole procedure but you can find it right here. Setting up a library is a hard process but once it is done, you should (and will) never be worried about it.

Basic Concepts

SDL2 Logo

There are few concepts of SDL. The window that is formed by using SDL acts like a graph or a two dimensional Cartesian coordinate system with 0 starting from the top-left of the screen for both horizontal and vertical axis (or x and y-axis in short). SDL make things easier by having objects like SDL_Window and SDL_Renderer. The first one sets a window on the screen and the second one draws on the window.

"Hello World" in SDL

A very brief SDL code is given below. This code shows a green color filled screen for just 3 seconds. For the sake of understanding, the code contains a lot of comments (even more than the code!)

#include <SDL.h>

#undef main //For Visual Studio 2015 Users (I am one of them)

//For SDL, you should have the following main method:
int main(int argc, char** args)
{
    //Initialize all the systems of SDL
    SDL_Init(SDL_INIT_EVERYTHING);

    //Create a window with a title, "Getting Started", in the centre
    //(or undefined x and y positions), with dimensions of 800 px width
    //and 600 px height and force it to be shown on screen
    SDL_Window* window = SDL_CreateWindow("Getting Started", SDL_WINDOWPOS_UNDEFINED,
        SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);

    //Create a renderer for the window created above, with the first display driver present
    //and with no additional settings
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);

    //Set the draw color of renderer to green
    SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);

    //Clear the renderer with the draw color
    SDL_RenderClear(renderer);

    //Update the renderer which will show the renderer cleared by the draw color which is green
    SDL_RenderPresent(renderer);

    //Pause for 3 seconds (or 3000 milliseconds)
    SDL_Delay(3000);
    
    //Destroy the renderer created above
    SDL_DestroyRenderer(renderer);

    //Destroy the window created above
    SDL_DestroyWindow(window);

    //Close all the systems of SDL initialized at the top
    SDL_Quit();

    return 0;
}

Concept behind the Code

There are still few things that may have confused you. The SDL_CreateRenderer() method have two parameters quite unusual. The one filled with '-1', is actually, selects the display driver. '-1' means to select the first display driver found on the computer. We will always use '-1' for this. The one filled with '0' is the additional settings of the renderer. By zero, we mean no additional settings. SDL_WINDOW_SHOWN is also an additional setting for the window which forces the window to be shown on the screen.

SDL_SetRenderDrawColor()

SDL_SetRenderDrawColor() method sets the draw color of the renderer. The first parameter is that of SDL_Renderer* which takes the renderer of which the draw color is to be setted. The second, third, fourth and fifth parameter is for setting colour. The second is for setting red component of the draw color, third for blue component and fourth, green. The set range is from 0 to 255. The last one sets the alpha value or transparency of the color. It may not effect the screen draw color.

NOTE:The order of destroying things is important! Generally, the trick for the order of destroying objects is to have the inverse order of creating objects. This is done to avoid memory issues.

All the other code is as clear and self-explanatory as it looks! See! SDL is very simple. But it is just a start. For more programming stuff, visit my blog: bacprogramming.wordpress.com

.

And as always, Happy Coding!