diff options
Diffstat (limited to 'src/crepe/system')
-rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 37 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.h | 44 | ||||
-rw-r--r-- | src/crepe/system/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 36 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.h | 49 |
5 files changed, 159 insertions, 9 deletions
diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp new file mode 100644 index 0000000..bf45362 --- /dev/null +++ b/src/crepe/system/AnimatorSystem.cpp @@ -0,0 +1,37 @@ + +#include <cstdint> +#include <functional> +#include <vector> + +#include "api/Animator.h" +#include "facade/SDLContext.h" +#include "util/log.h" + +#include "AnimatorSystem.h" +#include "ComponentManager.h" + +using namespace crepe; + +AnimatorSystem::AnimatorSystem() { dbg_trace(); } +AnimatorSystem::~AnimatorSystem() { dbg_trace(); } + +AnimatorSystem & AnimatorSystem::get_instance() { + static AnimatorSystem instance; + return instance; +} + +void AnimatorSystem::update() { + ComponentManager & mgr = ComponentManager::get_instance(); + + std::vector<std::reference_wrapper<Animator>> animations + = mgr.get_components_by_type<Animator>(); + + uint64_t tick = SDLContext::get_instance().get_ticks(); + 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; + a.spritesheet.sprite_rect = a.animator_rect; + } + } +} diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h new file mode 100644 index 0000000..969e9d1 --- /dev/null +++ b/src/crepe/system/AnimatorSystem.h @@ -0,0 +1,44 @@ +#pragma once + +#include "System.h" + +//TODO: +// control if flip works with animation system + +namespace crepe { + +/** + * \brief The AnimatorSystem is responsible for managing and updating all Animator components. + * + * This system is responsible for controlling the behavior of the animations for all entities + * that have the Animator component attached. It updates the animations by controlling their + * frame changes, looping behavior, and overall animation state. + */ +class AnimatorSystem : public System { + +public: + /** + * \brief Retrieves the singleton instance of the AnimatorSystem. + * + * \return A reference to the single instance of the AnimatorSystem. + * + * This method ensures that there is only one instance of the AnimatorSystem, following the + * singleton design pattern. It can be used to access the system globally. + */ + static AnimatorSystem & get_instance(); + + /** + * \brief Updates the Animator components. + * + * This method is called periodically (likely every frame) to update the state of all + * Animator components, moving the animations forward and managing their behavior (e.g., looping). + */ + void update() override; + +private: + // private because singleton + AnimatorSystem(); // dbg_trace + ~AnimatorSystem(); // dbg_trace +}; + +} // namespace crepe diff --git a/src/crepe/system/CMakeLists.txt b/src/crepe/system/CMakeLists.txt index ff6f66f..4c18b87 100644 --- a/src/crepe/system/CMakeLists.txt +++ b/src/crepe/system/CMakeLists.txt @@ -4,6 +4,7 @@ target_sources(crepe PUBLIC PhysicsSystem.cpp CollisionSystem.cpp RenderSystem.cpp + AnimatorSystem.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -12,4 +13,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES PhysicsSystem.h CollisionSystem.h RenderSystem.h + AnimatorSystem.h ) diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 5a07cc2..10211a3 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -20,7 +20,25 @@ RenderSystem & RenderSystem::get_instance() { return instance; } -void RenderSystem::update() { +void RenderSystem::clear_screen() const { + SDLContext::get_instance().clear_screen(); +} + +void RenderSystem::present_screen() const { + SDLContext::get_instance().present_screen(); +} +void RenderSystem::update_camera() { + ComponentManager & mgr = ComponentManager::get_instance(); + + std::vector<std::reference_wrapper<Camera>> cameras + = mgr.get_components_by_type<Camera>(); + + for (Camera & cam : cameras) { + SDLContext::get_instance().camera(cam); + this->curr_cam = &cam; + } +} +void RenderSystem::render_sprites() const { ComponentManager & mgr = ComponentManager::get_instance(); @@ -28,14 +46,16 @@ void RenderSystem::update() { = mgr.get_components_by_type<Sprite>(); SDLContext & render = SDLContext::get_instance(); - render.clear_screen(); - for (const Sprite & sprite : sprites) { - std::vector<std::reference_wrapper<Transform>> transforms + auto transforms = mgr.get_components_by_id<Transform>(sprite.game_object_id); - for (const Transform & transform : transforms) { - render.draw(sprite, transform); - } + render.draw(sprite, transforms[0], *curr_cam); } - render.present_screen(); +} + +void RenderSystem::update() { + this->clear_screen(); + this->update_camera(); + this->render_sprites(); + this->present_screen(); } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 4b910a4..70db21a 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -1,17 +1,64 @@ #pragma once +#include "api/Camera.h" + #include "System.h" namespace crepe { +/** + * \class RenderSystem + * \brief Manages rendering operations for all game objects. + * + * RenderSystem is responsible for rendering sprites, clearing and presenting the screen, + * and managing the active camera. It functions as a singleton, providing centralized + * rendering services for the application. + */ class RenderSystem : public System { public: + /** + * \brief Gets the singleton instance of RenderSystem. + * \return Reference to the RenderSystem instance. + */ static RenderSystem & get_instance(); - void update(); + + /** + * \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: + // Private constructor to enforce singleton pattern. RenderSystem(); ~RenderSystem(); + + //! Clears the screen in preparation for rendering. + void clear_screen() const; + + //! Presents the rendered frame to the display. + void present_screen() const; + + //! Updates the active camera used for rendering. + void update_camera(); + + //! 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. + */ + +private: + //! Pointer to the current active camera for rendering + Camera * curr_cam = nullptr; + // TODO: needs a better solution }; } // namespace crepe |