summaryrefslogtreecommitdiff
path: root/src/modules/voronoi
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2023-05-11 12:51:00 -0700
committerVito Caputo <vcaputo@pengaru.com>2023-05-11 15:19:25 -0700
commit0d9aa593e68f33da8ea71a04b930bb6093dbaccb (patch)
tree679fd3c55385e597176b51a7d4b4c297680ad341 /src/modules/voronoi
parentdede4eca3e2fca76f297b5f5b901434cb99eafb0 (diff)
modules/*: stop storing setup by value in contexts
With setup refcounting and a reference bound to the context, we should just dereference the single instance. The way setups are used it just as a read-only thing to affect context behavior... Note I've left the module-type-specific setup pointer despite it duplicating the setup pointer in the module_context. This is just a convenience thing so the accessors don't have to cast the general til_setup_t* to my_module_setup_t* everywhere.
Diffstat (limited to 'src/modules/voronoi')
-rw-r--r--src/modules/voronoi/voronoi.c18
1 files changed, 9 insertions, 9 deletions
diff --git a/src/modules/voronoi/voronoi.c b/src/modules/voronoi/voronoi.c
index 0248b08..5e40142 100644
--- a/src/modules/voronoi/voronoi.c
+++ b/src/modules/voronoi/voronoi.c
@@ -45,7 +45,7 @@ typedef struct voronoi_distances_t {
typedef struct voronoi_context_t {
til_module_context_t til_module_context;
unsigned seed;
- voronoi_setup_t setup;
+ voronoi_setup_t *setup;
voronoi_distances_t distances;
voronoi_cell_t cells[];
} voronoi_context_t;
@@ -60,7 +60,7 @@ static void voronoi_randomize(voronoi_context_t *ctxt)
{
float inv_rand_max= 1.f / (float)RAND_MAX;
- for (size_t i = 0; i < ctxt->setup.n_cells; i++) {
+ for (size_t i = 0; i < ctxt->setup->n_cells; i++) {
voronoi_cell_t *p = &ctxt->cells[i];
p->origin.x = ((float)rand_r(&ctxt->seed) * inv_rand_max) * 2.f - 1.f;
@@ -81,7 +81,7 @@ static til_module_context_t * voronoi_create_context(const til_module_t *module,
if (!ctxt)
return NULL;
- ctxt->setup = *(voronoi_setup_t *)setup;
+ ctxt->setup = (voronoi_setup_t *)setup;
ctxt->seed = seed;
voronoi_randomize(ctxt);
@@ -210,7 +210,7 @@ static void voronoi_calculate_distances(voronoi_context_t *ctxt)
#if 0
/* naive inefficient brute-force but correct algorithm */
- for (size_t i = 0; i < ctxt->setup.n_cells; i++) {
+ for (size_t i = 0; i < ctxt->setup->n_cells; i++) {
voronoi_distance_t *d = ctxt->distances.buf;
v2f_t dp = {};
@@ -233,7 +233,7 @@ static void voronoi_calculate_distances(voronoi_context_t *ctxt)
/* An attempt at implementing https://en.wikipedia.org/wiki/Jump_flooding_algorithm */
/* first assign the obvious zero-distance cell origins */
- for (size_t i = 0; i < ctxt->setup.n_cells; i++) {
+ for (size_t i = 0; i < ctxt->setup->n_cells; i++) {
voronoi_cell_t *c = &ctxt->cells[i];
size_t idx;
voronoi_distance_t *d;
@@ -246,7 +246,7 @@ static void voronoi_calculate_distances(voronoi_context_t *ctxt)
}
/* now for every distance sample neighbors */
- if (ctxt->setup.dirty) {
+ if (ctxt->setup->dirty) {
for (size_t step = 2; step <= MAX(ctxt->distances.width, ctxt->distances.height); step *= 2)
voronoi_jumpfill_pass(ctxt, &ds, step);
} else {
@@ -259,7 +259,7 @@ static void voronoi_calculate_distances(voronoi_context_t *ctxt)
static void voronoi_sample_colors(voronoi_context_t *ctxt, til_fb_fragment_t *fragment)
{
- for (size_t i = 0; i < ctxt->setup.n_cells; i++) {
+ for (size_t i = 0; i < ctxt->setup->n_cells; i++) {
voronoi_cell_t *p = &ctxt->cells[i];
int x, y;
@@ -288,13 +288,13 @@ static void voronoi_prepare_frame(til_module_context_t *context, til_stream_t *s
ctxt->distances.size = fragment->frame_width * fragment->frame_height;
ctxt->distances.buf = malloc(sizeof(voronoi_distance_t) * ctxt->distances.size);
- if (!ctxt->setup.randomize)
+ if (!ctxt->setup->randomize)
voronoi_calculate_distances(ctxt);
}
/* TODO: explore moving voronoi_calculate_distances() into render_fragment (threaded) */
- if (ctxt->setup.randomize) {
+ if (ctxt->setup->randomize) {
voronoi_randomize(ctxt);
voronoi_calculate_distances(ctxt);
}
© All Rights Reserved