home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 177.lha / Deemu_v1.0 / clock.c < prev    next >
C/C++ Source or Header  |  1988-04-28  |  5KB  |  216 lines

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