OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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=0)
 
void ReleaseParticleList (C4ParticleList *first, C4ParticleList *second=0)
 
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 ( )

Definition at line 1261 of file C4Particles.cpp.

1261  : frameCounterAdvancedEvent(false)
1262 {
1263  currentSimulationTime = 0;
1264  globalParticles = 0;
1265  ibo = 0;
1266  ibo_size = 0;
1267 }
C4ParticleSystem::~C4ParticleSystem ( )

Definition at line 1269 of file C4Particles.cpp.

References CalculateNextStep(), and Clear().

1270 {
1271  Clear();
1272 
1273  calculationThread.SignalStop();
1275 }
void CalculateNextStep()
Definition: C4Particles.h:481

Here is the call graph for this function:

Member Function Documentation

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:168

Here is the call graph for this function:

Here is the caller graph for this function:

void C4ParticleSystem::Clear ( )

Definition at line 1479 of file C4Particles.cpp.

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

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

1480 {
1481 #ifndef USE_CONSOLE
1482  if (ibo != 0) glDeleteBuffers(1, &ibo);
1483  ibo = 0; ibo_size = 0;
1484 
1485  currentSimulationTime = 0;
1487 #endif
1488  // clear definitions even in console mode
1489  definitions.Clear();
1490 }
C4ParticleSystemDefinitionList definitions
Definition: C4Particles.h:510

Here is the call graph for this function:

Here is the caller graph for this function:

void C4ParticleSystem::ClearAllParticles ( )

Definition at line 1492 of file C4Particles.cpp.

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

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

1493 {
1494 #ifndef USE_CONSOLE
1495  particleListAccessMutex.Enter();
1496  particleLists.clear();
1497  particleListAccessMutex.Leave();
1498 #endif
1499 }
virtual void Leave()
Definition: StdSync.h:161
virtual void Enter()
Definition: StdSync.h:160

Here is the call graph for this function:

Here is the caller graph for this function:

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 1341 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().

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

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, 0);
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:

C4ParticleList* C4ParticleSystem::GetGlobalParticles ( )
inline

Definition at line 496 of file C4Particles.h.

497  {
498 #ifndef USE_CONSOLE
499  return globalParticles;
500 #else
501  return 0;
502 #endif
503  }
GLuint C4ParticleSystem::GetIBO ( size_t  forParticleAmount)

Definition at line 1441 of file C4Particles.cpp.

Referenced by C4ParticleChunk::Draw().

1442 {
1443  PreparePrimitiveRestartIndices(forParticleAmount);
1444  return ibo;
1445 }

Here is the caller graph for this function:

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

Definition at line 1302 of file C4Particles.cpp.

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

Referenced by Create().

1303 {
1304 #ifdef USE_CONSOLE
1305  return 0;
1306 #else
1307  C4ParticleList *newList = 0;
1308 
1309  particleListAccessMutex.Enter();
1310  particleLists.emplace_back(forObject);
1311  newList = &particleLists.back();
1312  particleListAccessMutex.Leave();
1313 
1314  return newList;
1315 #endif
1316 }
virtual void Leave()
Definition: StdSync.h:161
virtual void Enter()
Definition: StdSync.h:160

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 1318 of file C4Particles.cpp.

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

Referenced by C4Object::ClearParticleLists().

1319 {
1320 #ifndef USE_CONSOLE
1321  particleListAccessMutex.Enter();
1322 
1323  for(std::list<C4ParticleList>::iterator iter = particleLists.begin(); iter != particleLists.end();)
1324  {
1325  C4ParticleList *list = &(*iter);
1326  if (list == first || list == second)
1327  {
1328  iter = particleLists.erase(iter);
1329  }
1330  else
1331  {
1332  ++iter;
1333  }
1334  }
1335 
1336  particleListAccessMutex.Leave();
1337 #endif
1338 }
virtual void Leave()
Definition: StdSync.h:161
virtual void Enter()
Definition: StdSync.h:160

Here is the call graph for this function:

Here is the caller graph for this function:

Friends And Related Function Documentation

friend class C4ParticleList
friend

Definition at line 525 of file C4Particles.h.

friend class CalculationThread
friend

Definition at line 454 of file C4Particles.h.

Member Data Documentation


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