https://github.com/dkosmari/sdl2xx
A C++23 wrapper for SDL2.
https://github.com/dkosmari/sdl2xx
cpp cpp23 sdl sdl2 zlib-license
Last synced: 10 days ago
JSON representation
A C++23 wrapper for SDL2.
- Host: GitHub
- URL: https://github.com/dkosmari/sdl2xx
- Owner: dkosmari
- License: other
- Created: 2025-04-21T01:18:28.000Z (19 days ago)
- Default Branch: main
- Last Pushed: 2025-04-29T05:32:29.000Z (11 days ago)
- Last Synced: 2025-04-30T14:28:27.371Z (10 days ago)
- Topics: cpp, cpp23, sdl, sdl2, zlib-license
- Language: C++
- Homepage:
- Size: 475 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: COPYING
- Authors: AUTHORS
Awesome Lists containing this project
README
# SDL2XX - A modern C++ wrapper for SDL2
SDL2XX is a modern C++ wrapper for SDL2. Currently the target is C++23.
It's licensed under the Zlib license (same as SDL2).
> Note: this is still a work in progress.
> Documentation is still missing, but the C++ API maps naturally to the C API.
## Simple Example
```cpp
#include
#include#include
using sdl::vec2;
using namespace sdl::literals;int main(int, char* [])
{
try {
sdl::init guard{sdl::init::flag::video};sdl::window win{"Simple App",
sdl::window::pos_centered,
{1280, 720},
0};sdl::renderer rend{win,
-1,
sdl::renderer::flag::accelerated | sdl::renderer::flag::present_vsync};auto box = sdl::rect::from_corners({300, 200},
win.get_size() - vec2{300, 200});bool running = true;
while (running) {
// Clear window to a navy blue background.
rend.set_color(0x000080_rgb);
rend.clear();// Draw yellow box.
rend.set_color(sdl::color::yellow);
rend.fill_box(box);rend.present();
// Process events.
while (auto event = sdl::events::poll()) {
switch (event->type) {case SDL_QUIT:
running = false;
break;}
}
}
}
catch (std::exception& e) {
std::cout << "Error: " << e.what() << std::endl;
return -1;
}
}
```See the [examples directory](examples) for more examples of the API usage.
## Key features
- Convenient short namespaces:
- `sdl` for the top-level namespace.
- `sdl::img` for `SDL_image`.
- `sdl::ttf` for `SDL_ttf`.
- `sdl::mix` for `SDL_mixer`.- Entities that are opaque pointers are handled through movable RAII classes. For
instance:
- `SDL_Window*` → `sdl::window`
- `SDL_Renderer*` → `sdl::renderer`
- `SDL_Surface*` → `sdl::surface`
- `SDL_Texture*` → `sdl::texture`
Objects can be in an "invalid" state, where they just store a null pointer. In such a
state, you can only construct, destroy or check for validity:
```cpp
sdl::window win{/* ... */};
// ...
if (/* ... */)
win.destroy(); // if this executes, win is now invalid
// ...
if (win)
win.minimize();if (win.is_valid())
win.maximize();
if (win.data())
use_raw_pointer(win.data());
```- Lifetime guard types:
- `sdl::init`: calls `SDL_Init()` and `SDL_Quit()`.
- `sdl::img::init`: calls `IMG_Init()` and `IMG_Quit()`.
- `sdl::ttf::init`: calls `TTF_Init()` and `TTF_Quit()`.- Various utility types are extended, with extra methods and operators:
- `SDL_Point` → `sdl::vec2` (or `sdl::point`)
- `SDL_FPoint` → `sdl::vec2f` (or `sdl::pointf`)
- `SDL_Rect` → `sdl::rect`
- `SDL_FRect` → `sdl::rectf`
- `SDL_Color` → `sdl::color`- Additional types:
- `sdl::degrees`, `sdl::degreesf`: represent angles in degrees.
- `sdl::radians`, `sdl::radiansf`: represent angles in radians.
- `sdl::unique_ptr<>`: uses `SDL_malloc()` and `SDL_free()` to manage memory allocations.
- `sdl::blob`: represents raw byte blobs.- Literals, found in `sdl::literals`:
- `_deg` → `sdl::degrees`
- `_degf` → `sdl::degreesf`
- `_rad` → `sdl::radians`
- `_radf` → `sdl::radiansf`
- `_rgb` → `sdl::color` (with `.a = 0xff`)
- `_rgba` → `sdl::color`
- Functions either throw `sdl::error` or return `std::expected<..., sdl::error>`.