A "port" of SDL3 in C++.
This basically wrap SDL3 naturally OO looking concepts into proper C++ classes and objects, trying to as straightforward as possible. It was inspired on SDL2pp, with the addition of flexible memory management and wrappers for string and callbacks.
- You have 3 options to add SDL3pp to your project:
- Download the single header (as zip or tarball) and add to your project;
- You can also download the the latest release (also available as tarball) then build and install the project;
- If you use CMake, you can also set up FetchContent to automatically do that for you;
- See the generated docs and particularly the API reference;
- See Example and Examples directory.
- Be header only, we are only wrapping SDL here;
- Mostly wrap the naturally OO-looking API into actual OO C++ constructs;
- Put everything into a
SDLnamespace instead of prefixes; - Interfaces should accept both C structs and the C++ wraps, so you can adapt a codebase gradually or just choose to use only what you deem necessary.
- Flexible, while we use RAII idiom by default, you have the choice to not use it and, for example, manage memory yourself.
#include <iostream>
#include <SDL3pp/SDL3pp.h>
#include <SDL3pp/SDL3pp_main.h>
using namespace std::chrono_literals;
int main(int argc, char** argv)
{
SDL::Init(SDL::INIT_VIDEO);
constexpr SDL::Point WINDOW_SZ = {400, 400};
auto [window, renderer] = SDL::CreateWindowAndRenderer("Test", WINDOW_SZ);
SDL::Texture characterTexture{
renderer, std::format("{}../assets/smiley.png", SDL::GetBasePath())};
SDL::FRect characterRect(SDL::FPoint(WINDOW_SZ) / 2 - SDL::FPoint{64, 64},
{128, 128});
bool running = true;
while (running) {
while (auto ev = SDL::PollEvent()) {
if (ev->type == SDL::EVENT_QUIT) { running = false; }
}
renderer.SetDrawColorFloat({.5f, .5f, .5f, 1.f});
renderer.RenderClear();
renderer.SetDrawColorFloat({0.f, 0.725f, 0.f, 1.f});
renderer.RenderFillRect(SDL::FRect{10, 10, 380, 380});
renderer.RenderTexture(characterTexture, {}, characterRect);
renderer.Present();
SDL::Delay(1ns);
}
return 0;
}If aren't using the "full" version, ensure you have the necessary dependencies installed:
- SDL3
- SDL3_image (optional)
- SDL3_mixer (optional)
- SDL3_ttf (optional)
Assuming you are on the source dir, you can build with:
cmake -S . -B build
cmake --build buildIf CMake can't find the dependencies, you might have to pass them through cmake-gui or using the following:
cmake -DSDL3_DIR=path-to-SDL3-dir build
cmake -DSDL3_image_DIR=path-to-SDL3_image-dir build
cmake -DSDL3_mixer_DIR=path-to-SDL3_mixer-dir build
cmake -DSDL3_ttf_DIR=path-to-SDL3_ttf-dir buildIf you have SDL3 already set up on your project, you can just copy the contents of amalgamation/ or include directly to your project.
If you like to have a whole system intallation, you can checkout the project build, and then you can install on you system with cmake:
cmake --install buildAlteratively can move into a custom location with:
cmake --install build --install-prefix <directory>If you are already using CMake for your project, you can use the following command to download from git automatically:
include(FetchContent)
# set(SDL3PP_FORCE_BUNDLED ON) # Uncomment this to make CMake to download SDL and SDL_image for you
FetchContent_Declare(SDL3ppExternal
URL https://github.com/talesm/SDL3pp/releases/download/0.11.3/SDL3pp-0.11.3.tar.gz
)
FetchContent_MakeAvailable(SDL3ppExternal)
# Link to your project
target_link_libraries(MyProject
PRIVATE SDL3pp::SDL3pp
)The SDL3PP_FORCE_BUNDLED option will bundle the SDL and satellites libraries instead of trying to find them on your system. If you have SDL already installed or if you are using some sort of package managment tool such conan or vcpkg, you probably don't need this.
This option actually downloads the libraries from the internet, so you need to be connected to use it. Besides SDL itself, it also includes SDL_image by default, and you can enable or disable other libraries with the following options:
set(SDL3PP_ENABLE_IMAGE ON) # SDL_image; already on by default, set to OFF to disable
set(SDL3PP_ENABLE_MIXER ON) # Enables SDL_mixer
set(SDL3PP_ENABLE_NET ON) # Enables SDL_net
set(SDL3PP_ENABLE_TTF ON) # Enables SDL_ttfYou can also set this options on the cmake's cache using a tool such as cmake-gui.