-
Notifications
You must be signed in to change notification settings - Fork 2
/
PManager.cpp
executable file
·209 lines (178 loc) · 4.73 KB
/
PManager.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include "PManager.h"
#include "Particle.h"
#include "PEmitter.h"
#include <glm/gtc/matrix_transform.hpp>
#include <cstdlib>
#ifdef KISS_PARTICLES_DEBUG
#include <sys/time.h>
#endif
ParticleManager::ParticleManager()
{
// Make sure to set up the default particle group.
groups_["default"] = new PGroup();
}
ParticleManager::~ParticleManager()
{
reset();
}
PGroup* ParticleManager::newGroup(const std::string &gname)
{
PGroup *p = new PGroup();
get()->groups_[gname] = p;
return p;
}
Particle * ParticleManager::newParticle(const std::string &groupName)
{
if (groups_.find(groupName) == groups_.end())
{
std::cerr << "FATAL ERROR: unable to find Particle Group `" << groupName << "`\n";
assert(false);
return NULL;
}
return groups_[groupName]->newParticle();
}
int ParticleManager::numParticles(void)
{
int cnt = 0;
std::map<std::string, PGroup*>::iterator pit;
for (pit = groups_.begin(); pit != groups_.end(); pit++)
{
cnt += pit->second->numParticles();
}
return cnt;
}
void ParticleManager::startUpdate(float dt)
{
update_dt_ = dt;
// Time the creation step
struct timeval tv;
gettimeofday(&tv, NULL);
int creation_start = tv.tv_usec;
// First create new particles.
std::list<Emitter*>::iterator eit;
for (eit = emitters_.begin(); eit != emitters_.end(); )
{
std::string og = (*eit)->getOutputGroup();
// Exit if we can't find the specified output group
if (!groups_.count(og))
{
std::cout << "FATAL ERROR: unknown particle group requested: " << og << std::endl;
assert(false);
}
// Emit into the output particle group
(*eit)->emit(dt);
if ((*eit)->isDone())
{
delete *eit;
eit = emitters_.erase(eit);
}
else
eit++;
}
// Record creation time
gettimeofday(&tv, NULL);
int creation_usec = tv.tv_usec - creation_start;
// be careful of wrap
creation_usec = creation_usec >= 0 ? creation_usec : 1e6 + creation_usec;
if (recording_)
times_[0] += creation_usec;
// Start timing update
updateStart_ = tv.tv_usec;
// Kick off each PGroup update
std::map<std::string, PGroup*>::iterator pit;
for (pit = groups_.begin(); pit != groups_.end(); pit++)
{
pit->second->startUpdate(dt);
}
}
void ParticleManager::update()
{
float dt = update_dt_;
// Finish the particle update (blocks until each group is completed)
std::map<std::string, PGroup*>::iterator pit;
for (pit = groups_.begin(); pit != groups_.end(); pit++)
{
pit->second->update();
}
// Finish timing
struct timeval tv;
gettimeofday(&tv, NULL);
int update_usec = tv.tv_usec - updateStart_;
update_usec = update_usec >= 0 ? update_usec : 1e6 + update_usec;
if (recording_)
{
times_[1] += update_usec;
frame_count_++;
}
}
void ParticleManager::render(float dt)
{
// Time the render
struct timeval tv;
gettimeofday(&tv, NULL);
suseconds_t start_usec = tv.tv_usec;
// draw existing particles
std::map<std::string, PGroup*>::iterator pit;
for (pit = groups_.begin(); pit != groups_.end(); pit++)
{
pit->second->render();
}
// Finish the timing
gettimeofday(&tv, NULL);
suseconds_t render_usec = tv.tv_usec - start_usec;
render_usec = render_usec >= 0 ? render_usec : 1e6 + render_usec;
if (recording_)
times_[2] += render_usec;
}
ParticleManager* ParticleManager::get()
{
static ParticleManager pm;
return ±
}
Emitter* ParticleManager::newEmitter()
{
return new Emitter();
}
void ParticleManager::addEmitter(Emitter *em)
{
//std::cout << "Adding a new emitter: " << em << std::endl;
emitters_.push_back(em);
}
void ParticleManager::quashEmitter(Emitter *e)
{
assert(e);
delete e;
emitters_.remove(e);
}
void ParticleManager::reset()
{
std::list<Emitter*>::iterator eit;
for (eit = emitters_.begin(); eit != emitters_.end(); eit++)
delete *eit;
emitters_.clear();
// Remove each pgroup and readd default
std::map<std::string, PGroup*>::iterator git;
for (git = groups_.begin(); git != groups_.end(); git++)
delete git->second;
groups_.clear();
groups_["default"] = new PGroup();
}
#ifdef KISS_PARTICLES_DEBUG
void ParticleManager::startRecording()
{
recording_= true;
frame_count_ = 0;
times_[0] = 0.f;
times_[1] = 0.f;
times_[2] = 0.f;
}
const double* ParticleManager::stopRecording()
{
recording_ = false;
times_[0] /= frame_count_;
times_[1] /= frame_count_;
times_[2] /= frame_count_;
return times_;
}
#endif