summaryrefslogtreecommitdiff
path: root/src/modules/sparkler/spark.c
blob: 8627baa8914e414dd3d677b56a4e6618bda05e86 (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
#include <stdlib.h>

#include "til_fb.h"

#include "helpers.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, const particles_conf_t *conf, particle_t *p)
{
	spark_ctxt_t	*ctxt = p->ctxt;

	p->props->drag = 20.0;
	p->props->mass = 0.1;
	p->props->virtual = 0;
	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, const particles_conf_t *conf, particle_t *p, til_fb_fragment_t *f)
{
	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, const particles_conf_t *conf, particle_t *p, int x, int y, til_fb_fragment_t *f)
{
	spark_ctxt_t	*ctxt = p->ctxt;

	if (!should_draw_expire_if_oob(particles, p, x, y, f, &ctxt->longevity))
		/* offscreen */
		return;

	til_fb_fragment_put_pixel_unchecked(f, 0, x, y, makergb(0xff, 0xa0, 0x20, ((float)ctxt->longevity / ctxt->lifetime)));
}


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