From c4ac389be39f9f70b3fda0b0070b5954d7c6978a Mon Sep 17 00:00:00 2001
From: Vito Caputo <vcaputo@pengaru.com>
Date: Sat, 30 Apr 2022 09:35:24 -0700
Subject: modules/checkers: add sampled overlay option

When used as an overlay, source the checker color from the
existing contents under the checker.  This basically becomes a
pixelating effect, but with a checkered layout.  If a dynamics
setting for odd+even (filled) were added, in combination with
sampled=on it would become plain pixelation.

This commit uses a single sample, but more could be used to be
more of an average than a potentially flickering checker
depending on what's going on underneath.  Probably another option
like samples=N when sampled=on.

If using this option, one probably isn't interested in texturing,
which illustrates a need for the fb_fragment_put/fill API to have
a texturing allowed toggle.  Then on a per-call basis the
texturing could be toggled which enables some very interesting
possibilities.
---
 src/modules/checkers/checkers.c | 51 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 49 insertions(+), 2 deletions(-)

(limited to 'src')

diff --git a/src/modules/checkers/checkers.c b/src/modules/checkers/checkers.c
index c4e02c1..841aadd 100644
--- a/src/modules/checkers/checkers.c
+++ b/src/modules/checkers/checkers.c
@@ -26,6 +26,8 @@
 #define CHECKERS_DEFAULT_PATTERN	CHECKERS_PATTERN_CHECKERED
 #define CHECKERS_DEFAULT_DYNAMICS	CHECKERS_DYNAMICS_ODD
 #define CHECKERS_DEFAULT_DYNAMICS_RATE	1.0
+#define CHECKERS_DEFAULT_SAMPLED	0
+#define CHECKERS_DEFAULT_COLOR		0xffffffff
 
 
 typedef enum checkers_pattern_t {
@@ -47,6 +49,8 @@ typedef struct checkers_setup_t {
 	checkers_pattern_t	pattern;
 	checkers_dynamics_t	dynamics;
 	float			rate;
+	uint32_t		color;
+	unsigned		sampled:1;
 } checkers_setup_t;
 
 typedef struct checkers_context_t {
@@ -60,6 +64,8 @@ static checkers_setup_t checkers_default_setup = {
 	.pattern = CHECKERS_DEFAULT_PATTERN,
 	.dynamics = CHECKERS_DEFAULT_DYNAMICS,
 	.rate = CHECKERS_DEFAULT_DYNAMICS_RATE,
+	.sampled = CHECKERS_DEFAULT_SAMPLED,
+	.color = CHECKERS_DEFAULT_COLOR,
 };
 
 
@@ -165,8 +171,14 @@ static void checkers_render_fragment(void *context, unsigned ticks, unsigned cpu
 
 	if (!state)
 		til_fb_fragment_clear(fragment);
-	else
-		til_fb_fragment_fill(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, 0xffffffff);
+	else {
+		uint32_t	color = ctxt->setup.color;
+
+		if (ctxt->setup.sampled && fragment->cleared)
+			color = til_fb_fragment_get_pixel_unchecked(fragment, fragment->x + (fragment->width >> 1), fragment->y + (fragment->height >> 1));
+
+		til_fb_fragment_fill(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, color);
+	}
 }
 
 
@@ -176,6 +188,7 @@ static int checkers_setup(const til_settings_t *settings, til_setting_t **res_se
 	const char	*pattern;
 	const char	*dynamics;
 	const char	*dynamics_rate;
+	const char	*sampled;
 	const char	*size_values[] = {
 				"4",
 				"8",
@@ -209,6 +222,11 @@ static int checkers_setup(const til_settings_t *settings, til_setting_t **res_se
 				".0001",
 				NULL
 			};
+	const char	*bool_values[] = {
+				"off",
+				"on",
+				NULL
+			};
 	int		r;
 
 	r = til_settings_get_and_describe_value(settings,
@@ -270,6 +288,20 @@ static int checkers_setup(const til_settings_t *settings, til_setting_t **res_se
 			return r;
 	}
 
+	r = til_settings_get_and_describe_value(settings,
+						&(til_setting_desc_t){
+							.name = "Sample checker colors when overlayed",
+							.key = "sampled",
+							.preferred = bool_values[CHECKERS_DEFAULT_SAMPLED],
+							.values = bool_values,
+							.annotations = NULL
+						},
+						&sampled,
+						res_setting,
+						res_desc);
+	if (r)
+		return r;
+
 	if (res_setup) {
 		checkers_setup_t	*setup;
 
@@ -306,6 +338,21 @@ static int checkers_setup(const til_settings_t *settings, til_setting_t **res_se
 		if (setup->dynamics != CHECKERS_DYNAMICS_ODD && setup->dynamics != CHECKERS_DYNAMICS_EVEN)
 			sscanf(dynamics_rate, "%f", &setup->rate);
 
+		/* FIXME TODO: I wonder if sampled should be more like a color mode {color, randomized, sampled, textured}
+		 * with the til_fb_fragment_* putters adding a flag for allowing texturing.
+		 * the problem here is that if a texture isn't made available on the fb, we wouldn't still be doing things
+		 * like sampling or randomizing the colors.  They should both be occurring, maybe there needs to be a separate
+		 * field for controlling wether texturing is permitted, not part of color mode.  like texturing=on,off
+		 */
+		if (!strcasecmp(sampled, "on"))
+			setup->sampled = 1;
+
+		/* XXX FIXME TODO: color setting, parse web-style #rrggbb? */
+		/* XXX FIXME TODO: color setting, parse web-style #rrggbb? */
+		/* XXX FIXME TODO: color setting, parse web-style #rrggbb? */
+		/* XXX FIXME TODO: color setting, parse web-style #rrggbb? */
+		setup->color = CHECKERS_DEFAULT_COLOR;
+
 		*res_setup = &setup->til_setup;
 	}
 
-- 
cgit v1.2.3