Post

C++ SDL Install

Logo

This guide will provide step-by-step instructions on how to add the Simple DirectMedia Layer (SDL) library to a Visual Studio C++ project. You will learn how to download and set up SDL, configure Visual Studio to recognize the library, and integrate SDL into your project for enhanced multimedia capabilities. By the end of this guide, you will be ready to utilize SDL in your Visual Studio C++ projects.

About SDL

Simple DirectMedia Layer (SDL) is a cross-platform development library designed to provide low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D.

Download SDL

Version
  • Create a folder in the Solution directory called “ThirdParty”
    • ThirdParty will contain libraries from external developers to be used in the project
ThirdParty Folder
  • Move the downloaded .zip file into the ThirdParty folder and extract it
    • Rename the extracted folder to “SDL2” to make is easier to reference the folder
    • Once extracted, the .zip file can be deleted
Unzip

Add SDL to the Solution Project(s)

If the Solution contains multiple Projects, the following steps will need to be done for each project. This is because each project needs the path to the SDL includes and libraries.

Projects

Add SDL Include

  • Open the Project Properties that SDL will be used in
    • Right-click the Project and select Properties
Project Properties

In the Project Properties, make sure that the Configuration is set to All Configurations and Platform is set to All Platforms.

Configurations
  • Add the directory of the SDL include folder to the Additional Include Directories.
    • Additional Include Directories is located in C/C++>General.
    • Add $(SolutionDir)ThirdParty\SDL2\include
1
$(SolutionDir)ThirdParty\SDL2\include
Include
  • Once the directory is added to the list of included directories, the SDL header can be add to the files.
    • Add the following to a file #include <SDL.h>

If the SDL include directory was properly added, building the project (CTRL+B) will result in no errors.

Add SDL Library

  • Open the Project Properties that SDL will be used in
    • Right-click the Project and select Properties
Project Properties
  • Add the directory of the SDL library folder in Project Properties.
    • Additional Library Directories is located in Librarian>General or Linker>Input
    • Add $(SolutionDir)ThirdParty\SDL2\lib\$(PlatformTarget)
1
$(SolutionDir)ThirdParty\SDL2\lib\$(PlatformTarget)
Library
  • Add the SDL .lib files that the project needs to function.
    • Additional Dependencies is located in Librarian>General or Linker>General
    • Add sdl2.lib and sdl2main.lib
1
2
sdl2.lib
sdl2main.lib
Library

If the SDL library was properly added, building the project (CTRL+B) will result in no errors.

  • Create a folder in the Solution directory called “Build”.
    • The Build folder will contain the SDL dll (dynamic link library) files.
Build
  • Copy the sdl2.dll file from the ThirdParty\sdl\lib\x64 directory to the Build folder.
    • The project is a x64 project (64-bit application).
DLL DLL
  • Set the Working Directory that the project will be run from in Project Properties.
    • Working Directory is located in Debugging.
    • Add $(SolutionDir)Build
1
$(SolutionDir)Build
Working

Create SDL Window

  • In main() add the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <SDL.h>
#include <iostream>

int main(int argc, char* argv[])
{
	// initialize SDL
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		std::cerr << "Error initializing SDL: " << SDL_GetError() << std::endl;
		return 1;
	}

	// create window
	// returns pointer to window if successful or nullptr if failed
	SDL_Window* window = SDL_CreateWindow("Game Engine",
		SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
		800, 600,
		SDL_WINDOW_SHOWN);
	if (window == nullptr)
	{
		std::cerr << "Error creating SDL window: " << SDL_GetError() << std::endl;
		SDL_Quit();
		return 1;
	}

	// create renderer
	SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);

	while (true)
	{
		// clear screen
		SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
		SDL_RenderClear(renderer);

		// draw line
		SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
		SDL_RenderDrawLine(renderer, 0, 0, 800, 600);

		// show screen
		SDL_RenderPresent(renderer);
	}

	return 0;
}
This post is licensed under CC BY 4.0 by the author.