Skip to content

JankoDedic/sdlw

Repository files navigation

sdlw

Build Status Build status GitHub license

Introduction

sdlw is a header-only wrapper for the SDL2 library which provides a uniform modern C++ interface for the original C API. It supports SDL2_image and SDL2_ttf extension libraries as well. Support for SDL2_mixer is in development.

Features

Modular design with namespaces

using namespace sdlw::video;
using namespace sdlw::events;

// or

using sdlw::video::window, sdlw::video::renderer;

Automatic resource management

auto main(int argc, char *argv[]) -> int {
    using sdlw::subsystem;
    using namespace sdlw::video;

    const auto video_subsystem = subsystem(subsystem_flags::video);
    auto win = window("title", {0, 0, 800, 600}, window_flags::shown);
    auto rend = renderer(win, renderer_flags::accelerated);

    return 0;
    // renderer and window automatically destroyed
    // subsystem automatically quits
}

Error handling

The original C API imposes a burden on the user to check error codes on every call site. Such a check would naturally be followed up by a call to SDL_GetError and a print function. Exceptions eliminate the noise on all call sites, and allow for printing error messages in a single place:

auto main(int argc, char *argv[]) -> int try {
    using sdlw::subsystem;
    using namespace sdlw::video;

    const auto video_subsystem = subsystem(subsystem_flags::video);
    auto win = window("title", {0, 0, 800, 600}, window_flags::shown);
    auto rend = renderer(win, renderer_flags::accelerated);

    return 0;
} catch (const sdlw::exception &e) {
    std::cerr << e.what() << '\n';
    return 1;
}

std::chrono integration

using namespace sdlw::time, std::chrono;
constexpr auto frame_time = 16ms;
auto last_update_time = clock::now();
for (;;) {
    if (clock::now() > last_update_time + frame_time) {
        game.update();
    }
}

Multiple return values and support for structured bindings (C++17)

auto [diagonal, horizontal, vertical] = sdlw::display::dpi(0);
auto [top, left, bottom, right] = win.border_sizes();