code / btui

Lines2.2K C1.2K Python549 Markdown219 make156 Lua49
(51 lines)
1 /*
2 * This file contains the source of a program used to demonstrate BTUI's 24-bit
3 * color support by printing a rapidfire rainbow.
4 */
5 #include <stdio.h>
6 #include <math.h>
7 #include "btui.h"
9 int main(void)
11 btui_t *bt = btui_enable();
12 if (!bt) return 1;
13 int done = 0;
14 double t = 0;
15 double a1 = 1.13, a2 = 1.23, a3 = 1.37;
16 double dt = 0.1;
17 char buf[1<<20];
18 setvbuf(bt->out, buf, _IOFBF, sizeof(buf));
19 btui_puts(bt, T_OFF(T_WRAP));
20 const char *title = " 24 BIT COLOR SUPPORT! ";
21 while (!done) {
22 int y = bt->height-1;
23 btui_move_cursor(bt, 0, y);
24 for (int x = 0; x < bt->width; x++) {
25 int r = (int)(255.0 * (0.5 + 0.5*sin(t*a1 + (double)(x) / 50.0)));
26 int g = (int)(255.0 * (0.5 + 0.5*sin(0.8 + t*a2 + (double)(x) / 50.0)));
27 int b = (int)(255.0 * (0.5 + 0.5*sin(1.3 + t*a3 + (double)(x) / 50.0)));
28 btui_set_bg(bt,
29 (r < 0 ? 0 : (r > 255 ? 255 : r)),
30 (g < 0 ? 0 : (g > 255 ? 255 : g)),
31 (b < 0 ? 0 : (b > 255 ? 255 : b)));
32 btui_puts(bt, " ");
34 btui_puts(bt, "\n");
35 btui_move_cursor(bt, (bt->width - (int)strlen(title)) / 2, 0);
36 btui_set_attributes(bt, BTUI_NORMAL | BTUI_BOLD);
37 btui_puts(bt, title);
38 btui_flush(bt);
39 usleep(10000);
40 t += dt;
42 int mouse_x = -1, mouse_y = -1;
43 int key = btui_getkey(bt, 0, &mouse_x, &mouse_y);
44 switch (key) {
45 case 'q': case KEY_CTRL_C: done = 1; break;
46 default: break;
49 btui_disable(bt);
50 return 0;