aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/system')
-rw-r--r--src/crepe/system/AnimatorSystem.cpp1
-rw-r--r--src/crepe/system/AnimatorSystem.h3
-rw-r--r--src/crepe/system/RenderSystem.cpp31
-rw-r--r--src/crepe/system/RenderSystem.h17
4 files changed, 25 insertions, 27 deletions
diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp
index 676e485..bc94253 100644
--- a/src/crepe/system/AnimatorSystem.cpp
+++ b/src/crepe/system/AnimatorSystem.cpp
@@ -16,6 +16,7 @@ void AnimatorSystem::update() {
uint64_t tick = SDLContext::get_instance().get_ticks();
for (Animator & a : animations) {
if (a.active) {
+ // (10 frames per second)
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
index 56cc7b3..f8179a9 100644
--- a/src/crepe/system/AnimatorSystem.h
+++ b/src/crepe/system/AnimatorSystem.h
@@ -21,12 +21,11 @@ public:
/**
* \brief Updates the Animator components.
*
- * This method is called periodically (likely every frame) to update the state of all
+ * This method is called to update the state of all
* Animator components, moving the animations forward and managing their behavior (e.g.,
* looping).
*/
void update() override;
- // FIXME: never say "likely" in the documentation lmao
};
} // namespace crepe
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index ad510f5..a1443cd 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -2,7 +2,6 @@
#include <cassert>
#include <cmath>
#include <functional>
-#include <iostream>
#include <stdexcept>
#include <vector>
@@ -10,8 +9,8 @@
#include "../api/ParticleEmitter.h"
#include "../api/Sprite.h"
#include "../api/Transform.h"
-#include "../api/Vector2.h"
#include "../facade/SDLContext.h"
+#include "api/Camera.h"
#include "RenderSystem.h"
@@ -21,7 +20,8 @@ using namespace std;
void RenderSystem::clear_screen() { this->context.clear_screen(); }
void RenderSystem::present_screen() { this->context.present_screen(); }
-void RenderSystem::update_camera() {
+
+const Camera & RenderSystem::update_camera() {
ComponentManager & mgr = this->component_manager;
RefVector<Camera> cameras = mgr.get_components_by_type<Camera>();
@@ -30,9 +30,13 @@ void RenderSystem::update_camera() {
for (Camera & cam : cameras) {
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->curr_cam_ref = &cam;
+ cam.pos = transform.position + cam.offset;
+ return cam;
}
+ throw std::runtime_error("No active cameras in current scene");
}
bool sorting_comparison(const Sprite & a, const Sprite & b) {
@@ -51,12 +55,12 @@ RefVector<Sprite> RenderSystem::sort(RefVector<Sprite> & objs) const {
void RenderSystem::update() {
this->clear_screen();
- this->update_camera();
this->render();
this->present_screen();
}
-bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) {
+bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam,
+ const double & scale) {
ComponentManager & mgr = this->component_manager;
@@ -72,19 +76,20 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale)
for (const Particle & p : em.data.particles) {
if (!p.active) continue;
- this->context.draw_particle(sprite, p.position, p.angle, scale,
- *this->curr_cam_ref);
+ this->context.draw_particle(sprite, p.position, p.angle, scale, cam);
}
}
return rendering_particles;
}
-void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) {
- this->context.draw(sprite, tm, *this->curr_cam_ref);
+void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam,
+ const Transform & tm) {
+ this->context.draw(sprite, tm, cam);
}
void RenderSystem::render() {
-
ComponentManager & mgr = this->component_manager;
+ const Camera & cam = this->update_camera();
+
RefVector<Sprite> sprites = mgr.get_components_by_type<Sprite>();
RefVector<Sprite> sorted_sprites = this->sort(sprites);
@@ -93,10 +98,10 @@ void RenderSystem::render() {
const Transform & transform
= mgr.get_components_by_id<Transform>(sprite.game_object_id).front().get();
- bool rendered_particles = this->render_particle(sprite, transform.scale);
+ bool rendered_particles = this->render_particle(sprite, cam, transform.scale);
if (rendered_particles) continue;
- this->render_normal(sprite, transform);
+ this->render_normal(sprite, cam, transform);
}
}
diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h
index 30b41cf..46ebb92 100644
--- a/src/crepe/system/RenderSystem.h
+++ b/src/crepe/system/RenderSystem.h
@@ -1,12 +1,11 @@
#pragma once
-#include <functional>
-#include <vector>
+#include <cmath>
#include "facade/SDLContext.h"
#include "System.h"
-#include <cmath>
+#include "types.h"
namespace crepe {
@@ -37,7 +36,7 @@ private:
void present_screen();
//! Updates the active camera used for rendering.
- void update_camera();
+ const Camera & update_camera();
//! Renders the whole screen
void render();
@@ -49,7 +48,7 @@ private:
* \param tm the Transform component for scale
* \return true if particles have been rendered
*/
- bool render_particle(const Sprite & sprite, const double & scale);
+ bool render_particle(const Sprite & sprite, const Camera & cam, const double & scale);
/**
* \brief renders a sprite with a Transform component on the screen
@@ -57,7 +56,7 @@ 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 Transform & tm);
+ void render_normal(const Sprite & sprite, const Camera & cam, const Transform & tm);
/**
* \brief sort a vector sprite objects with
@@ -71,16 +70,10 @@ private:
* \todo Include color handling for 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_ref = nullptr;
- // TODO: needs a better solution
-
SDLContext & context = SDLContext::get_instance();
};