home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 286.lha / glow.c < prev    next >
C/C++ Source or Header  |  1989-09-08  |  6KB  |  207 lines

  1. /*---------------------------------------------------------------*
  2.  *    glow.c                            08/31/89    Keith Young
  3.  *
  4.  *    This program shows an example 'C' VBlank Interrupt Server.  
  5.  *
  6.  *    In this example, the interrupt routine cycles a color register,
  7.  *    but there are any number of other useful things it could do every
  8.  *    time the screen is drawn ;-)
  9.  *
  10.  *    I wrote it not long ago (using information/code from various
  11.  *    sources) and after a recent query about Interrupts on CIS, I
  12.  *    decided to go ahead and post it. 
  13.  *    
  14.  *    I hereby place this code into the PUBLIC DOMAIN.  You may do
  15.  *    with this code, anything you wish (whatever floats your boat).
  16.  *
  17.  *    Just keep in mind....
  18.  *        a) this was a quick hack
  19.  *        b) It is only meant as an example.
  20.  *        c) I disclaim any and all responsibility for any ill-effects...
  21.  * 
  22.  *    This code was written using Manx 3.6a (where's that new
  23.  *    version... ??)
  24.  *
  25.  *  compile:    1> cc glow
  26.  *     link:    1> ln glow -lc
  27.  *
  28.  *    (if you get any warnings, you need to fix your functions.h
  29.  *    file <grin> )
  30.  *--------------------------------------------------------------*/
  31.  
  32. #include <intuition/intuitionbase.h>
  33. #include <graphics/gfxbase.h>
  34. #include <exec/interrupts.h>
  35. #include <exec/memory.h>
  36. #include <functions.h>
  37.  
  38. struct IntuitionBase    *IntuitionBase;
  39. struct GfxBase            *GfxBase;
  40.  
  41. struct ViewPort            *VPort;            /* ViewPort for color changes */
  42. struct Interrupt        VBlankServer;    /* our Interrupt Server */
  43.  
  44. unsigned long lag    = 1L;        /* lag time between color changes */
  45. unsigned long count = 0L;        /* counter used with lag (above) */
  46. unsigned long creg    = 0L;        /* color register to change */
  47. unsigned long hue     = 0L;        /* new color value */
  48. unsigned long add    = 1L;        /* adding to color? or subracting? */
  49.  
  50. /*---------------------------------------------------------------*
  51.  * GlowVBlank()
  52.  *
  53.  *    This is an example 'C' VBlank Interrupt routine. It first
  54.  * gets the registers setup correctly by calling the Manx supplied
  55.  * interrupt support routine 'int_start()'.  It then does it's thing
  56.  * (notice that it has access to the program's global variables) and
  57.  * resets the registers with 'int_end()'.
  58.  *
  59.  * Remember, keep it short, this is C, and gets called (roughly) 60
  60.  * times a second <grin>.
  61.  *--------------------------------------------------------------*/
  62. void
  63. GlowVBlank()
  64. {
  65.     int_start();                /* Manx Interrupt setup routine */
  66.  
  67.     if( lag )                    /* don't want to "divide by zero" */
  68.         count = ++count % lag;
  69.  
  70.     if( !count ) {                /* cycle color when counter wraps */
  71.         if( add ) {                /* adding color ? */
  72.             ++hue;
  73.             if( hue > 11 )
  74.                 add = 0;
  75.         }
  76.         else {                    /* else subracting */
  77.             --hue;
  78.             if( !hue )
  79.                 add = 1;
  80.         }
  81.         SetRGB4(VPort, creg, 0L, hue+2L, hue);    /* set the new color */
  82.     }
  83.  
  84.     int_end();                    /* Manx Interrupt cleanup routine */
  85. }
  86.  
  87. /*---------------------------------------------------------------*
  88.  *    AddVBlankServer()
  89.  *
  90.  *    Fill in the VBlankServer structure, then add the server
  91.  *--------------------------------------------------------------*/
  92. void
  93. AddVBlankServer()
  94. {
  95.     VBlankServer.is_Data        = (APTR)NULL;
  96.     VBlankServer.is_Code        = GlowVBlank;    /* pointer to our C routine */
  97.     VBlankServer.is_Node.ln_Succ= NULL;
  98.     VBlankServer.is_Node.ln_Pred= NULL;
  99.     VBlankServer.is_Node.ln_Type= NT_INTERRUPT;
  100.     VBlankServer.is_Node.ln_Pri    = 0;
  101.     VBlankServer.is_Node.ln_Name= "glow";
  102.  
  103.     AddIntServer(5L, &VBlankServer);
  104. }
  105.  
  106. /*---------------------------------------------------------------*
  107.  *    RemVBlankServer()
  108.  *
  109.  *    Remove the Interrupt Server
  110.  *--------------------------------------------------------------*/
  111. void
  112. RemVBlankServer()
  113. {
  114.     RemIntServer(5L, &VBlankServer);
  115. }
  116.  
  117. /*---------------------------------------------------------------*
  118.  * main()
  119.  *
  120.  *    You know, where the program starts...
  121.  * command line options:
  122.  *
  123.  *        1> glow [color register] [delay]
  124.  *
  125.  *        Where:
  126.  *    [color register] =    a value from 0 to 32 (but heck, I don't
  127.  *                        actually check it) that tells the program
  128.  *                        which register to cycle.
  129.  *    [delay]          =    is a value used to slow things down...
  130.  *                        0 = full speed, larger numbers = slower.
  131.  *--------------------------------------------------------------*/
  132. main(argc, argv)
  133. int argc;
  134. char *argv[];
  135. {
  136. extern unsigned long atol();
  137. register int i, j;
  138.  
  139.     if( argv[1] )            /* first arg = color register to cycle */
  140.         creg = atol( argv[1] );
  141.  
  142.     if( argv[2] )            /* second arg = delay value, bigger = slower */
  143.         lag = atol(argv[2]);
  144.  
  145.     /* extern void *OpenLibrary(); */
  146.     if(!(IntuitionBase = OpenLibrary("intuition.library",LIBRARY_VERSION)))
  147.         adios( 10, "No Intuition?" );
  148.  
  149.     if(!(GfxBase = OpenLibrary("graphics.library",LIBRARY_VERSION)))
  150.         adios( 20, "No GfxBase" );
  151.  
  152.     VPort = &IntuitionBase->FirstScreen->ViewPort;    /* get ViewPort ptr */
  153.  
  154.     AddVBlankServer();        /* add interrupt server */
  155.  
  156.     Delay( 1000L );            /* wait a while... */
  157.  
  158.     RemVBlankServer();        /* remove the server */
  159.  
  160.     Chk_Abort();            /* did the user hit ^C ? */
  161.  
  162.     adios( 0, "Done." );
  163. }
  164.  
  165. /*---------------------------------------------------------------*
  166.  * adios()
  167.  *
  168.  *    print msg to console, free anything allocated, close anything
  169.  * opened and exit() with supplied return code.
  170.  *--------------------------------------------------------------*/
  171. adios( rc, msg )
  172. int rc;
  173. char *msg;
  174. {
  175.     if( msg )
  176.         puts( msg );
  177.  
  178.     if(IntuitionBase)
  179.         CloseLibrary(IntuitionBase);
  180.  
  181.     if(GfxBase)
  182.         CloseLibrary(GfxBase);
  183.  
  184.     exit( rc );
  185. }
  186.  
  187. /*---------------------------------------------------------------*
  188.  * _abort()
  189.  *
  190.  *    This over-rides the Manx _abort() function that is
  191.  * called from Chk_Abort() when the user presses ^C in the CLI
  192.  * after starting the program, it calls our own clean-up routine.
  193.  *
  194.  *    This is needed to keep the program from terminating before
  195.  * we remove the interrupt server... otherwise, the interrupt
  196.  * server chain will get badly munged when it tries to execute
  197.  * code that is no longer there :).
  198.  *
  199.  *    As a side-note, it's a good idea to always include such a
  200.  * replacement _abort() routine, so that you can do your normal
  201.  * clean-up before exiting.
  202.  *--------------------------------------------------------------*/
  203. _abort()
  204. {
  205.     adios( 63, "** BREAK" );
  206. }
  207.