summaryrefslogtreecommitdiff
path: root/src/modules/sparkler/xplode.c
blob: bf50a10f27859ff754a8e8ce752d20fbb41fe2b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdarg.h>
#include <stdlib.h>

#include "til_fb.h"

#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 duration */

extern particle_ops_t spark_ops;
particle_ops_t	xplode_ops;

typedef struct _xplode_ctxt_t {
#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, unsigned n_params, va_list params)
{
	xplode_ctxt_t	*ctxt = p->ctxt;

#define PARAMS_ASSIGN_DEFAULTS
#include "xplode_params.def"

	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;

	return 1;
}


static particle_status_t xplode_sim(particles_t *particles, const particles_conf_t *conf, particle_t *p, til_fb_fragment_t *f)
{
	xplode_ctxt_t	*ctxt = p->ctxt;

	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->duration % 30)) {
		particle_props_t	props = *p->props;

		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;
}


static void xplode_draw(particles_t *particles, const particles_conf_t *conf, particle_t *p, int x, int y, til_fb_fragment_t *f)
{
	xplode_ctxt_t	*ctxt = p->ctxt;
	uint32_t	color;

	if (!should_draw_expire_if_oob(particles, p, x, y, f, &ctxt->remaining))
		return;

	if (ctxt->remaining == ctxt->duration) {
		/* always start with a white flash */
		color = makergb(0xff, 0xff, 0xa0, 1.0);
	} else {
		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);
}


particle_ops_t	xplode_ops = {
			.context_size = sizeof(xplode_ctxt_t),
			.sim = xplode_sim,
			.init = xplode_init,
			.draw = xplode_draw,
			.cleanup = NULL,
		};
© All Rights Reserved