diff options
Diffstat (limited to 'src/crepe/system/RenderSystem.cpp')
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 56 |
1 files changed, 47 insertions, 9 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(); } |