aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/system')
-rw-r--r--src/crepe/system/RenderSystem.cpp56
-rw-r--r--src/crepe/system/RenderSystem.h27
2 files changed, 70 insertions, 13 deletions
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index fa3d0de..c137de1 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -1,11 +1,12 @@
-#include <functional>
+#include <cmath>
#include <vector>
#include "../ComponentManager.h"
+#include "../api/ParticleEmitter.h"
#include "../api/Sprite.h"
#include "../api/Transform.h"
+#include "../api/Vector2.h"
#include "../facade/SDLContext.h"
-#include "../util/Log.h"
#include "RenderSystem.h"
@@ -17,28 +18,65 @@ void RenderSystem::present_screen() const { SDLContext::get_instance().present_s
void RenderSystem::update_camera() {
ComponentManager & mgr = this->component_manager;
- std::vector<std::reference_wrapper<Camera>> cameras = mgr.get_components_by_type<Camera>();
+ auto cameras = mgr.get_components_by_type<Camera>();
for (Camera & cam : cameras) {
- SDLContext::get_instance().camera(cam);
+ SDLContext::get_instance().set_camera(cam);
this->curr_cam = &cam;
}
}
-void RenderSystem::render_sprites() const {
+
+bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) const {
+
ComponentManager & mgr = this->component_manager;
+ SDLContext & render = SDLContext::get_instance();
+
+ auto emitters = mgr.get_components_by_id<ParticleEmitter>(sprite.game_object_id);
+
+ bool rendering_particles = false;
- std::vector<std::reference_wrapper<Sprite>> sprites = mgr.get_components_by_type<Sprite>();
+ for (const ParticleEmitter & em : emitters) {
+ if (!(&em.data.sprite == &sprite)) continue;
+ rendering_particles = true;
+ if (!em.active) continue;
+
+ for (const Particle & p : em.data.particles) {
+ if (!p.active) continue;
+ render.draw_particle(sprite, p.position, p.angle, scale, *this->curr_cam);
+ }
+ }
+ return rendering_particles;
+}
+void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) const {
+
+ ComponentManager & mgr = this->component_manager;
SDLContext & render = SDLContext::get_instance();
+
+ render.draw(sprite, tm, *curr_cam);
+}
+
+void RenderSystem::render() const {
+
+ ComponentManager & mgr = this->component_manager;
+
+ auto sprites = mgr.get_components_by_type<Sprite>();
for (const Sprite & sprite : sprites) {
- auto transforms = mgr.get_components_by_id<Transform>(sprite.game_object_id);
- render.draw(sprite, transforms[0], *curr_cam);
+ if (!sprite.active) continue;
+ const Transform & transform
+ = mgr.get_components_by_id<Transform>(sprite.game_object_id).front().get();
+
+ bool rendered_particles = this->render_particle(sprite, transform.scale);
+
+ if (rendered_particles) continue;
+
+ this->render_normal(sprite, transform);
}
}
void RenderSystem::update() {
this->clear_screen();
this->update_camera();
- this->render_sprites();
+ this->render();
this->present_screen();
}
diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h
index 87ec494..8841f72 100644
--- a/src/crepe/system/RenderSystem.h
+++ b/src/crepe/system/RenderSystem.h
@@ -1,8 +1,11 @@
#pragma once
#include "api/Camera.h"
+#include "api/Sprite.h"
+#include "api/Transform.h"
#include "System.h"
+#include <cmath>
namespace crepe {
@@ -22,7 +25,7 @@ public:
* This method is called to perform all rendering operations for the current game frame.
*/
void update() override;
-
+
private:
//! Clears the screen in preparation for rendering.
void clear_screen() const;
@@ -33,12 +36,28 @@ private:
//! Updates the active camera used for rendering.
void update_camera();
- //! Renders all active sprites to the screen.
- void render_sprites() const;
+ //! Renders the whole screen
+ void render() const;
+
+ /**
+ * \brief Renders all the particles on the screen from a given sprite.
+ *
+ * \param sprite renders the particles with given texture
+ * \param tm the Transform component for scale
+ * \return true if particles have been rendered
+ */
+ bool render_particle(const Sprite & sprite, const double & scale) const;
+
+ /**
+ * \brief renders a sprite with a Transform component on the screen
+ *
+ * \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) 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.