Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions core/2d/ParticleSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ void ParticleSystem::deallocOpacityFadeInMem()
_isOpacityFadeInAllocated = false;
}

// @return true if succeed in allocating
bool ParticleSystem::allocScaleInMem()
{
if (!_isScaleInAllocated)
Expand Down Expand Up @@ -940,9 +941,11 @@ void ParticleSystem::addParticles(int count, int animationIndex, int animationCe
SET_DELTA_COLOR(_particleData.colorG, _particleData.deltaColorG);
SET_DELTA_COLOR(_particleData.colorB, _particleData.deltaColorB);
SET_DELTA_COLOR(_particleData.colorA, _particleData.deltaColorA);


// Should skip these initialization when OpacityFadeIn or ScaleFadeIn is 0, otherwise
// particles would fail to show up.
// opacity fade in
if (_isOpacityFadeInAllocated)
if (_isOpacityFadeInAllocated && _spawnFadeIn + _spawnFadeInVar > 0)
{
for (int i = start; i < _particleCount; ++i)
{
Expand All @@ -952,7 +955,7 @@ void ParticleSystem::addParticles(int count, int animationIndex, int animationCe
}

// scale fade in
if (_isScaleInAllocated)
if (_isScaleInAllocated && _spawnScaleIn + _spawnScaleInVar > 0)
{
for (int i = start; i < _particleCount; ++i)
{
Expand Down Expand Up @@ -1540,7 +1543,9 @@ void ParticleSystem::resetSystem()
{
_isActive = true;
_elapsed = 0;
std::fill_n(_particleData.timeToLive, _particleCount, 0.0F);
// Setting _particleCount to zero could prevent Out-of-Range exception when updating
// particle's loop animation after calling setTotalParticle().
_particleCount = 0;
}

bool ParticleSystem::isFull()
Expand Down Expand Up @@ -2101,31 +2106,34 @@ void ParticleSystem::useHSV(bool hsv)

void ParticleSystem::setSpawnFadeIn(float time)
{
if (time != 0.0F && !allocOpacityFadeInMem())
// Modification is allowed when:
// - memory hasn't been allocated, time != 0.0F and allocation has done successfully; OR
// - memory has allocated.
if (!_isOpacityFadeInAllocated && (time == 0.0F || !allocOpacityFadeInMem()))
return;

_spawnFadeIn = time;
}

void ParticleSystem::setSpawnFadeInVar(float time)
{
if (time != 0.0F && !allocOpacityFadeInMem())
if (!_isOpacityFadeInAllocated && (time == 0.0F || !allocOpacityFadeInMem()))
return;

_spawnFadeInVar = time;
}

void ParticleSystem::setSpawnScaleIn(float time)
{
if (time != 0.0F && !allocScaleInMem())
if (!_isScaleInAllocated && (time == 0.0F || !allocScaleInMem()))
return;

_spawnScaleIn = time;
}

void ParticleSystem::setSpawnScaleInVar(float time)
{
if (time != 0.0F && !allocScaleInMem())
if (!_isScaleInAllocated && (time == 0.0F || !allocScaleInMem()))
return;

_spawnScaleInVar = time;
Expand Down
38 changes: 36 additions & 2 deletions core/2d/ParticleSystemQuad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ ParticleSystemQuad* ParticleSystemQuad::createWithTotalParticles(int numberOfPar
{
AXASSERT(numberOfParticles <= 10000,
"Adding more than 10000 particles will crash the renderer, the mesh generated has an index format of "
"U_SHORT (uint16_t)");
"U_SHORT (uint16_t).");

numberOfParticles = std::min(numberOfParticles, 10000);

ParticleSystemQuad* ret = new ParticleSystemQuad();
if (ret->initWithTotalParticles(numberOfParticles))
Expand Down Expand Up @@ -686,6 +688,11 @@ void ParticleSystemQuad::draw(Renderer* renderer, const Mat4& transform, uint32_

void ParticleSystemQuad::setTotalParticles(int tp)
{
AXASSERT(tp <= 10000,
"Adding more than 10000 particles will crash the renderer, the mesh generated has an index format of "
"U_SHORT (uint16_t).");

tp = (std::min)(tp, 10000);
// If we are setting the total number of particles to a number higher
// than what is allocated, we need to allocate new arrays
if (tp > _allocatedParticles)
Expand All @@ -694,10 +701,12 @@ void ParticleSystemQuad::setTotalParticles(int tp)
size_t quadsSize = sizeof(_quads[0]) * tp * 1;
size_t indicesSize = sizeof(_indices[0]) * tp * 6 * 1;

_particleData.release();
_particleData.release();

if (!_particleData.init(tp))
{
AXLOGW("Particle system: not enough memory");
_particleData.release();
return;
}
V3F_C4B_T2F_Quad* quadsNew = (V3F_C4B_T2F_Quad*)realloc(_quads, quadsSize);
Expand Down Expand Up @@ -729,6 +738,31 @@ void ParticleSystemQuad::setTotalParticles(int tp)

_totalParticles = tp;

// Reallocation of OpacityFadeIn, ScaleIn, Animation and HSV is independent of
// _particleData.init(), but relies on _totalParticles; before doing so, we have
// to delete their previous memory first, as allocation would check if allocation
// flag is false.

bool hasOpacityFadeInAllocated = _isOpacityFadeInAllocated,
hasScaleInAllocated = _isScaleInAllocated,
hasAnimAllocated = _isAnimAllocated,
hasHSVAllocated = _isHSVAllocated;

deallocOpacityFadeInMem();
deallocScaleInMem();
deallocAnimationMem();
deallocHSVMem();

bool isExtraAllocSuccessful = (!hasOpacityFadeInAllocated || allocOpacityFadeInMem() ) &&
(!hasScaleInAllocated || allocScaleInMem() ) &&
(!hasAnimAllocated || allocAnimationMem() ) &&
(!hasHSVAllocated || allocHSVMem() );
if (!isExtraAllocSuccessful){
AXLOGW("Particle system: not enough memory");
_particleData.release();
return;
}

// Init particles
if (_batchNode)
{
Expand Down