home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
gkdemo.zip
/
GKFILES.SET
/
ANIMATN.C
< prev
next >
Wrap
C/C++ Source or Header
|
1994-12-09
|
4KB
|
150 lines
/*
* Demo of sprite animation
*/
#define GKInclAll
#include <gk.h>
#define MAX_SPRITES 20
static GKObject images[4][6], gfximage[MAX_SPRITES];
static int series[MAX_SPRITES], series_len[MAX_SPRITES],
speed[MAX_SPRITES], sprites;
static GKObject frame;
static int x_dir[4]={ 1, 0, -1, 0 },
y_dir[4]={ 0, 1, 0, -1 };
/*----------------------------------------------------------------------*/
static void TimerNotify(obj, timestamp)
GKObject obj;
GKMsTime timestamp;
{
GKRectangle rect;
int fwidth, fheight, i;
fwidth=(int)gkGet(frame, GKWindow_Width, NULL) - 32;
fheight=(int)gkGet(frame, GKWindow_Height, NULL) - 32;
for (i=0; i < sprites; i++) {
if (series_len[i] <= 0) {
series[i]=rand() % 4;
series_len[i]=rand() % 50 + 10;
speed[i]=rand() % 5 + 4;
}
rect=*(GKRectangle *)gkGet(gfximage[i], GKGfxImage_Geometry, NULL);
rect.x += x_dir[series[i]] * speed[i];
rect.y += y_dir[series[i]] * speed[i];
if (rect.x < 0 || rect.y < 0 || rect.x > fwidth || rect.y > fheight) {
if (rect.x < 0)
rect.x = 0;
else if (rect.x > fwidth)
rect.x=fwidth;
if (rect.y < 0)
rect.y = 0;
else if (rect.y > fheight)
rect.y=fheight;
series_len[i]=1;
}
gkSet(gfximage[i],
GKGfxImage_ImageObj, images[series[i]][5 - series_len[i]%6],
GKGfxImage_Origin, rect.x, rect.y,
NULL);
--series_len[i];
}
}
/*----------------------------------------------------------------------*/
main(argc,argv)
int argc;
char **argv;
{
GKObject gfxbkg, gfxcont;
char buf[40];
int i, j;
gkInit(GKInit_Args, &argc, argv,
GKInit_StripArgs,
GKInit_ApplicationHasArgs, GKTRUE,
NULL);
if (argc > 1)
sprites=atoi(argv[1]);
else
sprites=4;
if ((sprites <= 0) || (sprites >= MAX_SPRITES))
sprites=MAX_SPRITES;
for (i=0; i < 4; i++) {
for (j=0; j < 6; ++j) {
ut_snprintf(buf, sizeof(buf), "eye%02d.xpm", (i*6)+j+1);
images[i][j]=gkCreate(NULL, GKImageClass,
GKImage_File, buf,
/* Prevent destruction of object */
GKIR_ReferenceObj,
NULL);
}
}
gfxcont=gkCreate(NULL, GKGfxContainerClass,
NULL);
/* You can turn the background on by using a 3rd argument */
if (argc >= 3) {
gfxbkg=gkCreate(gfxcont, GKGfxImageClass,
GKGfxImage_ImageFile, "bulb.xpm",
GKGfxImage_Tile, GKTRUE,
GKGfxImage_Geometry, 0, 0, 1024, 768,
NULL);
}
/* Create the number of sprites to run around */
for (i=0; i < sprites; i++) {
gfximage[i]=gkCreate(gfxcont, GKGfxImageClass,
GKGfxImage_ImageObj, images[0][0],
GKGfxImage_TransparentColor, "#ffffff",
NULL);
}
/* Sprites will pass behind this image */
gkCreate(gfxcont, GKGfxImageClass,
GKGfxImage_ImageFile, "fgimg.bmp",
GKGfxImage_Origin, gkXHMM(3 * GKCV_CM), gkYHMM(3 * GKCV_CM),
GKGfxImage_TransparentColor, "White",
NULL);
frame=gkCreate(NULL, GKFrameClass,
GKFrame_Title, "Animation Demo",
GKFrame_Icon, gfxcont,
GKWindow_BackgroundGfxObj, gfxcont,
GKWindow_Width, gkWidthHMM(8 * GKCV_CM),
GKWindow_Height, gkHeightHMM(8 * GKCV_CM),
GKWindow_DoubleBuffer, GKTRUE,
NULL);
gkCreate(NULL, GKTimerClass,
GKTimer_Interval, 10,
GKTimer_NotifyProc, (GKTimerNotifyProc)TimerNotify,
NULL);
gkDispatch();
/* Destroy images */
for (i=0; i < 4; i++) {
for (j=0; j < 6; ++j) {
gkSet(images[i][j],
GKIR_DereferenceObj,
NULL);
gkDestroy(images[i][j]);
}
}
return(0);
}