This repository has been archived by the owner on Apr 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
ParticleSystem.cpp
304 lines (252 loc) · 8.51 KB
/
ParticleSystem.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/////////////////////////////////////////////////////////////////////////////////
//
// Thor C++ Library
// Copyright (c) 2011-2022 Jan Haller
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
/////////////////////////////////////////////////////////////////////////////////
#include <Thor/Particles/ParticleSystem.hpp>
#include <Thor/Input/Detail/ConnectionImpl.hpp>
#include <Aurora/Tools/ForEach.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <algorithm>
#include <array>
#include <cmath>
#include <cassert>
namespace thor
{
namespace
{
// Erases emitter/affector at itr from ctr, if its time has expired. itr will point to the next element.
template <class Container>
void incrementCheckExpiry(Container& ctr, typename Container::iterator& itr, sf::Time dt)
{
// itr->second is the remaining time of the emitter/affector.
// Time::Zero means infinite time (no removal).
if (itr->timeUntilRemoval != sf::Time::Zero && (itr->timeUntilRemoval -= dt) <= sf::Time::Zero)
itr = ctr.erase(itr);
else
++itr;
}
sf::IntRect getFullRect(const sf::Texture& texture)
{
return sf::IntRect(0, 0, texture.getSize().x, texture.getSize().y);
}
} // namespace
// ---------------------------------------------------------------------------------------------------------------------------
ParticleSystem::ParticleSystem()
: mParticles()
, mAffectors()
, mEmitters()
, mTexture(nullptr)
, mTextureRects()
, mVertices(sf::Quads)
, mNeedsVertexUpdate(true)
, mQuads()
, mNeedsQuadUpdate(true)
{
}
ParticleSystem::ParticleSystem(ParticleSystem&& source)
: mParticles(std::move(source.mParticles))
, mAffectors(std::move(source.mAffectors))
, mEmitters(std::move(source.mEmitters))
, mTexture(std::move(source.mTexture))
, mTextureRects(std::move(source.mTextureRects))
, mVertices(std::move(source.mVertices))
, mNeedsVertexUpdate(std::move(source.mNeedsVertexUpdate))
, mQuads(std::move(source.mQuads))
, mNeedsQuadUpdate(std::move(source.mNeedsQuadUpdate))
{
}
ParticleSystem& ParticleSystem::operator= (ParticleSystem&& source)
{
mParticles = std::move(source.mParticles);
mAffectors = std::move(source.mAffectors);
mEmitters = std::move(source.mEmitters);
mTexture = std::move(source.mTexture);
mTextureRects = std::move(source.mTextureRects);
mVertices = std::move(source.mVertices);
mNeedsVertexUpdate = std::move(source.mNeedsVertexUpdate);
mQuads = std::move(source.mQuads);
mNeedsQuadUpdate = std::move(source.mNeedsQuadUpdate);
return *this;
}
void ParticleSystem::setTexture(const sf::Texture& texture)
{
mTexture = &texture;
mNeedsQuadUpdate = true;
}
unsigned int ParticleSystem::addTextureRect(const sf::IntRect& textureRect)
{
mTextureRects.push_back(textureRect);
mNeedsQuadUpdate = true;
return static_cast<unsigned int>(mTextureRects.size() - 1);
}
Connection ParticleSystem::addAffector(std::function<void(Particle&, sf::Time)> affector)
{
return addAffector(std::move(affector), sf::Time::Zero);
}
Connection ParticleSystem::addAffector(std::function<void(Particle&, sf::Time)> affector, sf::Time timeUntilRemoval)
{
mAffectors.push_back( Affector(std::move(affector), timeUntilRemoval) );
mAffectors.back().tracker = detail::makeIdConnectionImpl(mAffectors, mAffectors.back().id);
return Connection(mAffectors.back().tracker);
}
void ParticleSystem::clearAffectors()
{
mAffectors.clear();
}
Connection ParticleSystem::addEmitter(std::function<void(EmissionInterface&, sf::Time)> emitter)
{
return addEmitter(emitter, sf::Time::Zero);
}
Connection ParticleSystem::addEmitter(std::function<void(EmissionInterface&, sf::Time)> emitter, sf::Time timeUntilRemoval)
{
mEmitters.push_back( Emitter(std::move(emitter), timeUntilRemoval) );
mEmitters.back().tracker = detail::makeIdConnectionImpl(mEmitters, mEmitters.back().id);
return Connection(mEmitters.back().tracker);
}
void ParticleSystem::clearEmitters()
{
mEmitters.clear();
}
void ParticleSystem::update(sf::Time dt)
{
// Invalidate stored vertices
mNeedsVertexUpdate = true;
// Emit new particles and remove expiring emitters
for (EmitterContainer::iterator itr = mEmitters.begin(); itr != mEmitters.end(); )
{
itr->function(*this, dt);
incrementCheckExpiry(mEmitters, itr, dt);
}
// Affect existing particles
ParticleContainer::iterator writer = mParticles.begin();
for (ParticleContainer::iterator reader = mParticles.begin(); reader != mParticles.end(); ++reader)
{
// Apply movement and decrease lifetime
updateParticle(*reader, dt);
// If current particle is not dead
if (reader->passedLifetime < reader->totalLifetime)
{
// Only apply affectors to living particles
AURORA_FOREACH(auto& affectorPair, mAffectors)
affectorPair.function(*reader, dt);
// Go ahead
*writer++ = *reader;
}
}
// Remove particles dying this frame
mParticles.erase(writer, mParticles.end());
// Remove affectors expiring this frame
for (AffectorContainer::iterator itr = mAffectors.begin(); itr != mAffectors.end(); )
{
incrementCheckExpiry(mAffectors, itr, dt);
}
}
void ParticleSystem::clearParticles()
{
mParticles.clear();
}
void ParticleSystem::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// Check cached rectangles
if (mNeedsQuadUpdate)
{
computeQuads();
mNeedsQuadUpdate = false;
}
// Check cached vertices
if (mNeedsVertexUpdate)
{
computeVertices();
mNeedsVertexUpdate = false;
}
// Draw the vertex array with our texture
states.texture = mTexture;
target.draw(mVertices, states);
}
void ParticleSystem::emitParticle(const Particle& particle)
{
mParticles.push_back(particle);
}
void ParticleSystem::updateParticle(Particle& particle, sf::Time dt)
{
particle.passedLifetime += dt;
particle.position += dt.asSeconds() * particle.velocity;
particle.rotation += dt.asSeconds() * particle.rotationSpeed;
}
void ParticleSystem::computeVertices() const
{
// Clear vertex array (keeps memory allocated)
mVertices.clear();
// Fill vertex array
AURORA_FOREACH(const Particle& p, mParticles)
{
sf::Transform transform;
transform.translate(p.position);
transform.rotate(p.rotation);
transform.scale(p.scale);
// Ensure valid index -- if this fails, you have not called addTextureRect() enough times, or p.textureIndex is simply wrong
assert(p.textureIndex == 0 || p.textureIndex < mTextureRects.size());
const auto& quad = mQuads[p.textureIndex];
for (std::size_t i = 0; i < 4; ++i)
{
sf::Vertex vertex;
vertex.position = transform.transformPoint(quad[i].position);
vertex.texCoords = quad[i].texCoords;
vertex.color = p.color;
mVertices.append(vertex);
}
}
}
void ParticleSystem::computeQuads() const
{
// Ensure setTexture() has been called
assert(mTexture);
// No texture rects: Use full texture, cache single rectangle
if (mTextureRects.empty())
{
mQuads.resize(1);
computeQuad(mQuads[0], getFullRect(*mTexture));
}
// Specified texture rects: Cache every one
else
{
mQuads.resize(mTextureRects.size());
for (std::size_t i = 0; i < mTextureRects.size(); ++i)
computeQuad(mQuads[i], mTextureRects[i]);
}
}
void ParticleSystem::computeQuad(Quad& quad, const sf::IntRect& textureRect) const
{
sf::FloatRect rect(textureRect);
quad[0].texCoords = sf::Vector2f(rect.left, rect.top);
quad[1].texCoords = sf::Vector2f(rect.left + rect.width, rect.top);
quad[2].texCoords = sf::Vector2f(rect.left + rect.width, rect.top + rect.height);
quad[3].texCoords = sf::Vector2f(rect.left, rect.top + rect.height);
quad[0].position = sf::Vector2f(-rect.width, -rect.height) / 2.f;
quad[1].position = sf::Vector2f( rect.width, -rect.height) / 2.f;
quad[2].position = sf::Vector2f( rect.width, rect.height) / 2.f;
quad[3].position = sf::Vector2f(-rect.width, rect.height) / 2.f;
}
} // namespace thor