catclock.c 11 KB

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