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
20 int maxParticles)
21 : config(config), particles(maxParticles) {}
22
24 emissionRate = rate;
25 }
26
27 void ParticleEmitter::emit(int count) {
28 for (int i = 0; i < count; ++i) {
30 }
31 }
32
34 emitting = true;
35 }
36
38 emitting = false;
39 }
40
41 void ParticleEmitter::update(float deltaTime) {
42 // Auto-emit
43 if (emitting && emissionRate > 0.0F) {
44 emissionAccumulator += emissionRate * deltaTime;
45 while (emissionAccumulator >= 1.0F) {
47 emissionAccumulator -= 1.0F;
48 }
49 }
50
51 // Update alive particles (swap-and-shrink)
52 for (int i = 0; i < aliveCount;) {
53 auto& p = particles[i];
54 p.age += deltaTime;
55
56 if (p.age >= p.lifetime) {
58 aliveCount--;
59 continue;
60 }
61
62 p.velocity += config.gravity * deltaTime;
63 p.position += p.velocity * deltaTime;
64 ++i;
65 }
66 }
67
69 for (int i = 0; i < aliveCount; ++i) {
70 const auto& p = particles[i];
71 float t = p.age / p.lifetime;
72
73 // Interpolate visuals
74 float size = util::lerp(config.sizeStart, config.sizeEnd, t);
76
77 auto r = static_cast<Uint8>(
78 util::lerp(static_cast<float>(config.colorStart.r),
79 static_cast<float>(config.colorEnd.r), t));
80 auto g = static_cast<Uint8>(
81 util::lerp(static_cast<float>(config.colorStart.g),
82 static_cast<float>(config.colorEnd.g), t));
83 auto b = static_cast<Uint8>(
84 util::lerp(static_cast<float>(config.colorStart.b),
85 static_cast<float>(config.colorEnd.b), t));
86 auto a = static_cast<Uint8>(
87 util::lerp(static_cast<float>(config.colorStart.a),
88 static_cast<float>(config.colorEnd.a), t) *
89 alpha);
90
91 const Color color{r, g, b, a};
92
93 if (config.texture != nullptr) {
94 const Quad<float> dest(p.position.x - size / 2.0F,
95 p.position.y - size / 2.0F, size, size);
96
100 } else {
101 draw::circleFill(p.position, size / 2.0F, color);
102 }
103 }
104 }
105
107 return aliveCount;
108 }
109
111 if (aliveCount >= static_cast<int>(particles.size())) {
112 return;
113 }
114
115 auto& p = particles[aliveCount];
116 p.position = transform.position;
117 p.age = 0.0F;
119 p.alive = true;
120
121 const float speed = random::between(config.speedMin, config.speedMax);
122 const float angle = random::between(config.angleMin, config.angleMax);
123 p.velocity = Vec2<float>(std::cos(angle) * speed, std::sin(angle) * speed);
124
125 p.size = config.sizeStart;
126 p.rotation = 0.0F;
127
128 aliveCount++;
129 }
130
131} // namespace asw
void draw() override
Draw alive particles.
Definition particles.cpp:68
std::vector< Particle > particles
Definition particles.h:117
ParticleConfig config
Definition particles.h:116
int getAliveCount() const
Get the number of alive particles.
void setEmissionRate(float rate)
Set emission rate (particles per second), 0 to disable auto-emit.
Definition particles.cpp:23
ParticleEmitter()=default
Create a default ParticleEmitter with no particles.
void start()
Start continuous emission.
Definition particles.cpp:33
void emit(int count)
Emit a burst of particles at current position.
Definition particles.cpp:27
void stop()
Stop continuous emission.
Definition particles.cpp:37
void update(float deltaTime) override
Update particles.
Definition particles.cpp:41
A 2D rectangle in space.
Definition geometry.h:370
Vec2< T > position
The position of the rectangle.
Definition geometry.h:509
A 2D vector in space.
Definition geometry.h:22
float alpha
Opacity of the object.
Definition game.h:90
asw::Quad< float > transform
The transform of the object.
Definition game.h:71
Routines for drawing sprites and primitives to the screen.
void stretchSprite(const asw::Texture &tex, const asw::Quad< float > &position)
Draw a sprite with the option to stretch it.
Definition draw.cpp:68
void setAlpha(const asw::Texture &texture, float alpha)
Set the alpha of a texture.
Definition draw.cpp:333
void circleFill(const asw::Vec2< float > &position, float radius, asw::Color color)
Draw a filled circle.
Definition draw.cpp:297
int between(int min, int max)
Generate a random integer between min and max.
Definition random.cpp:17
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:12
uint8_t r
Definition color.h:12
uint8_t g
Definition color.h:12
uint8_t a
Definition color.h:12
Configuration for particle emitters.
Definition particles.h:23
Vec2< float > gravity
Definition particles.h:45
General utility functions.