Setting up SDL3 in Visual Studio 2022 using CMake
- Install SDL3 using this guide: How to install SDL 3.2.4 using CMake and Visual Studio 2022 for Desktop
- Run Visual Studio
- Click on the "Create a new project" button:
- Click on the "CMake Project" and click on the "Next" button:
- Type a project name, set a project location, check the "Place solution and project in the same direcory" checkbox, and click on the "Create" button:
- Wait a few seconds for "Ready" in the left bottom corner:
- Run the project on the empty green triangle (or Ctrl+F5):
- The program prints "Hello CMake" to the console:
- Double click on the "CMakePresets.json" file:
- Add the "SDL3_DIR" variable with the following "value" and "type" for "Debug" here:
{
"name": "x64-debug",
"displayName": "x64 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"SDL3_DIR": {
"value": "E:/libs/sdl-3.2.4-msvc/win/debug/cmake",
"type": "PATH"
}
}
},
Make the same for "Release":
{
"name": "x64-release",
"displayName": "x64 Release",
"inherits": "x64-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"SDL3": {
"value": "E:/libs/sdl-3.2.4-msvc/win/release/cmake",
"type": "PATH"
}
}
},
Double click on the "CMakeLists.txt" file:
Add the following code to the "CMakeLists.txt" file:
find_package(SDL3)
target_link_libraries(hello-sdl3-cpp PRIVATE SDL3::SDL3)
Double click on the "hello-sdl3-cpp.cpp" file:
Replace the "hello-sdl3-cpp.cpp" file content with with one:
#define SDL_MAIN_USE_CALLBACKS 1
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
static SDL_Window* window = NULL;
static SDL_Renderer* renderer = NULL;
SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[])
{
if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
if (!SDL_CreateWindowAndRenderer("Example",
400, 300, 0, &window, &renderer))
{
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_SetRenderVSync(renderer, 1);
return SDL_APP_CONTINUE;
}
SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event)
{
if (event->type == SDL_EVENT_QUIT) {
return SDL_APP_SUCCESS;
}
return SDL_APP_CONTINUE;
}
SDL_AppResult SDL_AppIterate(void* appstate)
{
SDL_SetRenderDrawColor(renderer, 33, 100, 33, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
return SDL_APP_CONTINUE;
}
void SDL_AppQuit(void* appstate, SDL_AppResult result)
{
}
Before you run the application you should add the following path to the PATH variable: E:/libs/sdl-3.2.4-msvc/win/debug/bin because SDL3.dll is required to run EXE
Press Ctrl+F5 to run the application
You see the result:
Official examples to run and study: https://github.com/libsdl-org/SDL/tree/main/examples/renderer