diff options
-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", |