home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 152.lha / UnobtrusiveAnalogClock / clock.c < prev    next >
C/C++ Source or Header  |  1988-04-26  |  6KB  |  188 lines

  1. /*
  2.  *  CLOCK.C     simple clock program
  3.  */
  4. #include <local/typedefs.h>
  5. short Params[] = {
  6.     'ST','RT', 0, 0,
  7.     'NW','  ', 0, 8, -1, 10, 40, 20,
  8.     'TE','XT', 0,16, 'HI','\0T','ES','T.', 0, 0, 0, 0,
  9.     'EN','D ', 0, 0
  10. };
  11. #define WPOSX   8
  12. #define WPOSY   9
  13. #define WWIDTH  10
  14. #define WHEIGHT 11
  15. APTR IntuitionBase;
  16. APTR GfxBase;
  17. NW Nw = {
  18.     0, 0, 0, 0, 0, 0,
  19.     CLOSEWINDOW|REFRESHWINDOW|ACTIVEWINDOW|INACTIVEWINDOW,
  20.     SIMPLE_REFRESH|WINDOWCLOSE|BACKDROP|RMBTRAP,
  21.     NULL, NULL, (ubyte *)"", NULL, NULL, 4, 4, -1, -1, WBENCHSCREEN
  22. };
  23. short Sin[60+15] = {
  24.       0,  27,  53,  79, 104, 128, 150, 171, 190, 207, 222, 234, 243, 250, 255,
  25.     256, 255, 250, 243, 234, 222, 207, 190, 171, 150, 128, 104,  79,  53,  27,
  26.       0, -27, -53, -79,-104,-128,-150,-171,-190,-207,-222,-234,-243,-250,-255,
  27.    -256,-255,-250,-243,-234,-222,-207,-190,-171,-150,-128,-104, -79, -53, -27,
  28.       0,  27,  53,  79, 104, 128, 150, 171, 190, 207, 222, 234, 243, 250, 255
  29. };
  30. #define Cos (Sin+15)
  31. _main(ac, av)
  32. char *av[];
  33. {
  34.     IOT  Iot;       /*  timer request,  1 second internals  */
  35.     SCR  Scr;
  36.     WIN  *Win;
  37.     PORT TPort;     /*  port for timer request              */
  38.     long TMask;
  39.     long WMask;
  40.     long error = 20;
  41.     char notdone = 1;
  42.     bzero(&Iot, sizeof(Iot));
  43.     bzero(&TPort, sizeof(TPort));
  44.     if (!(IntuitionBase = OpenLibrary("intuition.library", 0)))
  45.         goto dieilib;
  46.     if (!(GfxBase = OpenLibrary("graphics.library", 0)))
  47.         goto dieglib;
  48.     if (OpenDevice("timer.device", UNIT_VBLANK, &Iot, 0))
  49.         goto diedev;
  50.     if (!GetScreenData(&Scr, sizeof(Scr), WBENCHSCREEN, NULL))
  51.         goto diewin;
  52.  
  53.     Nw.Width  = Params[WWIDTH];
  54.     Nw.Height = Params[WHEIGHT];
  55.     Nw.LeftEdge = Params[WPOSX];
  56.     Nw.TopEdge  = Params[WPOSY];
  57.     if (Nw.Width <= 0)
  58.         Nw.Width += Scr.Width;
  59.     if (Nw.Height <= 0)
  60.         Nw.Height += Scr.Height;
  61.     if (Nw.LeftEdge < 0)
  62.         Nw.LeftEdge += Scr.Width - Nw.Width;
  63.     if (Nw.TopEdge < 0)
  64.         Nw.TopEdge  += Scr.Height- Nw.Height;
  65.     if (Nw.LeftEdge < 0 || Nw.TopEdge < 0 || Nw.LeftEdge + Nw.Width > Scr.Width || Nw.TopEdge + Nw.Height > Scr.Height) {
  66.         Nw.LeftEdge = Nw.TopEdge = 0;
  67.         Nw.Width = 64;
  68.         Nw.Height= 32;
  69.     }
  70.     if (!(Win = OpenWindow(&Nw)))
  71.         goto diewin;
  72.     ShowTitle(Win->WScreen, 0);
  73.     TPort.mp_Node.ln_Type = NT_MSGPORT;
  74.     TPort.mp_SigTask = FindTask(NULL);
  75.     TPort.mp_SigBit = AllocSignal(-1);
  76.     TPort.mp_Flags  = PA_SIGNAL;
  77.     NewList(&TPort.mp_MsgList);
  78.     Iot.tr_node.io_Message.mn_ReplyPort = &TPort;
  79.     Iot.tr_node.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  80.     Iot.tr_node.io_Command = TR_ADDREQUEST;
  81.     Iot.tr_time.tv_secs = 1;
  82.     Iot.tr_time.tv_micro= 0;
  83.     SendIO(&Iot);
  84.     TMask = 1 << TPort.mp_SigBit;
  85.     WMask = 1 << Win->UserPort->mp_SigBit;
  86.     error = 0;
  87.     while (notdone) {
  88.         register long mask = Wait(TMask|WMask);
  89.         if (mask & TMask) {
  90.             register short secs;
  91.             WaitIO(&Iot);
  92.             secs = UpdateWindow(Win);
  93.             Iot.tr_time.tv_secs = 65 - secs;
  94.             Iot.tr_time.tv_micro= 0;
  95.             Iot.tr_node.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  96.             SendIO(&Iot);
  97.         }
  98.         if (mask & WMask) {
  99.             register IMESS *im;
  100.             while (im = GetMsg(Win->UserPort)) {
  101.                 switch(im->Class) {
  102.                 case CLOSEWINDOW:
  103.                     notdone = 0;
  104.                     break;
  105.                 case REFRESHWINDOW:
  106.                     BeginRefresh(Win);
  107.                     EndRefresh(Win);
  108.                 case ACTIVEWINDOW:
  109.                 case INACTIVEWINDOW:
  110.                     UpdateWindow(Win);
  111.                     break;
  112.                 }
  113.                 ReplyMsg(im);
  114.             }
  115.         }
  116.     }
  117.     AbortIO(&Iot);
  118.     WaitIO(&Iot);
  119.     FreeSignal(TPort.mp_SigBit);
  120.     CloseWindow(Win);
  121. diewin:
  122.     CloseDevice(&Iot);
  123. diedev:
  124.     CloseLibrary(GfxBase);
  125. dieglib:
  126.     CloseLibrary(IntuitionBase);
  127. dieilib:
  128.     _exit(error);
  129. }
  130. /*
  131.  *  Update the window
  132.  *
  133.  *  (1) RectFill
  134.  *  (2) minute and hour hands
  135.  */
  136. UpdateWindow(win)
  137. register WIN *win;
  138. {
  139.     register RP *rp = win->RPort;
  140.     DATESTAMP DS;
  141.     short minutes;
  142.     short hours;
  143.     short radiusx, radiusy;
  144.     short centerx, centery;
  145.     DateStamp(&DS);
  146.     minutes = DS.ds_Minute % 60;
  147.     hours   = (DS.ds_Minute / 12) % 60;     /*  0 to 59 (partial hours indicator)   */
  148.     radiusx = (win->Width - 2) >> 1;
  149.     radiusy = (win->Height- 2) >> 1;
  150.     centerx = 1 + radiusx;
  151.     centery = 1 + radiusy;
  152.     SetAPen(rp, 0);
  153.     RectFill(rp, 0, 0, win->Width - 1, win->Height - 1);
  154.     SetAPen(rp, 1);
  155.     {
  156.         register short i;
  157.         for (i = 0; i < 60; i += 5) {
  158.             Move(rp, centerx + ((radiusx * Sin[i]) >> 8), centery + ((radiusy * -Cos[i]) >> 8));
  159.             Draw(rp, centerx + ((radiusx * Sin[i]) >> 8), centery + ((radiusy * -Cos[i]) >> 8));
  160.         }
  161.     }
  162.     radiusx -= 2;
  163.     radiusy -= 2;
  164.     SetAPen(rp, 3);                 /*  minutes */
  165.     Move(rp, centerx, centery);
  166.     Draw(rp, centerx + ((radiusx * Sin[minutes]) >> 8), centery + ((radiusy * -Cos[minutes]) >> 8));
  167.     SetAPen(rp, 2);                 /*  hours   */
  168.     Move(rp, centerx, centery);
  169.     Draw(rp, centerx + ((radiusx * Sin[hours]) / 384), centery + ((radiusy * -Cos[hours]) / 384));
  170.     return(DS.ds_Tick / 50 % 60);  /*  seconds */
  171. }
  172. #asm
  173.             ; bzero(buf, bytes)
  174.             ;       4sp   8sp
  175.             ;
  176.             ;   SIMPLE STUPID BZERO .. I don't even use DBF (don't want to
  177.             ;                           have to think at this time of night)
  178. _bzero:
  179.             moveq.l #0,D1
  180.             move.l  4(sp),A0
  181.             move.l  8(sp),D0
  182.             beq     bze
  183. bz1         move.b  D1,(A0)+
  184.             subq.l  #1,D0
  185.             bne     bz1
  186. bze         rts
  187. #endasm
  188.