aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-20 15:42:55 +0100
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-20 15:42:55 +0100
commitccb9dd63c6fc172fed9acaea31a6d67c457b37fe (patch)
tree38e56b5f8a9e248bc180be23c77d085b2b38ad65 /src
parenta0f39be4510665614d25d23d88fe1d0f0b5cc740 (diff)
fixed the reference bug in render particle, it was in the example problem
Diffstat (limited to 'src')
-rw-r--r--src/crepe/system/RenderSystem.cpp19
-rw-r--r--src/crepe/system/RenderSystem.h14
-rw-r--r--src/example/rendering_particle.cpp12
3 files changed, 25 insertions, 20 deletions
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index c599729..05b7337 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -18,9 +18,9 @@
using namespace crepe;
using namespace std;
-void RenderSystem::clear_screen() { this->context.clear_screen(); }
+void RenderSystem::clear_screen() const { this->context.clear_screen(); }
-void RenderSystem::present_screen() { this->context.present_screen(); }
+void RenderSystem::present_screen() const { this->context.present_screen(); }
void RenderSystem::update_camera() {
ComponentManager & mgr = this->component_manager;
@@ -42,7 +42,7 @@ bool sorting_comparison(const Sprite & a, const Sprite & b) {
}
std::vector<std::reference_wrapper<Sprite>>
-RenderSystem::sort(std::vector<std::reference_wrapper<Sprite>> & objs) {
+RenderSystem::sort(std::vector<std::reference_wrapper<Sprite>> & objs) const {
std::vector<std::reference_wrapper<Sprite>> sorted_objs(objs);
std::sort(sorted_objs.begin(), sorted_objs.end(), sorting_comparison);
@@ -57,7 +57,7 @@ void RenderSystem::update() {
this->present_screen();
}
-bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) {
+bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) const {
ComponentManager & mgr = this->component_manager;
@@ -67,7 +67,6 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale)
bool rendering_particles = false;
for (const ParticleEmitter & em : emitters) {
- cout << &em.data.sprite << " " << &sprite << endl;
if (!(&em.data.sprite == &sprite)) continue;
rendering_particles = true;
if (!em.active) continue;
@@ -80,19 +79,19 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale)
}
return rendering_particles;
}
-void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) {
+void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) const {
this->context.draw(sprite, tm, *this->curr_cam_ref);
}
-void RenderSystem::render() {
+void RenderSystem::render() const {
ComponentManager & mgr = this->component_manager;
vector<reference_wrapper<Sprite>> sprites = mgr.get_components_by_type<Sprite>();
- //vector<reference_wrapper<Sprite>> sorted_sprites = this->sort(sprites);
+ vector<reference_wrapper<Sprite>> sorted_sprites = this->sort(sprites);
- for (const Sprite & sprite : sprites) {
+ for (const Sprite & sprite : sorted_sprites) {
if (!sprite.active) continue;
- Transform & transform
+ const Transform & transform
= mgr.get_components_by_id<Transform>(sprite.game_object_id).front().get();
bool rendered_particles = this->render_particle(sprite, transform.scale);
diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h
index 0393f2f..1a11d5b 100644
--- a/src/crepe/system/RenderSystem.h
+++ b/src/crepe/system/RenderSystem.h
@@ -28,19 +28,19 @@ 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() ;
+ void clear_screen() const;
//! Presents the rendered frame to the display.
- void present_screen() ;
+ void present_screen() const;
//! Updates the active camera used for rendering.
void update_camera();
//! Renders the whole screen
- void render() ;
+ void render() const;
/**
* \brief Renders all the particles on the screen from a given sprite.
@@ -49,7 +49,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 double & scale) const;
/**
* \brief renders a sprite with a Transform component on the screen
@@ -57,7 +57,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 Transform & tm) const;
/**
* \brief sort a vector sprite objects with
@@ -66,7 +66,7 @@ private:
* \return returns a sorted reference vector
*/
std::vector<std::reference_wrapper<Sprite>>
- sort(std::vector<std::reference_wrapper<Sprite>> & objs) ;
+ sort(std::vector<std::reference_wrapper<Sprite>> & objs) const;
/**
* \todo Include color handling for sprites.
diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp
index 062f11f..5030bc6 100644
--- a/src/example/rendering_particle.cpp
+++ b/src/example/rendering_particle.cpp
@@ -15,6 +15,7 @@
#include <crepe/system/RenderSystem.h>
#include <chrono>
+#include <iostream>
#include <memory>
using namespace crepe;
@@ -27,11 +28,11 @@ int main(int argc, char * argv[]) {
ParticleSystem psys{mgr};
Color color(255, 255, 255, 255);
- //game_object.add_component<Sprite>(make_shared<Texture>("../asset/texture/img.png"), color, FlipSettings{false,false});
- Sprite test_sprite = game_object.add_component<Sprite>(
+ Sprite & test_sprite = game_object.add_component<Sprite>(
make_shared<Texture>("../asset/texture/img.png"), color, FlipSettings{false, false});
- game_object.add_component<ParticleEmitter>(ParticleEmitter::Data{
+ test_sprite.order_in_layer = 5;
+ auto test = game_object.add_component<ParticleEmitter>(ParticleEmitter::Data{
.position = {0, 0},
.max_particles = 10,
.emission_rate = 0.1,
@@ -52,6 +53,11 @@ int main(int argc, char * argv[]) {
});
game_object.add_component<Camera>(Color::WHITE);
+ game_object
+ .add_component<Sprite>(make_shared<Texture>("../asset/texture/img.png"), color,
+ FlipSettings{false, false})
+ .order_in_layer
+ = 6;
auto start = std::chrono::steady_clock::now();
while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) {