Compare commits

...

7 Commits

24 changed files with 1085 additions and 190 deletions

BIN
assets/neofox.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

View File

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

87
src/app.cppm Normal file
View File

@ -0,0 +1,87 @@
module;
#include <iostream>
#include <memory>
#include <SDL3/SDL.h>
export module app;
import config;
import core.engine;
import game.game;
import wrappers.sdl;
export
{
class App
{
std::unique_ptr<core::Engine> engine_{nullptr};
std::unique_ptr<game::Game> game_{nullptr};
public:
App() = default;
sdl::AppResult initialize()
{
try {
// Set SDL application metadata
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
sdl::Init(sdl::InitFlags::Video | sdl::InitFlags::Events);
// Create engine (includes window and renderer) and game state
engine_ = core::Engine::create();
game_ = game::Game::create(*engine_);
}
catch (const std::runtime_error& e) {
std::cerr << "Unhandled exception during initialization: " << e.what() << '\n';
return sdl::AppResult::Failure;
}
return sdl::AppResult::Continue;
}
sdl::AppResult handle_event(const sdl::Event* event)
{
try {
if (!engine_->handle_event(event)) {
game_->handle_event(event);
}
}
catch (const std::runtime_error& e) {
std::cerr << "Unhandled exception during event handling: " << e.what() << '\n';
return sdl::AppResult::Failure;
}
return engine_->keep_running() ? sdl::AppResult::Continue : sdl::AppResult::Success;
}
sdl::AppResult iterate()
{
try {
engine_->update();
game_->update();
game_->render();
}
catch (const std::runtime_error& e) {
std::cerr << "Unhandled exception during updating: " << e.what() << '\n';
return sdl::AppResult::Failure;
}
return engine_->keep_running() ? sdl::AppResult::Continue : sdl::AppResult::Success;
}
void shutdown() const
{
engine_->shutdown();
game_->shutdown();
}
};
}

36
src/config.cppm Normal file
View File

@ -0,0 +1,36 @@
module;
#include <format>
#ifdef NDEBUG
#define DEBUG_BOOL false
#else
#define DEBUG_BOOL true
#endif
export module config;
export namespace config
{
constexpr auto debug = DEBUG_BOOL;
constexpr auto app_name = "Rutile Game Prototype";
constexpr auto app_version = "0.0.1-dev";
constexpr auto app_identifier = "dev.binarydiv.rutile_game";
constexpr auto app_creator = "binaryDiv";
constexpr auto app_copyright = "Copyright (c) 2025 binaryDiv";
constexpr auto app_url = "https://git.0xbd.space/binaryDiv/rutile-game";
constexpr auto window_width = 640;
constexpr auto window_height = 480;
constexpr auto get_window_title()
{
if constexpr (debug) {
return std::format("{} ({}) [DEBUG]", app_name, app_version);
}
else {
return std::format("{} ({})", app_name, app_version);
}
}
}

View File

@ -0,0 +1,84 @@
module;
#include <cassert>
#include <optional>
#include <string>
#include <SDL3/SDL.h>
export module core.drawing.sprite;
import core.render_server;
import wrappers.sdl;
export namespace core
{
class Sprite
{
// Texture ID (managed by TextureManager in RenderServer)
TextureID texture_id_;
// Texture source boundaries: which part of the texture to render
sdl::FRect texture_boundaries_;
// Texture offset: static offset that is added to the render position (BEFORE scale is applied)
sdl::FPoint texture_offset_{0, 0};
// Texture scale: factors applied to width/height (AFTER offset is applied)
// TODO: Proper vector class?
sdl::FPoint texture_scale_{1, 1};
// Sprite position: where to render the sprite (world coordinates)
sdl::FPoint position_{0, 0};
public:
explicit Sprite(
const TextureID texture_id,
const sdl::FRect texture_boundaries,
const sdl::FPoint position,
const sdl::FPoint texture_offset = {0, 0},
const sdl::FPoint texture_scale = {1, 1}
)
: texture_id_{texture_id},
texture_boundaries_{texture_boundaries},
texture_offset_{texture_offset},
texture_scale_{texture_scale},
position_{position}
{
assert(texture_boundaries_.w > 0 && texture_boundaries_.h > 0);
}
static Sprite create_with_texture(
RenderServer& render_server,
const std::string& filename,
std::optional<sdl::FRect> texture_boundaries,
auto... args
)
{
const TextureID texture_id = render_server.load_texture(filename);
if (!texture_boundaries.has_value()) {
const sdl::Texture& texture = render_server.get_texture(texture_id);
texture_boundaries = texture.get_boundaries();
}
return Sprite(texture_id, texture_boundaries.value(), args...);
}
void set_position(const sdl::FPoint new_position)
{
position_ = new_position;
}
void draw(const RenderServer& render_server) const
{
const sdl::FRect dest_rect{
position_.x + (texture_scale_.x * texture_offset_.x),
position_.y + (texture_scale_.y * texture_offset_.y),
texture_scale_.x * texture_boundaries_.w,
texture_scale_.y * texture_boundaries_.h,
};
render_server.render_texture(texture_id_, &texture_boundaries_, &dest_rect);
}
};
}

105
src/core/engine.cppm Normal file
View File

@ -0,0 +1,105 @@
module;
#include <cassert>
#include <memory>
#include <SDL3/SDL.h>
export module core.engine;
import config;
import core.render_server;
import wrappers.sdl;
export namespace core
{
class Engine
{
// Whether this class is currently instantiated (to prevent multiple instances)
static bool instantiated_;
// If this is set to false, the application will exit
bool keep_running_ = true;
sdl::Window window_;
RenderServer render_server_;
// Private constructor
Engine(sdl::Window&& window, sdl::Renderer&& renderer)
: window_{std::move(window)},
render_server_{std::move(renderer)}
{
instantiated_ = true;
}
public:
Engine() = delete;
// No copy or move operations
Engine(const Engine&) = delete;
Engine& operator=(const Engine&) = delete;
Engine(Engine&&) = delete;
Engine& operator=(Engine&&) = delete;
~Engine()
{
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),
std::move(sdl_renderer)
}
};
}
bool keep_running() const
{
return keep_running_;
}
sdl::Window& get_window()
{
return window_;
}
RenderServer& get_render_server()
{
return render_server_;
}
// Handles an SDL event. Returns true if the event has been handled.
bool handle_event(const sdl::Event* event)
{
if (event->type == sdl::EventType::Quit) {
// Exit the application
keep_running_ = false;
return true;
}
return false;
}
void update()
{
}
void shutdown()
{
}
};
bool Engine::instantiated_ = false;
}

View File

@ -0,0 +1,89 @@
module;
#include <string>
#include <utility>
export module core.render_server;
import core.resource_manager;
import core.texture_loader;
import wrappers.sdl;
export namespace core
{
using TextureID = unsigned int;
using TextureManager = ResourceManager<TextureID, sdl::Texture, TextureLoader>;
class RenderServer
{
sdl::Renderer renderer_;
TextureLoader texture_loader_;
TextureManager texture_manager_;
public:
RenderServer() = delete;
explicit RenderServer(sdl::Renderer&& renderer)
: renderer_{std::move(renderer)},
texture_loader_{renderer_},
texture_manager_{texture_loader_}
{}
// No copy or move operations
RenderServer(const RenderServer&) = delete;
RenderServer& operator=(const RenderServer&) = delete;
RenderServer(RenderServer&&) = delete;
RenderServer& operator=(RenderServer&&) = delete;
~RenderServer() = default;
constexpr sdl::Renderer& get_renderer()
{
return renderer_;
}
/**
* Load a texture from a file (if not loaded yet) and return its texture ID.
*/
constexpr TextureID load_texture(const std::string& filename)
{
return texture_manager_.load_resource_by_name(filename);
}
/**
* Get a reference to a texture by its texture ID.
*/
constexpr const sdl::Texture& get_texture(const TextureID texture_id) const
{
return texture_manager_.get_resource(texture_id);
}
void start_frame() const
{
renderer_.clear();
}
void finish_frame() const
{
renderer_.present();
}
void render_texture(
const TextureID texture_id,
const sdl::FRect* src_rect,
const sdl::FRect* dest_rect
) const
{
render_texture(get_texture(texture_id), src_rect, dest_rect);
}
constexpr void render_texture(
const sdl::Texture& texture,
const sdl::FRect* src_rect,
const sdl::FRect* dest_rect
) const
{
renderer_.render_texture(texture, src_rect, dest_rect);
}
};
}

View File

@ -0,0 +1,122 @@
module;
#include <cassert>
#include <map>
#include <optional>
#include <string>
#include <vector>
export module core.resource_manager;
export namespace core
{
template <typename ResourceLoaderType, typename ResourceType>
concept IsResourceLoader = requires(ResourceLoaderType loader, const std::string& name)
{
{ loader.load_resource(name) } -> std::same_as<ResourceType>;
};
template <
typename ResourceIDType,
typename ResourceType,
IsResourceLoader<ResourceType> ResourceLoaderType
>
class ResourceManager
{
// Resource loader
ResourceLoaderType& resource_loader_;
// Registry of loaded resources, array index is resource ID
std::vector<ResourceType> resource_registry_;
// Mapping of all loaded resources from (file) name to resource ID
std::map<std::string, ResourceIDType> resource_name_map_;
public:
ResourceManager() = delete;
explicit ResourceManager(ResourceLoaderType& resource_loader)
: resource_loader_{resource_loader}
{}
// No copy or move operations
ResourceManager(const ResourceManager&) = delete;
ResourceManager& operator=(const ResourceManager&) = delete;
ResourceManager(ResourceManager&&) = delete;
ResourceManager& operator=(ResourceManager&&) = delete;
~ResourceManager() = default;
/**
* Adds a new resource to the registry and returns its ID.
*/
ResourceIDType add_resource(ResourceType&& resource)
{
// Add resource to end of vector
resource_registry_.push_back(std::move(resource));
// Return the index of the newly added resource
return static_cast<ResourceIDType>(resource_registry_.size() - 1);
}
/**
* Adds a new resource to the registry, associates the name with it and returns its ID.
*/
ResourceIDType add_resource(ResourceType&& resource, const std::string& name)
{
// Add resource
ResourceIDType resource_id = add_resource(std::move(resource));
// Associate name with resource ID for future access
resource_name_map_.emplace(name, resource_id);
return resource_id;
}
/**
* Gets the resource ID for a given resource name, or `std::nullopt` if the resource was not found.
*/
std::optional<ResourceIDType> get_resource_id_by_name(const std::string& name) const
{
// Check if resource is already loaded
if (
const auto search = resource_name_map_.find(name);
search != resource_name_map_.end()
) {
// Return resource ID
return search->second;
}
return std::nullopt;
}
/**
* Loads a resource (e.g. from a file) and adds it to the registry if it isn't loaded yet.
* Returns the resource ID.
*/
ResourceIDType load_resource_by_name(const std::string& name)
{
// Check if resource is already loaded
if (
auto resource_id = get_resource_id_by_name(name);
resource_id.has_value()
) {
return resource_id.value();
}
// Load resource and add it to the registry
return add_resource(resource_loader_.load_resource(name), name);
}
/**
* Returns a reference to the resource with the given ID.
* The reference is not guaranteed to be valid after adding new resources to the registry.
* Assumes that the resource ID is valid.
*/
const ResourceType& get_resource(const ResourceIDType resource_id) const
{
assert(resource_id < resource_registry_.size());
return resource_registry_[resource_id];
}
};
}

View File

@ -0,0 +1,35 @@
module;
#include <string>
export module core.texture_loader;
import core.resource_manager;
import wrappers.sdl;
import wrappers.sdl_image;
export namespace core
{
class TextureLoader
{
sdl::Renderer& renderer_;
public:
explicit TextureLoader(sdl::Renderer& renderer)
: renderer_{renderer}
{}
// No copy or move operations (reference)
TextureLoader(const TextureLoader&) = delete;
TextureLoader& operator=(const TextureLoader&) = delete;
TextureLoader(TextureLoader&&) = delete;
TextureLoader& operator=(TextureLoader&&) = delete;
~TextureLoader() = default;
sdl::Texture load_resource(const std::string& filename) const
{
return sdl_image::LoadTexture(renderer_, filename);
}
};
}

115
src/game/game.cppm Normal file
View File

@ -0,0 +1,115 @@
module;
#include <cassert>
#include <memory>
#include <optional>
#include <vector>
#include <SDL3/SDL.h>
export module game.game;
import core.drawing.sprite;
import core.engine;
import core.render_server;
import wrappers.sdl;
import wrappers.sdl_image;
export namespace game
{
class Game
{
// Whether this class is currently instantiated (to prevent multiple instances)
static bool instantiated_;
// Reference to the engine
core::Engine& engine_;
// Sprites for testing
core::Sprite player_sprite_;
std::vector<core::Sprite> sprites_;
// Private constructor
explicit Game(core::Engine& engine)
: engine_(engine),
player_sprite_{
core::Sprite::create_with_texture(
engine_.get_render_server(),
"assets/sprites/neocat_64.png",
std::nullopt,
sdl::FPoint{0, 0},
sdl::FPoint{-32, -32}
)
}
{}
public:
Game() = delete;
// No copy or move operations because we have a reference to the engine
Game(const Game&) = delete;
Game& operator=(const Game&) = delete;
Game(Game&&) = delete;
Game& operator=(Game&&) = delete;
~Game()
{
instantiated_ = false;
}
static std::unique_ptr<Game> create(core::Engine& engine)
{
// Prevent the class from being instantiated multiple times
assert(!instantiated_);
return std::unique_ptr<Game>{
new Game(engine)
};
}
// Handles an SDL event. Returns true if the event has been handled.
bool handle_event(const sdl::Event* event)
{
if (event->type == sdl::EventType::MouseMotion) {
player_sprite_.set_position({event->motion.x, event->motion.y});
return true;
}
if (event->type == sdl::EventType::MouseButtonUp) {
sprites_.push_back(
core::Sprite::create_with_texture(
engine_.get_render_server(),
"assets/sprites/neofox_64.png",
std::nullopt,
sdl::FPoint{event->motion.x, event->motion.y},
sdl::FPoint{-32, -32}
)
);
return true;
}
return false;
}
void update()
{
}
void render() const
{
const auto& render_server = engine_.get_render_server();
render_server.start_frame();
for (const core::Sprite& sprite : sprites_) {
sprite.draw(render_server);
}
player_sprite_.draw(render_server);
render_server.finish_frame();
}
void shutdown()
{
}
};
bool Game::instantiated_ = false;
}

View File

@ -1,27 +1,30 @@
import sdl_app;
import app;
#define SDL_MAIN_USE_CALLBACKS
#include <SDL3/SDL_main.h>
SDL_AppResult SDL_AppInit(void** appstate, int /*argc*/, char** /*argv*/)
SDL_AppResult SDL_AppInit(void** appstate, const int /*argc*/, char** /*argv*/)
{
*appstate = new AppState;
return sdl_app_init(static_cast<AppState*>(*appstate));
}
SDL_AppResult SDL_AppIterate(void* appstate)
{
return sdl_app_iterate(static_cast<AppState*>(appstate));
auto* app = new App;
*appstate = app;
return static_cast<SDL_AppResult>(app->initialize());
}
SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event)
{
return sdl_app_event(static_cast<AppState*>(appstate), event);
auto* app = static_cast<App*>(appstate);
return static_cast<SDL_AppResult>(app->handle_event(event));
}
void SDL_AppQuit(void* appstate, SDL_AppResult /*result*/)
SDL_AppResult SDL_AppIterate(void* appstate)
{
const auto* game_app_state = static_cast<AppState*>(appstate);
sdl_app_shutdown(game_app_state);
delete game_app_state;
auto* app = static_cast<App*>(appstate);
return static_cast<SDL_AppResult>(app->iterate());
}
void SDL_AppQuit(void* appstate, const SDL_AppResult /*result*/)
{
const auto* app = static_cast<App*>(appstate);
app->shutdown();
delete app;
}

View File

@ -1,95 +0,0 @@
module;
#include <iostream>
#include <string>
#include <SDL3/SDL.h>
export module sdl_app;
import sprite;
export {
struct AppState
{
SDL_Window* window = nullptr;
SDL_Renderer* renderer = nullptr;
Sprite* sprite = nullptr;
};
SDL_AppResult sdl_panic(
const std::string& error_prefix,
SDL_Window* window = nullptr,
SDL_Renderer* renderer = nullptr
)
{
std::cerr << error_prefix << ": " << SDL_GetError() << '\n';
if (renderer != nullptr) {
SDL_DestroyRenderer(renderer);
}
if (window != nullptr) {
SDL_DestroyWindow(window);
}
SDL_Quit();
return SDL_APP_FAILURE;
}
SDL_AppResult sdl_app_init(AppState* app_state)
{
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
return sdl_panic("SDL_Init error");
}
app_state->window = SDL_CreateWindow("BuildSystemTest", 640, 480, 0);
if (app_state->window == nullptr) {
return sdl_panic("SDL_CreateWindow error");
}
app_state->renderer = SDL_CreateRenderer(app_state->window, nullptr);
if (app_state->renderer == nullptr) {
return sdl_panic("SDL_CreateRenderer error", app_state->window);
}
try {
app_state->sprite = new Sprite(app_state->renderer, "assets/neocat.png", 100, 100);
}
catch (const std::runtime_error& e) {
return sdl_panic(e.what(), app_state->window, app_state->renderer);
}
return SDL_APP_CONTINUE;
}
SDL_AppResult sdl_app_event(const AppState* app_state, const SDL_Event* event)
{
if (event->type == SDL_EVENT_QUIT) {
return SDL_APP_SUCCESS;
}
if (event->type == SDL_EVENT_MOUSE_MOTION) {
app_state->sprite->move(
event->motion.x - 50,
event->motion.y - 50
);
}
return SDL_APP_CONTINUE;
}
SDL_AppResult sdl_app_iterate(const AppState* app_state)
{
SDL_RenderClear(app_state->renderer);
app_state->sprite->render(app_state->renderer);
SDL_RenderPresent(app_state->renderer);
return SDL_APP_CONTINUE;
}
void sdl_app_shutdown(const AppState* app_state)
{
delete app_state->sprite;
SDL_DestroyRenderer(app_state->renderer);
SDL_DestroyWindow(app_state->window);
}
}

View File

@ -1,81 +0,0 @@
module;
#include <stdexcept>
#include <string>
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
export module sprite;
export class Sprite
{
SDL_Texture* sdl_texture;
SDL_FRect dest_rect{0, 0, 0, 0};
public:
explicit Sprite(
SDL_Renderer* renderer,
const std::string& filename,
const int width,
const int height
)
{
SDL_Surface* texture_surface = IMG_Load(filename.c_str());
if (texture_surface == nullptr) {
throw std::runtime_error("IMG_Load error");
}
sdl_texture = SDL_CreateTextureFromSurface(renderer, texture_surface);
SDL_DestroySurface(texture_surface);
if (sdl_texture == nullptr) {
throw std::runtime_error("SDL_CreateTextureFromSurface error");
}
dest_rect.w = static_cast<float>(width);
dest_rect.h = static_cast<float>(height);
}
// Don't allow copy operations
Sprite(const Sprite&) = delete;
Sprite& operator=(const Sprite&) = delete;
// Move constructor
Sprite(Sprite&& other) noexcept
: sdl_texture(other.sdl_texture),
dest_rect(other.dest_rect)
{
other.sdl_texture = nullptr;
}
// Move assignment
Sprite& operator=(Sprite&& other) noexcept
{
// Move inner resources from other
sdl_texture = other.sdl_texture;
dest_rect = other.dest_rect;
// Reset other to make it safe for deletion
other.sdl_texture = nullptr;
return *this;
}
~Sprite()
{
if (sdl_texture != nullptr) {
SDL_DestroyTexture(sdl_texture);
}
}
void move(const float x, const float y)
{
dest_rect.x = x;
dest_rect.y = y;
}
void render(SDL_Renderer* renderer) const
{
SDL_RenderTexture(renderer, sdl_texture, nullptr, &dest_rect);
}
};

21
src/utils/memory.cppm Normal file
View File

@ -0,0 +1,21 @@
module;
export module utils.memory;
export namespace utils
{
/**
* Template to generate deleters for `std::unique_ptr` from functions, e.g. to free SDL resources.
*
* @tparam delete_func Function that takes a pointer to a resource and deletes the resource.
*/
template <auto delete_func>
struct FuncDeleter
{
template <typename T>
constexpr void operator()(T* ptr) const noexcept
{
delete_func(ptr);
}
};
}

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

@ -0,0 +1,24 @@
module;
#include <format>
#include <stdexcept>
#include <SDL3/SDL.h>
export module wrappers.sdl.error;
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.
class SDLException final : public std::runtime_error
{
public:
explicit SDLException(const std::string& sdl_func, const std::string& sdl_error)
: runtime_error(std::format("Error in SDL function {}: {}", sdl_func, sdl_error))
{}
explicit SDLException(const std::string& sdl_func)
: SDLException(sdl_func, SDL_GetError())
{}
};
}

View File

@ -0,0 +1,30 @@
module;
#include <SDL3/SDL.h>
export module wrappers.sdl.events;
export namespace sdl
{
// Simple alias for SDL_Event union
using Event = SDL_Event;
// Alias for EventType enum
using EventType_t = SDL_EventType;
/**
* Wrapper for the SDL_EventType enum.
*
* We're using a namespace here to emulate an enum-like interface, without having to copy the entire enum.
* More constants can be added on demand.
*/
namespace EventType
{
constexpr EventType_t Quit = SDL_EVENT_QUIT;
constexpr EventType_t KeyDown = SDL_EVENT_KEY_DOWN;
constexpr EventType_t KeyUp = SDL_EVENT_KEY_UP;
constexpr EventType_t MouseMotion = SDL_EVENT_MOUSE_MOTION;
constexpr EventType_t MouseButtonUp = SDL_EVENT_MOUSE_BUTTON_UP;
constexpr EventType_t MouseButtonDown = SDL_EVENT_MOUSE_BUTTON_DOWN;
}
}

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,140 @@
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_;
public:
Texture() = delete;
explicit Texture(SDL_Texture* raw_texture)
: raw_texture_{raw_texture}
{}
constexpr SDL_Texture* get_raw() const
{
assert(raw_texture_);
return raw_texture_.get();
}
constexpr int get_width() const
{
return raw_texture_->w;
}
constexpr int get_height() const
{
return raw_texture_->h;
}
constexpr FRect get_boundaries() const
{
return {0, 0, static_cast<float>(raw_texture_->w), static_cast<float>(raw_texture_->h)};
}
};
/**
* 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_;
public:
Renderer() = delete;
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_;
public:
Window() = delete;
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};
}
}