diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2017-04-26 16:31:48 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2017-04-26 16:32:28 -0700 |
commit | 3e2d1db3952e65e347faf496800ede12231c3b76 (patch) | |
tree | 0e6fab1e0f6fde27fc456c5d2f76e4005dbb9c41 /src/modules/sparkler | |
parent | 9f499b0590b1a7d56c36ec5d093e82795c3aba19 (diff) |
sparkler: utilize context struct for module state
Diffstat (limited to 'src/modules/sparkler')
-rw-r--r-- | src/modules/sparkler/sparkler.c | 54 |
1 files changed, 43 insertions, 11 deletions
diff --git a/src/modules/sparkler/sparkler.c b/src/modules/sparkler/sparkler.c index deb3c53..6d8d354 100644 --- a/src/modules/sparkler/sparkler.c +++ b/src/modules/sparkler/sparkler.c @@ -16,35 +16,67 @@ #define INIT_PARTS 100 +typedef struct sparkler_context_t { + particles_t *particles; +} sparkler_context_t; + extern particle_ops_t simple_ops; -/* Render a 3D particle system */ -static void sparkler_render_fragment(void *context, fb_fragment_t *fragment) +static void * sparkler_create_context(void) { - static particles_t *particles; static int initialized; - uint32_t *buf = fragment->buf; + sparkler_context_t *ctxt; if (!initialized) { srand(time(NULL) + getpid()); + initialized = 1; + } - particles = particles_new(); - particles_add_particles(particles, NULL, &simple_ops, INIT_PARTS); + ctxt = calloc(1, sizeof(sparkler_context_t)); + if (!ctxt) + return NULL; - initialized = 1; + ctxt->particles = particles_new(); + if (!ctxt->particles) { + free(ctxt); + return NULL; } + particles_add_particles(ctxt->particles, NULL, &simple_ops, INIT_PARTS); + + return ctxt; +} + + +static void sparkler_destroy_context(void *context) +{ + sparkler_context_t *ctxt = context; + + particles_free(ctxt->particles); + free(ctxt); +} + + +/* Render a 3D particle system */ +static void sparkler_render_fragment(void *context, fb_fragment_t *fragment) +{ + sparkler_context_t *ctxt = context; + uint32_t *buf = fragment->buf; + + fb_fragment_zero(fragment); - particles_age(particles); - particles_draw(particles, fragment); - particles_sim(particles); - particles_add_particles(particles, NULL, &simple_ops, INIT_PARTS / 4); + particles_age(ctxt->particles); + particles_draw(ctxt->particles, fragment); + particles_sim(ctxt->particles); + particles_add_particles(ctxt->particles, NULL, &simple_ops, INIT_PARTS / 4); } rototiller_module_t sparkler_module = { + .create_context = sparkler_create_context, + .destroy_context = sparkler_destroy_context, .render_fragment = sparkler_render_fragment, .name = "sparkler", .description = "Particle system with spatial interactions", |