summaryrefslogtreecommitdiff
path: root/src/modules/checkers/checkers.c
blob: a3c4957d54ddc780c373f8716d8d02e69640e596 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 *  Copyright (C) 2022 - Vito Caputo - <vcaputo@pengaru.com>
 *
 *  This program is free software: you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 2 as published
 *  by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

#include "til.h"
#include "til_fb.h"


#define CHECKERS_DEFAULT_SIZE		32
#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
#define CHECKERS_DEFAULT_TEXTURABLE	CHECKERS_TEXTURABLE_ON


typedef enum checkers_pattern_t {
	CHECKERS_PATTERN_CHECKERED,
	CHECKERS_PATTERN_RANDOM,
} checkers_pattern_t;

typedef enum checkers_dynamics_t {
	CHECKERS_DYNAMICS_ODD,
	CHECKERS_DYNAMICS_EVEN,
	CHECKERS_DYNAMICS_ALTERNATING,
	CHECKERS_DYNAMICS_RANDOM,
	CHECKERS_DYNAMICS_SONAR,
} checkers_dynamics_t;

typedef enum checkers_texturable_t {
	CHECKERS_TEXTURABLE_OFF,
	CHECKERS_TEXTURABLE_ON,
	CHECKERS_TEXTURABLE_RANDOM,
} checkers_texturable_t;

typedef struct checkers_setup_t {
	til_setup_t		til_setup;
	unsigned		size;
	checkers_pattern_t	pattern;
	checkers_dynamics_t	dynamics;
	float			rate;
	uint32_t		color;
	checkers_texturable_t	texturable;
	unsigned		sampled:1;
} checkers_setup_t;

typedef struct checkers_context_t {
	unsigned		tickoff;
	checkers_setup_t	setup;
} checkers_context_t;


static checkers_setup_t checkers_default_setup = {
	.size = CHECKERS_DEFAULT_SIZE,
	.pattern = CHECKERS_DEFAULT_PATTERN,
	.dynamics = CHECKERS_DEFAULT_DYNAMICS,
	.rate = CHECKERS_DEFAULT_DYNAMICS_RATE,
	.sampled = CHECKERS_DEFAULT_SAMPLED,
	.color = CHECKERS_DEFAULT_COLOR,
	.texturable = CHECKERS_DEFAULT_TEXTURABLE,
};


static void * checkers_create_context(unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
	checkers_context_t	*ctxt;

	if (!setup)
		setup = &checkers_default_setup.til_setup;

	ctxt = calloc(1, sizeof(checkers_context_t));
	if (!ctxt)
		return NULL;

	ctxt->setup = *(checkers_setup_t *)setup;
	ctxt->tickoff = ticks;

	return ctxt;
}


static void checkers_destroy_context(void *context)
{
	free(context);
}


static int checkers_fragmenter(void *context, unsigned n_cpus, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment)
{
	checkers_context_t	*ctxt = context;

	return til_fb_fragment_tile_single(fragment, ctxt->setup.size, number, res_fragment);
}


static void checkers_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
	*res_fragmenter = checkers_fragmenter;
}


static inline unsigned hash(unsigned x)
{
	x = ((x >> 16) ^ x) * 0x61C88647;
	x = ((x >> 16) ^ x) * 0x61C88647;
	x = ((x >> 16) ^ x) * 0x61C88647;
	x = (x >> 16) ^ x;

	return x;
}


static void checkers_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
	checkers_context_t	*ctxt = context;
	int			state;

	switch (ctxt->setup.pattern) {
	case CHECKERS_PATTERN_CHECKERED: {
		unsigned	tiles_per_row;

		tiles_per_row = fragment->frame_width / ctxt->setup.size;
		if (tiles_per_row * ctxt->setup.size < fragment->frame_width)
			tiles_per_row++;

		state = (fragment->number + (fragment->y / ctxt->setup.size) * !(tiles_per_row & 0x1)) & 0x1;
		break;
	}
	case CHECKERS_PATTERN_RANDOM:
		state = hash(fragment->number * 0x61C88647) & 0x1;
		break;
	}

	switch (ctxt->setup.dynamics) {
	case CHECKERS_DYNAMICS_ODD:
		break;
	case CHECKERS_DYNAMICS_EVEN:
		state = ~state & 0x1;
		break;
	case CHECKERS_DYNAMICS_ALTERNATING:
		state ^= ((unsigned)((float)ticks * ctxt->setup.rate) & 0x1);
		break;
	case CHECKERS_DYNAMICS_RANDOM: /* note: the big multiply here is just to get up out of the low bits */
		state &= hash(fragment->number * 0x61C88647 + (unsigned)((float)ticks * ctxt->setup.rate)) & 0x1;
		break;
	case CHECKERS_DYNAMICS_SONAR: {
		float	t = (float)((ticks - ctxt->tickoff) % 1000) * .001f * 2;
		float	dist_sq, x, y;

		x = fragment->x + (fragment->width >> 1);
		y = fragment->y + (fragment->height >> 1);
		x = x / (float)fragment->frame_width * 2.f - 1.f;
		y = y / (float)fragment->frame_height * 2.f - 1.f;

		dist_sq = x * x + y * y;

		t *= t;

		state &= (dist_sq > (t - .1f) && dist_sq < (t + .1f));
		break;
	}
	}

	if (!state)
		til_fb_fragment_clear(fragment);
	else {
		uint32_t	color = ctxt->setup.color;
		uint32_t	flags;

		if (ctxt->setup.sampled && fragment->cleared)
			color = til_fb_fragment_get_pixel_unchecked(fragment, fragment->x + (fragment->width >> 1), fragment->y + (fragment->height >> 1));

		switch (ctxt->setup.texturable) {
		case CHECKERS_TEXTURABLE_OFF:
			flags = 0;
			break;
		case CHECKERS_TEXTURABLE_ON:
			flags = TIL_FB_DRAW_FLAG_TEXTURABLE;
			break;
		case CHECKERS_TEXTURABLE_RANDOM:
			flags = TIL_FB_DRAW_FLAG_TEXTURABLE * (rand() % 2);
			break;
		}
		til_fb_fragment_fill(fragment, flags, color);
	}
}


static int checkers_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup)
{
	const char	*size;
	const char	*pattern;
	const char	*dynamics;
	const char	*dynamics_rate;
	const char	*sampled;
	const char	*texturable;
	const char	*size_values[] = {
				"4",
				"8",
				"16",
				"32",
				"64",
				"128",
				NULL
			};
	const char	*pattern_values[] = {
				"checkered",
				"random",
				NULL
			};
	const char	*dynamics_values[] = {
				"odd",
				"even",
				"alternating",
				"random",
				"sonar",
				NULL
			};
	const char	*dynamics_rate_values[] = {
				"1.0",
				".75",
				".5",
				".25",
				".1",
				".01",
				".001",
				".0001",
				NULL
			};
	const char	*bool_values[] = {
				"off",
				"on",
				NULL
			};
	const char	*texturable_values[] = {
				"off",
				"on",
				"random",
				NULL
			};
	int		r;

	r = til_settings_get_and_describe_value(settings,
						&(til_setting_desc_t){
							.name = "Checker size",
							.key = "size",
							.regex = "\\.[0-9]+",
							.preferred = TIL_SETTINGS_STR(CHECKERS_DEFAULT_SIZE),
							.values = size_values,
							.annotations = NULL
						},
						&size,
						res_setting,
						res_desc);
	if (r)
		return r;

	r = til_settings_get_and_describe_value(settings,
						&(til_setting_desc_t){
							.name = "Checkers pattern",
							.key = "pattern",
							.preferred = pattern_values[0],
							.values = pattern_values,
							.annotations = NULL
						},
						&pattern,
						res_setting,
						res_desc);
	if (r)
		return r;

	r = til_settings_get_and_describe_value(settings,
						&(til_setting_desc_t){
							.name = "Checkers dynamics",
							.key = "dynamics",
							.preferred = dynamics_values[0],
							.values = dynamics_values,
							.annotations = NULL
						},
						&dynamics,
						res_setting,
						res_desc);
	if (r)
		return r;

	if (strcasecmp(dynamics, "odd") && strcasecmp(dynamics, "even")) {
		r = til_settings_get_and_describe_value(settings,
							&(til_setting_desc_t){
								.name = "Checkers dynamics rate",
								.key = "dynamics_rate",
								.preferred = dynamics_rate_values[0],
								.values = dynamics_rate_values,
								.annotations = NULL
							},
							&dynamics_rate,
							res_setting,
							res_desc);
		if (r)
			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;

	r = til_settings_get_and_describe_value(settings,
						&(til_setting_desc_t){
							.name = "Texturable filled checkers",
							.key = "texturable",
							.preferred = texturable_values[CHECKERS_DEFAULT_TEXTURABLE],
							.values = texturable_values,
							.annotations = NULL
						},
						&texturable,
						res_setting,
						res_desc);
	if (r)
		return r;

	if (res_setup) {
		checkers_setup_t	*setup;

		setup = til_setup_new(sizeof(*setup), (void(*)(til_setup_t *))free);
		if (!setup)
			return -ENOMEM;

		sscanf(size, "%u", &setup->size);

		if (!strcmp(pattern, "checkered"))
			setup->pattern = CHECKERS_PATTERN_CHECKERED;
		else if (!strcmp(pattern, "random"))
			setup->pattern = CHECKERS_PATTERN_RANDOM;
		else {
			free(setup);
			return -EINVAL;
		}

		if (!strcmp(dynamics, "odd"))
			setup->dynamics = CHECKERS_DYNAMICS_ODD;
		else if (!strcmp(dynamics, "even"))
			setup->dynamics = CHECKERS_DYNAMICS_EVEN;
		else if (!strcmp(dynamics, "alternating"))
			setup->dynamics = CHECKERS_DYNAMICS_ALTERNATING;
		else if (!strcmp(dynamics, "random"))
			setup->dynamics = CHECKERS_DYNAMICS_RANDOM;
		else if (!strcmp(dynamics, "sonar"))
			setup->dynamics = CHECKERS_DYNAMICS_SONAR;
		else {
			free(setup);
			return -EINVAL;
		}

		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;

		for (size_t i = 0; i < nelems(texturable_values); i++) {
			if (!strcasecmp(texturable, texturable_values[i])) {
				setup->texturable = i;
				break;
			}
		}

		*res_setup = &setup->til_setup;
	}

	return 0;
}


til_module_t	checkers_module = {
	.create_context = checkers_create_context,
	.destroy_context = checkers_destroy_context,
	.prepare_frame = checkers_prepare_frame,
	.render_fragment = checkers_render_fragment,
	.setup = checkers_setup,
	.name = "checkers",
	.description = "Checker-patterned overlay (threaded)",
	.author = "Vito Caputo <vcaputo@pengaru.com>",
	.flags = TIL_MODULE_OVERLAYABLE,
};
© All Rights Reserved