diff options
| -rw-r--r-- | src/crepe/api/ParticleEmitter.cpp | 2 | ||||
| -rw-r--r-- | src/crepe/api/ParticleEmitter.h | 10 | ||||
| -rw-r--r-- | src/crepe/system/ParticleSystem.cpp | 6 | ||||
| -rw-r--r-- | src/crepe/system/RenderSystem.cpp | 2 | ||||
| -rw-r--r-- | src/test/ParticleTest.cpp | 37 | 
5 files changed, 32 insertions, 25 deletions
| diff --git a/src/crepe/api/ParticleEmitter.cpp b/src/crepe/api/ParticleEmitter.cpp index 1cfdceb..1e9cfaa 100644 --- a/src/crepe/api/ParticleEmitter.cpp +++ b/src/crepe/api/ParticleEmitter.cpp @@ -7,6 +7,6 @@ ParticleEmitter::ParticleEmitter(game_object_id_t game_object_id, const Sprite &  	: Component(game_object_id),  	  sprite(sprite), data(data) {  	for (size_t i = 0; i < this->data.max_particles; i++) { -		this->data.particles.emplace_back(); +		this->particles.emplace_back();  	}  } diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index cc54ffb..e0b117a 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -3,9 +3,11 @@  #include <cmath>  #include <vector> +#include "system/ParticleSystem.h" +#include "system/RenderSystem.h" +  #include "Component.h"  #include "Particle.h" -#include "system/ParticleSystem.h"  #include "types.h"  namespace crepe { @@ -69,9 +71,6 @@ public:  		vec2 force_over_time;  		//! particle boundary  		Boundary boundary; -		//! collection of particles -		std::vector<Particle> particles; -		  	};  public: @@ -87,7 +86,10 @@ public:  private:  	//! Saves time left over from last update event.  	friend ParticleSystem; +	friend RenderSystem;  	float spawn_accumulator  = 0; +	//! collection of particles +	std::vector<Particle> particles;  };  } // namespace crepe diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 85b8248..a56de60 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -34,7 +34,7 @@ void ParticleSystem::update() {      }  		// Update all particles -		for (Particle & particle : emitter.data.particles) { +		for (Particle & particle : emitter.particles) {  			if (particle.active) {  				particle.update(dt);  			} @@ -57,7 +57,7 @@ void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform &  	vec2 velocity  		= {random_speed * std::cos(angle_radians), random_speed * std::sin(angle_radians)}; -	for (Particle & particle : emitter.data.particles) { +	for (Particle & particle : emitter.particles) {  		if (!particle.active) {  			particle.reset(emitter.data.end_lifespan, initial_position, velocity,  						   random_angle); @@ -76,7 +76,7 @@ void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & t  	const float TOP = offset.y - half_height;  	const float BOTTOM = offset.y + half_height; -	for (Particle & particle : emitter.data.particles) { +	for (Particle & particle : emitter.particles) {  		const vec2 & position = particle.position;  		bool within_bounds = (position.x >= LEFT && position.x <= RIGHT && position.y >= TOP  							  && position.y <= BOTTOM); diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index cc633e8..505433a 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -87,7 +87,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale)  		rendering_particles = true;  		if (!em.active) continue; -		for (const Particle & p : em.data.particles) { +		for (const Particle & p : em.particles) {  			if (!p.active) continue;  			ctx.draw(SDLContext::RenderContext{ diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 9112a3f..70534f3 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -11,10 +11,14 @@  #include <gtest/gtest.h>  #include <math.h> +#define protected public +#define private public +  using namespace std;  using namespace std::chrono_literals;  using namespace crepe; +  class ParticlesTest : public ::testing::Test {  	Mediator m; @@ -57,6 +61,7 @@ public:  				},  				.sprite = test_sprite,  			}); +			  		}  		transforms = mgr.get_components_by_id<Transform>(0);  		Transform & transform = transforms.front().get(); @@ -76,7 +81,7 @@ public:  		emitter.data.end_lifespan = 0;  		emitter.data.force_over_time = vec2{0, 0};  		emitter.data.boundary = {0, 0, vec2{0, 0}, false}; -		for (auto & particle : emitter.data.particles) { +		for (auto & particle : emitter.particles) {  			particle.active = false;  		}  	} @@ -95,19 +100,19 @@ TEST_F(ParticlesTest, spawnParticle) {  	emitter.data.max_angle = 10;  	particle_system.update();  	//check if nothing happend -	EXPECT_EQ(emitter.data.particles[0].active, false); +	EXPECT_EQ(emitter.particles[0].active, false);  	emitter.data.emission_rate = 1;  	//check particle spawnes  	particle_system.update(); -	EXPECT_EQ(emitter.data.particles[0].active, true); +	EXPECT_EQ(emitter.particles[0].active, true);  	particle_system.update(); -	EXPECT_EQ(emitter.data.particles[1].active, true); +	EXPECT_EQ(emitter.particles[1].active, true);  	particle_system.update(); -	EXPECT_EQ(emitter.data.particles[2].active, true); +	EXPECT_EQ(emitter.particles[2].active, true);  	particle_system.update(); -	EXPECT_EQ(emitter.data.particles[3].active, true); +	EXPECT_EQ(emitter.particles[3].active, true); -	for (auto & particle : emitter.data.particles) { +	for (auto & particle : emitter.particles) {  		// Check velocity range  		EXPECT_GE(particle.velocity.x, emitter.data.min_speed);  		// Speed should be greater than or equal to min_speed @@ -139,7 +144,7 @@ TEST_F(ParticlesTest, moveParticleHorizontal) {  	emitter.data.emission_rate = 1;  	for (int a = 1; a < emitter.data.boundary.width / 2; a++) {  		particle_system.update(); -		EXPECT_EQ(emitter.data.particles[0].position.x, a); +		EXPECT_EQ(emitter.particles[0].position.x, a);  	}  } @@ -157,7 +162,7 @@ TEST_F(ParticlesTest, moveParticleVertical) {  	emitter.data.emission_rate = 1;  	for (int a = 1; a < emitter.data.boundary.width / 2; a++) {  		particle_system.update(); -		EXPECT_EQ(emitter.data.particles[0].position.y, a); +		EXPECT_EQ(emitter.particles[0].position.y, a);  	}  } @@ -177,7 +182,7 @@ TEST_F(ParticlesTest, boundaryParticleReset) {  	for (int a = 0; a < emitter.data.boundary.width / 2 + 1; a++) {  		particle_system.update();  	} -	EXPECT_EQ(emitter.data.particles[0].active, false); +	EXPECT_EQ(emitter.particles[0].active, false);  }  TEST_F(ParticlesTest, boundaryParticleStop) { @@ -197,12 +202,12 @@ TEST_F(ParticlesTest, boundaryParticleStop) {  		particle_system.update();  	}  	const double TOLERANCE = 0.01; -	EXPECT_NEAR(emitter.data.particles[0].velocity.x, 0, TOLERANCE); -	EXPECT_NEAR(emitter.data.particles[0].velocity.y, 0, TOLERANCE); -	if (emitter.data.particles[0].velocity.x != 0) -		EXPECT_NEAR(std::abs(emitter.data.particles[0].position.x), +	EXPECT_NEAR(emitter.particles[0].velocity.x, 0, TOLERANCE); +	EXPECT_NEAR(emitter.particles[0].velocity.y, 0, TOLERANCE); +	if (emitter.particles[0].velocity.x != 0) +		EXPECT_NEAR(std::abs(emitter.particles[0].position.x),  					emitter.data.boundary.height / 2, TOLERANCE); -	if (emitter.data.particles[0].velocity.y != 0) -		EXPECT_NEAR(std::abs(emitter.data.particles[0].position.y), +	if (emitter.particles[0].velocity.y != 0) +		EXPECT_NEAR(std::abs(emitter.particles[0].position.y),  					emitter.data.boundary.width / 2, TOLERANCE);  } |