]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/tclock.c
ncurses 5.0
[ncurses.git] / test / tclock.c
1 #include "test.priv.h"
2
3 #include <math.h>
4 #include <time.h>
5
6 /*
7   tclock - analog/digital clock for curses.
8   If it gives you joy, then
9   (a) I'm glad
10   (b) you need to get out more :-)
11
12   This program is copyright Howard Jones, September 1994
13   (ha.jones@ic.ac.uk). It may be freely distributed as
14   long as this copyright message remains intact, and any
15   modifications are clearly marked as such. [In fact, if
16   you modify it, I wouldn't mind the modifications back,
17   especially if they add any nice features. A good one
18   would be a precalc table for the 60 hand positions, so
19   that the floating point stuff can be ditched. As I said,
20   it was a 20 hackup minute job.]
21
22   COMING SOON: tfishtank. Be the envy of your mac-owning
23   colleagues.
24 */
25
26 /* To compile: cc -o tclock tclock.c -lcurses -lm */
27
28 #ifndef PI
29 #define PI 3.141592654
30 #endif
31
32 #define sign(_x) (_x<0?-1:1)
33
34 #define ASPECT 2.2
35 #define ROUND(value) ((int)((value) + 0.5))
36
37 #define A2X(angle,radius) ROUND(ASPECT * radius * sin(angle))
38 #define A2Y(angle,radius) ROUND(radius * cos(angle))
39
40 /* Plot a point */
41 static void
42 plot(int x, int y, char col)
43 {
44     mvaddch(y, x, (chtype) col);
45 }
46
47 /* Draw a diagonal(arbitrary) line using Bresenham's alogrithm. */
48 static void
49 dline(int pair, int from_x, int from_y, int x2, int y2, char ch)
50 {
51     int dx, dy;
52     int ax, ay;
53     int sx, sy;
54     int x, y;
55     int d;
56
57     if (has_colors())
58         attrset(COLOR_PAIR(pair));
59
60     dx = x2 - from_x;
61     dy = y2 - from_y;
62
63     ax = abs(dx * 2);
64     ay = abs(dy * 2);
65
66     sx = sign(dx);
67     sy = sign(dy);
68
69     x = from_x;
70     y = from_y;
71
72     if (ax > ay) {
73         d = ay - (ax / 2);
74
75         while (1) {
76             plot(x, y, ch);
77             if (x == x2)
78                 return;
79
80             if (d >= 0) {
81                 y += sy;
82                 d -= ax;
83             }
84             x += sx;
85             d += ay;
86         }
87     } else {
88         d = ax - (ay / 2);
89
90         while (1) {
91             plot(x, y, ch);
92             if (y == y2)
93                 return;
94
95             if (d >= 0) {
96                 x += sx;
97                 d -= ay;
98             }
99             y += sy;
100             d += ax;
101         }
102     }
103 }
104
105 int
106 main(
107     int argc GCC_UNUSED,
108     char *argv[]GCC_UNUSED)
109 {
110     int i, cx, cy;
111     double mradius, hradius, mangle, hangle;
112     double sangle, sradius, hours;
113     int hdx, hdy;
114     int mdx, mdy;
115     int sdx, sdy;
116     int ch;
117     int lastbeep = -1;
118     time_t tim;
119     struct tm *t;
120     char szChar[10];
121     int my_bg = COLOR_BLACK;
122
123     initscr();
124     noecho();
125     cbreak();
126     nodelay(stdscr, TRUE);
127     curs_set(0);
128
129     if (has_colors()) {
130         start_color();
131 #ifdef NCURSES_VERSION
132         if (use_default_colors() == OK)
133             my_bg = -1;
134 #endif
135         init_pair(1, COLOR_RED, my_bg);
136         init_pair(2, COLOR_MAGENTA, my_bg);
137         init_pair(3, COLOR_GREEN, my_bg);
138     }
139 #ifdef KEY_RESIZE
140     keypad(stdscr, TRUE);
141   restart:
142 #endif
143     cx = (COLS - 1) / 2;        /* 39 */
144     cy = LINES / 2;             /* 12 */
145     ch = (cx > cy) ? cy : cx;   /* usually cy */
146     mradius = (3 * cy) / 4;     /* 9 */
147     hradius = cy / 2;           /* 6 */
148     sradius = (2 * cy) / 3;     /* 8 */
149
150     for (i = 0; i < 12; i++) {
151         sangle = (i + 1) * (2.0 * PI) / 12.0;
152         sradius = (5 * cy) / 6; /* 10 */
153         sdx = A2X(sangle, sradius);
154         sdy = A2Y(sangle, sradius);
155         sprintf(szChar, "%d", i + 1);
156
157         mvaddstr(cy - sdy, cx + sdx, szChar);
158     }
159
160     mvaddstr(0, 0, "ASCII Clock by Howard Jones (ha.jones@ic.ac.uk),1994");
161
162     sradius = 8;
163     for (;;) {
164         napms(1000);
165
166         tim = time(0);
167         t = localtime(&tim);
168
169         hours = (t->tm_hour + (t->tm_min / 60.0));
170         if (hours > 12.0)
171             hours -= 12.0;
172
173         mangle = ((t->tm_min) * (2 * PI) / 60.0);
174         mdx = A2X(mangle, mradius);
175         mdy = A2Y(mangle, mradius);
176
177         hangle = ((hours) * (2.0 * PI) / 12.0);
178         hdx = A2X(hangle, hradius);
179         hdy = A2Y(hangle, hradius);
180
181         sangle = ((t->tm_sec) * (2.0 * PI) / 60.0);
182         sdx = A2X(sangle, sradius);
183         sdy = A2Y(sangle, sradius);
184
185         dline(3, cx, cy, cx + mdx, cy - mdy, '#');
186
187         attrset(A_REVERSE);
188         dline(2, cx, cy, cx + hdx, cy - hdy, '.');
189         attroff(A_REVERSE);
190
191         if (has_colors())
192             attrset(COLOR_PAIR(1));
193
194         plot(cx + sdx, cy - sdy, 'O');
195
196         if (has_colors())
197             attrset(COLOR_PAIR(0));
198
199         mvaddstr(LINES - 2, 0, ctime(&tim));
200         refresh();
201         if ((t->tm_sec % 5) == 0
202          && t->tm_sec != lastbeep) {
203             lastbeep = t->tm_sec;
204             beep();
205         }
206
207         if ((ch = getch()) != ERR) {
208 #ifdef KEY_RESIZE
209             if (ch == KEY_RESIZE) {
210                 erase();
211                 goto restart;
212             }
213 #endif
214             break;
215         }
216
217         plot(cx + sdx, cy - sdy, ' ');
218         dline(0, cx, cy, cx + hdx, cy - hdy, ' ');
219         dline(0, cx, cy, cx + mdx, cy - mdy, ' ');
220
221     }
222
223     curs_set(1);
224     endwin();
225     return 0;
226 }