summaryrefslogtreecommitdiff
path: root/src/modules/spokes/spokes.c
blob: 0aa8eed7a940424aee0c3986ab01d465ed556ee6 (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
#include <math.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

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

#include "draw.h"

/* Copyright (C) 2022 Philip J. Freeman <elektron@halo.nu> */

#define SPOKES_DEFAULT_ITERATIONS	3
#define SPOKES_DEFAULT_TWIST		0.0625
#define SPOKES_DEFAULT_THICKNESS	3

typedef struct spokes_context_t {
	til_module_context_t    til_module_context;
	int			iterations;
	float                   twist;
	int			thickness;
} spokes_context_t;

typedef struct spokes_setup_t {
	til_setup_t             til_setup;
	int			iterations;
	float                   twist;
	int			thickness;
} spokes_setup_t;

static void spokes_draw_line(til_fb_fragment_t *fragment, int x1, int y1, int x2, int y2, uint32_t color, int thickness)
{

	int x, y, offset;

	if(x1==x2 && y1==y2) {
		til_fb_fragment_put_pixel_checked(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, x1, y1, color);
		return;
	}

	int dx=x2-x1;
	int dy=y2-y1;

	if(abs(dx)>=abs(dy)) { /* iterate along x */
		float rate=(float)dy/(float)dx;
		if(dx>0) {
			for(x=x1; x<=x2; x++) {
				y=y1+round((x-x1)*rate);
				if(y>=0 && y<fragment->height)
					til_fb_fragment_put_pixel_checked(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, x, y, color);
				for(offset=1; offset<thickness; offset++) {
					if(y+offset<fragment->height)
						til_fb_fragment_put_pixel_checked(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, x, y+offset, color);
					if(y-offset>=0)
						til_fb_fragment_put_pixel_checked(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, x, y-offset, color);
				}
			}
		} else {
			for(x=x1; x>=x2; x--) {
				y=y1+round((x-x1)*rate);
				if(y>=0 && y<fragment->height)
					til_fb_fragment_put_pixel_checked(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, x, y, color);
				for(offset=1; offset<thickness; offset++) {
					if(y+offset<fragment->height)
						til_fb_fragment_put_pixel_checked(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, x, y+offset, color);
					if(y-offset>=0)
						til_fb_fragment_put_pixel_checked(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, x, y-offset, color);
				}
			}
		}

	} else { /* iterate along y */
		float rate=(float)dx/(float)dy;
		if(dy>0) {
			for(y=y1; y<=y2; y++) {
				x=x1+round((y-y1)*rate);
				if(x>=0 && x<fragment->width)
					til_fb_fragment_put_pixel_checked(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, x, y, color);
				for(offset=1; offset<thickness; offset++) {
					if(x+offset<fragment->width)
						til_fb_fragment_put_pixel_checked(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, x+offset, y, color);
					if(x-offset>=0)
						til_fb_fragment_put_pixel_checked(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, x-offset, y, color);
				}
			}
		} else {
			for(y=y1; y>=y2; y--) {
				x=x1+round((y-y1)*rate);
				if(x>=0 && x<fragment->width)
					til_fb_fragment_put_pixel_checked(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, x, y, color);
				for(offset=1; offset<thickness; offset++) {
					if(x+offset<fragment->width)
						til_fb_fragment_put_pixel_checked(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, x+offset, y, color);
					if(x-offset>=0)
						til_fb_fragment_put_pixel_checked(fragment, TIL_FB_DRAW_FLAG_TEXTURABLE, x-offset, y, color);
				}
			}
		}
	}
}

static void spokes_draw_segmented_line(til_fb_fragment_t *fragment, int iterations, double theta, int x1, int y1, int x2, int y2, uint32_t color, int thickness)
{
	/* recurse until iterations == 0 */

	if(iterations>0) {
		int midpoint_x=((x1+x2)/2);
		int midpoint_y=((y1+y2)/2);

		midpoint_x=cos(theta)*(midpoint_x-x1)-sin(theta)*(midpoint_y-y1)+x1;
		midpoint_y=sin(theta)*(midpoint_x-x1)+cos(theta)*(midpoint_y-y1)+y1;

		/* Check if any of the midpoints are outside of the drawable area and fix them. */
		if (midpoint_x<0) midpoint_x=0;
		if (midpoint_x>=fragment->width) midpoint_x=fragment->width-1;
		if (midpoint_y<0) midpoint_y=0;
		if (midpoint_y>=fragment->height) midpoint_y=fragment->height-1;

		spokes_draw_segmented_line(fragment, iterations-1, theta*0.5, x1, y1, midpoint_x, midpoint_y, color, thickness);
		spokes_draw_segmented_line(fragment, iterations-1, theta*-0.5, x2, y2, midpoint_x, midpoint_y, color, thickness);
		return;
	}
	spokes_draw_line(fragment, x1, y1, x2, y2, color, thickness);
}

static void spokes_render_fragment(til_module_context_t *context, til_stream_t *stream, unsigned ticks, unsigned cpu, til_fb_fragment_t **fragment_ptr)
{
	til_fb_fragment_t       *fragment = *fragment_ptr;

	int width = fragment->width, height = fragment->height;
	int stride;
	int offset;
	uint32_t color;

	int display_R, display_origin_x, display_origin_y;
	int origin_x, origin_y;

	spokes_context_t *ctxt = (spokes_context_t *)context;

	/* calculate theta for twist */
	double theta=M_PI*ctxt->twist;

	/* Based on the fragment's dimensions, calculate the origin and radius of the largest
	circle that can fully fit in the frame */

	if(width>=height) {
		display_R=(height-1)*0.5f;
		display_origin_x=((width-height)*.5f)+display_R;
		display_origin_y=display_R;
	} else {
		display_R=(width-1)*0.5f;
		display_origin_x=display_R;
		display_origin_y=((height-width)*.5f)+display_R;
	}

	/* calculate a moving origin for all the lines in this frame based on ticks */
	origin_x=display_origin_x+cos((float)ticks*0.001)*display_R*0.7f;
	origin_y=display_origin_y+sin((float)ticks*0.001)*display_R*0.7f;

	/* calculate an offset for outer line endpoints based on ticks */
	offset=(int)round((float)ticks*0.1); /* use the ticks. */

	/* rotate through RGB color space slowly based on ticks */
	color=makergb(
		(int)round(255.0*sinf((float)ticks*0.00001)),
		(int)round(255.0*sinf((float)ticks*0.00001+0.6667*M_PI)),
		(int)round(255.0*sinf((float)ticks*0.00001+1.3333*M_PI)),
		1);

	/* find the largest even stride for the fragments dimensions */
	stride=1;
	for(int i=2; i<(width+height)/2; i++) {
		if((width+height)%i==0) {
			stride=i;
		}
	}

	/* we're setup now to draw some shit */
	til_fb_fragment_clear(fragment);

	for(int i=0; i<=width+height-stride; i+=stride){ /* iterate over half the perimiter at a time... */
		int perimiter_x=-1, perimiter_y=-1;
		if(i+offset%stride<width) {
			perimiter_x=i+offset%stride;
			perimiter_y=0;
		} else {
			perimiter_x=width-1;
			perimiter_y=(i-width)+offset%stride;
		}

		spokes_draw_segmented_line(fragment, ctxt->iterations, theta, origin_x, origin_y, perimiter_x, perimiter_y, color, ctxt->thickness);

		/* Calculate and draw the mirror line... */
		perimiter_x=abs(perimiter_x-width);
		perimiter_y=abs(perimiter_y-height);
		spokes_draw_segmented_line(fragment, ctxt->iterations, theta, origin_x, origin_y, perimiter_x, perimiter_y, color, ctxt->thickness);
	}
}


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

	ctxt = til_module_context_new(module, sizeof(spokes_context_t), stream, seed, ticks, n_cpus, setup);
	if (!ctxt)
	        return NULL;
	ctxt->iterations = ((spokes_setup_t *)setup)->iterations;
	ctxt->twist = ((spokes_setup_t *)setup)->twist;
	ctxt->thickness = ((spokes_setup_t *)setup)->thickness;
	return &ctxt->til_module_context;

}


static void spokes_destroy_context(til_module_context_t *context)
{
	free(context);

}


int spokes_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	spokes_module = {
	.create_context = spokes_create_context,
	.destroy_context = spokes_destroy_context,
	.render_fragment = spokes_render_fragment,
	.setup = spokes_setup,
	.name = "spokes",
	.description = "Twisted spokes",
	.author = "Philip J Freeman <elektron@halo.nu>",
	.flags = TIL_MODULE_OVERLAYABLE,
};


int spokes_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	*iterations;
	const char	*iterations_values[] = {
				"1",
				"2",
				"3",
				"4",
				NULL
			};
	til_setting_t	*twist;
	const char	*twist_values[] = {
				"-4.0",
				"-2.0",
				"-1.0",
				"-0.5",
				"-0.25",
				"-0.125",
				"-0.0625",
				"-0.03125",
				"-0.015125",
				"0.0",
				"0.015125",
				"0.03125",
				"0.0625",
				"0.125",
				"0.25",
				"0.5",
				"1.0",
				"2.0",
				"4.0",
				NULL
			};
	til_setting_t	*thickness;
	const char	*thickness_values[] = {
				"1",
				"2",
				"3",
				"5",
				NULL
			};
	int r;

	r = til_settings_get_and_describe_setting(settings,
						&(til_setting_spec_t){
							.name = "Iterations",
							.key = "iterations",
							.regex = "[0-9]+",
							.preferred = TIL_SETTINGS_STR(SPOKES_DEFAULT_ITERATIONS),
							.values = iterations_values,
							.annotations = NULL
						},
						&iterations,
						res_setting,
						res_desc);
	if (r)
		return r;

	r = til_settings_get_and_describe_setting(settings,
						&(til_setting_spec_t){
							.name = "Twist",
							.key = "twist",
							.regex = "[0-9]+\\.[0-9]+",
							.preferred = TIL_SETTINGS_STR(SPOKES_DEFAULT_TWIST),
							.values = twist_values,
							.annotations = NULL
						},
						&twist,
						res_setting,
						res_desc);
	if (r)
		return r;

	r = til_settings_get_and_describe_setting(settings,
						&(til_setting_spec_t){
							.name = "Thickness",
							.key = "thickness",
							.regex = "[0-9]+",
							.preferred = TIL_SETTINGS_STR(SPOKES_DEFAULT_THICKNESS),
							.values = thickness_values,
							.annotations = NULL
						},
						&thickness,
						res_setting,
						res_desc);
	if (r)
		return r;

	if (res_setup) {
		spokes_setup_t	*setup;

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

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

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

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

		*res_setup = &setup->til_setup;
	}

	return 0;
}
© All Rights Reserved