OpenClonk
C4ParticleSystem Class Reference

#include <C4Particles.h>

Collaboration diagram for C4ParticleSystem:
[legend]

Public Member Functions

 C4ParticleSystem ()
 
 ~C4ParticleSystem ()
 
void CalculateNextStep ()
 
void Clear ()
 
void DrawGlobalParticles (C4TargetFacet cgo)
 
C4ParticleListGetGlobalParticles ()
 
C4ParticleListGetNewParticleList (C4Object *forTarget=nullptr)
 
void ReleaseParticleList (C4ParticleList *first, C4ParticleList *second=nullptr)
 
GLuint GetIBO (size_t forParticleAmount)
 
void Create (C4ParticleDef *of_def, C4ParticleValueProvider &x, C4ParticleValueProvider &y, C4ParticleValueProvider &speedX, C4ParticleValueProvider &speedY, C4ParticleValueProvider &lifetime, C4PropList *properties, int amount=1, C4Object *object=nullptr)
 
void ClearAllParticles ()
 

Public Attributes

C4ParticleSystemDefinitionList definitions
 

Friends

class CalculationThread
 
class C4ParticleList
 

Detailed Description

Definition at line 444 of file C4Particles.h.

Constructor & Destructor Documentation

◆ C4ParticleSystem()

C4ParticleSystem::C4ParticleSystem ( )

Definition at line 1259 of file C4Particles.cpp.

1259  : frameCounterAdvancedEvent(false)
1260 {
1261  currentSimulationTime = 0;
1262  globalParticles = 0;
1263  ibo = 0;
1264  ibo_size = 0;
1265 }

◆ ~C4ParticleSystem()

C4ParticleSystem::~C4ParticleSystem ( )

Definition at line 1267 of file C4Particles.cpp.

References CalculateNextStep(), Clear(), CStdCSec::Enter(), C4Game::FrameCounter, Game, INFINITE, CStdCSec::Leave(), CStdEvent::Reset(), and CStdEvent::WaitFor().

1268 {
1269  Clear();
1270 
1271  calculationThread.SignalStop();
1273 }
void CalculateNextStep()
Definition: C4Particles.h:481
Here is the call graph for this function:

Member Function Documentation

◆ CalculateNextStep()

void C4ParticleSystem::CalculateNextStep ( )
inline

Definition at line 481 of file C4Particles.h.

References CStdEvent::Set().

Referenced by C4Game::Execute(), and ~C4ParticleSystem().

482  {
483 #ifndef USE_CONSOLE
484  frameCounterAdvancedEvent.Set();
485 #endif
486  }
void Set()
Definition: StdSync.h:158
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Clear()

void C4ParticleSystem::Clear ( )

Definition at line 1477 of file C4Particles.cpp.

References C4ParticleSystemDefinitionList::Clear(), ClearAllParticles(), and definitions.

Referenced by C4Game::Clear(), and ~C4ParticleSystem().

1478 {
1479 #ifndef USE_CONSOLE
1480  if (ibo != 0) glDeleteBuffers(1, &ibo);
1481  ibo = 0; ibo_size = 0;
1482 
1483  currentSimulationTime = 0;
1485 #endif
1486  // clear definitions even in console mode
1487  definitions.Clear();
1488 }
C4ParticleSystemDefinitionList definitions
Definition: C4Particles.h:510
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ClearAllParticles()

void C4ParticleSystem::ClearAllParticles ( )

Definition at line 1490 of file C4Particles.cpp.

References CStdCSec::Enter(), and CStdCSec::Leave().

Referenced by Clear(), C4Game::LoadScenarioSection(), and C4Game::ReloadParticle().

1491 {
1492 #ifndef USE_CONSOLE
1493  particleListAccessMutex.Enter();
1494  particleLists.clear();
1495  particleListAccessMutex.Leave();
1496 #endif
1497 }
virtual void Leave()
Definition: StdSync.h:151
virtual void Enter()
Definition: StdSync.h:150
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Create()

void C4ParticleSystem::Create ( C4ParticleDef of_def,
C4ParticleValueProvider x,
C4ParticleValueProvider y,
C4ParticleValueProvider speedX,
C4ParticleValueProvider speedY,
C4ParticleValueProvider lifetime,
C4PropList properties,
int  amount = 1,
C4Object object = nullptr 
)

Definition at line 1339 of file C4Particles.cpp.

References C4ParticleChunk::AddNewParticle(), C4ParticleDef::Aspect, C4Particle::DrawingData::aspect, C4ParticleProperties::attachment, C4Object::BackParticles, C4ParticleProperties::blitMode, C4ATTACH_Back, C4ATTACH_Front, C4ATTACH_MoveRelative, C4ParticleProperties::colorAlpha, C4ParticleProperties::colorB, C4ParticleProperties::colorG, C4ParticleProperties::colorR, C4Particle::currentSpeedX, C4Particle::currentSpeedY, C4Particle::drawingData, C4ParticleValueProvider::Floatify(), C4ParticleProperties::Floatify(), C4Object::FrontParticles, C4ParticleList::GetFittingParticleChunk(), GetNewParticleList(), C4ParticleValueProvider::GetValue(), C4Particle::lifetime, C4ParticleList::Lock(), C4ParticleProperties::phase, C4Particle::properties, C4ParticleChunk::ReserveSpace(), C4ParticleProperties::Set(), C4Particle::DrawingData::SetColor(), C4Particle::DrawingData::SetOffset(), C4Particle::DrawingData::SetPhase(), C4Particle::SetPosition(), C4Particle::startingLifetime, and C4ParticleList::Unlock().

1340 {
1341  // todo: check amount etc
1342 
1343  C4ParticleList * pxList(0);
1344 
1345 
1346  // initialize the particle properties
1347  // this is done here, because it would also be the right place to implement caching
1348  C4ParticleProperties particleProperties;
1349  particleProperties.Set(properties);
1350 
1351  speedX.Floatify(10.f);
1352  speedY.Floatify(10.f);
1353 
1354  // position offset that will be added to the particle
1355  float xoff(0.f), yoff(0.f);
1356 
1357  // offset only for the drawing position - this is needed so that particles relative to an object work correctly
1358  float drawingOffsetX(0.f), drawingOffsetY(0.f);
1359 
1360  if (object != 0)
1361  {
1362  // for all types of particles add object's offset (mainly for collision etc.)
1363  xoff = object->GetX();
1364  yoff = object->GetY();
1365 
1366  if (particleProperties.attachment & C4ATTACH_MoveRelative)
1367  {
1368  drawingOffsetX = -xoff;
1369  drawingOffsetY = -yoff;
1370 
1371  // move relative implies that the particle needs to be in the object's particle list (back OR front)
1372  // just select the front particles here - will be overwritten below if necessary
1373  if (!(particleProperties.attachment & C4ATTACH_Front) && !(particleProperties.attachment & C4ATTACH_Back))
1374  particleProperties.attachment |= C4ATTACH_Front;
1375  }
1376 
1377  // figure out particle list to use
1378  if (particleProperties.attachment & C4ATTACH_Front)
1379  {
1380  if (!object->FrontParticles) object->FrontParticles = GetNewParticleList(object);
1381  pxList = object->FrontParticles;
1382  }
1383  else if (particleProperties.attachment & C4ATTACH_Back)
1384  {
1385  if (!object->BackParticles) object->BackParticles = GetNewParticleList(object);
1386  pxList = object->BackParticles;
1387  }
1388  }
1389 
1390  // no assigned list implies that we are going to use the global particles
1391  if (!pxList)
1392  {
1393  if (!globalParticles) globalParticles = GetNewParticleList();
1394  pxList = globalParticles;
1395  }
1396 
1397  // It is necessary to lock the particle list, because we will have it create a particle first that we are going to modify.
1398  // Inbetween creation of the particle and modification, the particle list's calculations should not be executed
1399  // (this could f.e. lead to the particle being removed before it was fully instantiated).
1400  pxList->Lock();
1401 
1402  // retrieve the fitting chunk for the particle (note that we tell the particle list, we already locked it)
1403  C4ParticleChunk *chunk = pxList->GetFittingParticleChunk(of_def, particleProperties.blitMode, particleProperties.attachment, true);
1404 
1405  // set up chunk to be able to contain enough particles
1406  chunk->ReserveSpace(static_cast<uint32_t>(amount));
1407 
1408  while (amount--)
1409  {
1410  // create a particle in the fitting chunk (note that we tell the particle list, we already locked it)
1411  C4Particle *particle = chunk->AddNewParticle();
1412 
1413  // initialize some more properties
1414  particle->properties = particleProperties;
1415  // this will adjust the initial values of the (possibly cached) particle properties
1416  particle->properties.Floatify();
1417 
1418  // setup some more non-property attributes of the particle
1419  // The particle having lifetime == startingLifetime will force all random values to alway-reevaluate.
1420  // Thus we need to guarantee that even before setting the lifetime (to allow a PV_Random for the lifetime).
1421  particle->lifetime = particle->startingLifetime = 0.0f;
1422  const float lifetime_value = lifetime.GetValue(particle);
1423  // Negative values are not allowed (would crash later); using a value of 0 is most likely visible to the scripter.
1424  if (lifetime_value >= 0.0f)
1425  particle->lifetime = particle->startingLifetime = lifetime_value;
1426 
1427  particle->currentSpeedX = speedX.GetValue(particle);
1428  particle->currentSpeedY = speedY.GetValue(particle);
1429  particle->drawingData.aspect = of_def->Aspect;
1430  particle->drawingData.SetOffset(drawingOffsetX, drawingOffsetY);
1431  particle->SetPosition(x.GetValue(particle) + xoff, y.GetValue(particle) + yoff);
1432  particle->drawingData.SetColor(particle->properties.colorR.GetValue(particle), particle->properties.colorG.GetValue(particle), particle->properties.colorB.GetValue(particle), particle->properties.colorAlpha.GetValue(particle));
1433  particle->drawingData.SetPhase((int)(particle->properties.phase.GetValue(particle) + 0.5f), of_def);
1434  }
1435 
1436  pxList->Unlock();
1437 }
void Floatify(float denominator)
C4ParticleProperties properties
Definition: C4Particles.h:314
C4ParticleValueProvider phase
Definition: C4Particles.h:210
C4ParticleValueProvider colorG
Definition: C4Particles.h:208
class C4ParticleList * BackParticles
Definition: C4Object.h:159
C4Particle * AddNewParticle()
void SetPhase(int phase, C4ParticleDef *sourceDef)
float startingLifetime
Definition: C4Particles.h:312
C4ParticleValueProvider colorAlpha
Definition: C4Particles.h:208
void Set(C4PropList *dataSource)
float lifetime
Definition: C4Particles.h:312
void ReserveSpace(uint32_t forAmount)
float currentSpeedX
Definition: C4Particles.h:310
float GetValue(C4Particle *forParticle)
float currentSpeedY
Definition: C4Particles.h:310
void SetPosition(float x, float y)
Definition: C4Particles.h:324
void SetColor(float r, float g, float b, float a=1.0f)
Definition: C4Particles.h:290
C4ParticleList * GetNewParticleList(C4Object *forTarget=nullptr)
C4ParticleValueProvider colorB
Definition: C4Particles.h:208
class C4ParticleList * FrontParticles
Definition: C4Object.h:159
C4ParticleValueProvider colorR
Definition: C4Particles.h:208
struct C4Particle::DrawingData drawingData
void SetOffset(float x, float y)
Definition: C4Particles.h:267
Here is the call graph for this function:

◆ DrawGlobalParticles()

void C4ParticleSystem::DrawGlobalParticles ( C4TargetFacet  cgo)
inline

Definition at line 489 of file C4Particles.h.

References C4ParticleList::Draw().

Referenced by C4Viewport::Draw().

490  {
491 #ifndef USE_CONSOLE
492  if (globalParticles) globalParticles->Draw(cgo, nullptr);
493 #endif
494  }
void Draw(C4TargetFacet cgo, C4Object *obj)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetGlobalParticles()

C4ParticleList* C4ParticleSystem::GetGlobalParticles ( )
inline

Definition at line 496 of file C4Particles.h.

497  {
498 #ifndef USE_CONSOLE
499  return globalParticles;
500 #else
501  return nullptr;
502 #endif
503  }

◆ GetIBO()

GLuint C4ParticleSystem::GetIBO ( size_t  forParticleAmount)

Definition at line 1439 of file C4Particles.cpp.

Referenced by C4ParticleChunk::Draw().

1440 {
1441  PreparePrimitiveRestartIndices(forParticleAmount);
1442  return ibo;
1443 }
Here is the caller graph for this function:

◆ GetNewParticleList()

C4ParticleList * C4ParticleSystem::GetNewParticleList ( C4Object forTarget = nullptr)

Definition at line 1300 of file C4Particles.cpp.

References CStdCSec::Enter(), and CStdCSec::Leave().

Referenced by Create().

1301 {
1302 #ifdef USE_CONSOLE
1303  return 0;
1304 #else
1305  C4ParticleList *newList = 0;
1306 
1307  particleListAccessMutex.Enter();
1308  particleLists.emplace_back(forObject);
1309  newList = &particleLists.back();
1310  particleListAccessMutex.Leave();
1311 
1312  return newList;
1313 #endif
1314 }
virtual void Leave()
Definition: StdSync.h:151
virtual void Enter()
Definition: StdSync.h:150
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ReleaseParticleList()

void C4ParticleSystem::ReleaseParticleList ( C4ParticleList first,
C4ParticleList second = nullptr 
)

Definition at line 1316 of file C4Particles.cpp.

References CStdCSec::Enter(), and CStdCSec::Leave().

Referenced by C4Object::ClearParticleLists().

1317 {
1318 #ifndef USE_CONSOLE
1319  particleListAccessMutex.Enter();
1320 
1321  for(std::list<C4ParticleList>::iterator iter = particleLists.begin(); iter != particleLists.end();)
1322  {
1323  C4ParticleList *list = &(*iter);
1324  if (list == first || list == second)
1325  {
1326  iter = particleLists.erase(iter);
1327  }
1328  else
1329  {
1330  ++iter;
1331  }
1332  }
1333 
1334  particleListAccessMutex.Leave();
1335 #endif
1336 }
virtual void Leave()
Definition: StdSync.h:151
virtual void Enter()
Definition: StdSync.h:150
Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ C4ParticleList

friend class C4ParticleList
friend

Definition at line 525 of file C4Particles.h.

◆ CalculationThread

friend class CalculationThread
friend

Definition at line 454 of file C4Particles.h.

Member Data Documentation

◆ definitions


The documentation for this class was generated from the following files: