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.cpp
Go to the documentation of this file.
1
8
10
11#include <cmath>
12
13#include "asw/modules/draw.h"
14#include "asw/modules/random.h"
15#include "asw/modules/util.h"
16
17namespace asw {
18
19ParticleEmitter::ParticleEmitter(const ParticleConfig& config, uint32_t maxParticles)
20 : config(config)
21 , particles(maxParticles)
22{
23}
24
26{
27 emission_rate = rate;
28}
29
30void ParticleEmitter::emit(uint32_t count)
31{
32 for (uint32_t i = 0; i < count; ++i) {
34 }
35}
36
38{
39 emitting = true;
40}
41
43{
44 emitting = false;
45}
46
48{
49 // Auto-emit
50 if (emitting && emission_rate > 0.0F) {
52 while (emission_accumulator >= 1.0F) {
55 }
56 }
57
58 // Update alive particles (swap-and-shrink)
59 for (uint32_t i = 0; i < alive_count;) {
60 auto& p = particles[i];
61 p.age += dt;
62
63 if (p.age >= p.lifetime) {
66 continue;
67 }
68
69 p.velocity += config.gravity * dt;
70 p.position += p.velocity * dt;
71 ++i;
72 }
73}
74
76{
77 for (uint32_t i = 0; i < alive_count; ++i) {
78 const auto& p = particles[i];
79 const float t = p.age / p.lifetime;
80
81 // Interpolate visuals
82 const float size = util::lerp(config.size_start, config.size_end, t);
84
85 if (config.texture != nullptr) {
86 const auto dest = Quad<float>(
87 p.position.x - (size / 2.0F), p.position.y - (size / 2.0F), size, size);
88
92 } else {
93 auto r = static_cast<uint8_t>(util::lerp(static_cast<float>(config.color_start.r),
94 static_cast<float>(config.color_end.r), t));
95 auto g = static_cast<uint8_t>(util::lerp(static_cast<float>(config.color_start.g),
96 static_cast<float>(config.color_end.g), t));
97 auto b = static_cast<uint8_t>(util::lerp(static_cast<float>(config.color_start.b),
98 static_cast<float>(config.color_end.b), t));
99 auto a = static_cast<uint8_t>(util::lerp(static_cast<float>(config.color_start.a),
100 static_cast<float>(config.color_end.a), t)
101 * alpha);
102
103 const Color color { r, g, b, a };
104
105 draw::circle_fill(p.position, size / 2.0F, color);
106 }
107 }
108}
109
111{
112 return alive_count;
113}
114
116{
117 if (alive_count >= particles.size()) {
118 return;
119 }
120
121 auto& p = particles[alive_count];
122 p.position = transform.position;
123 p.age = 0.0F;
125 p.alive = true;
126
127 const float speed = random::between(config.speed_min, config.speed_max);
128 const float angle = random::between(config.angle_min, config.angle_max);
129 p.velocity = Vec2<float>(std::cos(angle) * speed, std::sin(angle) * speed);
130
131 p.size = config.size_start;
132 p.rotation = 0.0F;
133
134 alive_count++;
135}
136
137} // namespace asw
void draw() override
Draw alive particles.
Definition particles.cpp:75
std::vector< Particle > particles
Definition particles.h:116
ParticleConfig config
Definition particles.h:115
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:30
void set_emission_rate(float rate)
Set emission rate (particles per second), 0 to disable auto-emit.
Definition particles.cpp:25
void start()
Start continuous emission.
Definition particles.cpp:37
void update(float dt) override
Update particles.
Definition particles.cpp:47
void stop()
Stop continuous emission.
Definition particles.cpp:42
A 2D rectangle in space.
Definition geometry.h:433
Vec2< T > position
The position of the rectangle.
Definition geometry.h:597
A 2D vector in space.
Definition geometry.h:21
float alpha
Opacity of the object.
Definition game.h:96
asw::Quad< float > transform
The transform of the object.
Definition game.h:77
Routines for drawing sprites and primitives to the screen.
void circle_fill(const asw::Vec2< float > &position, float radius, asw::Color color)
Draw a filled circle.
Definition draw.cpp:281
void stretch_sprite(const asw::Texture &tex, const asw::Quad< float > &position)
Draw a sprite with the option to stretch it.
Definition draw.cpp:70
void set_alpha(const asw::Texture &texture, float alpha)
Set the alpha of a texture.
Definition draw.cpp:317
int between(int min, int max)
Generate a random integer between min and max.
Definition random.cpp:18
T lerp(const T &a, const T &b, float t)
Lerp between two values.
Definition util.h:49
Lightweight particle emitter system.
Random module for the ASW library.
RGBA color struct with 8-bit channels.
Definition color.h:11
uint8_t b
Definition color.h:14
uint8_t r
Definition color.h:12
uint8_t g
Definition color.h:13
uint8_t a
Definition color.h:15
Configuration for particle emitters.
Definition particles.h:23
Vec2< float > gravity
Definition particles.h:45
General utility functions.