TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
tb_particle_manager.hpp
1 
9 #ifndef TurtleBrains_ParticleManager_hpp
10 #define TurtleBrains_ParticleManager_hpp
11 
12 #include <turtle_brains/core/tb_string.hpp>
13 #include <turtle_brains/core/tb_noncopyable.hpp>
14 #include <turtle_brains/core/tb_types.hpp>
15 #include <turtle_brains/math/tb_vector.hpp>
16 #include <turtle_brains/graphics/tb_sprite.hpp>
17 
18 #include <vector>
19 #include <list>
20 #include <map>
21 
22 namespace tbImplementation
23 {
25 
30  class Particle
31  {
32  public:
33  tbMath::Vector2 mPosition;
34  tbMath::Vector2 mVelocity;
35  tbMath::Vector2 mAcceleration;
36 
37  float mScale;
38  float mDragCoefficient;
39  tbCore::uint32 mFadeInTime;
40  tbCore::uint32 mFadeOutTime;
41  tbCore::uint32 mLifeTimer;
42  bool mGlobalAcceleration;
43 
44  tbGraphics::SpriteFrame mSpriteFrame;
45  tbGraphics::Color mColor;
46 
47  Particle(void);
48  bool Update(const float deltaTime);
49  };
50 
52 };
53 
54 namespace TurtleBrains
55 {
56  namespace Graphics
57  {
58  namespace Unstable
59  {
61 
62  template <typename Type> class RangedType
63  {
64  public:
65  explicit RangedType(const Type& value) :
66  mMinimumValue(value),
67  mMaximumValue(value)
68  {
69  }
70 
71  explicit RangedType(const Type& minimumValue, const Type& maximumValue) :
72  mMinimumValue(minimumValue),
73  mMaximumValue(maximumValue)
74  {
75  tb_error_if(maximumValue < minimumValue, "tbExternalError: Expected minimumValue to be less-than-or-equal-to maximumValue.");
76  }
77 
78  ~RangedType(void)
79  {
80  }
81 
82  void SetRange(const Type& minimumValue, const Type& maximumValue)
83  {
84  //tb_error_if(maximumValue < minimumValue, "tbExternalError: Expected minimumValue to be less-than-or-equal-to maximumValue.");
85  mMinimumValue = minimumValue;
86  mMaximumValue = maximumValue;
87  }
88 
89  const Type& Minimum(void) const { return mMinimumValue; }
90  const Type& Maximum(void) const { return mMaximumValue; }
91 
92  private:
93  Type mMinimumValue;
94  Type mMaximumValue;
95  };
96 
97  struct ParticleDefinition
98  {
99  tbGraphics::SpriteFrame mSpriteFrame;
100  RangedType<float> mSpawnDistanceRange;
101  RangedType<float> mDragCoefficientRange;
102  RangedType<float> mScaleRange;
103  RangedType<float> mSpeedRange;
104  RangedType<float> mLaunchAngleRange;
105  RangedType<tbMath::Vector2> mAccelerationRange;
106 
107  RangedType<tbCore::uint32> mParticleLifeTimeRange;
108  RangedType<tbCore::uint32> mParticleFadeInTimeRange;
109  RangedType<tbCore::uint32> mParticleFadeOutTimeRange;
110 
111  bool mGlobalAcceleration; //Users Gravity/Wind accelerations.
112  tbGraphics::Color mColor;
113 
114  explicit ParticleDefinition(const tbGraphics::SpriteFrame& spriteFrame);
115  };
116 
117  struct EmitterDefinition
118  {
119  TextureHandle mTextureHandle;
120  std::vector<ParticleDefinition> mParticleDefinitions;
121  tbCore::uint32 mLifeTime;
122  tbCore::uint32 mSpawnRate;
123  tbCore::uint8 mSpawnCount;
124 
125  explicit EmitterDefinition(const TextureHandle& textureHandle);
126  };
127 
132  class ParticleManager : tbCore::Noncopyable
133  {
134  public:
135  ParticleManager(void);
136  ~ParticleManager(void);
137 
138  bool LoadParticleSheetFromFile(const tbCore::tbString& particleSheetName, const tbCore::tbString& particleSheetFile);
139 
140  const EmitterDefinition& GetEmitterDefinition(const tbCore::tbString& particleSheet, const tbCore::tbString& emitterName) const;
141 
143 
144  void ReloadModifiedSheets(void);
145 
147 
149  void ClearAllParticles(void);
150  void ManageParticle(const tbImplementation::Particle& particle);
151  void UpdateParticles(const float deltaTime);
152  void RenderParticles(void) const;
153 
154  void SetAcceleration(const tbMath::Vector2& accelerations);
155 
156  size_t GetParticleCount(void);
157 
158  private:
159  typedef std::list<tbImplementation::Particle> ParticleList;
160  typedef std::map<tbGraphics::TextureHandle, ParticleList> ParticleTable;
161 
162  ParticleTable mParticleTable;
163 
164  struct ParticleSheet
165  {
166  typedef std::map<tbCore::tbString, ParticleDefinition> ParticleDefinitionTable;
167  typedef std::map<tbCore::tbString, EmitterDefinition> EmitterDefinitionTable;
168 
169  ParticleDefinitionTable mParticles;
170  EmitterDefinitionTable mEmitters;
171  };
172 
173  typedef std::map<tbCore::tbString, ParticleSheet> ParticleSheetTable;
174  ParticleSheetTable mParticleSheets;
175 
177  typedef std::map<tbCore::tbString, tbCore::tbString> FileToSheetTable;
178  FileToSheetTable mFileToSheets;
179  };
180 
181  extern ParticleManager theParticleManager;
182 
184  } /* namespace Unstable */
185  }; /* namespace Graphics */
186 }; /* namespace TurtleBrains */
187 
189 
190 #endif /* TurtleBrains_ParticleManager_hpp */
Definition: tb_vector.hpp:48
Give the GameScene and Entities something to display, Text, Sprites and AnimatedSprites help bring th...
Definition: tb_sprite.hpp:29
TurtleBrains::Core::ResourceHandle< tbImplementation::TextureHandleSaver > TextureHandle
Definition: tb_texture_manager.hpp:42
Definition: tb_noncopyable.hpp:22
Here is some information about the primary namespace.
Definition: tb_application_dialog.hpp:21
Definition: tb_color.hpp:24
uint8_t uint8
Unsigned integer with a size of 8 bits. Supports values from 0 to 255.
Definition: tb_types.hpp:23
constexpr const T & Maximum(const T &leftValue, const T &rightValue) noexcept
Definition: tb_math.hpp:76
uint32_t uint32
Unsigned integer with a size of 32 bits. Supports values from 0 to 4294967295, (2^32 - 1)...
Definition: tb_types.hpp:28
#define tb_error_if(errorTest, message,...)
Definition: tb_error.hpp:42
std::string tbString
Definition: tb_string.hpp:335
Definition: tb_application_dialog.hpp:19
constexpr const T & Minimum(const T &leftValue, const T &rightValue) noexcept
Definition: tb_math.hpp:89