From 5c3e0e3a86b948d03736d52197bbea93c9dc83ad Mon Sep 17 00:00:00 2001 From: binaryDiv Date: Mon, 17 Nov 2025 21:17:45 +0100 Subject: [PATCH 1/4] Refactor code into classes --- shuriken.yaml | 1 + src/app.cppm | 109 +++++++++++++++++++++++++++++++++++++++ src/config.cppm | 36 +++++++++++++ src/core/engine.cppm | 95 ++++++++++++++++++++++++++++++++++ src/core/exceptions.cppm | 24 +++++++++ src/core/renderer.cppm | 72 ++++++++++++++++++++++++++ src/core/window.cppm | 70 +++++++++++++++++++++++++ src/game/game.cppm | 72 ++++++++++++++++++++++++++ src/game/sprite.cppm | 88 +++++++++++++++++++++++++++++++ src/main.cpp | 31 ++++++----- src/sdl_app.cppm | 95 ---------------------------------- src/sprite.cppm | 81 ----------------------------- 12 files changed, 584 insertions(+), 190 deletions(-) create mode 100644 src/app.cppm create mode 100644 src/config.cppm create mode 100644 src/core/engine.cppm create mode 100644 src/core/exceptions.cppm create mode 100644 src/core/renderer.cppm create mode 100644 src/core/window.cppm create mode 100644 src/game/game.cppm create mode 100644 src/game/sprite.cppm delete mode 100644 src/sdl_app.cppm delete mode 100644 src/sprite.cppm diff --git a/shuriken.yaml b/shuriken.yaml index 8dd0a71..1c14083 100644 --- a/shuriken.yaml +++ b/shuriken.yaml @@ -8,3 +8,4 @@ default_target: linux targets: linux: output_file: rutile_game + cpp_flags_extra: -DDEBUG diff --git a/src/app.cppm b/src/app.cppm new file mode 100644 index 0000000..86c3f51 --- /dev/null +++ b/src/app.cppm @@ -0,0 +1,109 @@ +module; + +#include +#include +#include +#include + +export module app; + +import config; +import core.engine; +import core.exceptions; +import game.game; + +export +{ + class App + { + std::unique_ptr engine_ = nullptr; + std::unique_ptr game_ = nullptr; + + public: + App() = default; + + // No copy operations + App(const App&) = delete; + App& operator=(const App&) = delete; + + // Default move operations + App(App&&) = default; + App& operator=(App&&) = default; + + ~App() = default; + + 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"); + + // Initialize SDL subsystems + if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) { + throw core::SDLException("SDL_Init"); + } + + engine_ = std::make_unique(); + engine_->initialize(); + + game_ = std::make_unique(*engine_); + game_->initialize(); + } + catch (const std::runtime_error& e) { + std::cerr << "Unhandled exception during initialization: " << e.what() << '\n'; + return SDL_APP_FAILURE; + } + + return SDL_APP_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) + { + 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_APP_FAILURE; + } + + return engine_->keep_running() ? SDL_APP_CONTINUE : SDL_APP_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_APP_FAILURE; + } + + return engine_->keep_running() ? SDL_APP_CONTINUE : SDL_APP_SUCCESS; + } + + void shutdown() const + { + engine_->shutdown(); + game_->shutdown(); + } + }; +} diff --git a/src/config.cppm b/src/config.cppm new file mode 100644 index 0000000..4b522d1 --- /dev/null +++ b/src/config.cppm @@ -0,0 +1,36 @@ +module; + +#include + +#ifdef DEBUG +#define DEBUG_BOOL true +#else +#define DEBUG_BOOL false +#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); + } + } +} diff --git a/src/core/engine.cppm b/src/core/engine.cppm new file mode 100644 index 0000000..dda117d --- /dev/null +++ b/src/core/engine.cppm @@ -0,0 +1,95 @@ +module; + +#include +#include +#include + +export module core.engine; + +import config; +import core.renderer; +import core.window; + +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; + + Window window_; + Renderer renderer_; + + public: + Engine() + { + // Prevent the class from being instantiated multiple times + assert(!instantiated_); + instantiated_ = true; + } + + // No copy operations + Engine(const Engine&) = delete; + Engine& operator=(const Engine&) = delete; + + // Default move operations + Engine(Engine&&) = default; + Engine& operator=(Engine&&) = default; + + ~Engine() + { + instantiated_ = false; + } + + bool keep_running() const + { + return keep_running_; + } + + Window& get_window() + { + return window_; + } + + Renderer& get_renderer() + { + 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) + { + if (event->type == SDL_EVENT_QUIT) { + // Exit the application + keep_running_ = false; + return true; + } + + return false; + } + + void update() + { + } + + void shutdown() + { + } + }; + + bool Engine::instantiated_ = false; +} diff --git a/src/core/exceptions.cppm b/src/core/exceptions.cppm new file mode 100644 index 0000000..2ee8696 --- /dev/null +++ b/src/core/exceptions.cppm @@ -0,0 +1,24 @@ +module; + +#include +#include +#include + +export module core.exceptions; + +export namespace core +{ + // 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()) + {} + }; +} diff --git a/src/core/renderer.cppm b/src/core/renderer.cppm new file mode 100644 index 0000000..2a8ef7b --- /dev/null +++ b/src/core/renderer.cppm @@ -0,0 +1,72 @@ +module; + +#include + +export module core.renderer; + +export namespace core +{ + class Renderer + { + // TODO: Use unique_ptr with custom deleters + SDL_Renderer* sdl_renderer_ = nullptr; + + public: + Renderer() = default; + + explicit Renderer(SDL_Renderer* sdl_renderer) + : sdl_renderer_(sdl_renderer) + {} + + // No copy operations + Renderer(const Renderer&) = delete; + Renderer& operator=(const Renderer&) = delete; + + // Move constructor + Renderer(Renderer&& other) noexcept + : sdl_renderer_(other.sdl_renderer_) + { + other.sdl_renderer_ = nullptr; + } + + // Move assignment + Renderer& operator=(Renderer&& other) noexcept + { + sdl_renderer_ = other.sdl_renderer_; + other.sdl_renderer_ = nullptr; + return *this; + } + + ~Renderer() + { + if (sdl_renderer_ != nullptr) { + SDL_DestroyRenderer(sdl_renderer_); + } + } + + // TODO: Remove this when not needed anymore + SDL_Renderer* get_sdl_renderer() const + { + return sdl_renderer_; + } + + // TODO: Rename clear/present to start_render/finish_render or similar? + void clear() const + { + SDL_RenderClear(sdl_renderer_); + } + + void present() const + { + SDL_RenderPresent(sdl_renderer_); + } + + // 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 + { + SDL_RenderTexture(sdl_renderer_, texture, src_rect, dest_rect); + } + }; +} diff --git a/src/core/window.cppm b/src/core/window.cppm new file mode 100644 index 0000000..9bccb78 --- /dev/null +++ b/src/core/window.cppm @@ -0,0 +1,70 @@ +module; + +#include +#include + +export module core.window; + +import core.exceptions; +import core.renderer; + +export namespace core +{ + class Window + { + // TODO: Use unique_ptr with custom deleters + SDL_Window* sdl_window_ = nullptr; + + public: + Window() = default; + + explicit Window(SDL_Window* sdl_window) + : sdl_window_(sdl_window) + {} + + // No copy operations + Window(const Window&) = delete; + Window& operator=(const Window&) = delete; + + // Move constructor + Window(Window&& other) noexcept + : sdl_window_(other.sdl_window_) + { + other.sdl_window_ = nullptr; + } + + // Move assignment + Window& operator=(Window&& other) noexcept + { + sdl_window_ = other.sdl_window_; + other.sdl_window_ = nullptr; + return *this; + } + + ~Window() + { + if (sdl_window_ != nullptr) { + SDL_DestroyWindow(sdl_window_); + } + } + + static std::pair 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}}; + } + }; +} diff --git a/src/game/game.cppm b/src/game/game.cppm new file mode 100644 index 0000000..4b7a6a1 --- /dev/null +++ b/src/game/game.cppm @@ -0,0 +1,72 @@ +module; + +#include +#include + +export module game.game; + +import core.engine; +import core.renderer; +import game.sprite; + +export namespace game +{ + class Game + { + core::Engine& engine_; + + // Sprite for testing + std::unique_ptr sprite_ = nullptr; + + public: + Game() = delete; + + explicit Game(core::Engine& engine) + : engine_(engine) + {} + + // No copy operations + Game(const Game&) = delete; + Game& operator=(const Game&) = delete; + + // No move operations - TODO? + Game(Game&&) = delete; + Game& operator=(Game&&) = delete; + + ~Game() = default; + + void initialize() + { + sprite_ = std::make_unique(engine_.get_renderer(), "assets/neocat.png", 100, 100); + } + + // Handles an SDL event. Returns true if the event has been handled. + bool handle_event(const SDL_Event* event) const + { + if (event->type == SDL_EVENT_MOUSE_MOTION) { + sprite_->move( + event->motion.x - 50, + event->motion.y - 50 + ); + return true; + } + return false; + } + + void update() + { + } + + void render() const + { + const auto& renderer = engine_.get_renderer(); + renderer.clear(); + sprite_->draw(renderer); + renderer.present(); + } + + void shutdown() + { + } + }; +} diff --git a/src/game/sprite.cppm b/src/game/sprite.cppm new file mode 100644 index 0000000..9c4d9b7 --- /dev/null +++ b/src/game/sprite.cppm @@ -0,0 +1,88 @@ +module; + +#include +#include +#include + +export module game.sprite; + +import core.exceptions; +import core.renderer; + +// TODO: Move this to a different namespace (core, drawing, ...?) +export namespace game +{ + class Sprite + { + // TODO: Move texture to separate class + SDL_Texture* sdl_texture; + SDL_FRect dest_rect{0, 0, 0, 0}; + + public: + explicit Sprite( + core::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 core::SDLException("IMG_Load"); + } + + sdl_texture = SDL_CreateTextureFromSurface(renderer.get_sdl_renderer(), texture_surface); + SDL_DestroySurface(texture_surface); + + if (sdl_texture == nullptr) { + throw core::SDLException("SDL_CreateTextureFromSurface"); + } + + dest_rect.w = static_cast(width); + dest_rect.h = static_cast(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 draw(const core::Renderer& renderer) const + { + renderer.render_texture(sdl_texture, nullptr, &dest_rect); + } + }; +} diff --git a/src/main.cpp b/src/main.cpp index cdfe69e..be80748 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,27 +1,30 @@ -import sdl_app; +import app; #define SDL_MAIN_USE_CALLBACKS #include -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)); -} - -SDL_AppResult SDL_AppIterate(void* appstate) -{ - return sdl_app_iterate(static_cast(appstate)); + auto* app = new App; + *appstate = app; + return app->initialize(); } SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) { - return sdl_app_event(static_cast(appstate), event); + auto* app = static_cast(appstate); + return 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); - sdl_app_shutdown(game_app_state); - delete game_app_state; + auto* app = static_cast(appstate); + return app->iterate(); +} + +void SDL_AppQuit(void* appstate, const SDL_AppResult /*result*/) +{ + auto* app = static_cast(appstate); + app->shutdown(); + delete app; } diff --git a/src/sdl_app.cppm b/src/sdl_app.cppm deleted file mode 100644 index 7e14111..0000000 --- a/src/sdl_app.cppm +++ /dev/null @@ -1,95 +0,0 @@ -module; - -#include -#include -#include - -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); - } -} diff --git a/src/sprite.cppm b/src/sprite.cppm deleted file mode 100644 index 3318180..0000000 --- a/src/sprite.cppm +++ /dev/null @@ -1,81 +0,0 @@ -module; - -#include -#include -#include -#include - -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(width); - dest_rect.h = static_cast(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); - } -}; From 4277f4c818194b7ddaeeacc728f3181d1b5e3633 Mon Sep 17 00:00:00 2001 From: binaryDiv Date: Fri, 21 Nov 2025 01:09:05 +0100 Subject: [PATCH 2/4] Use unique_ptr to manage SDL pointers --- src/app.cppm | 10 ------ src/core/engine.cppm | 8 ++--- src/core/renderer.cppm | 45 +++++++-------------------- src/core/window.cppm | 34 ++++---------------- src/game/game.cppm | 12 ++++--- src/game/sprite.cppm | 64 +++++++++----------------------------- src/resources/texture.cppm | 63 +++++++++++++++++++++++++++++++++++++ src/utils/memory.cppm | 21 +++++++++++++ 8 files changed, 128 insertions(+), 129 deletions(-) create mode 100644 src/resources/texture.cppm create mode 100644 src/utils/memory.cppm diff --git a/src/app.cppm b/src/app.cppm index 86c3f51..37c5483 100644 --- a/src/app.cppm +++ b/src/app.cppm @@ -22,16 +22,6 @@ export public: App() = default; - // No copy operations - App(const App&) = delete; - App& operator=(const App&) = delete; - - // Default move operations - App(App&&) = default; - App& operator=(App&&) = default; - - ~App() = default; - SDL_AppResult initialize() { try { diff --git a/src/core/engine.cppm b/src/core/engine.cppm index dda117d..2af5b7b 100644 --- a/src/core/engine.cppm +++ b/src/core/engine.cppm @@ -31,13 +31,11 @@ export namespace core instantiated_ = true; } - // No copy operations + // No copy or move operations Engine(const Engine&) = delete; Engine& operator=(const Engine&) = delete; - - // Default move operations - Engine(Engine&&) = default; - Engine& operator=(Engine&&) = default; + Engine(Engine&&) = delete; + Engine& operator=(Engine&&) = delete; ~Engine() { diff --git a/src/core/renderer.cppm b/src/core/renderer.cppm index 2a8ef7b..337c6f5 100644 --- a/src/core/renderer.cppm +++ b/src/core/renderer.cppm @@ -1,15 +1,20 @@ module; +#include #include export module core.renderer; +import utils.memory; + export namespace core { class Renderer { - // TODO: Use unique_ptr with custom deleters - SDL_Renderer* sdl_renderer_ = nullptr; + std::unique_ptr< + SDL_Renderer, + utils::FuncDeleter + > sdl_renderer_ = nullptr; public: Renderer() = default; @@ -18,47 +23,21 @@ export namespace core : sdl_renderer_(sdl_renderer) {} - // No copy operations - Renderer(const Renderer&) = delete; - Renderer& operator=(const Renderer&) = delete; - - // Move constructor - Renderer(Renderer&& other) noexcept - : sdl_renderer_(other.sdl_renderer_) - { - other.sdl_renderer_ = nullptr; - } - - // Move assignment - Renderer& operator=(Renderer&& other) noexcept - { - sdl_renderer_ = other.sdl_renderer_; - other.sdl_renderer_ = nullptr; - return *this; - } - - ~Renderer() - { - if (sdl_renderer_ != nullptr) { - SDL_DestroyRenderer(sdl_renderer_); - } - } - // TODO: Remove this when not needed anymore - SDL_Renderer* get_sdl_renderer() const + constexpr SDL_Renderer* get_sdl_renderer() const { - return sdl_renderer_; + return sdl_renderer_.get(); } // TODO: Rename clear/present to start_render/finish_render or similar? void clear() const { - SDL_RenderClear(sdl_renderer_); + SDL_RenderClear(sdl_renderer_.get()); } void present() const { - SDL_RenderPresent(sdl_renderer_); + SDL_RenderPresent(sdl_renderer_.get()); } // TODO: Replace SDL_Texture pointer with Texture class @@ -66,7 +45,7 @@ export namespace core // to just type-alias it?) void render_texture(SDL_Texture* texture, const SDL_FRect* src_rect, const SDL_FRect* dest_rect) const { - SDL_RenderTexture(sdl_renderer_, texture, src_rect, dest_rect); + SDL_RenderTexture(sdl_renderer_.get(), texture, src_rect, dest_rect); } }; } diff --git a/src/core/window.cppm b/src/core/window.cppm index 9bccb78..85bcc95 100644 --- a/src/core/window.cppm +++ b/src/core/window.cppm @@ -1,5 +1,6 @@ module; +#include #include #include @@ -7,13 +8,16 @@ export module core.window; import core.exceptions; import core.renderer; +import utils.memory; export namespace core { class Window { - // TODO: Use unique_ptr with custom deleters - SDL_Window* sdl_window_ = nullptr; + std::unique_ptr< + SDL_Window, + utils::FuncDeleter + > sdl_window_ = nullptr; public: Window() = default; @@ -22,32 +26,6 @@ export namespace core : sdl_window_(sdl_window) {} - // No copy operations - Window(const Window&) = delete; - Window& operator=(const Window&) = delete; - - // Move constructor - Window(Window&& other) noexcept - : sdl_window_(other.sdl_window_) - { - other.sdl_window_ = nullptr; - } - - // Move assignment - Window& operator=(Window&& other) noexcept - { - sdl_window_ = other.sdl_window_; - other.sdl_window_ = nullptr; - return *this; - } - - ~Window() - { - if (sdl_window_ != nullptr) { - SDL_DestroyWindow(sdl_window_); - } - } - static std::pair create_window_and_renderer( const std::string& title, const int width, diff --git a/src/game/game.cppm b/src/game/game.cppm index 4b7a6a1..8896bf2 100644 --- a/src/game/game.cppm +++ b/src/game/game.cppm @@ -8,6 +8,7 @@ export module game.game; import core.engine; import core.renderer; import game.sprite; +import resources.texture; export namespace game { @@ -25,11 +26,9 @@ export namespace game : engine_(engine) {} - // No copy operations + // No copy or move operations because we have a reference to the engine Game(const Game&) = delete; Game& operator=(const Game&) = delete; - - // No move operations - TODO? Game(Game&&) = delete; Game& operator=(Game&&) = delete; @@ -37,7 +36,12 @@ export namespace game void initialize() { - sprite_ = std::make_unique(engine_.get_renderer(), "assets/neocat.png", 100, 100); + auto texture = resources::Texture::load_from_file( + engine_.get_renderer().get_sdl_renderer(), + "assets/neocat.png" + ); + + sprite_ = std::make_unique(std::move(texture), 100, 100); } // Handles an SDL event. Returns true if the event has been handled. diff --git a/src/game/sprite.cppm b/src/game/sprite.cppm index 9c4d9b7..089656c 100644 --- a/src/game/sprite.cppm +++ b/src/game/sprite.cppm @@ -2,87 +2,53 @@ module; #include #include -#include export module game.sprite; import core.exceptions; import core.renderer; +import resources.texture; // TODO: Move this to a different namespace (core, drawing, ...?) export namespace game { class Sprite { - // TODO: Move texture to separate class - SDL_Texture* sdl_texture; - SDL_FRect dest_rect{0, 0, 0, 0}; + // 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}; public: explicit Sprite( - core::Renderer& renderer, - const std::string& filename, + resources::Texture&& texture, const int width, const int height ) + : texture_{std::move(texture)} { - SDL_Surface* texture_surface = IMG_Load(filename.c_str()); - if (texture_surface == nullptr) { - throw core::SDLException("IMG_Load"); - } - - sdl_texture = SDL_CreateTextureFromSurface(renderer.get_sdl_renderer(), texture_surface); - SDL_DestroySurface(texture_surface); - - if (sdl_texture == nullptr) { - throw core::SDLException("SDL_CreateTextureFromSurface"); - } - - dest_rect.w = static_cast(width); - dest_rect.h = static_cast(height); + dest_rect_.w = static_cast(width); + dest_rect_.h = static_cast(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; - } + // Default move operations + Sprite(Sprite&& other) = default; + Sprite& operator=(Sprite&& other) = default; - // 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); - } - } + ~Sprite() = default; void move(const float x, const float y) { - dest_rect.x = x; - dest_rect.y = y; + dest_rect_.x = x; + dest_rect_.y = y; } void draw(const core::Renderer& renderer) const { - renderer.render_texture(sdl_texture, nullptr, &dest_rect); + renderer.render_texture(texture_.get_sdl_texture(), nullptr, &dest_rect_); } }; } diff --git a/src/resources/texture.cppm b/src/resources/texture.cppm new file mode 100644 index 0000000..bb724fa --- /dev/null +++ b/src/resources/texture.cppm @@ -0,0 +1,63 @@ +module; + +#include +#include +#include +#include + +export module resources.texture; + +import core.exceptions; +import utils.memory; + +export namespace resources +{ + class Texture + { + std::unique_ptr< + SDL_Texture, + utils::FuncDeleter + > 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(); + } + }; +} diff --git a/src/utils/memory.cppm b/src/utils/memory.cppm new file mode 100644 index 0000000..d1e16ad --- /dev/null +++ b/src/utils/memory.cppm @@ -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 + struct FuncDeleter + { + template + constexpr void operator()(T* ptr) const noexcept + { + delete_func(ptr); + } + }; +} From d180b8a5b4c81fbf08d89653eea952e62132983d Mon Sep 17 00:00:00 2001 From: binaryDiv Date: Sun, 23 Nov 2025 21:50:57 +0100 Subject: [PATCH 3/4] Add wrappers for SDL functions and structs --- shuriken.yaml | 2 +- src/app.cppm | 49 +++---- src/config.cppm | 6 +- src/core/engine.cppm | 54 +++++--- src/core/renderer.cppm | 42 +++--- src/core/window.cppm | 48 ------- src/game/game.cppm | 12 +- src/game/sprite.cppm | 13 +- src/main.cpp | 8 +- src/resources/texture.cppm | 63 --------- src/wrappers/sdl.cppm | 11 ++ .../sdl/error.cppm} | 4 +- src/wrappers/sdl/events.cppm | 11 ++ src/wrappers/sdl/init.cppm | 62 +++++++++ src/wrappers/sdl/rect.cppm | 42 ++++++ src/wrappers/sdl/render.cppm | 125 ++++++++++++++++++ src/wrappers/sdl/video.cppm | 36 +++++ src/wrappers/sdl_image.cppm | 28 ++++ 18 files changed, 410 insertions(+), 206 deletions(-) delete mode 100644 src/core/window.cppm delete mode 100644 src/resources/texture.cppm create mode 100644 src/wrappers/sdl.cppm rename src/{core/exceptions.cppm => wrappers/sdl/error.cppm} (92%) create mode 100644 src/wrappers/sdl/events.cppm create mode 100644 src/wrappers/sdl/init.cppm create mode 100644 src/wrappers/sdl/rect.cppm create mode 100644 src/wrappers/sdl/render.cppm create mode 100644 src/wrappers/sdl/video.cppm create mode 100644 src/wrappers/sdl_image.cppm diff --git a/shuriken.yaml b/shuriken.yaml index 1c14083..5983465 100644 --- a/shuriken.yaml +++ b/shuriken.yaml @@ -8,4 +8,4 @@ default_target: linux targets: linux: output_file: rutile_game - cpp_flags_extra: -DDEBUG + # TODO: In a release build, set -DNDEBUG diff --git a/src/app.cppm b/src/app.cppm index 37c5483..fe1ab7f 100644 --- a/src/app.cppm +++ b/src/app.cppm @@ -2,15 +2,14 @@ module; #include #include -#include #include 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(); - engine_->initialize(); + engine_ = core::Engine::create(); game_ = std::make_unique(*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 diff --git a/src/config.cppm b/src/config.cppm index 4b522d1..0ed9a38 100644 --- a/src/config.cppm +++ b/src/config.cppm @@ -2,10 +2,10 @@ module; #include -#ifdef DEBUG -#define DEBUG_BOOL true -#else +#ifdef NDEBUG #define DEBUG_BOOL false +#else +#define DEBUG_BOOL true #endif export module config; diff --git a/src/core/engine.cppm b/src/core/engine.cppm index 2af5b7b..aae33ba 100644 --- a/src/core/engine.cppm +++ b/src/core/engine.cppm @@ -1,17 +1,19 @@ module; #include -#include +#include #include 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,13 +25,16 @@ export namespace core Window window_; Renderer renderer_; + // Private constructor + Engine(Window&& window, Renderer&& renderer) + : window_{std::move(window)}, + renderer_{std::move(renderer)} + { + instantiated_ = true; + } + public: - Engine() - { - // Prevent the class from being instantiated multiple times - assert(!instantiated_); - instantiated_ = true; - } + Engine() = delete; // No copy or move operations Engine(const Engine&) = delete; @@ -42,6 +47,26 @@ export namespace core instantiated_ = false; } + static std::unique_ptr 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( + 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 diff --git a/src/core/renderer.cppm b/src/core/renderer.cppm index 337c6f5..1704c2f 100644 --- a/src/core/renderer.cppm +++ b/src/core/renderer.cppm @@ -1,51 +1,47 @@ module; -#include -#include +#include 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_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); } }; } diff --git a/src/core/window.cppm b/src/core/window.cppm deleted file mode 100644 index 85bcc95..0000000 --- a/src/core/window.cppm +++ /dev/null @@ -1,48 +0,0 @@ -module; - -#include -#include -#include - -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_window_ = nullptr; - - public: - Window() = default; - - explicit Window(SDL_Window* sdl_window) - : sdl_window_(sdl_window) - {} - - static std::pair 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}}; - } - }; -} diff --git a/src/game/game.cppm b/src/game/game.cppm index 8896bf2..4216681 100644 --- a/src/game/game.cppm +++ b/src/game/game.cppm @@ -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() diff --git a/src/game/sprite.cppm b/src/game/sprite.cppm index 089656c..7221d2a 100644 --- a/src/game/sprite.cppm +++ b/src/game/sprite.cppm @@ -1,13 +1,12 @@ module; -#include +#include #include 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_); } }; } diff --git a/src/main.cpp b/src/main.cpp index be80748..4a0a4fc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(app->initialize()); } SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) { auto* app = static_cast(appstate); - return app->handle_event(event); + return static_cast(app->handle_event(event)); } SDL_AppResult SDL_AppIterate(void* appstate) { auto* app = static_cast(appstate); - return app->iterate(); + return static_cast(app->iterate()); } void SDL_AppQuit(void* appstate, const SDL_AppResult /*result*/) { - auto* app = static_cast(appstate); + const auto* app = static_cast(appstate); app->shutdown(); delete app; } diff --git a/src/resources/texture.cppm b/src/resources/texture.cppm deleted file mode 100644 index bb724fa..0000000 --- a/src/resources/texture.cppm +++ /dev/null @@ -1,63 +0,0 @@ -module; - -#include -#include -#include -#include - -export module resources.texture; - -import core.exceptions; -import utils.memory; - -export namespace resources -{ - class Texture - { - std::unique_ptr< - SDL_Texture, - utils::FuncDeleter - > 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(); - } - }; -} diff --git a/src/wrappers/sdl.cppm b/src/wrappers/sdl.cppm new file mode 100644 index 0000000..8e8088d --- /dev/null +++ b/src/wrappers/sdl.cppm @@ -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; diff --git a/src/core/exceptions.cppm b/src/wrappers/sdl/error.cppm similarity index 92% rename from src/core/exceptions.cppm rename to src/wrappers/sdl/error.cppm index 2ee8696..ceaedf0 100644 --- a/src/core/exceptions.cppm +++ b/src/wrappers/sdl/error.cppm @@ -4,9 +4,9 @@ module; #include #include -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. diff --git a/src/wrappers/sdl/events.cppm b/src/wrappers/sdl/events.cppm new file mode 100644 index 0000000..a168af0 --- /dev/null +++ b/src/wrappers/sdl/events.cppm @@ -0,0 +1,11 @@ +module; + +#include + +export module wrappers.sdl.events; + +export namespace sdl +{ + // Simple alias for SDL_Event union + using Event = SDL_Event; +} diff --git a/src/wrappers/sdl/init.cppm b/src/wrappers/sdl/init.cppm new file mode 100644 index 0000000..9044776 --- /dev/null +++ b/src/wrappers/sdl/init.cppm @@ -0,0 +1,62 @@ +module; + +#include +#include + +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 + { + 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"); + } + } +} diff --git a/src/wrappers/sdl/rect.cppm b/src/wrappers/sdl/rect.cppm new file mode 100644 index 0000000..a42648a --- /dev/null +++ b/src/wrappers/sdl/rect.cppm @@ -0,0 +1,42 @@ +module; + +#include + +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); + } + }; +} diff --git a/src/wrappers/sdl/render.cppm b/src/wrappers/sdl/render.cppm new file mode 100644 index 0000000..1838e1b --- /dev/null +++ b/src/wrappers/sdl/render.cppm @@ -0,0 +1,125 @@ +module; + +#include +#include +#include + +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 + > 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 + > 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 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}}; + } +} diff --git a/src/wrappers/sdl/video.cppm b/src/wrappers/sdl/video.cppm new file mode 100644 index 0000000..126033c --- /dev/null +++ b/src/wrappers/sdl/video.cppm @@ -0,0 +1,36 @@ +module; + +#include +#include +#include + +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 + > 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(); + } + }; +} diff --git a/src/wrappers/sdl_image.cppm b/src/wrappers/sdl_image.cppm new file mode 100644 index 0000000..a434564 --- /dev/null +++ b/src/wrappers/sdl_image.cppm @@ -0,0 +1,28 @@ +module; + +#include +#include +#include + +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}; + } +} From e37eb0b03ff3ff39c2e18a773d480efea3298598 Mon Sep 17 00:00:00 2001 From: binaryDiv Date: Sun, 23 Nov 2025 23:46:10 +0100 Subject: [PATCH 4/4] Replace Game::initialize() with factory method --- src/app.cppm | 9 ++++---- src/core/engine.cppm | 4 ++-- src/game/game.cppm | 45 +++++++++++++++++++++++++----------- src/game/sprite.cppm | 10 -------- src/wrappers/sdl/render.cppm | 8 +++---- src/wrappers/sdl/video.cppm | 4 ++-- 6 files changed, 43 insertions(+), 37 deletions(-) diff --git a/src/app.cppm b/src/app.cppm index fe1ab7f..7b59948 100644 --- a/src/app.cppm +++ b/src/app.cppm @@ -15,8 +15,8 @@ export { class App { - std::unique_ptr engine_ = nullptr; - std::unique_ptr game_ = nullptr; + std::unique_ptr engine_{nullptr}; + std::unique_ptr game_{nullptr}; public: App() = default; @@ -36,10 +36,9 @@ export // 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_ = std::make_unique(*engine_); - game_->initialize(); + game_ = game::Game::create(*engine_); } catch (const std::runtime_error& e) { std::cerr << "Unhandled exception during initialization: " << e.what() << '\n'; diff --git a/src/core/engine.cppm b/src/core/engine.cppm index aae33ba..3d50323 100644 --- a/src/core/engine.cppm +++ b/src/core/engine.cppm @@ -59,12 +59,12 @@ export namespace core 0 ); - return std::unique_ptr( + return std::unique_ptr{ new Engine{ std::move(sdl_window), Renderer{std::move(sdl_renderer)} } - ); + }; } bool keep_running() const diff --git a/src/game/game.cppm b/src/game/game.cppm index 4216681..1d68ace 100644 --- a/src/game/game.cppm +++ b/src/game/game.cppm @@ -1,5 +1,6 @@ module; +#include #include #include @@ -15,35 +16,49 @@ 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_; // Sprite for testing - std::unique_ptr sprite_ = nullptr; + std::unique_ptr sprite_{nullptr}; + + // Private constructor + explicit Game(core::Engine& engine) + : engine_(engine) + { + // 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" + ); + + sprite_ = std::make_unique(std::move(texture), 100, 100); + } public: Game() = delete; - explicit Game(core::Engine& engine) - : engine_(engine) - {} - // 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() = default; - - void initialize() + ~Game() { - // 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" - ); + instantiated_ = false; + } - sprite_ = std::make_unique(std::move(texture), 100, 100); + static std::unique_ptr create(core::Engine& engine) + { + // Prevent the class from being instantiated multiple times + assert(!instantiated_); + return std::unique_ptr{ + new Game(engine) + }; } // Handles an SDL event. Returns true if the event has been handled. @@ -75,4 +90,6 @@ export namespace game { } }; + + bool Game::instantiated_ = false; } diff --git a/src/game/sprite.cppm b/src/game/sprite.cppm index 7221d2a..901ea28 100644 --- a/src/game/sprite.cppm +++ b/src/game/sprite.cppm @@ -29,16 +29,6 @@ export namespace game dest_rect_.h = static_cast(height); } - // Don't allow copy operations - Sprite(const Sprite&) = delete; - Sprite& operator=(const Sprite&) = delete; - - // Default move operations - Sprite(Sprite&& other) = default; - Sprite& operator=(Sprite&& other) = default; - - ~Sprite() = default; - void move(const float x, const float y) { dest_rect_.x = x; diff --git a/src/wrappers/sdl/render.cppm b/src/wrappers/sdl/render.cppm index 1838e1b..e0b28c0 100644 --- a/src/wrappers/sdl/render.cppm +++ b/src/wrappers/sdl/render.cppm @@ -21,10 +21,10 @@ export namespace sdl std::unique_ptr< SDL_Texture, utils::FuncDeleter - > raw_texture_ = nullptr; + > raw_texture_; public: - Texture() = default; + Texture() = delete; explicit Texture(SDL_Texture* raw_texture) : raw_texture_{raw_texture} @@ -45,10 +45,10 @@ export namespace sdl std::unique_ptr< SDL_Renderer, utils::FuncDeleter - > raw_renderer_ = nullptr; + > raw_renderer_; public: - Renderer() = default; + Renderer() = delete; explicit Renderer(SDL_Renderer* raw_renderer) : raw_renderer_{raw_renderer} diff --git a/src/wrappers/sdl/video.cppm b/src/wrappers/sdl/video.cppm index 126033c..f80c93f 100644 --- a/src/wrappers/sdl/video.cppm +++ b/src/wrappers/sdl/video.cppm @@ -18,10 +18,10 @@ export namespace sdl std::unique_ptr< SDL_Window, utils::FuncDeleter - > raw_window_ = nullptr; + > raw_window_; public: - Window() = default; + Window() = delete; explicit Window(SDL_Window* raw_window) : raw_window_{raw_window}