Post

C++ SDL Install

Logo

This guide provides step-by-step instructions to add the Simple DirectMedia Layer (SDL) library to a Visual Studio C++ project. You’ll learn how to download and set up SDL, configure Visual Studio, and integrate SDL into your project. By the end, you’ll be ready to use SDL in your Visual Studio C++ projects.

About SDL

Simple DirectMedia Layer (SDL) is a cross-platform development library that provides low-level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D. SDL is distributed as a Dynamic Link Library (DLL); the .dll file must be included to run your application.

Download SDL

  • Go to the SDL website: https://www.libsdl.org/
    • Navigate to Download SDL Releases
    • Download the latest SDL3-devel-####-VC.zip
Version
  • Create a folder named ThirdParty in your Source directory. This folder will store external libraries used in your project.
ThirdParty Folder
  • Move the downloaded .zip file into the ThirdParty folder and extract it.
    • Rename the extracted folder to “SDL3” for easier reference.
    • After extraction, delete the .zip file.
Unzip

Add SDL to the Solution Project(s)

Add SDL Include

  • Open the Project Properties for the project that will use SDL (e.g., Engine).
    • Right-click the project and select Properties.
Project Properties

In Project Properties, ensure Configuration is set to All Configurations and Platform is set to All Platforms.

Configurations
  • Add the SDL include directory to Additional Include Directories (C/C++ > General):
    • $(SolutionDir)Source\ThirdParty\SDL3\include
1
$(SolutionDir)Source\ThirdParty\SDL3\include
Include

If your solution contains multiple projects, repeat the Additional Include Directories step for each project that uses SDL. This steps needs to be done on the Game project also.

Add SDL Library

The SDL library setup only applies to the Engine project.

  • Add the SDL library directory in Project Properties (Librarian > General or Linker > Input):
    • $(SolutionDir)Source\ThirdParty\SDL3\lib\$(PlatformTarget)
1
$(SolutionDir)Source\ThirdParty\SDL3\lib\$(PlatformTarget)
Library
  • Add the required SDL .lib files to Additional Dependencies (Librarian > General or Linker > General):
    • sdl3.lib
1
sdl3.lib
Library

If the SDL library is set up correctly, building the project (CTRL+B) should result in no errors.

  • Create a folder named Build in your solution directory if it doesn’t exist. This folder will store the SDL DLL files.
Build
  • Copy sdl3.dll from ThirdParty\SDL3\lib\x64 to the Build folder. For 64-bit projects, use the x64 version of the DLL.
DLL DLL
  • In the Game project properties, set the Working Directory (Debugging) to the Build folder:
    • $(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
45
46
47
48
49
#include <SDL3/SDL.h>
#include <iostream>

int main(int argc, char* argv[]) {
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window* window = SDL_CreateWindow("SDL3 Project", 1280, 1024, 0);
    if (window == nullptr) {
        std::cerr << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }

    SDL_Renderer* renderer = SDL_CreateRenderer(window, NULL);
    if (renderer == nullptr) {
        std::cerr << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }

    SDL_Event e;
    bool quit = false;

    // Define a rectangle
    SDL_FRect greenSquare{ 270, 190, 200, 200 };

    while (!quit) {
        while (SDL_PollEvent(&e)) {
            if (e.type == SDL_EVENT_QUIT) {
                quit = true;
            }
        }

        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // Set render draw color to black
        SDL_RenderClear(renderer); // Clear the renderer

        SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); // Set render draw color to green
        SDL_RenderFillRect(renderer, &greenSquare); // Render the rectangle

        SDL_RenderPresent(renderer); // Render the screen
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

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