Add wrappers for SDL functions and structs

This commit is contained in:
Lexi / Zoe 2025-11-23 21:50:57 +01:00
parent 4277f4c818
commit d180b8a5b4
Signed by: binaryDiv
GPG Key ID: F8D4956E224DA232
18 changed files with 410 additions and 206 deletions

View File

@ -8,4 +8,4 @@ default_target: linux
targets:
linux:
output_file: rutile_game
cpp_flags_extra: -DDEBUG
# TODO: In a release build, set -DNDEBUG

View File

@ -2,15 +2,14 @@ module;
#include <iostream>
#include <memory>
#include <string>
#include <SDL3/SDL.h>
export module app;
import config;
import core.engine;
import core.exceptions;
import game.game;
import wrappers.sdl;
export
{
@ -22,45 +21,35 @@ export
public:
App() = default;
SDL_AppResult initialize()
sdl::AppResult initialize()
{
try {
// Set SDL application metadata
set_app_metadata(SDL_PROP_APP_METADATA_NAME_STRING, config::app_name);
set_app_metadata(SDL_PROP_APP_METADATA_VERSION_STRING, config::app_version);
set_app_metadata(SDL_PROP_APP_METADATA_IDENTIFIER_STRING, config::app_identifier);
set_app_metadata(SDL_PROP_APP_METADATA_CREATOR_STRING, config::app_creator);
set_app_metadata(SDL_PROP_APP_METADATA_COPYRIGHT_STRING, config::app_copyright);
set_app_metadata(SDL_PROP_APP_METADATA_URL_STRING, config::app_url);
set_app_metadata(SDL_PROP_APP_METADATA_TYPE_STRING, "game");
sdl::SetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING, config::app_name);
sdl::SetAppMetadataProperty(SDL_PROP_APP_METADATA_VERSION_STRING, config::app_version);
sdl::SetAppMetadataProperty(SDL_PROP_APP_METADATA_IDENTIFIER_STRING, config::app_identifier);
sdl::SetAppMetadataProperty(SDL_PROP_APP_METADATA_CREATOR_STRING, config::app_creator);
sdl::SetAppMetadataProperty(SDL_PROP_APP_METADATA_COPYRIGHT_STRING, config::app_copyright);
sdl::SetAppMetadataProperty(SDL_PROP_APP_METADATA_URL_STRING, config::app_url);
sdl::SetAppMetadataProperty(SDL_PROP_APP_METADATA_TYPE_STRING, "game");
// Initialize SDL subsystems
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
throw core::SDLException("SDL_Init");
}
sdl::Init(sdl::InitFlags::Video | sdl::InitFlags::Events);
engine_ = std::make_unique<core::Engine>();
engine_->initialize();
engine_ = core::Engine::create();
game_ = std::make_unique<game::Game>(*engine_);
game_->initialize();
}
catch (const std::runtime_error& e) {
std::cerr << "Unhandled exception during initialization: " << e.what() << '\n';
return SDL_APP_FAILURE;
return sdl::AppResult::Failure;
}
return SDL_APP_CONTINUE;
return sdl::AppResult::Continue;
}
static void set_app_metadata(const char* property_name, const char* value)
{
if (!SDL_SetAppMetadataProperty(property_name, value)) {
throw core::SDLException("SDL_SetAppMetadataProperty");
}
}
SDL_AppResult handle_event(const SDL_Event* event)
sdl::AppResult handle_event(const sdl::Event* event)
{
try {
if (!engine_->handle_event(event)) {
@ -69,13 +58,13 @@ export
}
catch (const std::runtime_error& e) {
std::cerr << "Unhandled exception during event handling: " << e.what() << '\n';
return SDL_APP_FAILURE;
return sdl::AppResult::Failure;
}
return engine_->keep_running() ? SDL_APP_CONTINUE : SDL_APP_SUCCESS;
return engine_->keep_running() ? sdl::AppResult::Continue : sdl::AppResult::Success;
}
SDL_AppResult iterate()
sdl::AppResult iterate()
{
try {
engine_->update();
@ -84,10 +73,10 @@ export
}
catch (const std::runtime_error& e) {
std::cerr << "Unhandled exception during updating: " << e.what() << '\n';
return SDL_APP_FAILURE;
return sdl::AppResult::Failure;
}
return engine_->keep_running() ? SDL_APP_CONTINUE : SDL_APP_SUCCESS;
return engine_->keep_running() ? sdl::AppResult::Continue : sdl::AppResult::Success;
}
void shutdown() const

View File

@ -2,10 +2,10 @@ module;
#include <format>
#ifdef DEBUG
#define DEBUG_BOOL true
#else
#ifdef NDEBUG
#define DEBUG_BOOL false
#else
#define DEBUG_BOOL true
#endif
export module config;

View File

@ -1,17 +1,19 @@
module;
#include <cassert>
#include <tuple>
#include <memory>
#include <SDL3/SDL.h>
export module core.engine;
import config;
import core.renderer;
import core.window;
import wrappers.sdl;
export namespace core
{
using Window = sdl::Window;
class Engine
{
// Whether this class is currently instantiated (to prevent multiple instances)
@ -23,14 +25,17 @@ export namespace core
Window window_;
Renderer renderer_;
public:
Engine()
// Private constructor
Engine(Window&& window, Renderer&& renderer)
: window_{std::move(window)},
renderer_{std::move(renderer)}
{
// Prevent the class from being instantiated multiple times
assert(!instantiated_);
instantiated_ = true;
}
public:
Engine() = delete;
// No copy or move operations
Engine(const Engine&) = delete;
Engine& operator=(const Engine&) = delete;
@ -42,6 +47,26 @@ export namespace core
instantiated_ = false;
}
static std::unique_ptr<Engine> create()
{
// Prevent the class from being instantiated multiple times
assert(!instantiated_);
auto [sdl_window, sdl_renderer] = sdl::CreateWindowAndRenderer(
config::get_window_title(),
config::window_width,
config::window_height,
0
);
return std::unique_ptr<Engine>(
new Engine{
std::move(sdl_window),
Renderer{std::move(sdl_renderer)}
}
);
}
bool keep_running() const
{
return keep_running_;
@ -57,19 +82,8 @@ export namespace core
return renderer_;
}
// TODO: Should this be moved to the constructor?
void initialize()
{
std::tie(window_, renderer_) = Window::create_window_and_renderer(
config::get_window_title(),
config::window_width,
config::window_height,
0
);
}
// Handles an SDL event. Returns true if the event has been handled.
bool handle_event(const SDL_Event* event)
bool handle_event(const sdl::Event* event)
{
if (event->type == SDL_EVENT_QUIT) {
// Exit the application

View File

@ -1,51 +1,47 @@
module;
#include <memory>
#include <SDL3/SDL.h>
#include <utility>
export module core.renderer;
import utils.memory;
import wrappers.sdl;
export namespace core
{
// TODO: Rename this class to RenderServer or something to distinguish it from sdl::Renderer?
class Renderer
{
std::unique_ptr<
SDL_Renderer,
utils::FuncDeleter<SDL_DestroyRenderer>
> sdl_renderer_ = nullptr;
sdl::Renderer sdl_renderer_;
public:
Renderer() = default;
Renderer() = delete;
explicit Renderer(SDL_Renderer* sdl_renderer)
: sdl_renderer_(sdl_renderer)
explicit Renderer(sdl::Renderer&& sdl_renderer)
: sdl_renderer_{std::move(sdl_renderer)}
{}
// TODO: Remove this when not needed anymore
constexpr SDL_Renderer* get_sdl_renderer() const
constexpr sdl::Renderer& get_sdl_renderer()
{
return sdl_renderer_.get();
return sdl_renderer_;
}
// TODO: Rename clear/present to start_render/finish_render or similar?
void clear() const
void start_frame() const
{
SDL_RenderClear(sdl_renderer_.get());
sdl_renderer_.clear();
}
void present() const
void finish_frame() const
{
SDL_RenderPresent(sdl_renderer_.get());
sdl_renderer_.present();
}
// TODO: Replace SDL_Texture pointer with Texture class
// TODO: Also replace SDL_FRect with something SDL-independent (although for performance it might make sense
// to just type-alias it?)
void render_texture(SDL_Texture* texture, const SDL_FRect* src_rect, const SDL_FRect* dest_rect) const
void render_texture(
const sdl::Texture& texture,
const sdl::FRect* src_rect,
const sdl::FRect* dest_rect
) const
{
SDL_RenderTexture(sdl_renderer_.get(), texture, src_rect, dest_rect);
sdl_renderer_.render_texture(texture, src_rect, dest_rect);
}
};
}

View File

@ -1,48 +0,0 @@
module;
#include <memory>
#include <string>
#include <SDL3/SDL.h>
export module core.window;
import core.exceptions;
import core.renderer;
import utils.memory;
export namespace core
{
class Window
{
std::unique_ptr<
SDL_Window,
utils::FuncDeleter<SDL_DestroyWindow>
> sdl_window_ = nullptr;
public:
Window() = default;
explicit Window(SDL_Window* sdl_window)
: sdl_window_(sdl_window)
{}
static std::pair<Window, Renderer> create_window_and_renderer(
const std::string& title,
const int width,
const int height,
const SDL_WindowFlags window_flags
)
{
SDL_Window* sdl_window = nullptr;
SDL_Renderer* sdl_renderer = nullptr;
if (!SDL_CreateWindowAndRenderer(
title.c_str(), width, height, window_flags, &sdl_window, &sdl_renderer
)) {
throw SDLException("SDL_CreateWindowAndRenderer");
}
return {Window{sdl_window}, Renderer{sdl_renderer}};
}
};
}

View File

@ -8,7 +8,8 @@ export module game.game;
import core.engine;
import core.renderer;
import game.sprite;
import resources.texture;
import wrappers.sdl;
import wrappers.sdl_image;
export namespace game
{
@ -36,7 +37,8 @@ export namespace game
void initialize()
{
auto texture = resources::Texture::load_from_file(
// TODO: Texture should be a reference/pointer to an object managed by a ResourceManager or similar.
auto texture = sdl_image::LoadTexture(
engine_.get_renderer().get_sdl_renderer(),
"assets/neocat.png"
);
@ -45,7 +47,7 @@ export namespace game
}
// Handles an SDL event. Returns true if the event has been handled.
bool handle_event(const SDL_Event* event) const
bool handle_event(const sdl::Event* event) const
{
if (event->type == SDL_EVENT_MOUSE_MOTION) {
sprite_->move(
@ -64,9 +66,9 @@ export namespace game
void render() const
{
const auto& renderer = engine_.get_renderer();
renderer.clear();
renderer.start_frame();
sprite_->draw(renderer);
renderer.present();
renderer.finish_frame();
}
void shutdown()

View File

@ -1,13 +1,12 @@
module;
#include <string>
#include <utility>
#include <SDL3/SDL.h>
export module game.sprite;
import core.exceptions;
import core.renderer;
import resources.texture;
import wrappers.sdl;
// TODO: Move this to a different namespace (core, drawing, ...?)
export namespace game
@ -15,12 +14,12 @@ export namespace game
class Sprite
{
// TODO: Texture should be a reference/pointer to an object managed by a ResourceManager or similar.
resources::Texture texture_;
SDL_FRect dest_rect_{0, 0, 0, 0};
sdl::Texture texture_;
sdl::FRect dest_rect_{0, 0, 0, 0};
public:
explicit Sprite(
resources::Texture&& texture,
sdl::Texture&& texture,
const int width,
const int height
)
@ -48,7 +47,7 @@ export namespace game
void draw(const core::Renderer& renderer) const
{
renderer.render_texture(texture_.get_sdl_texture(), nullptr, &dest_rect_);
renderer.render_texture(texture_, nullptr, &dest_rect_);
}
};
}

View File

@ -7,24 +7,24 @@ SDL_AppResult SDL_AppInit(void** appstate, const int /*argc*/, char** /*argv*/)
{
auto* app = new App;
*appstate = app;
return app->initialize();
return static_cast<SDL_AppResult>(app->initialize());
}
SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event)
{
auto* app = static_cast<App*>(appstate);
return app->handle_event(event);
return static_cast<SDL_AppResult>(app->handle_event(event));
}
SDL_AppResult SDL_AppIterate(void* appstate)
{
auto* app = static_cast<App*>(appstate);
return app->iterate();
return static_cast<SDL_AppResult>(app->iterate());
}
void SDL_AppQuit(void* appstate, const SDL_AppResult /*result*/)
{
auto* app = static_cast<App*>(appstate);
const auto* app = static_cast<App*>(appstate);
app->shutdown();
delete app;
}

View File

@ -1,63 +0,0 @@
module;
#include <memory>
#include <string>
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
export module resources.texture;
import core.exceptions;
import utils.memory;
export namespace resources
{
class Texture
{
std::unique_ptr<
SDL_Texture,
utils::FuncDeleter<SDL_DestroyTexture>
> sdl_texture_ = nullptr;
public:
Texture() = default;
explicit Texture(SDL_Texture* sdl_texture)
: sdl_texture_(sdl_texture)
{}
static Texture create_from_surface(
SDL_Renderer* sdl_renderer,
SDL_Surface* sdl_surface
)
{
SDL_Texture* sdl_texture = SDL_CreateTextureFromSurface(sdl_renderer, sdl_surface);
if (sdl_texture == nullptr) {
throw core::SDLException("SDL_CreateTextureFromSurface");
}
return Texture{sdl_texture};
}
static Texture load_from_file(
SDL_Renderer* sdl_renderer,
const std::string& filename
)
{
SDL_Texture* sdl_texture = IMG_LoadTexture(sdl_renderer, filename.c_str());
if (sdl_texture == nullptr) {
throw core::SDLException("IMG_LoadTexture");
}
return Texture{sdl_texture};
}
// TODO: Do we need this?
constexpr SDL_Texture* get_sdl_texture() const
{
return sdl_texture_.get();
}
};
}

11
src/wrappers/sdl.cppm Normal file
View File

@ -0,0 +1,11 @@
module;
export module wrappers.sdl;
// Export submodules
export import wrappers.sdl.error;
export import wrappers.sdl.events;
export import wrappers.sdl.init;
export import wrappers.sdl.rect;
export import wrappers.sdl.render;
export import wrappers.sdl.video;

View File

@ -4,9 +4,9 @@ module;
#include <stdexcept>
#include <SDL3/SDL.h>
export module core.exceptions;
export module wrappers.sdl.error;
export namespace core
export namespace sdl
{
// Exception that wraps an SDL error (which SDL function caused the error, what's the error).
// SDL_GetError() is used in the constructor to get the error message unless specified explicitly.

View File

@ -0,0 +1,11 @@
module;
#include <SDL3/SDL.h>
export module wrappers.sdl.events;
export namespace sdl
{
// Simple alias for SDL_Event union
using Event = SDL_Event;
}

View File

@ -0,0 +1,62 @@
module;
#include <type_traits>
#include <SDL3/SDL.h>
export module wrappers.sdl.init;
import wrappers.sdl.error;
export namespace sdl
{
using InitFlags_t = SDL_InitFlags;
/**
* Wrapper for the SDL_InitFlags enum/constants.
*
* We're using a namespace here to emulate an enum-like interface. If we used an actual enum, we would have to
* explicitly define bit-wise operations for it (as well as for every other kind of bit flag enum.
* (Maybe in the future this can be replaced with a more elegant template-based solution or something.)
*/
namespace InitFlags
{
constexpr InitFlags_t Audio = SDL_INIT_AUDIO;
constexpr InitFlags_t Video = SDL_INIT_VIDEO;
constexpr InitFlags_t Joystick = SDL_INIT_JOYSTICK;
constexpr InitFlags_t Haptic = SDL_INIT_HAPTIC;
constexpr InitFlags_t Gamepad = SDL_INIT_GAMEPAD;
constexpr InitFlags_t Events = SDL_INIT_EVENTS;
constexpr InitFlags_t Sensor = SDL_INIT_SENSOR;
constexpr InitFlags_t Camera = SDL_INIT_CAMERA;
}
/**
* Wrapper around the SDL_AppResult enum.
*/
enum class AppResult : std::underlying_type_t<SDL_AppResult>
{
Continue = SDL_APP_CONTINUE,
Success = SDL_APP_SUCCESS,
Failure = SDL_APP_FAILURE,
};
/**
* Wrapper around SDL_Init().
*/
constexpr void Init(const InitFlags_t flags)
{
if (!SDL_Init(flags)) {
throw SDLException("SDL_Init");
}
}
/**
* Wrapper around SDL_SetAppMetadataProperty().
*/
constexpr void SetAppMetadataProperty(const char* property_name, const char* value)
{
if (!SDL_SetAppMetadataProperty(property_name, value)) {
throw SDLException("SDL_SetAppMetadataProperty");
}
}
}

View File

@ -0,0 +1,42 @@
module;
#include <SDL3/SDL.h>
export module wrappers.sdl.rect;
export namespace sdl
{
// Wrappers around SDL_Point and SDL_FPoint (change to wrapper classes when needed)
using FPoint = SDL_FPoint;
using Point = SDL_Point;
/**
* Wrapper around SDL_FRect using class inheritance.
*/
class FRect : public SDL_FRect
{
/**
* Wrapper around SDL_RectEmptyFloat().
* @return True if the floating point rectangle takes no space.
*/
constexpr bool is_empty() const
{
return SDL_RectEmptyFloat(this);
}
};
/**
* Wrapper around SDL_Rect using class inheritance.
*/
class Rect : public SDL_Rect
{
/**
* Wrapper around SDL_RectEmpty().
* @return True if the rectangle takes no space.
*/
constexpr bool is_empty() const
{
return SDL_RectEmpty(this);
}
};
}

View File

@ -0,0 +1,125 @@
module;
#include <cassert>
#include <memory>
#include <SDL3/SDL.h>
export module wrappers.sdl.render;
import utils.memory;
import wrappers.sdl.error;
import wrappers.sdl.rect;
import wrappers.sdl.video;
export namespace sdl
{
/**
* Wrapper around SDL_Texture that manages its lifecycle with a unique_ptr.
*/
class Texture
{
std::unique_ptr<
SDL_Texture,
utils::FuncDeleter<SDL_DestroyTexture>
> raw_texture_ = nullptr;
public:
Texture() = default;
explicit Texture(SDL_Texture* raw_texture)
: raw_texture_{raw_texture}
{}
constexpr SDL_Texture* get_raw() const
{
assert(raw_texture_);
return raw_texture_.get();
}
};
/**
* Wrapper around SDL_Renderer that manages its lifecycle with a unique_ptr.
*/
class Renderer
{
std::unique_ptr<
SDL_Renderer,
utils::FuncDeleter<SDL_DestroyRenderer>
> raw_renderer_ = nullptr;
public:
Renderer() = default;
explicit Renderer(SDL_Renderer* raw_renderer)
: raw_renderer_{raw_renderer}
{}
constexpr SDL_Renderer* get_raw() const
{
assert(raw_renderer_);
return raw_renderer_.get();
}
/**
* Wrapper around SDL_RenderClear().
* Uses assert to verify the function returned true.
*/
constexpr void clear() const
{
// TODO: These functions probably should never fail? Change this to a runtime exception if necessary.
if (!SDL_RenderClear(get_raw())) {
assert(!"SDL_RenderClear returned false");
}
}
/**
* Wrapper around SDL_RenderPresent().
* Uses assert to verify the function returned true.
*/
constexpr void present() const
{
if (!SDL_RenderPresent(get_raw())) {
assert(!"SDL_RenderPresent returned false");
}
}
/**
* Wrapper around SDL_RenderTexture().
* Uses assert to verify the function returned true.
*/
constexpr void render_texture(
const Texture& texture,
const FRect* src_rect,
const FRect* dest_rect
) const
{
if (!SDL_RenderTexture(get_raw(), texture.get_raw(), src_rect, dest_rect)) {
assert(!"SDL_RenderTexture returned false");
}
}
};
/**
* Wrapper around SDL_CreateWindowAndRenderer().
* Returns a pair of a Window and a Renderer object.
* Throws an SDLException on failure.
*/
std::pair<Window, Renderer> CreateWindowAndRenderer(
const std::string& title,
const int width,
const int height,
const SDL_WindowFlags window_flags
)
{
SDL_Window* raw_window = nullptr;
SDL_Renderer* raw_renderer = nullptr;
if (!SDL_CreateWindowAndRenderer(
title.c_str(), width, height, window_flags, &raw_window, &raw_renderer
)) {
throw SDLException("SDL_CreateWindowAndRenderer");
}
return {Window{raw_window}, Renderer{raw_renderer}};
}
}

View File

@ -0,0 +1,36 @@
module;
#include <cassert>
#include <memory>
#include <SDL3/SDL.h>
export module wrappers.sdl.video;
import utils.memory;
export namespace sdl
{
/**
* Wrapper around SDL_Window that manages its lifecycle with a unique_ptr.
*/
class Window
{
std::unique_ptr<
SDL_Window,
utils::FuncDeleter<SDL_DestroyWindow>
> raw_window_ = nullptr;
public:
Window() = default;
explicit Window(SDL_Window* raw_window)
: raw_window_{raw_window}
{}
constexpr SDL_Window* get_raw() const
{
assert(raw_window_);
return raw_window_.get();
}
};
}

View File

@ -0,0 +1,28 @@
module;
#include <string>
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
export module wrappers.sdl_image;
import utils.memory;
import wrappers.sdl.error;
import wrappers.sdl.render;
export namespace sdl_image
{
/**
* Wrapper around IMG_LoadTexture().
*/
sdl::Texture LoadTexture(const sdl::Renderer& renderer, const std::string& filename)
{
SDL_Texture* sdl_texture = IMG_LoadTexture(renderer.get_raw(), filename.c_str());
if (sdl_texture == nullptr) {
throw sdl::SDLException("IMG_LoadTexture");
}
return sdl::Texture{sdl_texture};
}
}