Mountain  1.0.0
Simple C++ 2D Game Framework
particle_system.hpp
1 #pragma once
2 
3 #include <Maths/vector2.hpp>
4 
5 #include "Mountain/core.hpp"
6 #include "Mountain/rendering/gpu_buffer.hpp"
7 #include "Mountain/rendering/gpu_vertex_array.hpp"
8 #include "Mountain/rendering/particle_system_modules.hpp"
9 #include "Mountain/resource/compute_shader.hpp"
11 #include "Mountain/utils/color.hpp"
12 #include "Mountain/utils/list.hpp"
13 
14 // OpenGL type forward declaration
15 // ReSharper disable once CppInconsistentNaming
16 struct __GLsync; // NOLINT(clang-diagnostic-reserved-identifier, bugprone-reserved-identifier)
17 
18 namespace Mountain
19 {
21  {
23  float_t time = 0.f;
25  uint32_t count = 30;
27  uint32_t cycles = 1;
29  float_t interval = 0.01f;
31  float_t probability = 1.f;
32  };
33 
35  {
36  public:
37  // Particle system settings
38 
39  Vector2 position;
40  float_t rotation;
41  float_t duration = 5.f;
42  bool_t looping = true;
43  float_t startDelay;
44  bool_t useUnscaledDeltaTime = false;
45 
46  // Emission settings
47 
48  float_t emissionRateOverTime = 10.f;
49  float_t emissionRateOverDistance = 0.f;
50  List<ParticleSystemBurst> emissionBursts;
51 
52  // Particle settings
53 
54  float_t particleLifetime = 5.f;
55  float_t particleSpeed = 5.f;
56  Color particleStartColor = Color::White();
57 
58  // Modules
59 
61  ParticleSystemModules::Types enabledModules = ParticleSystemModules::Types::Default;
62 
63  MOUNTAIN_API ParticleSystem() = default;
64  MOUNTAIN_API explicit ParticleSystem(uint32_t maxParticles);
65  MOUNTAIN_API virtual ~ParticleSystem();
66 
67  DEFAULT_COPY_MOVE_OPERATIONS(ParticleSystem)
68 
69  MOUNTAIN_API void Update();
70  MOUNTAIN_API void Render();
71  MOUNTAIN_API void RenderImGui();
72  MOUNTAIN_API void RenderDebug();
73 
74  MOUNTAIN_API void TogglePlay();
75  MOUNTAIN_API void Restart();
76  MOUNTAIN_API void Stop();
77 
80  [[nodiscard]]
81  MOUNTAIN_API uint32_t GetCurrentParticles();
82 
83  [[nodiscard]]
84  MOUNTAIN_API uint32_t GetMaxParticles() const;
87  MOUNTAIN_API void SetMaxParticles(uint32_t newMaxParticles);
88 
89  [[nodiscard]]
90  MOUNTAIN_API bool_t IsPlaying() const;
91 
92  private:
93  uint32_t m_MaxParticles = 0;
94  int32_t* m_LiveParticles = nullptr;
95  __GLsync* m_SyncObject = nullptr;
96 
97  float_t m_PlaybackTime = 0.f; // System lifetime
98  float_t m_LastPlaybackTime = 0.f;
99  double_t m_SpawnTimer = 0.0;
100  bool_t m_Playing = true;
101  Vector2 m_LastPosition;
102 
103  Pointer<ComputeShader> m_UpdateComputeShader;
104 
105  Pointer<Shader> m_DrawShader;
106  Graphics::GpuBuffer m_LiveSsbo, m_ParticleSsbo;
107  Graphics::GpuVertexArray m_DrawVao;
108 
109  bool_t m_GuiParticleBurstTimeHeld = false;
110  Vector2 m_RenderTargetSize;
111 
112  void SetComputeShaderUniforms(float_t deltaTime) const;
113  void SpawnNewParticles();
114 
116  static void WaitBufferSync(__GLsync* syncObject);
118  static void LockBuffer(__GLsync*& syncObject);
119  };
120 }
Defines the Mountain::List class.
Defines the Mountain::Shader class.
float_t interval
The time interval between each burst cycle.
A dynamic array implementation. Wrapper around the std::vector class.
Definition: list.hpp:24
Defines multiple color structs.
uint32_t cycles
How many times the burst should repeat.
Custom Mountain smart pointer. Represents both a std::shared_ptr and a std::weak_ptr.
float_t probability
The probability for the burst to happen.
static constexpr Color White()
Constant for White.
The Color struct represents a color in RGBA color space.
Definition: color.hpp:26
uint32_t count
The number of particle to spawn in the burst.
Low-level interface for OpenGL vertex arrays.
Low-level interface for OpenGL buffers.
Definition: gpu_buffer.hpp:12
float_t time
The time at which the burst should occur.
Contains all declarations of the Mountain Framework.
Definition: audio.hpp:22