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

  1. /* 
  2.  * Demo of sprite animation 
  3.  */
  4. #define GKInclAll
  5. #include <gk.h>
  6.  
  7. #define MAX_SPRITES 20
  8.  
  9. static GKObject     images[4][6], gfximage[MAX_SPRITES];
  10. static int          series[MAX_SPRITES], series_len[MAX_SPRITES],
  11.                     speed[MAX_SPRITES], sprites;
  12. static GKObject     frame;
  13.  
  14. static int          x_dir[4]={ 1, 0, -1, 0 },
  15.                     y_dir[4]={ 0, 1, 0, -1 };
  16.  
  17. /*----------------------------------------------------------------------*/
  18. static void TimerNotify(obj, timestamp)
  19. GKObject    obj;
  20. GKMsTime    timestamp;
  21. {
  22.     GKRectangle rect;
  23.     int         fwidth, fheight, i;
  24.  
  25.     fwidth=(int)gkGet(frame, GKWindow_Width, NULL) - 32;
  26.     fheight=(int)gkGet(frame, GKWindow_Height, NULL) - 32;
  27.  
  28.     for (i=0; i < sprites; i++) {
  29.         if (series_len[i] <= 0) {
  30.             series[i]=rand() % 4;
  31.             series_len[i]=rand() % 50 + 10;
  32.             speed[i]=rand() % 5 + 4;
  33.         }
  34.  
  35.         rect=*(GKRectangle *)gkGet(gfximage[i], GKGfxImage_Geometry, NULL);
  36.         rect.x += x_dir[series[i]] * speed[i];
  37.         rect.y += y_dir[series[i]] * speed[i];
  38.  
  39.         if (rect.x < 0 || rect.y < 0 || rect.x > fwidth || rect.y > fheight) {
  40.             if (rect.x < 0)
  41.                 rect.x = 0;
  42.             else if (rect.x > fwidth)
  43.                 rect.x=fwidth;
  44.         
  45.             if (rect.y < 0)
  46.                 rect.y = 0;
  47.             else if (rect.y > fheight)
  48.                 rect.y=fheight;
  49.  
  50.             series_len[i]=1;
  51.         }
  52.         
  53.         gkSet(gfximage[i],
  54.               GKGfxImage_ImageObj, images[series[i]][5 - series_len[i]%6],
  55.               GKGfxImage_Origin, rect.x, rect.y,
  56.               NULL);
  57.  
  58.         --series_len[i];
  59.     }
  60. }
  61.  
  62. /*----------------------------------------------------------------------*/
  63. main(argc,argv)
  64. int     argc;
  65. char    **argv;
  66. {
  67.     GKObject    gfxbkg, gfxcont;
  68.     char        buf[40];
  69.     int         i, j;
  70.  
  71.     gkInit(GKInit_Args, &argc, argv,
  72.            GKInit_StripArgs,
  73.            GKInit_ApplicationHasArgs, GKTRUE,
  74.            NULL);
  75.  
  76.     if (argc > 1)
  77.         sprites=atoi(argv[1]);
  78.     else
  79.         sprites=4;
  80.  
  81.     if ((sprites <= 0) || (sprites >= MAX_SPRITES))
  82.         sprites=MAX_SPRITES;
  83.  
  84.     for (i=0; i < 4; i++) {
  85.         for (j=0; j < 6; ++j) {
  86.             ut_snprintf(buf, sizeof(buf), "eye%02d.xpm", (i*6)+j+1);
  87.             images[i][j]=gkCreate(NULL, GKImageClass,
  88.                                   GKImage_File, buf,
  89.                                   /* Prevent destruction of object  */
  90.                                   GKIR_ReferenceObj,
  91.                                   NULL);
  92.         }
  93.     }
  94.  
  95.     gfxcont=gkCreate(NULL, GKGfxContainerClass,
  96.                      NULL);
  97.  
  98.     /* You can turn the background on by using a 3rd argument   */
  99.     if (argc >= 3) {
  100.         gfxbkg=gkCreate(gfxcont, GKGfxImageClass,
  101.                         GKGfxImage_ImageFile, "bulb.xpm",
  102.                         GKGfxImage_Tile, GKTRUE,
  103.                         GKGfxImage_Geometry, 0, 0, 1024, 768,
  104.                         NULL);
  105.     }
  106.  
  107.     /* Create the number of sprites to run around   */
  108.     for (i=0; i < sprites; i++) {
  109.         gfximage[i]=gkCreate(gfxcont, GKGfxImageClass,
  110.                             GKGfxImage_ImageObj, images[0][0],
  111.                             GKGfxImage_TransparentColor, "#ffffff",
  112.                             NULL);
  113.     }
  114.  
  115.     /* Sprites will pass behind this image  */
  116.     gkCreate(gfxcont, GKGfxImageClass,
  117.              GKGfxImage_ImageFile, "fgimg.bmp",
  118.              GKGfxImage_Origin, gkXHMM(3 * GKCV_CM), gkYHMM(3 * GKCV_CM),
  119.              GKGfxImage_TransparentColor, "White",
  120.              NULL);
  121.  
  122.     frame=gkCreate(NULL, GKFrameClass,
  123.                    GKFrame_Title, "Animation Demo",
  124.                    GKFrame_Icon, gfxcont,
  125.                    GKWindow_BackgroundGfxObj, gfxcont,
  126.                    GKWindow_Width, gkWidthHMM(8 * GKCV_CM),
  127.                    GKWindow_Height, gkHeightHMM(8 * GKCV_CM),
  128.                    GKWindow_DoubleBuffer, GKTRUE,
  129.                    NULL);
  130.  
  131.     gkCreate(NULL, GKTimerClass,
  132.              GKTimer_Interval, 10,
  133.              GKTimer_NotifyProc, (GKTimerNotifyProc)TimerNotify,
  134.              NULL);
  135.  
  136.     gkDispatch();
  137.  
  138.     /* Destroy images   */
  139.     for (i=0; i < 4; i++) {
  140.         for (j=0; j < 6; ++j) {
  141.             gkSet(images[i][j],
  142.                   GKIR_DereferenceObj,
  143.                   NULL);
  144.             gkDestroy(images[i][j]);
  145.         }
  146.     }
  147.  
  148.     return(0);
  149. }
  150.