SDL C++
Introduction
Simple DirectMedia Layer (SDL) is a free cross-platform multimedia
library that implements a single programming interface to the graphics subsystem,
audio devices and input tools for a wide range of platforms.
This library is actively used when writing cross-platform
multimedia programs (mostly games).
The operating systems officially supported are: Linux, Microsoft Windows,
Mac OS X, iOS and Android.
The SDL API is available for the languages: C, C++, C#, VB.NET, D, Ada, Vala, Eiffel, Haskell,
Erlang, Euphoria, Java, Lisp, Lua, ML, Pascal, Perl, PHP, Pike, PureBasic, Python, Go and Ruby.
The core of SDL contains a basic, very limited range of capabilities.
Additional functionality is provided by extension libraries,
which are usually included in the SDL distribution.
Since version 2.0, SDL is distributed under the terms of the zlib licence;
before that the GNU LGPL licence was used, which allowed the library to be freely used
in commercial programs provided that it is used in them
as a dynamically linked library.
Downloading the library
SDL2 can be downloaded from the site
libsdl.org
You need to download from the Development Libraries subsection
as of April 2020 the version was 2.0.12
so I chose
SDL2-devel-2.0.12-VC.zip (Visual C++ 32/64-bit)
After downloading, create a folder Development on drive C, unpack
the archive there and rename the folder to SDL2
Creating a project
After that, in Visual Studio create an empty C++ project. A console application for win32, for example. And in this project create the file main.cpp
Then right-click on the project name and go to Properties at the very bottom of the context menu. Or press ALT + Enter
C/C++ General - include
C/C++ → General → Additional Include Directories
Linker General - lib
Linker → General → Additional Library Directories → New Line
Browse (…)
Go to the SDL2 folder, then to the lib folder and choose x86 or x64
Go there and click Select Folder
Linker Input
Linker → Input → Additional Dependencies → <Edit…>
Enter in two lines:
SDL2.lib
SDL2main.lib
And click OK
SDL2.dll
In the SDL2 folder go to lib → x86 (or x64 if you chose x64 in the previous steps)
Copy the file SDL2.dll
In Visual Studio right-click on the project name and in the context menu choose Open Folder in File Explorer
Paste the copied SDL2.dll into the folder that opens
Video
Drawing an empty window in SDL
#include <SDL.h>
#include <iostream>
SDL_Window* g_pWindow = 0;
SDL_Renderer* g_pRenderer = 0;
bool g_bRunning = false; // this will create a loop
bool init(const char* title, int xpos, int ypos, int
height, int width, int flags)
{
// initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) >= 0)
{
// if succeeded create our window
g_pWindow = SDL_CreateWindow(title, xpos, ypos,
height, width, flags);
// if the window creation succeeded create our renderer
if (g_pWindow != 0)
{
g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
}
}
else
{
return false; // sdl could not initialize
}
return true;
}
void render()
{
// set to dark red
SDL_SetRenderDrawColor(g_pRenderer, 128, 0, 20, 255);
// clear the window to black
SDL_RenderClear(g_pRenderer);
// show the window
SDL_RenderPresent(g_pRenderer);
}
int main(int argc, char* argv[])
{
if (init("Chapter 1: Setting up SDL",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640,
480, SDL_WINDOW_SHOWN))
{
g_bRunning = true;
}
else
{
return 1; // something is wrong
}
while (g_bRunning)
{
render();
}
// clean up SDL
SDL_Quit();
return 0;
}
The code on this page was not run: it needs the SDL2 headers and a display, neither of which this environment has.
Article author: Andrei Olegovich