Particles.cpp
Upload User: xuczgm
Upload Date: 2022-04-25
Package Size: 8601k
Code Size: 3k
Category:

Special Effects

Development Platform:

Visual C++

  1. /****************************************************************************
  2.  Particles.cpp
  3.  Author   :   Dave Astle
  4.  Date     :   2/1/2001
  5. 粒子系统,爆炸
  6.  Written for OpenGL Game Programming
  7. *****************************************************************************/
  8. #include "stdafx.h"
  9. #include "Particles.h"
  10. /*****************************************************************************
  11.  CParticleSystem::Constructor
  12.  Store initialization values and set defaults.
  13. *****************************************************************************/
  14. CParticleSystem::CParticleSystem(int maxParticles, CVector origin)
  15. {
  16.   m_maxParticles = maxParticles;
  17.   m_origin = origin;
  18.   m_particleList = NULL;
  19. } // end CParticleSystem::Constructor
  20. /*****************************************************************************
  21.  CParticleSystem::Emit()
  22.  
  23.  Creates the number of new particles specified by the parameter, using
  24.  the general particle system values with some random element. Note that
  25.  only initial values will be randomized. Final values will not. This may
  26.  be changed in the future.
  27. *****************************************************************************/
  28. int CParticleSystem::Emit(int numParticles)
  29. {
  30.   // create numParticles new particles (if there's room)
  31.   while (numParticles && (m_numParticles < m_maxParticles))
  32.   {
  33.     // initialize the current particle and increase the count
  34.     InitializeParticle(m_numParticles++);
  35.     --numParticles;
  36.   }
  37.   return numParticles;
  38. } // end CParticleSystem::Emit
  39. /*****************************************************************************
  40.  CParticleSystem::InitializeSystem()
  41.  
  42.  Allocate memory for the maximum number of particles in the system
  43. *****************************************************************************/
  44. void CParticleSystem::InitializeSystem()
  45. {
  46.   // if this is just a reset, free the memory
  47.   if (m_particleList)
  48.   {
  49.     delete[] m_particleList;
  50.     m_particleList = NULL;
  51.   }
  52.   // allocate the maximum number of particles
  53.   m_particleList = new particle_t[m_maxParticles];
  54.   // reset the number of particles and accumulated time
  55.   m_numParticles = 0;
  56.   m_accumulatedTime = 0.0f;
  57. } // end CParticleSystem::InitializeSystem
  58. /*****************************************************************************
  59.  CParticleSystem::KillSystem()
  60.  
  61.  Tells the emitter to stop emitting. If the parameter is true, all live
  62.  particles are killed as well.  Otherwise, they are allowed to die off on
  63.  their own.
  64. *****************************************************************************/
  65. void CParticleSystem::KillSystem()
  66. { if (m_particleList)
  67.   { delete[] m_particleList;
  68.     m_particleList = NULL;
  69.   }
  70.   m_numParticles = 0;
  71. } // end CParticleSystem::KillSystem