Skip to content

Commit

Permalink
Thruster test added (See #99)
Browse files Browse the repository at this point in the history
  • Loading branch information
abekkine committed May 6, 2018
1 parent 1a2249b commit 604fa21
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 24 deletions.
28 changes: 8 additions & 20 deletions testing/particle-test/BigBang.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@

#include "ParticleManager.h"

#include <time.h>

class Star : public Particle {
public:
Star(float L) : Particle(L) {}
~Star() {}
void Update(double time_step) {
Particle::Update(time_step);
for (int i=0; i<3; ++i) {
color[i] = life / totalLife;
}
size += 0.1;
Fade();
size += 0.2;
}
};

Expand All @@ -24,22 +20,14 @@ class BigBang : public ParticleManager {
: ParticleManager(n, m)
{}
~BigBang() {}
void Init() {
srand48(clock());
}
void AddParticle() {
Star *p;
for (int i=0; i<kBurstSize; ++i) {
p = new Star(5.0);
double a = drand48() * 2.0 * M_PI;
double s = drand48() * 10.0;
p->vx = s * cos(a);
p->vy = s * sin(a);
particles.push_back(p);
}
}
void Update(double time_step) {
ParticleManager::Update(time_step);
p = new Star(5.0);
double a = drand48() * 2.0 * M_PI;
double s = drand48() * 10.0;
p->vx = s * cos(a);
p->vy = s * sin(a);
particles.push_back(p);
}
};

Expand Down
5 changes: 4 additions & 1 deletion testing/particle-test/Particle.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Particle {
, vx(0.0), vy(0.0)
, ax(0.0), ay(0.0)
{}
~Particle() {}
virtual ~Particle() {}
void Render() {
if (life > 0.0) {
glPointSize(size);
Expand All @@ -29,6 +29,9 @@ class Particle {
vy += ay * time_step;
life -= time_step;
}
void Fade() {
color[3] = life / totalLife;
}

float totalLife;
float life;
Expand Down
15 changes: 12 additions & 3 deletions testing/particle-test/ParticleManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "Particle.h"

#include <time.h>

#include <vector>

class ParticleManager {
Expand All @@ -11,18 +13,25 @@ class ParticleManager {
: kMaxParticles(numParticles)
, kBurstSize(numBurst)
{}
~ParticleManager() {}
virtual void Init() = 0;
virtual ~ParticleManager() {}
virtual void Init() {
srand48(clock());
}
virtual void AddParticle() = 0;
void Render() {
for (auto p : particles) {
p->Render();
}
}
void AddParticles() {
for (int i=0; i<kBurstSize; ++i) {
AddParticle();
}
}
virtual void Update(double time_step) {

if (particles.size() < (kMaxParticles-kBurstSize)) {
AddParticle();
AddParticles();
}

for (auto iP = particles.begin(); iP != particles.end(); ++iP) {
Expand Down
39 changes: 39 additions & 0 deletions testing/particle-test/Thruster.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef THRUSTER_H_
#define THRUSTER_H_

#include "ParticleManager.h"

#include <time.h>

class Smoke : public Particle {
public:
Smoke(float life) : Particle(life) {}
~Smoke() {}
void Update(double time_step) {
Particle::Update(time_step);
Fade();
size += 0.2;
}
};

class Thruster : public ParticleManager {
public:
Thruster(int n, int m) : ParticleManager(n, m) {}
~Thruster() {}
void AddParticle() {
Smoke *p;
p = new Smoke(1.0);
double a = drand48() * M_PI / 6.0;
double s = drand48() * 10.0 + 5.0;
p->vx = s * cos(a);
p->vy = s * sin(a);
p->color[0] = 1.0;
p->color[1] = 0.5;
p->color[2] = 0.0;

particles.push_back(p);
}
};

#endif // THRUSTER_H_

10 changes: 10 additions & 0 deletions testing/particle-test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
#include <GL/glut.h>

#include "BigBang.h"
#include "Thruster.h"

BigBang* bang = 0;
Thruster* thruster = 0;

void render();
void idle();
Expand Down Expand Up @@ -55,6 +57,9 @@ void init() {

bang = new BigBang(2000, 5);
bang->Init();

thruster = new Thruster(500, 5);
thruster->Init();
}

void update_viewport() {
Expand Down Expand Up @@ -94,11 +99,16 @@ void render() {
if (bang != 0) {
bang->Render();
}

if (thruster != 0) {
thruster->Render();
}
}

void idle() {

bang->Update(0.02);
thruster->Update(0.02);

usleep(20000);
}
Expand Down

0 comments on commit 604fa21

Please sign in to comment.