summaryrefslogtreecommitdiff
path: root/src/modules/pixbounce/pixbounce.c
blob: 3efa7974e1b7ea4f7ed375339b538d922b863cdd (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
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#include "til.h"
#include "draw.h"

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

int pix_width = 16;
int pix_height = 16;
int num_pix = 4;

char pix_map[][16*16] = {

	{
	0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
	0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
	0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
	0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
	0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0,
	1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1,
	1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
	1, 0, 0, 0, 0, 0, 0, 0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, 0,
	0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0,
	0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0,
	0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0,
	0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0,
	1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1,
	1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1,
	1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1,
	1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1,
	0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0,
	0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0,
	0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0,
	0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0,
	0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
	},
};

typedef struct pixbounce_context_t {
	unsigned	n_cpus;
	int		x, y;
	int		x_dir, y_dir;
	int		pix_num;

} pixbounce_context_t;

/* randomly pick another pixmap from the list, excluding the current one. */
static int pick_pix(int num_pics, int last_pic)
{
	int pix_num = last_pic;

	while(pix_num == last_pic)
		pix_num = rand() % num_pics;

	return pix_num;
}

static void * pixbounce_create_context(unsigned ticks, unsigned num_cpus)
{
	pixbounce_context_t *ctxt;

	ctxt = malloc(sizeof(pixbounce_context_t));
	if (!ctxt)
		return NULL;

	srand(time(NULL) + getpid());

	ctxt->n_cpus = num_cpus;
	ctxt->x = 0;
	ctxt->y = 8;
	ctxt->x_dir = 1;
	ctxt->y_dir = 1;
	ctxt->pix_num = rand() % num_pix;

	return ctxt;
}

static void pixbounce_destroy_context(void *context)
{
	pixbounce_context_t *ctxt = context;
	free(context);
}

static void pixbounce_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
	pixbounce_context_t *ctxt = context;

	int	multiplier_x, multiplier_y, multiplier;
	int	width = fragment->width, height = fragment->height;

	/* blank the frame */
	til_fb_fragment_zero(fragment);

	/* check for very small fragment */
	if(pix_width*2>width||pix_height*2>height)
		return;

	/* calculate multiplyer for the pixmap */
	multiplier_x = width / pix_width;
	multiplier_y = height / pix_height;

	if(multiplier_x>=multiplier_y) {
		multiplier = multiplier_y * 77 / 100;
	} else if(multiplier_y>multiplier_x) {
		multiplier = multiplier_x * 77 / 100;
	}

	/* translate pixmap to multiplier size and draw it to the fragment */
	for(int cursor_y=0; cursor_y < pix_height*multiplier; cursor_y++) {
		for(int cursor_x=0; cursor_x < pix_width*multiplier; cursor_x++) {
			int pix_offset = ((cursor_y/multiplier)*pix_width) + (cursor_x/multiplier);
			if(pix_map[ctxt->pix_num][pix_offset] == 0) continue;
			til_fb_fragment_put_pixel_unchecked(
					fragment, ctxt->x+cursor_x, ctxt->y+cursor_y,
					makergb(0xFF, 0xFF, 0xFF, 1)
				);
		}
	}

	/* update pixmap location */
	if(ctxt->x+ctxt->x_dir < 0) {
		ctxt->x_dir = 1;
		ctxt->pix_num = pick_pix(num_pix, ctxt->pix_num);
	}
	if((ctxt->x+(pix_width*multiplier))+ctxt->x_dir > width) {
		ctxt->x_dir = -1;
		ctxt->pix_num = pick_pix(num_pix, ctxt->pix_num);
	}
	if(ctxt->y+ctxt->y_dir < 0) {
		ctxt->y_dir = 1;
		ctxt->pix_num = pick_pix(num_pix, ctxt->pix_num);
	}
	if((ctxt->y+(pix_height*multiplier))+ctxt->y_dir > height) {
		ctxt->y_dir = -1;
		ctxt->pix_num = pick_pix(num_pix, ctxt->pix_num);
	}
	ctxt->x = ctxt->x+ctxt->x_dir;
	ctxt->y = ctxt->y+ctxt->y_dir;
}

til_module_t	pixbounce_module = {
	.create_context  = pixbounce_create_context,
	.destroy_context = pixbounce_destroy_context,
	.render_fragment = pixbounce_render_fragment,
	.name = "pixbounce",
	.description = "Pixmap bounce",
	.author = "Philip J Freeman <elektron@halo.nu>",
};
© All Rights Reserved