source: trunk/ncurses/test/tclock.c@ 3003

Last change on this file since 3003 was 2621, checked in by bird, 19 years ago

GNU ncurses 5.5

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