catclock.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* Copyright 1985 Massachusetts Institute of Technology */
  2. #include <u.h>
  3. #include <libc.h>
  4. #include <draw.h>
  5. #include <event.h>
  6. #define add addpt
  7. #define sub subpt
  8. int wind = 1;
  9. typedef enum{
  10. Odd=1,
  11. Nonzero=~0
  12. }Windrule;
  13. #include "catback.p"
  14. #include "eyes.p"
  15. #define CATWID 150 /* width of body bitmap */
  16. #define CATHGT 300 /* height of body bitmap */
  17. #define TAILWID 150 /* width of tail bitmap */
  18. #define TAILHGT 89 /* height of tail bitmap */
  19. #define MINULEN 27 /* length of minute hand */
  20. #define HOURLEN 15 /* length of hour hand */
  21. #define HANDWID 4 /* width of clock hands */
  22. #define UPDATE (1000/NTAIL) /* ms/update -- tail waves at roughly 1/2 hz */
  23. #define BLACK (~0)
  24. #define WHITE 0
  25. #define NTP 7
  26. Point tp[NTP]={ /* tail polygon */
  27. 0, 0,
  28. 0,76,
  29. 3,82,
  30. 10,84,
  31. 18,82,
  32. 21,76,
  33. 21,70,
  34. };
  35. #define NTAIL 16
  36. Image *eye[NTAIL+1];
  37. Image *tail[NTAIL+1];
  38. Image *cat; /* cat body */
  39. Image *eyes; /* eye background */
  40. Point toffs={ 74, -15 }; /* tail polygon offset */
  41. Point tailoffs={0, 211}; /* tail bitmap offset, relative to body */
  42. Point eyeoffs={49, 30}; /* eye bitmap offset, relative to body */
  43. Point catoffs; /* cat offset, relative to screen */
  44. int xredraw;
  45. int crosseyed;
  46. void drawclock(void);
  47. void drawhand(int, int, double);
  48. void init(void);
  49. Image *draweye(double);
  50. Image *drawtail(double);
  51. Image *eballoc(Rectangle, int);
  52. //int myfillpoly(Image *, Point [], int, Windrule, int, Fcode);
  53. //void mydrawpoly(Image *, Point [], int, int, Fcode);
  54. Image *eballoc(Rectangle r, int chan){
  55. Image *b=allocimage(display, r, chan, 0, DWhite);
  56. if(b==0){
  57. fprint(2, "catclock: can't allocate bitmap\n");
  58. exits("allocimage");
  59. }
  60. return b;
  61. }
  62. void
  63. eloadimage(Image *i, Rectangle r, uchar *d, int nd)
  64. {
  65. int n;
  66. n = loadimage(i, r, d, nd);
  67. if(n < nd) {
  68. fprint(2, "loadimage fails: %r\n");
  69. exits("loadimage");
  70. }
  71. }
  72. int round(double x){
  73. return x>=0.?x+.5:x-.5;
  74. }
  75. void
  76. redraw(Image *screen)
  77. {
  78. Rectangle r = Rect(0,0,Dx(screen->r), Dy(screen->r));
  79. catoffs.x=(Dx(r)-CATWID)/2;
  80. catoffs.y=(Dy(r)-CATHGT)/2;
  81. if(!ptinrect(catoffs, r)) fprint(2, "catclock: window too small, resize!\n");
  82. xredraw=1;
  83. }
  84. void
  85. eresized(int new)
  86. {
  87. if(new && getwindow(display, Refmesg) < 0)
  88. fprint(2,"can't reattach to window");
  89. redraw(screen);
  90. }
  91. void main(int argc, char *argv[]){
  92. int i;
  93. ARGBEGIN{
  94. case 'c': crosseyed++; break;
  95. default:
  96. fprint(2, "Usage: %s [-c]\n", argv0);
  97. exits("usage");
  98. }ARGEND
  99. initdraw(0, 0, "cat clock");
  100. einit(Emouse);
  101. redraw(screen);
  102. for(i=0; i<nelem(catback_bits); i++)
  103. catback_bits[i] ^= 0xFF;
  104. for(i=0; i<nelem(eyes_bits); i++)
  105. eyes_bits[i] ^= 0xFF;
  106. cat=eballoc(Rect(0, 0, CATWID, CATHGT), GREY1);
  107. eloadimage(cat, cat->r, catback_bits, sizeof(catback_bits));
  108. // wrbitmap(cat, cat->r.min.y, cat->r.max.y, catback_bits);
  109. for(i=0;i<=NTAIL;i++){
  110. tail[i]=drawtail(i*PI/NTAIL);
  111. eye[i]=draweye(i*PI/NTAIL);
  112. }
  113. for(;;){
  114. if(ecanmouse()) emouse(); /* don't get resize events without this! */
  115. drawclock();
  116. flushimage(display, 1);
  117. // bflush();
  118. sleep(UPDATE);
  119. }
  120. }
  121. /*
  122. * Draw a clock hand, theta is clockwise angle from noon
  123. */
  124. void drawhand(int length, int width, double theta){
  125. double c=cos(theta), s=sin(theta);
  126. double ws=width*s, wc=width*c;
  127. Point vhand[4];
  128. vhand[0]=add(screen->r.min, add(catoffs, Pt(CATWID/2+round(length*s), CATHGT/2-round(length*c))));
  129. vhand[1]=add(screen->r.min, add(catoffs, Pt(CATWID/2-round(ws+wc), CATHGT/2+round(wc-ws))));
  130. vhand[2]=add(screen->r.min, add(catoffs, Pt(CATWID/2-round(ws-wc), CATHGT/2+round(wc+ws))));
  131. vhand[3] = vhand[0];
  132. fillpoly(screen, vhand, 4, wind, display->white,
  133. addpt(screen->r.min, vhand[0]));
  134. poly(screen, vhand, 4, Endsquare, Endsquare, 0, display->black,
  135. addpt(screen->r.min, vhand[0]));
  136. // myfillpoly(&screen, vhand, 3, Nonzero, WHITE, S);
  137. // mydrawpoly(&screen, vhand, 3, BLACK, S);
  138. }
  139. /*
  140. * draw a cat tail, t is time (mod 1 second)
  141. */
  142. Image *drawtail(double t){
  143. Image *bp;
  144. double theta=.4*sin(t+3.*PIO2)-.08; /* an assymetric tail leans to one side */
  145. double s=sin(theta), c=cos(theta);
  146. Point rtp[NTP];
  147. int i;
  148. bp=eballoc(Rect(0, 0, TAILWID, TAILHGT), GREY1);
  149. for(i=0;i!=NTP;i++)
  150. rtp[i]=add(Pt(tp[i].x*c+tp[i].y*s, -tp[i].x*s+tp[i].y*c), toffs);
  151. fillpoly(bp, rtp, NTP, wind, display->black, rtp[0]);
  152. return bp;
  153. }
  154. /*
  155. * draw the cat's eyes, t is time (mod 1 second)
  156. */
  157. Image *draweye(double t){
  158. Image *bp;
  159. double u;
  160. double angle=0.7*sin(t+3*PIO2)+PI/2.0; /* direction eyes point */
  161. Point pts[100];
  162. int i, j;
  163. struct{
  164. double x, y, z;
  165. }pt;
  166. if(eyes==0){
  167. eyes=eballoc(Rect(0, 0, eyes_width, eyes_height), GREY1);
  168. eloadimage(eyes, eyes->r, eyes_bits, sizeof(eyes_bits));
  169. // wrbitmap(eyes, eyes->r.min.y, eyes->r.max.y, eyes_bits);
  170. }
  171. bp=eballoc(eyes->r, GREY1);
  172. draw(bp, bp->r, eyes, nil, ZP);
  173. // bitblt(bp, bp->r.min, eyes, eyes->r, S);
  174. for(i=0,u=-PI/2.0;u<PI/2.0;i++,u+=0.25){
  175. pt.x=cos(u)*cos(angle+PI/7.0);
  176. pt.y=sin(u);
  177. pt.z=2.+cos(u)*sin(angle+PI/7.0);
  178. pts[i].x=(pt.z==0.0?pt.x:pt.x/pt.z)*23.0+12.0;
  179. pts[i].y=(pt.z==0.0?pt.y:pt.y/pt.z)*23.0+11.0;
  180. }
  181. for(u=PI/2.0;u>-PI/2.0;i++,u-=0.25){
  182. pt.x=cos(u)*cos(angle-PI/7.0);
  183. pt.y=sin(u);
  184. pt.z=2.+cos(u)*sin(angle-PI/7.0);
  185. pts[i].x=(pt.z==0.0?pt.x:pt.x/pt.z)*23.0+12.0;
  186. pts[i].y=(pt.z==0.0?pt.y:pt.y/pt.z)*23.0+11.0;
  187. }
  188. fillpoly(bp, pts, i, wind, display->black, pts[0]);
  189. // fillpoly(bp, pts, i, Nonzero, BLACK, S);
  190. if(crosseyed){
  191. angle=0.7*sin(PI-t+3*PIO2)+PI/2.0;
  192. for(i=0,u=-PI/2.0;u<PI/2.0;i++,u+=0.25){
  193. pt.x=cos(u)*cos(angle+PI/7.0);
  194. pt.y=sin(u);
  195. pt.z=2.+cos(u)*sin(angle+PI/7.0);
  196. pts[i].x=(pt.z==0.0?pt.x:pt.x/pt.z)*23.0+12.0;
  197. pts[i].y=(pt.z==0.0?pt.y:pt.y/pt.z)*23.0+11.0;
  198. }
  199. for(u=PI/2.0;u>-PI/2.0;i++,u-=0.25){
  200. pt.x=cos(u)*cos(angle-PI/7.0);
  201. pt.y=sin(u);
  202. pt.z=2.+cos(u)*sin(angle-PI/7.0);
  203. pts[i].x=(pt.z==0.0?pt.x:pt.x/pt.z)*23.0+12.0;
  204. pts[i].y=(pt.z==0.0?pt.y:pt.y/pt.z)*23.0+11.0;
  205. }
  206. }
  207. for(j=0;j<i;j++) pts[j].x+=31;
  208. fillpoly(bp, pts, i, wind, display->black, pts[0]);
  209. // fillpoly(bp, pts, i, Nonzero, BLACK, S);
  210. return bp;
  211. }
  212. void
  213. drawclock(void){
  214. static int t=0, dt=1;
  215. static Tm otm;
  216. Tm tm=*localtime(time(0));
  217. tm.hour%=12;
  218. if(xredraw || tm.min!=otm.min || tm.hour!=otm.hour){
  219. if(xredraw){
  220. draw(screen, screen->r, display->white, nil, ZP);
  221. border(screen, screen->r, 4, display->black, ZP);
  222. //bitblt(&screen, screen.r.min, &screen, screen.r, Zero);
  223. //border(&screen, screen.r, 4, F);
  224. }
  225. draw(screen, screen->r, cat, nil, mulpt(catoffs, -1));
  226. flushimage(display, 1);
  227. //bitblt(&screen, catoffs, cat, cat->r, S);
  228. drawhand(MINULEN, HANDWID, 2.*PI*tm.min/60.);
  229. drawhand(HOURLEN, HANDWID, 2.*PI*(tm.hour+tm.min/60.)/12.);
  230. xredraw=0;
  231. }
  232. draw(screen, screen->r, tail[t], nil,
  233. mulpt(add(catoffs, tailoffs), -1));
  234. draw(screen, screen->r, eye[t], nil,
  235. mulpt(add(catoffs, eyeoffs), -1));
  236. //bitblt(&screen, add(catoffs, tailoffs), tail[t], tail[t]->r, S);
  237. //bitblt(&screen, add(catoffs, eyeoffs), eye[t], eye[t]->r, S);
  238. t+=dt;
  239. if(t<0 || t>NTAIL){
  240. t-=2*dt;
  241. dt=-dt;
  242. }
  243. otm=tm;
  244. }
  245. #ifdef NOTDEF
  246. void drawpoly(Bitmap *dst, Point p[], int np, int v, Fcode f){
  247. int i;
  248. Point q=p[np-1];
  249. for(i=0;i!=np;i++){
  250. segment(dst, p[i], q, v, f);
  251. q=p[i];
  252. }
  253. }
  254. /*
  255. * Fillpoly -- a polygon tiler
  256. * Updating the edgelist from scanline to scanline could be quicker if no
  257. * edges cross: we can just merge the incoming edges. The code can handle
  258. * multiply-connected polygons with holes, but the interface can't. If
  259. * the scan-line filling routine were a parameter, we could do textured
  260. * polygons, polyblt, and other such stuff.
  261. */
  262. typedef struct edge Edge;
  263. struct edge{
  264. Point p; /* point of crossing current scan-line */
  265. int maxy; /* scan line at which to discard edge */
  266. int dx; /* x increment if x fraction<1 */
  267. int dx1; /* x increment if x fraction>=1 */
  268. int x; /* x fraction, scaled by den */
  269. int num; /* x fraction increment for unit y change, scaled by den */
  270. int den; /* x fraction increment for unit x change, scaled by num */
  271. int dwind; /* increment of winding number on passing this edge */
  272. Edge *next; /* next edge on current scanline */
  273. Edge *prev; /* previous edge on current scanline */
  274. };
  275. void insert(Edge *ep, Edge **yp){
  276. while(*yp && (*yp)->p.x<ep->p.x) yp=&(*yp)->next;
  277. ep->next=*yp;
  278. *yp=ep;
  279. if(ep->next){
  280. ep->prev=ep->next->prev;
  281. ep->next->prev=ep;
  282. if(ep->prev)
  283. ep->prev->next=ep;
  284. }
  285. else
  286. ep->prev=0;
  287. }
  288. int myfillpoly(Bitmap *b, Point vert[], int nvert, Windrule w, int v, Fcode f){
  289. Edge *edges, *ep, *nextep, **ylist, **eylist, **yp;
  290. Point *p, *q, *evert, p0, p1, p10;
  291. int dy, nbig, y, left, right, wind, nwind;
  292. edges=(Edge *)malloc(nvert*sizeof(Edge));
  293. if(edges==0){
  294. NoSpace:
  295. return 0;
  296. }
  297. ylist=(Edge **)malloc((b->r.max.y-b->r.min.y)*sizeof(Edge *));
  298. if(ylist==0) goto NoSpace;
  299. eylist=ylist+(b->r.max.y-b->r.min.y);
  300. for(yp=ylist;yp!=eylist;yp++) *yp=0;
  301. evert=vert+nvert;
  302. for(p=evert-1, q=vert, ep=edges;q!=evert;p=q, q++, ep++){
  303. if(p->y==q->y) continue;
  304. if(p->y<q->y){
  305. p0=*p;
  306. p1=*q;
  307. ep->dwind=1;
  308. }
  309. else{
  310. p0=*q;
  311. p1=*p;
  312. ep->dwind=-1;
  313. }
  314. if(p1.y<=b->r.min.y) continue;
  315. if(p0.y>=b->r.max.y) continue;
  316. ep->p=p0;
  317. if(p1.y>b->r.max.y)
  318. ep->maxy=b->r.max.y;
  319. else
  320. ep->maxy=p1.y;
  321. p10=sub(p1, p0);
  322. if(p10.x>=0){
  323. ep->dx=p10.x/p10.y;
  324. ep->dx1=ep->dx+1;
  325. }
  326. else{
  327. p10.x=-p10.x;
  328. ep->dx=-(p10.x/p10.y); /* this nonsense rounds toward zero */
  329. ep->dx1=ep->dx-1;
  330. }
  331. ep->x=0;
  332. ep->num=p10.x%p10.y;
  333. ep->den=p10.y;
  334. if(ep->p.y<b->r.min.y){
  335. dy=b->r.min.y-ep->p.y;
  336. ep->x+=dy*ep->num;
  337. nbig=ep->x/ep->den;
  338. ep->p.x+=ep->dx1*nbig+ep->dx*(dy-nbig);
  339. ep->x%=ep->den;
  340. ep->p.y=b->r.min.y;
  341. }
  342. insert(ep, ylist+(ep->p.y-b->r.min.y));
  343. }
  344. left=0;
  345. for(yp=ylist,y=b->r.min.y;yp!=eylist;yp++,y++){
  346. wind=0;
  347. for(ep=*yp;ep;ep=nextep){
  348. nwind=wind+ep->dwind;
  349. if(nwind&w){ /* inside */
  350. if(!(wind&w)){
  351. left=ep->p.x;
  352. if(left<b->r.min.x) left=b->r.min.x;
  353. }
  354. }
  355. else if(wind&w){
  356. right=ep->p.x;
  357. if(right>=b->r.max.x) right=b->r.max.x;
  358. if(right>left)
  359. segment(b, Pt(left, y), Pt(right, y), v, f);
  360. }
  361. wind=nwind;
  362. nextep=ep->next;
  363. if(++ep->p.y!=ep->maxy){
  364. ep->x+=ep->num;
  365. if(ep->x>=ep->den){
  366. ep->x-=ep->den;
  367. ep->p.x+=ep->dx1;
  368. }
  369. else
  370. ep->p.x+=ep->dx;
  371. insert(ep, yp+1);
  372. }
  373. }
  374. }
  375. free((char *)edges);
  376. free((char *)ylist);
  377. return 1;
  378. }
  379. #endif