diff options
Diffstat (limited to 'src/crepe/system')
-rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 27 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.h | 7 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 21 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.h | 11 |
4 files changed, 40 insertions, 26 deletions
diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 8bb6465..9aede7f 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -1,4 +1,4 @@ -#include <cstdint> + #include "../api/Animator.h" #include "../facade/SDLContext.h" @@ -13,12 +13,27 @@ void AnimatorSystem::update() { RefVector<Animator> animations = mgr.get_components_by_type<Animator>(); - uint64_t tick = SDLContext::get_instance().get_ticks(); + double elapsed_time = this->timer.get_current_time(); + for (Animator & a : animations) { if (!a.active) continue; - // (10 frames per second) - a.curr_row = (tick / 100) % a.row; - a.spritesheet.mask.x = (a.curr_row * a.spritesheet.mask.w) + a.curr_col; - a.spritesheet.mask = a.spritesheet.mask; + + Animator::Data & ctx = a.data; + double frame_duration = 1.0f / ctx.fps; + + int last_frame = ctx.curr_row; + + int cycle_end = (ctx.cycle_end == -1) ? a.row : ctx.cycle_end; + int total_frames = cycle_end - ctx.cycle_start; + + int curr_frame = static_cast<int>(elapsed_time / frame_duration) % total_frames; + + ctx.curr_row = ctx.cycle_start + curr_frame; + a.spritesheet.mask.x = ctx.curr_row * a.spritesheet.mask.w; + a.spritesheet.mask.y = (ctx.curr_col * a.spritesheet.mask.h); + + if (!ctx.looping && curr_frame == ctx.cycle_start && last_frame == total_frames - 1) { + a.active = false; + } } } diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h index f8179a9..e8a5158 100644 --- a/src/crepe/system/AnimatorSystem.h +++ b/src/crepe/system/AnimatorSystem.h @@ -1,9 +1,7 @@ #pragma once #include "System.h" - -//TODO: -// control if flip works with animation system +#include "api/LoopTimer.h" namespace crepe { @@ -26,6 +24,9 @@ public: * looping). */ void update() override; + +private: + LoopTimer & timer = LoopTimer::get_instance(); }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 92dba43..111ad7d 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -21,7 +21,7 @@ void RenderSystem::clear_screen() { this->context.clear_screen(); } void RenderSystem::present_screen() { this->context.present_screen(); } -const Camera & RenderSystem::update_camera() { +SDLContext::CameraValues RenderSystem::update_camera() { ComponentManager & mgr = this->mediator.component_manager; RefVector<Camera> cameras = mgr.get_components_by_type<Camera>(); @@ -32,16 +32,17 @@ const Camera & RenderSystem::update_camera() { if (!cam.active) continue; const Transform & transform = mgr.get_components_by_id<Transform>(cam.game_object_id).front().get(); - this->context.set_camera(cam); - this->cam_pos = transform.position + cam.offset; - return cam; + SDLContext::CameraValues cam_val = this->context.set_camera(cam); + cam_val.cam_pos = transform.position + cam.data.offset; + return cam_val; } throw std::runtime_error("No active cameras in current scene"); } bool sorting_comparison(const Sprite & a, const Sprite & b) { - if (a.sorting_in_layer < b.sorting_in_layer) return true; - if (a.sorting_in_layer == b.sorting_in_layer) return a.order_in_layer < b.order_in_layer; + if (a.data.sorting_in_layer < b.data.sorting_in_layer) return true; + if (a.data.sorting_in_layer == b.data.sorting_in_layer) + return a.data.order_in_layer < b.data.order_in_layer; return false; } @@ -59,7 +60,7 @@ void RenderSystem::update() { this->present_screen(); } -bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, +bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam, const double & scale) { ComponentManager & mgr = this->mediator.component_manager; @@ -80,7 +81,6 @@ bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, this->context.draw(SDLContext::RenderContext{ .sprite = sprite, .cam = cam, - .cam_pos = this->cam_pos, .pos = p.position, .angle = p.angle, .scale = scale, @@ -89,12 +89,11 @@ bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, } return rendering_particles; } -void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam, +void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam, const Transform & tm) { this->context.draw(SDLContext::RenderContext{ .sprite = sprite, .cam = cam, - .cam_pos = this->cam_pos, .pos = tm.position, .angle = tm.rotation, .scale = tm.scale, @@ -103,7 +102,7 @@ void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam, void RenderSystem::render() { ComponentManager & mgr = this->mediator.component_manager; - const Camera & cam = this->update_camera(); + const SDLContext::CameraValues & cam = this->update_camera(); RefVector<Sprite> sprites = mgr.get_components_by_type<Sprite>(); RefVector<Sprite> sorted_sprites = this->sort(sprites); diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 096d058..eaf1213 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -37,7 +37,7 @@ private: void present_screen(); //! Updates the active camera used for rendering. - const Camera & update_camera(); + SDLContext::CameraValues update_camera(); //! Renders the whole screen void render(); @@ -52,7 +52,8 @@ private: * constructor is now protected i cannot make tmp inside * \return true if particles have been rendered */ - bool render_particle(const Sprite & sprite, const Camera & cam, const double & scale); + bool render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam, + const double & scale); /** * \brief renders a sprite with a Transform component on the screen @@ -60,7 +61,8 @@ private: * \param sprite the sprite component that holds all the data * \param tm the Transform component that holds the position,rotation and scale */ - void render_normal(const Sprite & sprite, const Camera & cam, const Transform & tm); + void render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam, + const Transform & tm); /** * \brief sort a vector sprite objects with @@ -79,9 +81,6 @@ private: private: // FIXME: retrieve sdlcontext via mediator after #PR57 SDLContext & context = SDLContext::get_instance(); - - //! camera postion in the current scene - vec2 cam_pos; }; } // namespace crepe |