Tuesday, March 19, 2019

Getting started with SDL2

The Simple Directmedia Layer (SDL) is a mature framework that has been around for quite some time. Though mainly targeted at writing games it is good for any program that requires the creation of arbitrary graphics screens, like for example retro computer emulators. One of the more interesting aspects of SDL is that it is cross-platform. It runs fine on your PC, Mac or Linux computer. And this includes the ever so popular Raspberry Pi.
There are many excellent guides for setting up SDL on every platform.
TwinkleBearDev SDL2 Tutorials
Parallel Realities Game Tutorials
Lazy Foo Productions 

Visual Studio

There is a good step-by step guide on WikiHow to configure Visual Studio. It however is already outdated since the easiest way to set it up today is by using the Nuget package manager. One of the major benefits of going this way is that you can now simply switch between 32 and 64 bit builds by just selecting the target platform in the VS project type dropdown. So : Lets Get Started !

First use 'File->New->Project' to create a 'Visual C++' -> 'Empty Project'.

Right click the 'Project' and select 'Manage NuGet packages'. In the NuGet Package manager go to the 'Browse' tab, and search for SDL2. Select the latest SDL2 package and click install. 
Then scroll down an also install the 'SDL2_image', 'SDL2_ttf' and 'SDL2_mixer' packages for image handling, font management and sound. This basically configures your project completely for use with the SDL2 framework. 
The only thing you have to do manually is selecting the SubSystem target. To do this open up the System page and choose either Console or Windows from the drop down. If you choose Windows you won’t get a console window that opens up with stdout, if you choose Console you will. My advice is to choose Console, as the console window is really handy as a debugging tool.
Note that when you switch your target from 32 to 64 bit or vice-versa you will again have to choose the SubSystem target or your build will fail because it 'Cannot find an entry point for main()'



You are now ready to start your first SDL project. 
Right click the 'Source Files' folder and select 'Add ->; New Item...'. Select a 'C++ File', give it meaningful name if you want, and click OK. Open the empty C++ file, and copy the following:
#include "sdl.h"
#include "sdl_image.h"
#include <iostream>
#include <stdio>


const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

int main(int argc, char* args[]) {
    SDL_Window* window = NULL;
    SDL_Surface* screenSurface = NULL;
    SDL_Renderer *renderer=NULL;
    SDL_Texture *texture=NULL;
    SDL_Event event;
    SDL_Rect r;

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("Could not initialize SDL. SDL_Error: %s\n", SDL_GetError());
    }
    else {
        window = SDL_CreateWindow("SDL Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if (window == NULL) {
            printf("Cannot create window. SDL_Error: %s\n", SDL_GetError());
        }
        else {
            SDL_Rect arect;
            screenSurface = SDL_GetWindowSurface(window);
            SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0x00, 0x00, 0x44));
            arect.x = 64; arect.y = 64; arect.w = SCREEN_WIDTH - 128; arect.h = SCREEN_HEIGHT - 128;
            SDL_FillRect(screenSurface, &arect, SDL_MapRGB(screenSurface->format, 0x00, 0x77, 0x77));
            SDL_UpdateWindowSurface(window);
            SDL_Delay(500);
        }
    }

}


Run the program, and if all is well it will show a window with a blue border and green centre.