aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system/RenderSystem.cpp
diff options
context:
space:
mode:
authorJAROWMR <jarorutjes07@gmail.com>2024-12-01 23:05:27 +0100
committerJAROWMR <jarorutjes07@gmail.com>2024-12-01 23:05:27 +0100
commitb6fdd1d644368226054094a282f5db24b31b5ba2 (patch)
treef995c792d6b8330a7880dada88d470bd357279e7 /src/crepe/system/RenderSystem.cpp
parentcbd4b97d348c46f4f43fe59683a5e3d1bdbc500f (diff)
parent647eb8e318f1ed1e3ec18505ea4df57025e6ffd5 (diff)
merge with master
Diffstat (limited to 'src/crepe/system/RenderSystem.cpp')
-rw-r--r--src/crepe/system/RenderSystem.cpp46
1 files changed, 33 insertions, 13 deletions
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index ad510f5..c196bb1 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -2,15 +2,14 @@
#include <cassert>
#include <cmath>
#include <functional>
-#include <iostream>
#include <stdexcept>
#include <vector>
#include "../ComponentManager.h"
+#include "../api/Camera.h"
#include "../api/ParticleEmitter.h"
#include "../api/Sprite.h"
#include "../api/Transform.h"
-#include "../api/Vector2.h"
#include "../facade/SDLContext.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;
+ this->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,35 @@ 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(SDLContext::RenderContext{
+ .sprite = sprite,
+ .cam = cam,
+ .cam_pos = this->cam_pos,
+ .pos = p.position,
+ .angle = p.angle,
+ .scale = scale,
+ });
}
}
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(SDLContext::RenderContext{
+ .sprite = sprite,
+ .cam = cam,
+ .cam_pos = this->cam_pos,
+ .pos = tm.position,
+ .angle = tm.rotation,
+ .scale = tm.scale,
+ });
}
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 +113,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);
}
}