Skip to content

Commit 906d998

Browse files
committed
Refactor a bit xrParticles
1 parent 252a735 commit 906d998

File tree

4 files changed

+105
-95
lines changed

4 files changed

+105
-95
lines changed

src/xrParticles/particle_actions_collection.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "stdafx.h"
2-
#pragma hdrstop
32

43
#include "particle_actions_collection.h"
54
#include "particle_effect.h"
@@ -631,7 +630,7 @@ void PABounce::Execute(ParticleEffect* effect, const float dt, float& tm_max)
631630
if (position.Within(pnext))
632631
{
633632
// See if we were inside on previous timestep.
634-
BOOL pinside = position.Within(m.pos);
633+
const bool pinside = position.Within(m.pos);
635634

636635
// Normal to surface. This works for a sphere. Isn't
637636
// computed quite right, should extrapolate particle
@@ -640,10 +639,10 @@ void PABounce::Execute(ParticleEffect* effect, const float dt, float& tm_max)
640639
n.normalize_safe();
641640

642641
// Compute tangential and normal components of velocity
643-
float nmag = m.vel * n;
642+
const float nmag = m.vel * n;
644643

645-
pVector vn(n * nmag); // Normal Vn = (V.N)N
646-
pVector vt = m.vel - vn; // Tangent Vt = V - Vn
644+
const pVector vn(n * nmag); // Normal Vn = (V.N)N
645+
const pVector vt = m.vel - vn; // Tangent Vt = V - Vn
647646

648647
if (pinside)
649648
{

src/xrParticles/particle_actions_collection.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct PARTICLES_API PABounce : public ParticleAction
3737

3838
struct PARTICLES_API PACopyVertexB : public ParticleAction
3939
{
40-
BOOL copy_pos; // True to copy pos to posB.
40+
bool copy_pos; // True to copy pos to posB.
4141

4242
_METHODS;
4343
};
@@ -106,7 +106,7 @@ struct PARTICLES_API PAJet : public ParticleAction
106106
struct PARTICLES_API PAKillOld : public ParticleAction
107107
{
108108
float age_limit; // Exact age at which to kill particles.
109-
BOOL kill_less_than; // True to kill particles less than limit.
109+
bool kill_less_than; // True to kill particles less than limit.
110110

111111
_METHODS;
112112
};
@@ -191,7 +191,7 @@ struct PARTICLES_API PAScatter : public ParticleAction
191191

192192
struct PARTICLES_API PASink : public ParticleAction
193193
{
194-
BOOL kill_inside; // True to dispose of particles *inside* domain
194+
bool kill_inside; // True to dispose of particles *inside* domain
195195
pDomain positionL; // Disposal region (in local space)
196196
pDomain position; // Disposal region
197197

@@ -200,7 +200,7 @@ struct PARTICLES_API PASink : public ParticleAction
200200

201201
struct PARTICLES_API PASinkVelocity : public ParticleAction
202202
{
203-
BOOL kill_inside; // True to dispose of particles with vel *inside* domain
203+
bool kill_inside; // True to dispose of particles with vel *inside* domain
204204
pDomain velocityL; // Disposal region (in local space)
205205
pDomain velocity; // Disposal region
206206

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,93 @@
11
#include "stdafx.h"
2-
#pragma hdrstop
32

43
#include "particle_effect.h"
4+
5+
namespace PAPI
6+
{
7+
ParticleEffect::ParticleEffect(int mp)
8+
{
9+
owner = nullptr;
10+
param = 0;
11+
b_cb = nullptr;
12+
d_cb = nullptr;
13+
p_count = 0;
14+
max_particles = mp;
15+
particles_allocated = max_particles;
16+
17+
particles = xr_alloc<Particle>(max_particles);
18+
//Msg("Allocated %u bytes (%u particles) with base address 0x%p", max_particles * sizeof(Particle), max_particles, particles);
19+
}
20+
21+
ParticleEffect::~ParticleEffect() { xr_free(particles); }
22+
23+
int ParticleEffect::Resize(u32 max_count)
24+
{
25+
// Reducing max.
26+
if (particles_allocated >= max_count)
27+
{
28+
max_particles = max_count;
29+
30+
// May have to kill particles.
31+
if (p_count > max_particles)
32+
p_count = max_particles;
33+
34+
return max_count;
35+
}
36+
37+
// Allocate particles.
38+
Particle* new_particles = xr_alloc<Particle>(max_count);
39+
if (!new_particles)
40+
{
41+
// ERROR - Not enough memory. Just give all we've got.
42+
max_particles = particles_allocated;
43+
return max_particles;
44+
}
45+
46+
//Msg("Re-allocated %u bytes (%u particles) with base address 0x%p", max_count * sizeof(Particle), max_count, new_particles);
47+
48+
CopyMemory(new_particles, particles, p_count*sizeof(Particle));
49+
xr_free(particles);
50+
51+
particles = new_particles;
52+
53+
max_particles = max_count;
54+
particles_allocated = max_count;
55+
return max_count;
56+
}
57+
58+
void ParticleEffect::Remove(int i)
59+
{
60+
if (0 == p_count)
61+
return;
62+
Particle& m = particles[i];
63+
64+
if (d_cb)
65+
d_cb(owner, param, m, i);
66+
67+
m = particles[--p_count]; // íå ìåíÿòü ïðàâèëî óäàëåíèÿ !!! (dependence ParticleGroup)
68+
//Msg("pDel() : %u", p_count);
69+
}
70+
71+
bool ParticleEffect::Add(const pVector& pos, const pVector& posB, const pVector& size, const pVector& rot,
72+
const pVector& vel, u32 color, const float age /*= 0.0f*/, u16 frame /*= 0*/,
73+
u16 flags /*= 0*/)
74+
{
75+
if (p_count >= max_particles)
76+
return false;
77+
Particle& P = particles[p_count];
78+
P.pos = pos;
79+
P.posB = posB;
80+
P.size = size;
81+
P.rot.x = rot.x;
82+
P.vel = vel;
83+
P.color = color;
84+
P.age = age;
85+
P.frame = frame;
86+
P.flags.assign(flags);
87+
if (b_cb)
88+
b_cb(owner, param, P, p_count);
89+
p_count++;
90+
//Msg("pAdd() : %u", p_count);
91+
return true;
92+
}
93+
} // namespace PAPI

src/xrParticles/particle_effect.h

Lines changed: 7 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#pragma once
2-
#ifndef particle_effectH
3-
#define particle_effectH
42

53
namespace PAPI
64
{
75
// A effect of particles - Info and an array of Particles
8-
struct ParticleEffect
6+
struct PARTICLES_API ParticleEffect
97
{
108
u32 p_count; // Number of particles currently existing.
119
u32 max_particles; // Max particles allowed in effect.
@@ -16,91 +14,15 @@ struct ParticleEffect
1614
void* owner;
1715
u32 param;
1816

19-
ParticleEffect(int mp)
20-
{
21-
owner = nullptr;
22-
param = 0;
23-
b_cb = nullptr;
24-
d_cb = nullptr;
25-
p_count = 0;
26-
max_particles = mp;
27-
particles_allocated = max_particles;
17+
ParticleEffect(int mp);
2818

29-
particles = xr_alloc<Particle>(max_particles);
30-
//Msg("Allocated %u bytes (%u particles) with base address 0x%p", max_particles * sizeof(Particle), max_particles, particles);
31-
}
19+
~ParticleEffect();
3220

33-
~ParticleEffect() { xr_free(particles); }
21+
int Resize(u32 max_count);
3422

35-
int Resize(u32 max_count)
36-
{
37-
// Reducing max.
38-
if (particles_allocated >= max_count)
39-
{
40-
max_particles = max_count;
41-
42-
// May have to kill particles.
43-
if (p_count > max_particles)
44-
p_count = max_particles;
45-
46-
return max_count;
47-
}
48-
49-
// Allocate particles.
50-
Particle* new_particles = xr_alloc<Particle>(max_count);
51-
if (!new_particles)
52-
{
53-
// ERROR - Not enough memory. Just give all we've got.
54-
max_particles = particles_allocated;
55-
return max_particles;
56-
}
57-
58-
//Msg("Re-allocated %u bytes (%u particles) with base address 0x%p", max_count * sizeof(Particle), max_count, new_particles);
59-
60-
CopyMemory(new_particles, particles, p_count * sizeof(Particle));
61-
xr_free(particles);
62-
63-
particles = new_particles;
64-
65-
max_particles = max_count;
66-
particles_allocated = max_count;
67-
return max_count;
68-
}
69-
70-
void Remove(int i)
71-
{
72-
if (0 == p_count)
73-
return;
74-
Particle& m = particles[i];
75-
if (d_cb)
76-
d_cb(owner, param, m, i);
77-
m = particles[--p_count]; // не менять правило удаления !!! (dependence ParticleGroup)
78-
//Msg("pDel() : %u", p_count);
79-
}
23+
void Remove(int i);
8024

8125
bool Add(const pVector& pos, const pVector& posB, const pVector& size, const pVector& rot, const pVector& vel,
82-
u32 color, const float age = 0.0f, u16 frame = 0, u16 flags = 0)
83-
{
84-
if (p_count >= max_particles)
85-
return false;
86-
Particle& P = particles[p_count];
87-
P.pos = pos;
88-
P.posB = posB;
89-
P.size = size;
90-
P.rot.x = rot.x;
91-
P.vel = vel;
92-
P.color = color;
93-
P.age = age;
94-
P.frame = frame;
95-
P.flags.assign(flags);
96-
if (b_cb)
97-
b_cb(owner, param, P, p_count);
98-
p_count++;
99-
//Msg("pAdd() : %u", p_count);
100-
return true;
101-
}
26+
u32 color, const float age = 0.0f, u16 frame = 0, u16 flags = 0);
10227
};
103-
};
104-
105-
//---------------------------------------------------------------------------
106-
#endif
28+
} // namespace PAPI

0 commit comments

Comments
 (0)