SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
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 mostly wrapping thing here;
  • Mostly wrap the naturally OO-looking API into actual OO C++ constructs;
    • Also add little quick improvements like using vocabulary types to better fit C++ idioms;
    • See Transformations for more details;
  • Put everything into a SDL namespace instead of prefixes;
    • For uniformity we also wrap non OO entities at lower priority (like aliases, functions, constants, etc);
  • 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.

Documentation

Example

#include <iostream>
#include <SDL3pp/SDL3pp.h>
#include <SDL3pp/SDL3pp_main.h>
using namespace std::chrono_literals;
int main(int argc, char** argv)
{
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.SetDrawColor(SDL::FColor{.5f, .5f, .5f, 1.f});
renderer.RenderClear();
renderer.SetDrawColor(SDL::FColor{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;
}
Initialize the SDL library.
Definition SDL3pp_init.h:555
std::optional< Event > PollEvent()
Poll for currently pending events.
Definition SDL3pp_events.h:1301
const char * GetBasePath()
Get the directory where the application was run from.
Definition SDL3pp_filesystem.h:128
std::pair< Window, Renderer > CreateWindowAndRenderer(StringParam title, SDL_Point size, WindowFlags window_flags=0)
Create a window and default renderer.
Definition SDL3pp_render.h:3110
void Delay(std::chrono::nanoseconds duration)
Wait a specified duration before returning.
Definition SDL3pp_timer.h:126
constexpr EventType EVENT_QUIT
User-requested quit.
Definition SDL3pp_events.h:67
constexpr InitFlags INIT_VIDEO
INIT_VIDEO implies INIT_EVENTS, should be initialized on the main thread
Definition SDL3pp_init.h:88
The bits of this structure can be directly reinterpreted as a float-packed color which uses the PIXEL...
Definition SDL3pp_pixels.h:1816
The structure that defines a point (using floating point values).
Definition SDL3pp_rect.h:479
A rectangle, with the origin at the upper left (using floats).
Definition SDL3pp_rect.h:1439
The structure that defines a point (using integers)
Definition SDL3pp_rect.h:41
Handle to an owned texture.
Definition SDL3pp_render.h:2922

See more examples at examples directory

Building

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

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

Installing is not supported yet, but hopefully will be in the future.