home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gkdemo.zip / GKFILES.SET / CLOCK.C < prev    next >
C/C++ Source or Header  |  1994-12-09  |  4KB  |  158 lines

  1. /*
  2.  * Clock demo - Clock face courtesy of CorelDraw clipart
  3.  */
  4. #define GKInclAll
  5. #include <gk.h>
  6.  
  7. #ifndef M_PI
  8. #define M_PI    3.141592654
  9. #endif
  10.  
  11. #define ANGLE_MAX    (M_PI * 2.0)
  12. #define ANGLE_INCR    (ANGLE_MAX / 60.0)
  13. #define CX            205
  14. #define CY            197
  15. #define TX(x)        ((x)+CX)
  16. #define TY(y)        ((y)+CY)
  17. #define ROTX(pt,a)    (((short)((pt.x)*cos_v[a] - (pt.y)*sin_v[a])) + CX)
  18. #define ROTY(pt,a)    (((short)((pt.x)*sin_v[a] + (pt.y)*cos_v[a])) + CY)
  19. #define SHADOW_OFS    4
  20.  
  21. static GKPoint    hour_pts[] = { 10,-57, 0,-87, -10,-57 },
  22.                 min_pts[] = {  10,-94, 0,-141, -10,-94 },
  23.                 sec_pts[] = { 5,-82, 0,-130, -5,-82 };
  24.  
  25. static double    sin_v[60],
  26.                 cos_v[60];
  27.  
  28. static int        last_hour_angle = -1, last_min = -1;
  29.  
  30.  
  31. /*----------------------------------------------------------------------*/
  32. static void    UpdateClock()
  33. {
  34.     time_t        t;
  35.     struct tm    *ltm;
  36.     int            a, i;
  37.     GKPoint        pts[5];
  38.  
  39.     time(&t);
  40.     ltm=localtime(&t);
  41.  
  42.     /*
  43.      * Hour hand moves gradually like a real clock. Calculate angle
  44.      * position (0-59) of hour hand. 720=3600/5
  45.      */
  46.     a=((long)(ltm->tm_hour%12)*3600 + ltm->tm_min*60 + ltm->tm_sec)/720;
  47.  
  48.     if (a != last_hour_angle) {
  49.         /* Rotate and set points of Hour hand     */
  50.         pts[0].x = TX(0);                    pts[0].y = TY(0);
  51.         pts[1].x = ROTX(hour_pts[0], a);    pts[1].y = ROTY(hour_pts[0], a);
  52.         pts[2].x = ROTX(hour_pts[1], a);    pts[2].y = ROTY(hour_pts[1], a);
  53.         pts[3].x = ROTX(hour_pts[2], a);    pts[3].y = ROTY(hour_pts[2], a);
  54.         pts[4].x = TX(0);                    pts[4].y = TY(0);
  55.  
  56.         gkSet(gkFindInstance("HourHand"),
  57.               GKGfxLine_PointArray, 5, pts,
  58.               NULL);
  59.  
  60.         gkSet(gkFindInstance("HourHandOutline"),
  61.               GKGfxLine_PointArray, 5, pts,
  62.               NULL);
  63.  
  64.         for (i=1; i < 4; ++i) {
  65.             pts[i].x += SHADOW_OFS;
  66.             pts[i].y += SHADOW_OFS;
  67.         }
  68.  
  69.         gkSet(gkFindInstance("HourHandShadow"),
  70.               GKGfxLine_PointArray, 5, pts,
  71.               NULL);
  72.  
  73.         last_hour_angle=a;
  74.     }
  75.  
  76.     /* Rotate and set points of minute hand     */
  77.     a=ltm->tm_min;
  78.     if (a != last_min) {
  79.         pts[0].x = TX(0);                pts[0].y = TY(0);
  80.         pts[1].x = ROTX(min_pts[0], a);    pts[1].y = ROTY(min_pts[0], a);
  81.         pts[2].x = ROTX(min_pts[1], a);    pts[2].y = ROTY(min_pts[1], a);
  82.         pts[3].x = ROTX(min_pts[2], a);    pts[3].y = ROTY(min_pts[2], a);
  83.         pts[4].x = TX(0);                pts[4].y = TY(0);
  84.  
  85.         gkSet(gkFindInstance("MinuteHand"),
  86.               GKGfxLine_PointArray, 5, pts,
  87.               NULL);
  88.  
  89.         gkSet(gkFindInstance("MinuteHandOutline"),
  90.               GKGfxLine_PointArray, 5, pts,
  91.               NULL);
  92.  
  93.         for (i=1; i < 4; ++i) {
  94.             pts[i].x += SHADOW_OFS;
  95.             pts[i].y += SHADOW_OFS;
  96.         }
  97.  
  98.         gkSet(gkFindInstance("MinuteHandShadow"),
  99.               GKGfxLine_PointArray, 5, pts,
  100.               NULL);
  101.  
  102.         last_min=a;
  103.     }
  104.  
  105.     a=ltm->tm_sec;
  106.     pts[0].x = TX(0);                pts[0].y = TY(0);
  107.     pts[1].x = ROTX(sec_pts[0], a);    pts[1].y = ROTY(sec_pts[0], a);
  108.     pts[2].x = ROTX(sec_pts[1], a);    pts[2].y = ROTY(sec_pts[1], a);
  109.     pts[3].x = ROTX(sec_pts[2], a);    pts[3].y = ROTY(sec_pts[2], a);
  110.     pts[4].x = TX(0);                pts[4].y = TY(0);
  111.  
  112.     gkSet(gkFindInstance("SecondHand"),
  113.           GKGfxLine_PointArray, 5, pts,
  114.           NULL);
  115. }
  116.  
  117. /*----------------------------------------------------------------------*/
  118. static void    TimerNotify(GKObject obj, GKMsTime timestamp)
  119. {
  120.     UpdateClock();
  121. }
  122.  
  123. /*----------------------------------------------------------------------*/
  124. main(argc,argv)
  125. int        argc;
  126. char    **argv;
  127. {
  128.     int        i;
  129.     double    angle;
  130.  
  131.     gkRegisterNamedProcedure("TimerNotify",
  132.                              (GKNamedProcedure)TimerNotify);
  133.  
  134.     gkInit(GKInit_Args, &argc, argv,
  135.            NULL);
  136.  
  137.     /* Pre-calculate the sine and cosine for the 60 angles */
  138.     for (angle=0.0, i=0; i < 60; angle = angle + ANGLE_INCR, ++i) {
  139.             sin_v[i]=sin(angle);
  140.             cos_v[i]=cos(angle);
  141.     }
  142.  
  143.     /* TZ must be set for localtime to work    */
  144.     if (getenv("TZ") == NULL)
  145.         putenv("TZ=GMT0");
  146.  
  147.     if (gkCreateObjectGroup("ClockGroup") == -1) {
  148.         gkError("Could not create ClockGroup");
  149.         return(1);
  150.     }
  151.  
  152.     /* Calculate initial hand positions    */
  153.     UpdateClock();
  154.     gkDispatch();
  155.  
  156.     return(0);
  157. }
  158.