-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathParticleBatch2D.cpp
77 lines (63 loc) · 1.71 KB
/
ParticleBatch2D.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
#include "ParticleBatch2D.h"
namespace NeroEngine{
ParticleBatch2D::ParticleBatch2D()
{
}
ParticleBatch2D::~ParticleBatch2D()
{
delete[] _particles;
}
void ParticleBatch2D::init(int maxParticlesNum, float decayRate, GLTexture texture, std::function<void(Particle2D&, float)> funcUpdate /*= defaultUpdateFunc*/){
_maxParticlesNum = maxParticlesNum;
_particles = new Particle2D[_maxParticlesNum];
_decayRate = decayRate;
_texture = texture;
_funcUpdate = funcUpdate;
}
void ParticleBatch2D::update(float deltaTime){
for (int i = 0; i < _maxParticlesNum;i++){
if (_particles[i]._life>0){
_funcUpdate(_particles[i], deltaTime);
_particles[i]._life -= _decayRate*deltaTime;
}
}
}
void ParticleBatch2D::draw(SpriteBatch* spriteBatch){
glm::vec4 uvRect(0.0f, 0.0f, 1.0f, 1.0f);
for (int i = 0; i < _maxParticlesNum; i++){
auto &p = _particles[i];
if (p._life>0){
glm::vec4 destRect(p._position.x,p._position.y,p._width,p._width);
spriteBatch->draw(destRect,uvRect,_texture.id,0.0f,p._color);
}
}
}
void ParticleBatch2D::addParticle(const glm::vec2& position,
const glm::vec2& velocity,
const Color& color,
float width){
int particleIndex = findFreeParticles();
auto& p = _particles[particleIndex];
p._life = 1.0f;
p._position = position;
p._velocity = velocity;
p._color = color;
p._width = width;
}
int ParticleBatch2D::findFreeParticles(){
for (int i = _lastFreeParticles; i < _maxParticlesNum;i++){
if (_particles[i]._life<=0){
_lastFreeParticles = i;
return i;
}
}
for (int i = 0; i < _lastFreeParticles; i++){
if (_particles[i]._life <= 0){
_lastFreeParticles = i;
return i;
}
}
//δ·¢ÏÖ
return 0;
}
}