Glusoft

Install SDL3 on Windows

SDL3 Hello World Project

The goal of this tutorial is to install SDL3 from an archive and also compile it on Windows.

Install SDL3 from an archive for Visual Studio

Download the right archive

Download SDL3

Go to the official SDL3 website and download the devel archive for visual studio, for me the latest version of the archive is called SDL3-devel-3.2.4-VC.zip

Extract the archive

Choose a folder where you want to extract the archive

Create the Hello World project

Install CMake

Next, you'll need to install the CMake software from the official website. CMake is a build system that allows you to set up up the project for multiple compilers.

Create the hello world project

Create the folder where you want to add your files, you can call it HelloSDL3.

Create the CMakeLists.txt file

You can create the CMakeList file inside the folder with this in it :

cmake_minimum_required(VERSION 3.10)
project(HelloProject)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

find_package(SDL3 REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} PRIVATE SDL3::SDL3)

Create the main file

You need to create the main file next to the camekList file with this in it:

#include <SDL3/SDL.h>
#include <iostream>

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

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

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

    SDL_Event e;
    bool quit = false;

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

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

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

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

    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);
    SDL_Quit();

    return 0;
}

Create the project with Cmake

You need to create a build folder next to the CmakeList file and then open the CMake GUI program, select the correct path for the project and click on Configure.
You should set the SDL3_DIR for me it's C:/Users/vulcain/Downloads/SDL3-3.2.4/cmake
After the path is set click on Configure again, everything should be good click on Generate you should have something like that:

CMake GUI on windows for SDL3

After the project is generated click on Open Project, visual studio should launch.

Select the HelloProject and set it as a startup project then compile the project and you should have the green rectangle displayed on the screen.

The full project

You can download the full project: SDL3_HelloProject_win.7z