home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
gkdemo.zip
/
GKFILES.SET
/
CLOCK.C
< prev
next >
Wrap
C/C++ Source or Header
|
1994-12-09
|
4KB
|
158 lines
/*
* Clock demo - Clock face courtesy of CorelDraw clipart
*/
#define GKInclAll
#include <gk.h>
#ifndef M_PI
#define M_PI 3.141592654
#endif
#define ANGLE_MAX (M_PI * 2.0)
#define ANGLE_INCR (ANGLE_MAX / 60.0)
#define CX 205
#define CY 197
#define TX(x) ((x)+CX)
#define TY(y) ((y)+CY)
#define ROTX(pt,a) (((short)((pt.x)*cos_v[a] - (pt.y)*sin_v[a])) + CX)
#define ROTY(pt,a) (((short)((pt.x)*sin_v[a] + (pt.y)*cos_v[a])) + CY)
#define SHADOW_OFS 4
static GKPoint hour_pts[] = { 10,-57, 0,-87, -10,-57 },
min_pts[] = { 10,-94, 0,-141, -10,-94 },
sec_pts[] = { 5,-82, 0,-130, -5,-82 };
static double sin_v[60],
cos_v[60];
static int last_hour_angle = -1, last_min = -1;
/*----------------------------------------------------------------------*/
static void UpdateClock()
{
time_t t;
struct tm *ltm;
int a, i;
GKPoint pts[5];
time(&t);
ltm=localtime(&t);
/*
* Hour hand moves gradually like a real clock. Calculate angle
* position (0-59) of hour hand. 720=3600/5
*/
a=((long)(ltm->tm_hour%12)*3600 + ltm->tm_min*60 + ltm->tm_sec)/720;
if (a != last_hour_angle) {
/* Rotate and set points of Hour hand */
pts[0].x = TX(0); pts[0].y = TY(0);
pts[1].x = ROTX(hour_pts[0], a); pts[1].y = ROTY(hour_pts[0], a);
pts[2].x = ROTX(hour_pts[1], a); pts[2].y = ROTY(hour_pts[1], a);
pts[3].x = ROTX(hour_pts[2], a); pts[3].y = ROTY(hour_pts[2], a);
pts[4].x = TX(0); pts[4].y = TY(0);
gkSet(gkFindInstance("HourHand"),
GKGfxLine_PointArray, 5, pts,
NULL);
gkSet(gkFindInstance("HourHandOutline"),
GKGfxLine_PointArray, 5, pts,
NULL);
for (i=1; i < 4; ++i) {
pts[i].x += SHADOW_OFS;
pts[i].y += SHADOW_OFS;
}
gkSet(gkFindInstance("HourHandShadow"),
GKGfxLine_PointArray, 5, pts,
NULL);
last_hour_angle=a;
}
/* Rotate and set points of minute hand */
a=ltm->tm_min;
if (a != last_min) {
pts[0].x = TX(0); pts[0].y = TY(0);
pts[1].x = ROTX(min_pts[0], a); pts[1].y = ROTY(min_pts[0], a);
pts[2].x = ROTX(min_pts[1], a); pts[2].y = ROTY(min_pts[1], a);
pts[3].x = ROTX(min_pts[2], a); pts[3].y = ROTY(min_pts[2], a);
pts[4].x = TX(0); pts[4].y = TY(0);
gkSet(gkFindInstance("MinuteHand"),
GKGfxLine_PointArray, 5, pts,
NULL);
gkSet(gkFindInstance("MinuteHandOutline"),
GKGfxLine_PointArray, 5, pts,
NULL);
for (i=1; i < 4; ++i) {
pts[i].x += SHADOW_OFS;
pts[i].y += SHADOW_OFS;
}
gkSet(gkFindInstance("MinuteHandShadow"),
GKGfxLine_PointArray, 5, pts,
NULL);
last_min=a;
}
a=ltm->tm_sec;
pts[0].x = TX(0); pts[0].y = TY(0);
pts[1].x = ROTX(sec_pts[0], a); pts[1].y = ROTY(sec_pts[0], a);
pts[2].x = ROTX(sec_pts[1], a); pts[2].y = ROTY(sec_pts[1], a);
pts[3].x = ROTX(sec_pts[2], a); pts[3].y = ROTY(sec_pts[2], a);
pts[4].x = TX(0); pts[4].y = TY(0);
gkSet(gkFindInstance("SecondHand"),
GKGfxLine_PointArray, 5, pts,
NULL);
}
/*----------------------------------------------------------------------*/
static void TimerNotify(GKObject obj, GKMsTime timestamp)
{
UpdateClock();
}
/*----------------------------------------------------------------------*/
main(argc,argv)
int argc;
char **argv;
{
int i;
double angle;
gkRegisterNamedProcedure("TimerNotify",
(GKNamedProcedure)TimerNotify);
gkInit(GKInit_Args, &argc, argv,
NULL);
/* Pre-calculate the sine and cosine for the 60 angles */
for (angle=0.0, i=0; i < 60; angle = angle + ANGLE_INCR, ++i) {
sin_v[i]=sin(angle);
cos_v[i]=cos(angle);
}
/* TZ must be set for localtime to work */
if (getenv("TZ") == NULL)
putenv("TZ=GMT0");
if (gkCreateObjectGroup("ClockGroup") == -1) {
gkError("Could not create ClockGroup");
return(1);
}
/* Calculate initial hand positions */
UpdateClock();
gkDispatch();
return(0);
}