ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
particles.h
Go to the documentation of this file.
1
8
9#ifndef ASW_PARTICLES_H
10#define ASW_PARTICLES_H
11
12#include <vector>
13
14#include "./camera.h"
15#include "./color.h"
16#include "./game.h"
17#include "./geometry.h"
18#include "./types.h"
19
20namespace asw {
21
25 // Lifetime in seconds
26 float lifetime_min { 1.0F };
27 float lifetime_max { 2.0F };
28
29 // Speed in pixels per second
30 float speed_min { 50.0F };
31 float speed_max { 100.0F };
32
33 // Direction (radians)
34 float angle_min { 0.0F };
35 float angle_max { 6.2832F }; // Full circle
36
37 // Visual
38 Color color_start { 255, 255, 255, 255 };
39 Color color_end { 255, 255, 255, 0 };
40 float alpha_start { 1.0F };
41 float alpha_end { 0.0F };
42 float size_start { 4.0F };
43 float size_end { 1.0F };
44
45 // Physics in pixels per second squared
46 Vec2<float> gravity { 0.0F, 0.0F };
47
48 // Optional texture (nullptr = use circleFill)
49 Texture texture { nullptr };
50};
51
55public:
58 ParticleEmitter() = default;
59
65 explicit ParticleEmitter(const ParticleConfig& config, uint32_t maxParticles = 256);
66
71 void set_emission_rate(float rate);
72
77 void emit(uint32_t count);
78
81 void start();
82
85 void stop();
86
91 void update(float dt) override;
92
95 void draw() override;
96
101 void draw(const Camera& camera);
102
107 uint32_t get_alive_count() const;
108
109private:
110 void draw_offset(const Vec2<float>& offset);
111
112 struct Particle {
115 float lifetime { 0.0F };
116 float age { 0.0F };
117 float size { 0.0F };
118 float rotation { 0.0F };
119 bool alive { false };
120 };
121
122 void spawn_particle();
123
125 std::vector<Particle> particles;
126 uint32_t alive_count { 0 };
127
128 // Emission rate in particles per second
129 float emission_rate { 0.0F };
130 float emission_accumulator { 0.0F };
131 bool emitting { false };
132};
133
134} // namespace asw
135
136#endif // ASW_PARTICLES_H
2D camera for scrolling worlds
2D camera that follows a target, stays inside the world and shakes.
Definition camera.h:21
Particle emitter that integrates with the scene system.
Definition particles.h:54
void draw() override
Draw alive particles. Positions are in screen space.
Definition particles.cpp:76
std::vector< Particle > particles
Definition particles.h:125
ParticleConfig config
Definition particles.h:124
uint32_t get_alive_count() const
Get the number of alive particles.
ParticleEmitter()=default
Create a default ParticleEmitter with no particles.
void emit(uint32_t count)
Emit a burst of particles at current position.
Definition particles.cpp:31
void set_emission_rate(float rate)
Set emission rate (particles per second), 0 to disable auto-emit.
Definition particles.cpp:26
void start()
Start continuous emission.
Definition particles.cpp:38
void draw_offset(const Vec2< float > &offset)
Definition particles.cpp:86
void update(float dt) override
Update particles.
Definition particles.cpp:48
void stop()
Stop continuous emission.
Definition particles.cpp:43
A 2D vector in space.
Definition geometry.h:27
Game Object.
Definition game.h:42
Common geometry types.
Definition camera.cpp:16
std::shared_ptr< SDL_Texture > Texture
Alias for a shared pointer to an SDL_Texture.
Definition types.h:76
RGBA color struct with 8-bit channels.
Definition color.h:18
Configuration for particle emitters.
Definition particles.h:24
Vec2< float > gravity
Definition particles.h:46
Types used throughout the ASW library.