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}; + } +}