diff options
author | Vito Caputo <vcaputo@gnugeneration.com> | 2023-08-30 13:45:07 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2023-08-30 13:49:54 -0700 |
commit | 20d097ab1fe8ef5b62f4169f4353cc0074c27e4b (patch) | |
tree | 412f1069b7a3fa3953df02b54302bbdca3ddf6fb /src/modules/sparkler/xplode.c | |
parent | a634b2df74222be85a03b1895ee10135822031c3 (diff) |
sparkler: parameterize particles
Having everything in fixed defines severely constrains the
diversity of particle behaviors and appearances.
This commit has been sitting around bitrotting since 2017, but
now that there's all this settings infra. and randomizing via
rtv, it seems worth landing, so I've rebased and am merging to
prevent a bitrot->rebase recurrence.
As-is, this commit ~minimally establishes a somewhat streamlined
parameterizing mechanism w/X-Macro patterns, while wiring up a
few of the obvious use cases surrounding xplode/burst, colorizing
the default sparkler explosions while at it.
It appears that when I first hacked this up I did some
experimentation with parameters as well, so there are some tweaks
to the behavior as opposed to a strict conversion of the fixed
defines to parameters. They seem minor enough to just leave be.
Plus a few minor optimizations like converting divides to
multiplies were in there.
Future commits can now wire up settings to choose from parameter
presets for different sparklers...
Diffstat (limited to 'src/modules/sparkler/xplode.c')
-rw-r--r-- | src/modules/sparkler/xplode.c | 49 |
1 files changed, 29 insertions, 20 deletions
diff --git a/src/modules/sparkler/xplode.c b/src/modules/sparkler/xplode.c index 1534369..bf50a10 100644 --- a/src/modules/sparkler/xplode.c +++ b/src/modules/sparkler/xplode.c @@ -1,3 +1,4 @@ +#include <stdarg.h> #include <stdlib.h> #include "til_fb.h" @@ -5,31 +6,38 @@ #include "helpers.h" #include "particle.h" #include "particles.h" +#include "xplode.h" -/* a "xplode" particle type, emitted by rockets in large numbers at the end of their lifetime */ -#define XPLODE_MAX_DECAY_RATE 10 -#define XPLODE_MIN_DECAY_RATE 5 -#define XPLODE_MAX_LIFETIME 150 -#define XPLODE_MIN_LIFETIME 5 +/* a "xplode" particle type, emitted by rockets in large numbers at the end of their duration */ extern particle_ops_t spark_ops; particle_ops_t xplode_ops; typedef struct _xplode_ctxt_t { - int decay_rate; - int longevity; - int lifetime; +#define PARAMS_DECLARE_STRUCT +#include "xplode_params.def" + int remaining; } xplode_ctxt_t; -static int xplode_init(particles_t *particles, const particles_conf_t *conf, particle_t *p) +static int xplode_init(particles_t *particles, const particles_conf_t *conf, particle_t *p, unsigned n_params, va_list params) { xplode_ctxt_t *ctxt = p->ctxt; - ctxt->decay_rate = rand_within_range(conf->seedp, XPLODE_MIN_DECAY_RATE, XPLODE_MAX_DECAY_RATE); - ctxt->lifetime = ctxt->longevity = rand_within_range(conf->seedp, XPLODE_MIN_LIFETIME, XPLODE_MAX_LIFETIME); +#define PARAMS_ASSIGN_DEFAULTS +#include "xplode_params.def" - p->props->drag = 10.9; + for (; n_params; n_params--) { + switch (va_arg(params, xplode_param_t)) { +#define PARAMS_IMPLEMENT_SWITCH +#include "xplode_params.def" + default: + return 0; + } + } + + ctxt->remaining = ctxt->duration; + p->props->drag = 10.5; p->props->mass = 0.3; p->props->virtual = 0; @@ -41,17 +49,17 @@ static particle_status_t xplode_sim(particles_t *particles, const particles_conf { xplode_ctxt_t *ctxt = p->ctxt; - if (!ctxt->longevity || (ctxt->longevity -= ctxt->decay_rate) <= 0) { - ctxt->longevity = 0; + if (!ctxt->remaining || (ctxt->remaining -= ctxt->decay_rate) <= 0) { + ctxt->remaining = 0; return PARTICLE_DEAD; } /* litter some small sparks behind the explosion particle */ - if (!(ctxt->lifetime % 30)) { + if (!(ctxt->duration % 30)) { particle_props_t props = *p->props; - props.velocity = (float)rand_within_range(conf->seedp, 10, 50) / 10000.0; - particles_spawn_particle(particles, p, &props, &xplode_ops); + props.velocity = (float)rand_within_range(conf->seedp, 10, 50) * .0001; + particles_spawn_particle(particles, p, &props, &xplode_ops, 1, XPLODE_PARAM_COLOR_UINT, ctxt->color); } return PARTICLE_ALIVE; @@ -63,13 +71,14 @@ static void xplode_draw(particles_t *particles, const particles_conf_t *conf, pa xplode_ctxt_t *ctxt = p->ctxt; uint32_t color; - if (!should_draw_expire_if_oob(particles, p, x, y, f, &ctxt->longevity)) + if (!should_draw_expire_if_oob(particles, p, x, y, f, &ctxt->remaining)) return; - if (ctxt->longevity == ctxt->lifetime) { + if (ctxt->remaining == ctxt->duration) { + /* always start with a white flash */ color = makergb(0xff, 0xff, 0xa0, 1.0); } else { - color = makergb(0xff, 0xff, 0x00, ((float)ctxt->longevity / ctxt->lifetime)); + color = makergb((ctxt->color & 0xff0000) >> 16, (ctxt->color & 0xff00) >> 8, ctxt->color & 0xff, ((float)ctxt->remaining / ctxt->duration)); } til_fb_fragment_put_pixel_unchecked(f, 0, x, y, color); |