From 506de66aaecc9b82415dde46058b848e46bc7258 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 8 Nov 2024 20:21:36 +0100 Subject: nitpicks (merge #27) --- src/crepe/api/Animator.cpp | 4 +- src/crepe/api/Animator.h | 6 +-- src/crepe/api/Camera.cpp | 9 ++-- src/crepe/api/Camera.h | 42 ++++++++-------- src/crepe/api/Color.h | 4 +- src/crepe/api/Sprite.cpp | 4 +- src/crepe/api/Sprite.h | 17 +++---- src/crepe/api/Texture.cpp | 4 +- src/crepe/api/Texture.h | 76 ++++++++++++++--------------- src/crepe/facade/SDLContext.cpp | 33 ++++++++----- src/crepe/facade/SDLContext.h | 95 ++++++++++++++++++------------------- src/crepe/system/AnimatorSystem.cpp | 13 +++-- src/crepe/system/AnimatorSystem.h | 22 ++------- src/crepe/system/RenderSystem.cpp | 9 ++-- src/crepe/system/RenderSystem.h | 80 +++++++++++++------------------ 15 files changed, 195 insertions(+), 223 deletions(-) diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 4b4d4be..8b396af 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -3,14 +3,14 @@ #include "util/log.h" +#include "Animator.h" #include "Component.h" #include "Sprite.h" -#include "Animator.h" using namespace crepe; Animator::Animator(uint32_t id, Sprite & ss, int row, int col, int col_animator) - : Component(id), spritesheet(ss), row(row), col(col){ + : Component(id), spritesheet(ss), row(row), col(col) { dbg_trace(); animator_rect = spritesheet.sprite_rect; diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index ae0a896..def0240 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -9,7 +9,6 @@ namespace crepe { class AnimatorSystem; class SDLContext; - /** * \brief The Animator component is used to animate sprites by managing the movement * and frame changes within a sprite sheet. @@ -39,8 +38,7 @@ public: Animator(uint32_t id, Sprite & spritesheet, int row, int col, int col_animate); - - ~Animator(); + ~Animator(); // dbg_trace Animator(const Animator &) = delete; Animator(Animator &&) = delete; Animator & operator=(const Animator &) = delete; @@ -71,7 +69,7 @@ private: //! AnimatorSystem adjust the private member parameters of Animator; friend class AnimatorSystem; - //! SDLContext reads the Animator member var's + //! SDLContext reads the Animator member var's friend class SDLContext; }; } // namespace crepe diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index dbbfb32..820a6a8 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -3,15 +3,14 @@ #include "util/log.h" -#include "Component.h" -#include "Color.h" #include "Camera.h" +#include "Color.h" +#include "Component.h" using namespace crepe; -Camera::Camera(uint32_t id, const Color & color) - : Component(id), bg_color(color), aspect_width(640), aspect_height(480), - zoom(1), x(0), y(0) { +Camera::Camera(uint32_t id, const Color & bg_color) + : Component(id), bg_color(bg_color) { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index 708a523..ba3a9ef 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -19,41 +19,37 @@ class Camera : public Component { public: /** - * \brief Constructs a Camera with the specified ID and background color. - * \param id Unique identifier for the camera component. - * \param bg_color Background color for the camera view. - */ + * \brief Constructs a Camera with the specified ID and background color. + * \param id Unique identifier for the camera component. + * \param bg_color Background color for the camera view. + */ Camera(uint32_t id, const Color & bg_color); - - /** - * \brief Destroys the Camera instance. - */ - ~Camera(); + ~Camera(); // dbg_trace only public: - //! \brief Background color of the camera view. + //! Background color of the camera view. Color bg_color; - //! \brief Aspect ratio height for the camera. - double aspect_height; + //! Aspect ratio height for the camera. + double aspect_height = 480; - //! \brief Aspect ratio width for the camera. - double aspect_width; + //! Aspect ratio width for the camera. + double aspect_width = 640; - //! \brief X-coordinate of the camera position. - double x; + //! X-coordinate of the camera position. + double x = 0.0; - //! \brief Y-coordinate of the camera position. - double y; + //! Y-coordinate of the camera position. + double y = 0.0; - //! \brief Zoom level of the camera view. - double zoom; + //! Zoom level of the camera view. + double zoom = 1.0; public: /** - * \brief Gets the maximum number of camera instances allowed. - * \return Maximum instance count as an integer. - */ + * \brief Gets the maximum number of camera instances allowed. + * \return Maximum instance count as an integer. + */ virtual int get_instances_max() const { return 10; } }; } // namespace crepe diff --git a/src/crepe/api/Color.h b/src/crepe/api/Color.h index 4ebe3a3..aa47bf4 100644 --- a/src/crepe/api/Color.h +++ b/src/crepe/api/Color.h @@ -2,8 +2,9 @@ #include -namespace crepe{ +namespace crepe { +// TODO: make Color a struct w/o constructors/destructors class Color { // FIXME: can't these colors be defined as a `static constexpr const Color` @@ -21,6 +22,7 @@ public: static const Color & get_black(); private: + // TODO: why are these private!? uint8_t r; uint8_t g; uint8_t b; diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index db96c32..f9cd761 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -1,11 +1,11 @@ #include -#include "facade/SDLContext.h" #include "../util/log.h" +#include "facade/SDLContext.h" #include "Component.h" -#include "Texture.h" #include "Sprite.h" +#include "Texture.h" using namespace std; using namespace crepe; diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 1db32d7..deb3f93 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -4,22 +4,21 @@ #include #include "Color.h" -#include "Texture.h" #include "Component.h" - +#include "Texture.h" namespace crepe { struct Rect { int w = 0; - int h = 0; - int x = 0; - int y = 0; + int h = 0; + int x = 0; + int y = 0; }; struct FlipSettings { - bool flip_x = false; - bool flip_y = false; + bool flip_x = false; + bool flip_y = false; }; class SDLContext; @@ -35,7 +34,6 @@ class AnimatorSystem; class Sprite : public Component { public: - // TODO: Loek comment in github #27 will be looked another time // about shared_ptr Texture /** @@ -53,10 +51,9 @@ public: */ ~Sprite(); - //! Texture used for the sprite const std::shared_ptr sprite_image; - //! Color tint of the sprite + //! Color tint of the sprite Color color; //! Flip settings for the sprite FlipSettings flip; diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index c31f704..5ebd23d 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -29,11 +29,11 @@ void Texture::load(unique_ptr res) { this->texture = std::move(ctx.texture_from_path(res->canonical())); } -int Texture::get_width() const{ +int Texture::get_width() const { if (this->texture == nullptr) return 0; return SDLContext::get_instance().get_width(*this); } -int Texture::get_height() const{ +int Texture::get_height() const { if (this->texture == nullptr) return 0; return SDLContext::get_instance().get_width(*this); } diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h index 9bda5fe..b89bc17 100644 --- a/src/crepe/api/Texture.h +++ b/src/crepe/api/Texture.h @@ -25,51 +25,53 @@ class Animator; class Texture { public: - /** - * \brief Constructs a Texture from a file path. - * \param src Path to the image file to be loaded as a texture. - */ - Texture(const char * src); - - /** - * \brief Constructs a Texture from an Asset resource. - * \param res Unique pointer to an Asset resource containing texture data. - */ - Texture(std::unique_ptr res); - - /** - * \brief Destroys the Texture instance, freeing associated resources. - */ - ~Texture(); - - /** - * \brief Gets the width of the texture. - * \return Width of the texture in pixels. - */ - int get_width() const; - - /** - * \brief Gets the height of the texture. - * \return Height of the texture in pixels. - */ - int get_height() const; + /** + * \brief Constructs a Texture from a file path. + * \param src Path to the image file to be loaded as a texture. + */ + Texture(const char * src); + + /** + * \brief Constructs a Texture from an Asset resource. + * \param res Unique pointer to an Asset resource containing texture data. + */ + Texture(std::unique_ptr res); + + /** + * \brief Destroys the Texture instance, freeing associated resources. + */ + ~Texture(); + // FIXME: this constructor shouldn't be necessary because this class doesn't + // manage memory + + /** + * \brief Gets the width of the texture. + * \return Width of the texture in pixels. + */ + int get_width() const; + + /** + * \brief Gets the height of the texture. + * \return Height of the texture in pixels. + */ + int get_height() const; private: - /** - * \brief Loads the texture from an Asset resource. - * \param res Unique pointer to an Asset resource to load the texture from. - */ - void load(std::unique_ptr res); + /** + * \brief Loads the texture from an Asset resource. + * \param res Unique pointer to an Asset resource to load the texture from. + */ + void load(std::unique_ptr res); private: //! The texture of the class from the library std::unique_ptr> texture; - //! Grants SDLContext access to private members. - friend class SDLContext; + //! Grants SDLContext access to private members. + friend class SDLContext; - //! Grants Animator access to private members. - friend class Animator; + //! Grants Animator access to private members. + friend class Animator; }; } // namespace crepe diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 5b9ca71..c4c96e2 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -46,11 +46,11 @@ SDLContext::SDLContext() { << SDL_GetError() << std::endl; return; } - this->game_window = {tmp_window, [](SDL_Window* window) { SDL_DestroyWindow(window); }}; + this->game_window + = {tmp_window, [](SDL_Window * window) { SDL_DestroyWindow(window); }}; - - SDL_Renderer* tmp_renderer = SDL_CreateRenderer(this->game_window.get(), -1, - SDL_RENDERER_ACCELERATED); + SDL_Renderer * tmp_renderer = SDL_CreateRenderer( + this->game_window.get(), -1, SDL_RENDERER_ACCELERATED); if (!tmp_renderer) { // FIXME: throw exception std::cerr << "Renderer could not be created! SDL_Error: " @@ -59,7 +59,9 @@ SDLContext::SDLContext() { return; } - this->game_renderer = {tmp_renderer, [](SDL_Renderer* renderer) { SDL_DestroyRenderer(renderer); }}; + this->game_renderer = {tmp_renderer, [](SDL_Renderer * renderer) { + SDL_DestroyRenderer(renderer); + }}; int img_flags = IMG_INIT_PNG; if (!(IMG_Init(img_flags) & img_flags)) { @@ -150,27 +152,32 @@ void SDLContext::camera(const Camera & cam) { cam.bg_color.g, cam.bg_color.b, cam.bg_color.a); } -const uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); } +uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); } -std::unique_ptr> SDLContext::texture_from_path(const std::string & path) { +std::unique_ptr> +SDLContext::texture_from_path(const std::string & path) { SDL_Surface * tmp = IMG_Load(path.c_str()); - if (tmp == nullptr) { + if (tmp == nullptr) { throw Exception("surface cannot be load from %s", path.c_str()); } - std::unique_ptr> img_surface; - img_surface = {tmp, [](SDL_Surface * surface) { SDL_FreeSurface(surface); }}; + std::unique_ptr> + img_surface; + img_surface + = {tmp, [](SDL_Surface * surface) { SDL_FreeSurface(surface); }}; SDL_Texture * tmp_texture = SDL_CreateTextureFromSurface( this->game_renderer.get(), img_surface.get()); - if ( tmp_texture == nullptr) { + if (tmp_texture == nullptr) { throw Exception("Texture cannot be load from %s", path.c_str()); } - std::unique_ptr> img_texture; - img_texture = {tmp_texture, [](SDL_Texture * texture) { SDL_DestroyTexture(texture); }}; + std::unique_ptr> + img_texture; + img_texture = {tmp_texture, + [](SDL_Texture * texture) { SDL_DestroyTexture(texture); }}; return img_texture; } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 93166f2..e358c21 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -11,13 +11,15 @@ #include "../api/Transform.h" #include "api/Camera.h" -//FIXME: this needs to be removed +// FIXME: this needs to be removed const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; namespace crepe { -//TODO: Wouter will fix this. cause user cannot this at the moment +// TODO: SDL_Keycode is defined in a header not distributed with crepe, which +// means this typedef is unusable when crepe is packaged. Wouter will fix this +// later. typedef SDL_Keycode CREPE_KEYCODES; class Texture; @@ -34,9 +36,9 @@ class SDLContext { public: /** - * \brief Gets the singleton instance of SDLContext. - * \return Reference to the SDLContext instance. - */ + * \brief Gets the singleton instance of SDLContext. + * \return Reference to the SDLContext instance. + */ static SDLContext & get_instance(); SDLContext(const SDLContext &) = delete; @@ -48,9 +50,9 @@ private: //! will only use handle_events friend class LoopManager; /** - * \brief Handles SDL events such as window close and input. - * \param running Reference to a boolean flag that controls the main loop. - */ + * \brief Handles SDL events such as window close and input. + * \param running Reference to a boolean flag that controls the main loop. + */ void handle_events(bool & running); private: @@ -58,22 +60,22 @@ private: friend class AnimatorSystem; /** - * \brief Gets the current SDL ticks since the program started. - * \return Current ticks in milliseconds as a constant uint64_t. - */ - const uint64_t get_ticks() const; + * \brief Gets the current SDL ticks since the program started. + * \return Current ticks in milliseconds as a constant uint64_t. + */ + uint64_t get_ticks() const; private: /** - * \brief Constructs an SDLContext instance. - * Initializes SDL, creates a window and renderer. - */ + * \brief Constructs an SDLContext instance. + * Initializes SDL, creates a window and renderer. + */ SDLContext(); /** - * \brief Destroys the SDLContext instance. - * Cleans up SDL resources, including the window and renderer. - */ + * \brief Destroys the SDLContext instance. + * Cleans up SDL resources, including the window and renderer. + */ ~SDLContext(); private: @@ -84,23 +86,24 @@ private: friend class Animator; /** - * \brief Loads a texture from a file path. - * \param path Path to the image file. - * \return Pointer to the created SDL_Texture. - */ - std::unique_ptr> texture_from_path(const std::string & path); + * \brief Loads a texture from a file path. + * \param path Path to the image file. + * \return Pointer to the created SDL_Texture. + */ + std::unique_ptr> + texture_from_path(const std::string & path); /** - * \brief Gets the width of a texture. - * \param texture Reference to the Texture object. - * \return Width of the texture as an integer. - */ + * \brief Gets the width of a texture. + * \param texture Reference to the Texture object. + * \return Width of the texture as an integer. + */ int get_width(const Texture &) const; /** - * \brief Gets the height of a texture. - * \param texture Reference to the Texture object. - * \return Height of the texture as an integer. - */ + * \brief Gets the height of a texture. + * \param texture Reference to the Texture object. + * \return Height of the texture as an integer. + */ int get_height(const Texture &) const; private: @@ -108,37 +111,33 @@ private: friend class RenderSystem; /** - * \brief Draws a sprite to the screen using the specified transform and camera. - * \param sprite Reference to the Sprite to draw. - * \param transform Reference to the Transform for positioning. - * \param camera Reference to the Camera for view adjustments. - */ + * \brief Draws a sprite to the screen using the specified transform and camera. + * \param sprite Reference to the Sprite to draw. + * \param transform Reference to the Transform for positioning. + * \param camera Reference to the Camera for view adjustments. + */ void draw(const Sprite & sprite, const Transform & transform, const Camera & camera); - /** - * \brief Clears the screen, preparing for a new frame. - */ + //! Clears the screen, preparing for a new frame. void clear_screen(); - /** - * \brief Presents the rendered frame to the screen. - */ + //! Presents the rendered frame to the screen. void present_screen(); /** - * \brief Sets the current camera for rendering. - * \param camera Reference to the Camera object. - */ + * \brief Sets the current camera for rendering. + * \param camera Reference to the Camera object. + */ void camera(const Camera & camera); private: - //! sdl Window - std::unique_ptr> game_window; + std::unique_ptr> game_window; //! renderer for the crepe engine - std::unique_ptr> game_renderer; + std::unique_ptr> + game_renderer; //! viewport for the camera window SDL_Rect viewport = {0, 0, 640, 480}; diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 4ea889a..bf45362 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -3,17 +3,16 @@ #include #include +#include "api/Animator.h" #include "facade/SDLContext.h" #include "util/log.h" -#include "api/Animator.h" -#include "ComponentManager.h" #include "AnimatorSystem.h" +#include "ComponentManager.h" using namespace crepe; AnimatorSystem::AnimatorSystem() { dbg_trace(); } - AnimatorSystem::~AnimatorSystem() { dbg_trace(); } AnimatorSystem & AnimatorSystem::get_instance() { @@ -22,12 +21,13 @@ AnimatorSystem & AnimatorSystem::get_instance() { } void AnimatorSystem::update() { - ComponentManager& mgr = ComponentManager::get_instance(); + ComponentManager & mgr = ComponentManager::get_instance(); - std::vector> animations = mgr.get_components_by_type(); + std::vector> animations + = mgr.get_components_by_type(); uint64_t tick = SDLContext::get_instance().get_ticks(); - for(Animator& a : animations){ + for (Animator & a : animations) { if (a.active) { a.curr_row = (tick / 100) % a.row; a.animator_rect.x = (a.curr_row * a.animator_rect.w) + a.curr_col; @@ -35,4 +35,3 @@ void AnimatorSystem::update() { } } } - diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h index c377ce9..969e9d1 100644 --- a/src/crepe/system/AnimatorSystem.h +++ b/src/crepe/system/AnimatorSystem.h @@ -2,9 +2,7 @@ #include "System.h" - - -//TODO: +//TODO: // control if flip works with animation system namespace crepe { @@ -38,21 +36,9 @@ public: void update() override; private: - /** - * \brief Private constructor for the AnimatorSystem. - * - * The constructor is private to enforce the singleton pattern, ensuring that only - * one instance of this system can exist. - */ - AnimatorSystem(); - - /** - * \brief Private destructor for the AnimatorSystem. - * - * The destructor cleans up any resources used by the AnimatorSystem. It is private - * to maintain the singleton pattern and prevent direct deletion. - */ - ~AnimatorSystem(); + // private because singleton + AnimatorSystem(); // dbg_trace + ~AnimatorSystem(); // dbg_trace }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 849d810..10211a3 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -20,7 +20,9 @@ RenderSystem & RenderSystem::get_instance() { return instance; } -void RenderSystem::clear_screen() const { SDLContext::get_instance().clear_screen(); } +void RenderSystem::clear_screen() const { + SDLContext::get_instance().clear_screen(); +} void RenderSystem::present_screen() const { SDLContext::get_instance().present_screen(); @@ -45,8 +47,9 @@ void RenderSystem::render_sprites() const { SDLContext & render = SDLContext::get_instance(); for (const Sprite & sprite : sprites) { - auto transforms = mgr.get_components_by_id(sprite.game_object_id); - render.draw(sprite, transforms[0] , *curr_cam); + auto transforms + = mgr.get_components_by_id(sprite.game_object_id); + render.draw(sprite, transforms[0], *curr_cam); } } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index ec80a0e..70db21a 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -4,7 +4,6 @@ #include "System.h" - namespace crepe { /** @@ -18,63 +17,48 @@ namespace crepe { class RenderSystem : public System { public: - /** - * \brief Gets the singleton instance of RenderSystem. - * \return Reference to the RenderSystem instance. - */ - static RenderSystem & get_instance(); + /** + * \brief Gets the singleton instance of RenderSystem. + * \return Reference to the RenderSystem instance. + */ + static RenderSystem & get_instance(); - /** - * \brief Updates the RenderSystem for the current frame. - * This method is called to perform all rendering operations for the current game frame. - */ - void update() override; + /** + * \brief Updates the RenderSystem for the current frame. + * This method is called to perform all rendering operations for the current game frame. + */ + void update() override; private: - /** - * \brief Constructs a RenderSystem instance. - * Private constructor to enforce singleton pattern. - */ - RenderSystem(); - - /** - * \brief Destroys the RenderSystem instance. - */ - ~RenderSystem(); + // Private constructor to enforce singleton pattern. + RenderSystem(); + ~RenderSystem(); - /** - * \brief Clears the screen in preparation for rendering. - */ - void clear_screen() const; + //! Clears the screen in preparation for rendering. + void clear_screen() const; - /** - * \brief Presents the rendered frame to the display. - */ - void present_screen() const; + //! Presents the rendered frame to the display. + void present_screen() const; - /** - * \brief Updates the active camera used for rendering. - */ - void update_camera(); + //! Updates the active camera used for rendering. + void update_camera(); - /** - * \brief Renders all active sprites to the screen. - */ - void render_sprites() const; + //! Renders all active sprites to the screen. + void render_sprites() const; - /** - * \todo Include color handling for sprites. - * \todo Implement particle emitter rendering with sprites. - * \todo Add text rendering using SDL_ttf for text components. - * \todo Implement a text component and a button component. - * \todo Ensure each sprite is checked for active status before rendering. - * \todo Sort all layers by order before rendering. - * \todo Consider adding text input functionality. - */ + /** + * \todo Include color handling for sprites. + * \todo Implement particle emitter rendering with sprites. + * \todo Add text rendering using SDL_ttf for text components. + * \todo Implement a text component and a button component. + * \todo Ensure each sprite is checked for active status before rendering. + * \todo Sort all layers by order before rendering. + * \todo Consider adding text input functionality. + */ private: //! Pointer to the current active camera for rendering - // \todo needs a better solution - Camera * curr_cam; + Camera * curr_cam = nullptr; + // TODO: needs a better solution }; } // namespace crepe -- cgit v1.2.3