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

#include "draw.h"
#include "rototiller.h"

/* Copyright (C) 2018 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, 0, 0, 0, 0, 0, 0, 0, 1,
	1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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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,
	},
};

/* 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_render_fragment(void *context, unsigned cpu, fb_fragment_t *fragment)
{
	static int	initialized=0;
	static int	x, y, multiplier;
	static int	x_dir, y_dir;
	static int	pix_num;

	int		width = fragment->width, height = fragment->height;

	if(!initialized) {
		srand(time(NULL) + getpid());
		int multiplier_x = width / pix_width;
		int 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;
		}
		x = rand()%(width - (pix_width * multiplier));
		y = rand()%(height - (pix_height * multiplier));
		pix_num = rand() % num_pix;
		x_dir = 1;
		y_dir = 1;
		initialized = 1;
	}

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

	/* 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[pix_num][pix_offset] == 0) continue;
			fb_fragment_put_pixel_unchecked(
					fragment, x+cursor_x, y+cursor_y,
					makergb(0xFF, 0xFF, 0xFF, 1)
				);
		}
	}

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

rototiller_module_t	pixbounce_module = {
	.render_fragment = pixbounce_render_fragment,
	.name = "pixbounce",
	.description = "pixmap bounce",
	.author = "Philip J Freeman <elektron@halo.nu>",
	.license = "GPLv2",
};
© All Rights Reserved