-
Notifications
You must be signed in to change notification settings - Fork 9
/
demo_bounce.c
85 lines (68 loc) · 1.25 KB
/
demo_bounce.c
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
/*
* demo_bounce.c: bouncing physics box
*/
#include <math.h>
#include <signal.h>
#include "../tuibox.h"
ui_t u;
void draw(ui_box_t *b, char *out){
int x, y;
char tmp[256];
sprintf(out, "\x1b[48;2;255;255;255m");
for(y=0;y<b->h;y++){
for(x=0;x<b->w;x++){
strcat(out, " ");
}
strcat(out, "\n");
}
}
void stop(){
ui_free(&u);
exit(0);
}
int main(){
char watch = 0;
int id;
ui_box_t *b;
double vx = 2.0, vy = 0.0,
ax = 0.0, ay = 0.1;
ui_new(0, &u);
/* Add new UI elements to screen 0 */
id = ui_add(
10, 10,
20, 10,
0,
&watch, 1, /* Dirty bit: Run draw() once, and never again */
draw,
NULL, NULL,
NULL, NULL,
&u
);
b = ui_get(id, &u);
signal(SIGTERM, stop);
signal(SIGQUIT, stop);
signal(SIGINT, stop);
for(;;){
b->x = round((double)b->x + vx);
b->y = round((double)b->y + vy);
vx += ax;
vy += ay;
if(b->x < 1){
b->x = 1;
vx *= -1.0;
} else if(b->x+b->w > u.ws.ws_col){
b->x = u.ws.ws_col - b->w;
vx *= -1.0;
}
if(b->y < 1){
b->y = 1;
vy *= -1.0;
} else if(b->y+b->h > u.ws.ws_row){
b->y = u.ws.ws_row - b->h;
vy *= -1.0;
}
ui_draw(&u);
usleep(10000);
}
return 0;
}