summaryrefslogtreecommitdiff
path: root/src/modules/pan/pan.c
blob: d080366bddc74d6c9a8862121a63fa02783a68e9 (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
#include <math.h>

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

/* Copyright (C) 2023 - Vito Caputo <vcaputo@pengaru.com> */

/* This implements a rudimentary panning module primarily for overlay use,
 *
 * TODO:
 * - the minimal default tile could be more clever
 * - runtime setting for panning velocity
 * - optimization
 * (see comments)
 */

#define PAN_DEFAULT_Y_COMPONENT	-.5
#define PAN_DEFAULT_X_COMPONENT	.25
#define PAN_DEFAULT_TILE_SIZE	32

typedef struct pan_context_t {
	til_module_context_t	til_module_context;

	til_fb_fragment_t	*snapshot;
	float			xoffset, yoffset;
	til_fb_fragment_t	tile;
	uint32_t		tile_buf[PAN_DEFAULT_TILE_SIZE * PAN_DEFAULT_TILE_SIZE];
} pan_context_t;

typedef struct pan_setup_t {
	til_setup_t		til_setup;

	float			x, y;
} pan_setup_t;


static til_module_context_t * pan_create_context(const til_module_t *module, til_stream_t *stream, unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
	pan_context_t	*ctxt;

	ctxt = til_module_context_new(module, sizeof(pan_context_t), stream, seed, ticks, n_cpus, setup);
	if (!ctxt)
		return NULL;

	ctxt->tile = (til_fb_fragment_t){
				.buf = ctxt->tile_buf,
				.frame_width = PAN_DEFAULT_TILE_SIZE,
				.frame_height = PAN_DEFAULT_TILE_SIZE,
				.width = PAN_DEFAULT_TILE_SIZE,
				.height = PAN_DEFAULT_TILE_SIZE,
				.pitch = PAN_DEFAULT_TILE_SIZE,
		     };

	{
		uint32_t	color = (((uint32_t)rand_r(&seed) & 0xff) << 16) |
					(((uint32_t)rand_r(&seed) & 0xff) << 8) |
					((uint32_t)rand_r(&seed) & 0xff);

		/* FIXME TODO: make a more interesting default pattern, still influenced by seed tho */
		for (int y = 0; y < PAN_DEFAULT_TILE_SIZE; y++) {
			for (int x = 0; x < PAN_DEFAULT_TILE_SIZE; x++) {
				uint32_t	c = (((x*y & 0xff) << 16) | ((x*y & 0xff) << 8) | (x*y & 0xff));

				ctxt->tile_buf[y * PAN_DEFAULT_TILE_SIZE + x] = color ^ c;
			}
		}
	}

	return &ctxt->til_module_context;
}


static void pan_prepare_frame(til_module_context_t *context, til_stream_t *stream, unsigned ticks, til_fb_fragment_t **fragment_ptr, til_frame_plan_t *res_frame_plan)
{
	pan_context_t		*ctxt = (pan_context_t *)context;
	pan_setup_t		*s = (pan_setup_t *)context->setup;
	til_fb_fragment_t	*fragment = *fragment_ptr;
	float			dt = (ticks - context->last_ticks) * .1f;
	til_fb_fragment_t	*snapshot = &ctxt->tile;

	if (fragment->cleared)
		ctxt->snapshot = til_fb_fragment_snapshot(fragment_ptr, 0);

	if (ctxt->snapshot)
		snapshot = ctxt->snapshot;

	ctxt->xoffset += dt * s->x;
	ctxt->xoffset = fmodf(ctxt->xoffset, snapshot->frame_width);
	ctxt->yoffset += dt * s->y;
	ctxt->yoffset = fmodf(ctxt->yoffset, snapshot->frame_height);

	*res_frame_plan = (til_frame_plan_t){ .fragmenter = til_fragmenter_slice_per_cpu_x16 };

}


/* like til_fb_fragment_get_pixel_clipped, but wraps around (maybe move to til_fb.h?) */
static inline uint32_t pan_get_pixel_wrapped(til_fb_fragment_t *fragment, int x, int y)
{
	int	xcoord, ycoord;

	/* The need for such casts makes me wonder if til_fragment_t.*{width,height} should just be ints,
	 * which annoys me because those members should never actually be negative.  But without the casts,
	 * the modulos do the entirely wrong thing for negative coordinates.
	 */
	xcoord = x % (int)fragment->frame_width;
	if (xcoord < 0)
		xcoord += fragment->frame_width;

	ycoord = y % (int)fragment->frame_height;
	if (ycoord < 0)
		ycoord += fragment->frame_height;

	return til_fb_fragment_get_pixel_unchecked(fragment, xcoord, ycoord);
}


static void pan_render_fragment(til_module_context_t *context, til_stream_t *stream, unsigned ticks, unsigned cpu, til_fb_fragment_t **fragment_ptr)
{
	pan_context_t		*ctxt = (pan_context_t *)context;
	til_fb_fragment_t	*fragment = *fragment_ptr;
	int			xoff, yoff;
	til_fb_fragment_t	*snapshot = ctxt->snapshot ? : &ctxt->tile;

	xoff = ctxt->xoffset;
	yoff = ctxt->yoffset;

	for (int y = 0; y < fragment->height; y++) {
		int	ycoord = fragment->y + y + yoff;

		for (int x = 0; x < fragment->width; x++) {
			int		xcoord = fragment->x + x + xoff;
			uint32_t	pixel;

			/* TODO: optimize this to at least do contiguous row copies,
			 * as-is it's doing modulos per-pixel!
			 */

			pixel = pan_get_pixel_wrapped(snapshot, xcoord, ycoord);
			til_fb_fragment_put_pixel_unchecked(fragment, 0, fragment->x + x, fragment->y + y, pixel);
		}
	}

	*fragment_ptr = fragment;
}


static int pan_finish_frame(til_module_context_t *context, til_stream_t *stream, unsigned int ticks, til_fb_fragment_t **fragment_ptr)
{
	pan_context_t	*ctxt = (pan_context_t *)context;

	if (ctxt->snapshot)
		ctxt->snapshot = til_fb_fragment_reclaim(ctxt->snapshot);

	return 0;
}


static int pan_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup);


til_module_t	pan_module = {
	.create_context = pan_create_context,
	.prepare_frame = pan_prepare_frame,
	.render_fragment = pan_render_fragment,
	.finish_frame = pan_finish_frame,
	.setup = pan_setup,
	.name = "pan",
	.description = "Simple panning effect (threaded)",
	.author = "Vito Caputo <vcaputo@pengaru.com>",
	.flags = TIL_MODULE_OVERLAYABLE,
};


static int pan_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup)
{
	til_setting_t	*x, *y;
	const char	*component_values[] = {
				"-1",
				"-.8",
				"-.7",
				"-.5",
				"-.25",
				"-.2",
				"-.1",
				"-.05",
				"0",
				".05",
				".1",
				".2",
				".25",
				".5",
				".7",
				".8",
				"1",
				NULL
			};
	int		r;

	r = til_settings_get_and_describe_setting(settings,
						&(til_setting_spec_t){
							.name = "Pan direction vector X component",
							.key = "x",
							.preferred = TIL_SETTINGS_STR(PAN_DEFAULT_X_COMPONENT),
							.values = component_values,
							.annotations = NULL
						},
						&x,
						res_setting,
						res_desc);
	if (r)
		return r;

	r = til_settings_get_and_describe_setting(settings,
						&(til_setting_spec_t){
							.name = "Pan direction vector Y component",
							.key = "y",
							.preferred = TIL_SETTINGS_STR(PAN_DEFAULT_Y_COMPONENT),
							.values = component_values,
							.annotations = NULL
						},
						&y,
						res_setting,
						res_desc);
	if (r)
		return r;

	if (res_setup) {
		pan_setup_t	*setup;
		float		l;

		setup = til_setup_new(settings, sizeof(*setup), NULL, &pan_module);
		if (!setup)
			return -ENOMEM;

		if (sscanf(x->value, "%f", &setup->x) != 1)
			return til_setup_free_with_failed_setting_ret_err(&setup->til_setup, x, res_setting, -EINVAL);

		if (sscanf(y->value, "%f", &setup->y) != 1)
			return til_setup_free_with_failed_setting_ret_err(&setup->til_setup, y, res_setting, -EINVAL);

		l = sqrtf(setup->x * setup->x + setup->y * setup->y);
		if (l) {
			setup->x /= l;
			setup->y /= l;
		}

		*res_setup = &setup->til_setup;
	}

	return 0;
}
© All Rights Reserved