summaryrefslogtreecommitdiff
path: root/modules/sparkler/spark.c
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@gnugeneration.com>2016-12-13 07:51:23 -0800
committerVito Caputo <vcaputo@gnugeneration.com>2016-12-13 07:51:23 -0800
commit8add1663d9a02db2bc65224cdceb480733a81379 (patch)
treefea6aa880a366c007d2b7fdda87c746e6345b301 /modules/sparkler/spark.c
parentaf49b97cd819cec3a19b1ff5ed6076a0d23f4233 (diff)
sparkler: introduce a particle system
A while ago I made this particle system on SDL, and had the beginnings of an octree implemented within it, but never finished actually using the octree to accelerate the proximity searches. This now has the octree completed and of course more particle interactions now that neighbors could be found more quickly. The simulation somewhat resembles a fireworks display. Every particle is drawn as a single pixel. The visual effect is dominated by spontaneously spawned rockets which explode into thousands of particles accompanied by bursts that thrust particles away from the explosion radially in an expanding sphere resembling a shock wave. When the shock wave happens to strike another rocket, it explodes, resulting in another shock wave. This can produce spectacular chain reactions, so it's worth running for some time and seeing what transpires.
Diffstat (limited to 'modules/sparkler/spark.c')
-rw-r--r--modules/sparkler/spark.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/modules/sparkler/spark.c b/modules/sparkler/spark.c
new file mode 100644
index 0000000..ea68ac2
--- /dev/null
+++ b/modules/sparkler/spark.c
@@ -0,0 +1,63 @@
+#include <stdlib.h>
+
+#include "draw.h"
+#include "particle.h"
+#include "particles.h"
+
+/* a "spark" particle type, emitted from behind rockets */
+#define SPARK_MAX_DECAY_RATE 20
+#define SPARK_MIN_DECAY_RATE 2
+#define SPARK_MAX_LIFETIME 150
+#define SPARK_MIN_LIFETIME 1
+
+typedef struct _spark_ctxt_t {
+ int decay_rate;
+ int longevity;
+ int lifetime;
+} spark_ctxt_t;
+
+
+static int spark_init(particles_t *particles, particle_t *p)
+{
+ spark_ctxt_t *ctxt = p->ctxt;
+
+ p->props->drag = 20.0;
+ p->props->mass = 0.1;
+ ctxt->decay_rate = rand_within_range(SPARK_MIN_DECAY_RATE, SPARK_MAX_DECAY_RATE);
+ ctxt->lifetime = ctxt->longevity = rand_within_range(SPARK_MIN_LIFETIME, SPARK_MAX_LIFETIME);
+
+ return 1;
+}
+
+
+static particle_status_t spark_sim(particles_t *particles, particle_t *p)
+{
+ spark_ctxt_t *ctxt = p->ctxt;
+
+ if (!ctxt->longevity || (ctxt->longevity -= ctxt->decay_rate) <= 0) {
+ ctxt->longevity = 0;
+ return PARTICLE_DEAD;
+ }
+
+ return PARTICLE_ALIVE;
+}
+
+
+static void spark_draw(particles_t *particles, particle_t *p, int x, int y, fb_fragment_t *f)
+{
+ spark_ctxt_t *ctxt = p->ctxt;
+
+ if (!draw_pixel(f, x, y, makergb(0xff, 0xa0, 0x20, ((float)ctxt->longevity / ctxt->lifetime)))) {
+ /* offscreen */
+ ctxt->longevity = 0;
+ }
+}
+
+
+particle_ops_t spark_ops = {
+ .context_size = sizeof(spark_ctxt_t),
+ .sim = spark_sim,
+ .init = spark_init,
+ .draw = spark_draw,
+ .cleanup = NULL,
+ };
© All Rights Reserved