home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff269.lzh / SpinPointer / SpinPointer.c < prev    next >
C/C++ Source or Header  |  1989-11-06  |  6KB  |  288 lines

  1. /* Spinning Pointer - Great as a "busy" indicator.
  2.  * Author:        Mark R. Rinfret
  3.  * Date:        02/10/88
  4.  * Description:
  5.  *        The SpinPointer function allows you to indicate a task's
  6.  *        relative "busy-ness" (sic) by creating the illusion of a
  7.  *      spinning disk, using the mouse pointer.  This idea was 
  8.  *        copied from an "other brand" computer that I use at work.
  9.  *        The idea is to embed a call of the form
  10.  *
  11.  *            SpinPointer(window, 1);     |* clockwise *|
  12.  *                or 
  13.  *            SpinPointer(window, -1);    |* counter clockwise *|
  14.  *
  15.  *        in a processing loop.  Care should be chosen in the placement
  16.  *        of the call so that you don't introduce undue overhead.  At the
  17.  *        same time, you want some amount of activity or the illusion of
  18.  *        rotation won't be effective.  To reset the mouse pointer to its
  19.  *        original condition, call SpinPointer with a value of 0:
  20.  *            SpinPointer(window, 0);
  21.  *
  22.  *        I invite others to design a better representation (spiral disk? 
  23.  *        with more phases?).  That is simply a matter of generating new 
  24.  *        sprite image data.
  25.  */
  26.  
  27. /* Define DEBUG if you want to compile and link the demo version. */
  28.  
  29. #define DEBUG
  30.  
  31. #include <stdio.h>
  32. #include <exec/memory.h>
  33.  
  34. void *AllocMem();
  35. #define USHORT unsigned short
  36.  
  37. /* The ambitious may want to use SetRGB4 with the following:
  38.  * static USHORT sprite_colors[3] = { 0x0d22, 0x0000, 0x0fca }; 
  39.  */
  40.  
  41. typedef struct sprite {                /* Give it some structure. */
  42.     USHORT spriteData[36]; 
  43.     } Sprite;
  44.  
  45. #define SPRITECOUNT        4
  46.  
  47. static Sprite spinSprites[SPRITECOUNT] = {    /* # phases of rotation */
  48.      {
  49.         0x0000, 0x0000,
  50.         0x0000, 0x0000,
  51.         0x0000, 0x0000,
  52.         0x0000, 0x0000,
  53.         0x0ff0, 0x00f0,
  54.         0x1ff8, 0x00f8,
  55.         0x3fcc, 0x00fc,
  56.         0x7fce, 0x00fe,
  57.         0x7ffe, 0x00fe,
  58.         0x7ffe, 0x00fe,
  59.         0x7ffe, 0x7f00,
  60.         0x7ffe, 0x7f00,
  61.         0x7ffe, 0x7f00,
  62.         0x3ffc, 0x3f00,
  63.         0x1ff8, 0x1f00,
  64.         0x0ff0, 0x0f00,
  65.         0x0000, 0x0000,
  66.         0x0000, 0x0000
  67.     }, 
  68.  
  69.  
  70.     {
  71.         0x0000, 0x0000,
  72.         0x0000, 0x0000,
  73.         0x0000, 0x0000,
  74.         0x0000, 0x0000,
  75.         0x0ff0, 0x0f00,
  76.         0x1ff8, 0x1f00,
  77.         0x3ffc, 0x3f00,
  78.         0x7ffe, 0x7f00,
  79.         0x7ffe, 0x7f00,
  80.         0x7ffe, 0x7f00,
  81.         0x7ffe, 0x00fe,
  82.         0x7ffe, 0x00fe,
  83.         0x7fce, 0x00fe,
  84.         0x3fcc, 0x00fc,
  85.         0x1ff8, 0x00f8,
  86.         0x0ff0, 0x00f0,
  87.         0x0000, 0x0000,
  88.         0x0000, 0x0000
  89.     },
  90.  
  91.  
  92.     {
  93.         0x0000, 0x0000,
  94.         0x0000, 0x0000,
  95.         0x0000, 0x0000,
  96.         0x0000, 0x0000,
  97.         0x0ff0, 0x00f0,
  98.         0x1ff8, 0x00f8,
  99.         0x3ffc, 0x00fc,
  100.         0x7ffe, 0x00fe,
  101.         0x7ffe, 0x00fe,
  102.         0x7ffe, 0x00fe,
  103.         0x7ffe, 0x7f00,
  104.         0x7ffe, 0x7f00,
  105.         0x73fe, 0x7f00,
  106.         0x33fc, 0x3f00,
  107.         0x1ff8, 0x1f00,
  108.         0x0ff0, 0x0f00,
  109.         0x0000, 0x0000,
  110.         0x0000, 0x0000
  111.     },
  112.  
  113.     {
  114.         0x0000, 0x0000,
  115.         0x0000, 0x0000,
  116.         0x0000, 0x0000,
  117.         0x0000, 0x0000,
  118.         0x0ff0, 0x0f00,
  119.         0x1ff8, 0x1f00,
  120.         0x33fc, 0x3f00,
  121.         0x73fe, 0x7f00,
  122.         0x7ffe, 0x7f00,
  123.         0x7ffe, 0x7f00,
  124.         0x7ffe, 0x00fe,
  125.         0x7ffe, 0x00fe,
  126.         0x7ffe, 0x00fe,
  127.         0x3ffc, 0x00fc,
  128.         0x1ff8, 0x00f8,
  129.         0x0ff0, 0x00f0,
  130.         0x0000, 0x0000,
  131.         0x0000, 0x0000
  132.     }
  133. };
  134.  
  135. /* Set up, disable or spin the pointer.
  136.  * Called with:
  137.  *        w:        window in which to spin the pointer
  138.  *        code:    controls behavior of pointer as follows -
  139.  *              < 0 => Spin pointer backward
  140.  *              > 0 => Spin pointer forward
  141.  *                  0 => revert to original pointer
  142.  * Returns:
  143.  *        0    => successful call
  144.  *        1    => Oops!
  145.  */
  146.  
  147. #define IMAGESIZE    SPRITECOUNT * sizeof(Sprite)
  148.  
  149. static Sprite *images;
  150. static int spriteIndex;
  151.  
  152. int
  153. SpinPointer(w, value)
  154.     struct Window *w; int value;
  155. {
  156.  
  157.     if (value == 0) {
  158.         ClearPointer();
  159.         if (images) FreeMem(images, (long) IMAGESIZE);
  160.         images = NULL;
  161.     }
  162.     else {
  163.         if (!images) {        /* Must allocate image area in CHIP ram. */
  164.             if (! (images = AllocMem((long) IMAGESIZE, MEMF_CHIP) ) ) 
  165.                 return 1;
  166.  
  167.             movmem(&spinSprites, images, IMAGESIZE);
  168.             spriteIndex = 0;
  169.         }
  170.         else
  171.             ClearPointer();
  172.         if (value < 0) {
  173.             if (--spriteIndex < 0) spriteIndex = 3;
  174.         }
  175.         else
  176.             if (++spriteIndex >= SPRITECOUNT) spriteIndex = 0;
  177.  
  178.         /* If you change the sprite's dimensions, make sure you
  179.          * change the constants in this call.
  180.          */
  181.         SetPointer(w, &images[spriteIndex], 16L, 16L, -7L, -7L);
  182.     }
  183.     return 0;
  184. }
  185.  
  186.  
  187. #ifdef DEBUG
  188. #include <intuition/intuition.h>
  189.  
  190. void *OpenLibrary();
  191. struct Window * OpenWindow();
  192.  
  193.  
  194. /* Intuition handler for DEBUG mode. */
  195.  
  196. struct Window *testWindow;
  197.  
  198. /* New window structure */
  199.  
  200. struct NewWindow    nw = {
  201.     0,0,640,200,0,1,
  202.  
  203. /* IDCMP Flags */
  204.  
  205.     NULL,    
  206.  
  207. /* Flags */
  208.     WINDOWSIZING | WINDOWDRAG | WINDOWDEPTH,
  209.  
  210.     NULL,                            /* First gadget */
  211.     NULL,                            /* Checkmark */
  212.     (UBYTE *)"SpinPointer Demo - Click in this window",    /* Window title */
  213.     NULL,                            /* No custom streen */
  214.     NULL,                            /* Not a super bitmap window */
  215.     100,60,                            /* MinWidth, MinHeight */
  216.     640,200,                        /* Not used, but set up anyway */
  217.     WBENCHSCREEN
  218. };
  219.  
  220.  
  221. struct IntuitionBase * IntuitionBase;
  222. struct GfxBase *GfxBase;
  223.  
  224. CloseIntuition()
  225. {
  226.     if ( IntuitionBase ) {
  227.         if ( testWindow ) {
  228.             ClearMenuStrip( testWindow );
  229.             CloseWindow( testWindow );
  230.         }
  231.         if ( GfxBase ) CloseLibrary( GfxBase );
  232.         CloseLibrary( IntuitionBase );
  233.     }                        /* end if IntuitionBase */
  234. }
  235.  
  236. int
  237. InitIntuition()
  238. {
  239.  
  240.     IntuitionBase = (struct IntuitionBase *)
  241.         OpenLibrary("intuition.library", 0L);
  242.     if ( ! IntuitionBase ) {
  243.         puts("I can't open Intuition!\n");
  244.         return 1;
  245.     }
  246.  
  247.     GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 0L);
  248.     if ( ! GfxBase ) {
  249.         puts("I can't open the graphics library!\n");
  250.         return 1;
  251.     }
  252.  
  253.     testWindow = OpenWindow(&nw);    /* open the window */
  254.     if ( testWindow == NULL ) {
  255.         puts("I can't open the main window!\n");
  256.         return 1;
  257.     }
  258.  
  259.     return 0;
  260. }
  261.  
  262. main()
  263. {
  264.     unsigned delay;
  265.     long delayCount = 10L;
  266.  
  267.     if (InitIntuition()) {
  268.         puts("Failed to initialize Intuition stuff.");
  269.         exit(1);
  270.     }
  271.     while (delayCount > 1) {
  272.         for (delay = 0; delay < 24; ++delay) {
  273.             if (SpinPointer(testWindow, 1)) {
  274.                 puts("SpinPointer() call failed!");
  275.                 goto done;
  276.                 break;    
  277.             }
  278.             Delay(delayCount);
  279.         }
  280.         --delayCount; 
  281.     }
  282.     SpinPointer(testWindow, 0);
  283. done:
  284.     CloseIntuition();
  285. }
  286. #endif
  287. /* EOF */
  288.