-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
157 lines (127 loc) · 2.89 KB
/
main.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
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
#include <stdio.h>
#include <stdlib.h>
#include <ncurses.h>
#include <signal.h>
#include <unistd.h>
#include <unistd.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#define DEV 1
/* C11 Standard: 7.142
* The header <signal.h> declare a type ... sig_atomic_t which is the
* (possibly volatile-qualified) integer type of an object that can be accessed
* as an atomic entity, even in the presence of asynchronous interrupts.
*/
static volatile sig_atomic_t running = 1;
void sig_handler(int _) {
(void)_;
running = 0;
}
uint64_t get_posix_clock_time();
void line(int x0, int y0, int x1, int y1);
void pixel(int x, int y);
int main(int argc, char *argv[])
{
int dx0 = 1;
int dy0 = 1;
int dx1 = -1;
int dy1 = -1;
int row, col;
signal(SIGINT, sig_handler);
// ncurses shit
WINDOW* win = initscr();
cbreak();
noecho();
nodelay(win, TRUE);
curs_set(0);
getmaxyx(stdscr, row, col);
//mvprintw(0,0,"%d,%d", row, col);
srandom(4);
int x0 = random() / ( RAND_MAX / row + 1);
int y0 = random() / ( RAND_MAX / col + 1);
int x1 = random() / ( RAND_MAX / row + 1);
int y1 = random() / ( RAND_MAX / col + 1);
uint64_t tstart;
int sleept = 25000;
while (running) {
int ch = getch() & A_CHARTEXT;
if (ch == 75 || ch == 107)
sleept += 1000;
if (ch == 74 || ch == 106)
sleept -= 1000;
if (ch == 32)
do {
// Zzzz...
} while (getch() != 32);
if (ch == 114 || ch == 82) {
clear();
x0 = random() / ( RAND_MAX / row + 1);
y0 = random() / ( RAND_MAX / col + 1);
x1 = random() / ( RAND_MAX / row + 1);
y1 = random() / ( RAND_MAX / col + 1);
refresh();
}
#if defined(_SC_MONOTONIC_CLOCK) && defined(DEV)
// if (sysconf (_SC_MONOTONIC_CLOCK) > 0) {
// mvprintw(0, 0, "%s", "Cock ready");
// tstart = get_posix_clock_time();
// }
#endif
if (x0 >= row -1 || x0 <= 0)
dx0 = dx0 * -1;
if (y0 >= col -1 || y0 <= 0)
dy0 = dy0 * -1;
if (x1 >= row -1 || x1 <= 0)
dx1 = dx1 * -1;
if (y1 >= col -1 || y1 <= 0)
dy1 = dy1 * -1;
x0 += dx0;
y0 += dy0;
x1 += dx1;
y1 += dy1;
line(x0, y0, x1, y1);
usleep(sleept);
refresh();
}
endwin();
return EXIT_SUCCESS;
}
void pixel(int x, int y)
{
move(x, y);
int ch = inch() & A_CHARTEXT;
if (ch == 64)
printw("%c", 32);
else
printw("%c", 64);
}
void line(int x0, int y0, int x1, int y1)
{
// Bresenham's line algorithm
// y - y1 = (y2 - y1) / (x2 - x1) * (x - x1)
int dx = abs(x1-x0), sx = x0 < x1 ? 1 : -1;
int dy = abs(y1-y0), sy = y0 < y1 ? 1 : -1;
int err = (dx>dy ? dx : -dy)/2, e2;
for (;;) {
pixel(x0, y0);
if (x0==x1 && y0==y1) break;
e2 = err;
if (e2 >-dx) {
err -= dy;
x0 += sx;
}
if (e2 < dy) {
err += dx;
y0 += sy;
}
}
}
uint64_t get_posix_clock_time ()
{
struct timespec ts;
if (clock_gettime (CLOCK_MONOTONIC, &ts) == 0)
return (uint64_t) (ts.tv_sec * 1000000 + ts.tv_nsec / 1000);
else
return 0;
}