Skip to content

talesm/SDL3pp

Repository files navigation

SDL3pp

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.

Quick start / TLDR

Goals

  • 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 SDL namespace 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.

Example

#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;
}

examples directory

Building

If aren't using the "full" version, ensure you have the necessary dependencies installed:

  • SDL3
  • SDL3_image (optional)
  • SDL3_ttf (optional)

Assuming you are on the source dir, you can build with:

cmake -S . -B build
cmake --build build

If 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_ttf_DIR=path-to-SDL3_ttf-dir build

Installing

If you have SDL3 already set up on your project, you can just copy the contents of amalgamation/ or include directly to your project.

System installation

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 build

Alteratively can move into a custom location with:

cmake --install build --install-prefix <directory>

Using Cmake's FetchContent

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) # Enable this to force CMake to download SDL, SDL_image and SDL_ttf
FetchContent_Declare(SDL3ppExternal
  URL https://github.com/talesm/SDL3pp/releases/download/0.8.1/SDL3pp-0.8.1.tar.gz
)
FetchContent_MakeAvailable(SDL3ppExternal)

# Link to your project
target_link_libraries(MyProject
  PRIVATE SDL3pp::SDL3pp
)