1 
2 /*
3  *  ZX80 timing, controlled video refresh, and graphics demo.
4  *  Stefano Bodrato
5  *
6  *  Build examples
7  *
8  *  zcc +zx80 -create-app -llib3d clock.c
9 
10  $Id: clock.c,v 1.1 2013-01-07 14:39:58 stefano Exp $
11 */
12 
13 #include <graphics.h>
14 #include <time.h>
15 #include <games.h>
16 #include <lib3d.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 
20 
21 int x,y;
22 int x_min,y_min;
23 int x_hr,y_hr;
24 int i,j,k,t;
25 int sz, long_sz, short_sz;
26 int cx,cy;
27 int tm;
28 int flag;
29 char hr[10],mn[10];
30 
main()31 main()
32 {
33 	sz=getmaxy()/2-1;
34 	long_sz=sz-2;
35 	short_sz=sz/2;
36 
37 	cx=getmaxx()/2;
38 	cy=getmaxy()/2;
39 
40 	printf("%cTime set..\n\n  Hours: ",12);
41 	gets(hr);
42 	k=atoi(hr);
43 	printf("\n  Minutes: ");
44 	scanf("%s",hr);
45 	j=atoi(hr);
46 
47 	k=k*5+(j/12);
48 	if (k<15)
49 		k=k+45;
50 	else
51 		k-=15;
52 
53 	if (j<15)
54 		j=j+45;
55 	else
56 		j-=15;
57 
58 	clg();
59 	gen_tv_field_init(0);
60 
61 	circle(cx,cy,cy,1);
62 
63 	for (i=0;i<60;i++) {
64 		x=icos(i*6)*sz/256;
65 		y=isin(i*6)*sz/256;
66 
67 		plot (cx+x,cy+y);
68 
69 	}
70 
71 	x=-1;
72 
73 	i=0;
74 
75 //	tm=clock();
76 	flag=1;
77 
78 	while (getk()!=' ') {
79 	gen_tv_field();
80 		//tm=clock();
81 		if (i++ == 59) i=0;
82 		if (i == 45) {
83 			if (x != -1) {
84 				// min
85 				undraw(cx-1,cy+1,cx+x_min,cy+y_min);
86 				undraw(cx+1,cy-1,cx+x_min,cy+y_min);
87 				undraw(cx+1,cy+1,cx+x_min,cy+y_min);
88 				undraw(cx-1,cy-1,cx+x_min,cy+y_min);
89 				flag=1;
90 			}
91 			if (j++ == 59) {
92 				j=0;
93 			}
94 			if (j == 45) {
95 				if (x != -1) {
96 					undraw(cx,cy,cx+x_hr,cy+y_hr);
97 					undraw(cx-1,cy+1,cx+x_hr-1,cy+y_hr+1);
98 					undraw(cx+1,cy-1,cx+x_hr+1,cy+y_hr-1);
99 					undraw(cx+1,cy+1,cx+x_hr+1,cy+y_hr+1);
100 					undraw(cx-1,cy-1,cx+x_hr-1,cy+y_hr-1);
101 					flag=1;
102 				}
103 				if (k++ == 59) k=0;
104 			}
105 		}
106 
107 		if (x != -1) {
108 			//sec
109 			//undraw(cx+(x/2.9),cy+(y/2),cx+x,cy+y);
110 			unplot(cx+x,cy+y);
111 		}
112 	gen_tv_field();
113 		x=icos(i*6)*long_sz/256;
114 	gen_tv_field();
115 		y=isin(i*6)*long_sz/256;
116 
117 	gen_tv_field();
118 		x_min=icos(j*6)*long_sz/256;
119 	gen_tv_field();
120 		y_min=isin(j*6)*long_sz/256;
121 
122 	gen_tv_field();
123 		x_hr=icos(k*6)*short_sz/256;
124 	gen_tv_field();
125 		y_hr=isin(k*6)*short_sz/256;
126 
127 		// sec
128 	//gen_tv_field();
129 	// Count up to 50 frames to wait a whole second
130 	// 52 *could* be valid for the USA, but Eigthyone
131 	// seems to run faster.. is it the emulator or me ?
132 	// Every minute some extra delay happens, so the clock
133 	// is not very precise  :/
134 	for (t=0;t<42;t++) {
135 		gen_tv_field();
136 		plot(cx+x,cy+y);
137 	}
138 		//	draw(cx+(x/2),cy+(y/2),cx+x,cy+y);
139 		//draw(cx,cy,cx+x,cy+y);
140 	gen_tv_field();
141 
142 	if (flag) {
143 			// min
144 			draw(cx-1,cy+1,cx+x_min,cy+y_min);
145 			draw(cx+1,cy-1,cx+x_min,cy+y_min);
146 			draw(cx+1,cy+1,cx+x_min,cy+y_min);
147 			draw(cx-1,cy-1,cx+x_min,cy+y_min);
148 
149 			// hr
150 			draw(cx,cy,cx+x_hr,cy+y_hr);
151 			draw(cx-1,cy+1,cx+x_hr-1,cy+y_hr+1);
152 			draw(cx+1,cy-1,cx+x_hr+1,cy+y_hr-1);
153 			draw(cx+1,cy+1,cx+x_hr+1,cy+y_hr+1);
154 			draw(cx-1,cy-1,cx+x_hr-1,cy+y_hr-1);
155 		}
156 
157 //		circle(cx,cy,3,1);
158 
159 		flag=0;
160 
161 		//while ((clock() < (tm+CLOCKS_PER_SEC))&&(clock() > CLOCKS_PER_SEC)) {}
162 		//tm=clock();
163 
164 	}
165 
166 }
167