-
Notifications
You must be signed in to change notification settings - Fork 2
/
emitter.cpp
153 lines (121 loc) · 2.95 KB
/
emitter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "emitter.h"
#include <stddef.h>
#include "particle.h"
#include "renderer.h"
#include "operator.h"
#include "initializer.h"
Emitter::Emitter(const Vector3D &pos, float itv, int max) :
_position(pos),
_interval(itv),
_maxParticles(max),
_renderer(NULL),
_initializers(NULL),
_operators(NULL),
_particles(NULL),
_elapsed(itv) {
_particles = new Particle*[_maxParticles];
for(int i = 0; i < _maxParticles; i++) {
_particles[i] = NULL;
}
_initializers = new Initializer*[MAX_INITIALIZERS];
for(int i = 0; i < MAX_INITIALIZERS; i++) {
_initializers[i] = NULL;
}
_operators = new Operator*[MAX_OPERATORS];
for(int i = 0; i < MAX_OPERATORS; i++) {
_operators[i] = NULL;
}
}
Emitter::~Emitter() {
for(int i = 0; i < _maxParticles; i++) {
if(_particles[i]) {
delete _particles[i];
_particles[i] = NULL;
}
}
delete[] _particles;
}
void Emitter::update(float elapsed) {
for(int i = 0; i < _maxParticles; i++) {
if(_particles[i]) {
Particle* p = _particles[i];
p->update(elapsed);
applyOperators(p, elapsed);
if(p->dead) {
removeParticle(i);
}
}
}
_elapsed += elapsed;
while(_elapsed > _interval) {
createParticle();
_elapsed -= _interval;
}
}
void Emitter::draw() {
if(_renderer) {
for(int i = 0; i < _maxParticles; i++) {
if(_particles[i]) {
_renderer->render(_particles[i]);
}
}
}
}
void Emitter::renderer(Renderer *rend) {
if(_renderer) {
delete[] _renderer;
}
_renderer = rend;
}
bool Emitter::addInitializer(Initializer *initr) {
int index;
// We should store the top of the array
for(index = 0; index < MAX_INITIALIZERS; index++) {
if(!_initializers[index]) break;
}
if(index >= MAX_INITIALIZERS) return false;
_initializers[index] = initr;
return true;
}
bool Emitter::addOperator(Operator *opr) {
int index;
// We should store the top of the array
for(index = 0; index < MAX_OPERATORS; index++) {
if(!_operators[index]) break;
}
if(index >= MAX_OPERATORS) return false;
_operators[index] = opr;
return true;
}
bool Emitter::createParticle() {
int index;
for(index = 0; index < _maxParticles; index++) {
if(!_particles[index]) break;
}
if(index >= _maxParticles) return false;
Particle* particle = new Particle(_position);
applyInitializers(particle);
_particles[index] = particle;
return true;
}
void Emitter::removeParticle(int index) {
delete _particles[index];
_particles[index] = NULL;
//@TODO: Keep track of free indexes (?)
}
void Emitter::applyInitializers(Particle *p) {
int index;
for(index = 0; index < MAX_INITIALIZERS; index++) {
if(_initializers[index]) {
_initializers[index]->apply(p, this);
}
}
}
void Emitter::applyOperators(Particle *p, float elapsed) {
int index;
for(index = 0; index < MAX_OPERATORS; index++) {
if(_operators[index]) {
_operators[index]->apply(p, elapsed);
}
}
}