Improve code and add stutter compensation.

This commit is contained in:
DelinWorks 2022-05-24 09:42:27 +03:00
parent 3c3069b4fc
commit ab3854f51a
2 changed files with 21 additions and 35 deletions

View File

@ -1016,25 +1016,20 @@ bool ParticleSystem::addAnimationIndex(unsigned short index, cocos2d::Rect rect,
return true; return true;
} }
void ParticleSystem::simulate(float seconds) void ParticleSystem::simulate(float seconds, float frameRate)
{
simulateFPS(seconds, 1.0F / Director::getInstance()->getAnimationInterval());
}
void ParticleSystem::simulateFPS(float seconds, float frameRate)
{ {
auto l_updatePaused = _updatePaused; auto l_updatePaused = _updatePaused;
_updatePaused = false; _updatePaused = false;
seconds = seconds == SIMULATION_USE_PARTICLE_LIFETIME ? seconds = seconds == SIMULATION_USE_PARTICLE_LIFETIME ?
getLife() + getLifeVar() : seconds; getLife() + getLifeVar() : seconds;
frameRate = frameRate == SIMULATION_USE_GAME_ANIMATION_INTERVAL ?
1.0F / Director::getInstance()->getAnimationInterval() : frameRate;
auto delta = 1.0F / frameRate; auto delta = 1.0F / frameRate;
float lastDelta = 0.0F;
if (seconds > delta) if (seconds > delta)
{ {
while (seconds > 0.0F) while (seconds > 0.0F)
{ {
this->update(delta); this->update(delta);
lastDelta = seconds;
seconds -= delta; seconds -= delta;
} }
this->update(seconds); this->update(seconds);
@ -1044,16 +1039,10 @@ void ParticleSystem::simulateFPS(float seconds, float frameRate)
_updatePaused = l_updatePaused; _updatePaused = l_updatePaused;
} }
void ParticleSystem::resimulate(float seconds) void ParticleSystem::resimulate(float seconds, float frameRate)
{ {
this->resetSystem(); this->resetSystem();
this->simulate(seconds); this->simulate(seconds, frameRate);
}
void ParticleSystem::resimulateFPS(float seconds, float frameRate)
{
this->resetSystem();
this->simulateFPS(seconds, frameRate);
} }
void ParticleSystem::onEnter() void ParticleSystem::onEnter()
@ -1111,6 +1100,12 @@ void ParticleSystem::update(float dt)
_componentContainer->visit(dt); _componentContainer->visit(dt);
} }
if (dt > 0.5F)
{
this->simulate(dt, 15);
return;
}
if (_fixedFPS != 0) if (_fixedFPS != 0)
{ {
_fixedFPSDelta += dt; _fixedFPSDelta += dt;

View File

@ -271,6 +271,9 @@ public:
/** The simulation's seconds are set to the particles' lifetime specified inclusive of variant. */ /** The simulation's seconds are set to the particles' lifetime specified inclusive of variant. */
SIMULATION_USE_PARTICLE_LIFETIME = -1, SIMULATION_USE_PARTICLE_LIFETIME = -1,
/** The simulation's framerate is set to the animation interval specified in director. */
SIMULATION_USE_GAME_ANIMATION_INTERVAL = -1,
}; };
/** Creates an initializes a ParticleSystem from a plist file. /** Creates an initializes a ParticleSystem from a plist file.
@ -948,34 +951,22 @@ public:
*/ */
void setPositionType(PositionType type) { _positionType = type; } void setPositionType(PositionType type) { _positionType = type; }
/** Advance the particle system and make it seem like it ran for this many seconds.
* The frame rate used for simulation accuracy is the screens refresh rate.
*
* @param seconds Seconds to advance. value of -1 means (SIMULATION_USE_PARTICLE_LIFETIME)
*/
void simulate(float seconds = SIMULATION_USE_PARTICLE_LIFETIME);
/** Advance the particle system and make it seem like it ran for this many seconds. /** Advance the particle system and make it seem like it ran for this many seconds.
* *
* @param seconds Seconds to advance. value of -1 means (SIMULATION_USE_PARTICLE_LIFETIME) * @param seconds Seconds to advance. value of -1 means (SIMULATION_USE_PARTICLE_LIFETIME)
* @param frameRate Frame rate to run the simulation with (preferred: 30.0) The higher this value is the more accurate the simulation will be at the cost of performance. * @param frameRate Frame rate to run the simulation with (preferred: 30.0) The higher this value is the more accurate the simulation will be at the cost of performance. value of -1 means (SIMULATION_USE_GAME_ANIMATION_INTERVAL)
*/ */
void simulateFPS(float seconds = SIMULATION_USE_PARTICLE_LIFETIME, float frameRate = 30.0F); void simulate(float seconds = SIMULATION_USE_PARTICLE_LIFETIME,
float frameRate = SIMULATION_USE_GAME_ANIMATION_INTERVAL);
/** Resets the particle system and then advances the particle system and make it seem like it ran for this many /** Resets the particle system and then advances the particle system and make it seem like it ran for this many
* seconds. The frame rate used for simulation accuracy is the screens refresh rate. * seconds. The frame rate used for simulation accuracy is the screens refresh rate.
* *
* @param seconds Seconds to advance. value of -1 means (SIMULATION_USE_PARTICLE_LIFETIME) * @param seconds Seconds to advance. value of -1 means (SIMULATION_USE_PARTICLE_LIFETIME)
* @param frameRate Frame rate to run the simulation with (preferred: 30.0) The higher this value is the more accurate the simulation will be at the cost of performance. value of -1 means (SIMULATION_USE_GAME_ANIMATION_INTERVAL)
*/ */
void resimulate(float seconds = SIMULATION_USE_PARTICLE_LIFETIME); void resimulate(float seconds = SIMULATION_USE_PARTICLE_LIFETIME,
float frameRate = SIMULATION_USE_GAME_ANIMATION_INTERVAL);
/** Resets the particle system and then advances the particle system and make it seem like it ran for this many
* seconds. The frame rate used for simulation accuracy is the screens refresh rate.
*
* @param seconds Seconds to advance. value of -1 means (SIMULATION_USE_PARTICLE_LIFETIME)
* @param frameRate Frame rate to run the simulation with (preferred: 30.0) The higher this value is the more accurate the simulation will be at the cost of performance.
*/
void resimulateFPS(float seconds = SIMULATION_USE_PARTICLE_LIFETIME, float frameRate = 30.0F);
// Overrides // Overrides
virtual void onEnter() override; virtual void onEnter() override;