home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d9xx / d952 / machv.lha / MachV / Blankers / Lissajous.c < prev    next >
C/C++ Source or Header  |  1993-12-31  |  8KB  |  295 lines

  1. #ifdef AZTEC_C
  2. #include <functions.h>
  3. #else
  4. #include <clib/exec_protos.h>
  5. #include <clib/graphics_protos.h>
  6. #include <clib/intuition_protos.h>
  7. #endif
  8.  
  9. #include <stdlib.h>
  10. #include <exec/ports.h>
  11. #include <dos/dos.h>
  12. #include <math.h>
  13. #include <intuition/intuition.h>
  14. #include <intuition/intuitionbase.h>
  15.  
  16. /* Example of using simpleblanker. This program simply draws lines using
  17.  * equations based on Lissajous. It is not the greatest screen blanker since
  18.  * the lines are on screen for awhile. It is painfully slow on a stock
  19.  * Amiga with no 68881 or 68882. After the lines are drawn, the pattern is
  20.  * repeated using only the background color to erase.
  21.  */
  22.  
  23. /*
  24.  * MachIV will execute a macro named "Blank" when the timeout says to blank
  25.  * the screen. It also executes a macro named "Unblank" when screen blanking
  26.  * should terminate and a macro named "MachIVQuit" when MachIV is quiting.
  27.  * These macros could anything, but to communicate with MachIV, they would
  28.  * be something like this:
  29.  *
  30.  * MSC_PUTMSG"MyScreenBlanker,1"     - Name this macro Blank.
  31.  * MSC_PUTMSG"MyScreenBlanker,2"     - Name this macro Unblank.
  32.  * MSC_PUTMSG"MyScreenBlanker,3"     - Name this macro MachIVQuit.
  33.  *
  34.  * SimpleBlanker waits on its port named "MyScreenBlanker" for one of three
  35.  * signals. When it receives a BLANK (1) signal, it loops doing whatever
  36.  * graphics on the screen that you program in. In this loop it also checks
  37.  * for an UNBLANK (2), QUIT (3) or ctrl-c signal. Messages sent are actually
  38.  * IntuiMessages structs but only Class, Code and Qualifier are filled in by
  39.  * MachIV. Only Class is used in this program.
  40.  *
  41.  * Usage:  Run <nil: >nil: lissajous
  42.  *
  43.  * SimpleBlanker also quits when you execute it a second time or send it
  44.  * a Ctrl-C via the "break" command or some similar program.
  45.  *
  46.  * Compiles under Aztec 5.2a and SAS/C 5.10a.
  47.  *
  48.  * Aztec:
  49.  *
  50.  *     cc SimpleBlanker -f8
  51.  *     ln SimpleBlanker.o -lm8 -lc
  52.  *
  53.  *     or without a floating point chip (68881 or 68882)
  54.  *
  55.  *     cc SimpleBlanker
  56.  *     ln SimpleBlanker.o -lm -lc
  57.  *
  58.  *     or with or without a 68881/68882
  59.  *
  60.  *     cc SimpleBlanker -fa
  61.  *     ln SimpleBlanker.o -lma -lc
  62.  *
  63.  *
  64.  * Lattice:
  65.  *
  66.  *     lc  -f8 -Lm SimpleBlanker
  67.  *
  68.  *     or without a 68881/68882
  69.  *
  70.  *     lc  -fi -Lm SimpleBlanker
  71.  *
  72.  *     or with or without a 68881/68882
  73.  *
  74.  *     lc  -fl -Lm SimpleBlanker
  75.  *
  76.  * Again, the program is terribly slow without at least a 14Mhz 68020.
  77.  * A 68881 or 68882 is even better.
  78.  *
  79.  * The supplied program was compiled with Aztec using -fa and -lma so it
  80.  * will run with or without a coprocessor. It does require
  81.  * mathieeedoubbas.library in the libs: directory.
  82.  *
  83.  * Both compilers allow the // comment and I've used it here. If you are
  84.  * using an older compiler, you will need to change them to the standard
  85.  * comment form which I can't even show you here or it would be a nested
  86.  * comment!
  87.  */
  88.  
  89. // Signals sent from MachIV to your port.
  90.  
  91. #define BLANK   1
  92. #define UNBLANK 2
  93. #define QUIT    3
  94.  
  95. // Screen blanking constants.
  96.  
  97. #define SCREEN_DEPTH    4
  98. #define SCREEN_WIDTH  640
  99. #define SCREEN_HEIGHT 400
  100. #define XCENTER       SCREEN_WIDTH / 2
  101. #define YCENTER       SCREEN_HEIGHT / 2
  102. #define XRADIUS       270
  103. #define YRADIUS       170
  104. #ifndef PI
  105. #define PI            3.14159
  106. #endif
  107. #define DEGREE        PI / 180
  108.  
  109. // Proto declaration.
  110.  
  111. void main(void);
  112. static void drawlissa(void);
  113. static void die(void);
  114.  
  115. // Global Variables
  116.  
  117. int Enable_Abort=0;
  118.  
  119. struct IntuitionBase *IntuitionBase;
  120. struct GfxBase *GfxBase;
  121.  
  122. char *portname = "MyScreenBlanker";
  123.  
  124. struct NewScreen newScreen = {0,0,SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_DEPTH,
  125.                               -1,-1,LACE | HIRES,CUSTOMSCREEN,NULL,NULL,NULL,NULL};
  126. struct Screen *s;
  127.  
  128. UWORD CMap[16] = {    /* Colors */
  129.  0x000,0xF00,0xF02,0xE04,0xE06,0xD08,0xD09,0xD0B,
  130.  0xC0C,0xA0C,0x80C,0x60B,0x40B,0x30B,0x10A,0x00A
  131. };
  132.  
  133. struct MsgPort      *portptr,*rp;
  134. struct IntuiMessage *imsg, quitmsg;
  135.  
  136. ULONG mode,action,sigmask,signals;
  137. BOOL  wipe;
  138.  
  139. double a,b,t,at,bt,sat,x,y,z;
  140. ULONG  z2;
  141.  
  142. void main(void)
  143. {
  144.   // First see if we are already running. If so, Put a message telling it
  145.   // to quit. Wait for SimpleBlanker to Reply.
  146.  
  147.   if ((portptr = FindPort((STRPTR)portname)) != NULL) {
  148.     if (rp = CreateMsgPort()) {   // Create a reply port for us.
  149.       quitmsg.ExecMessage.mn_Node.ln_Type = NT_MESSAGE;
  150.       quitmsg.ExecMessage.mn_ReplyPort = rp;
  151.       quitmsg.ExecMessage.mn_Length = 32;
  152.       quitmsg.Class = QUIT;
  153.       PutMsg(portptr,(struct Message*)&quitmsg);
  154.       WaitPort(rp); // Wait for SimpleBlanker to do a ReplyMsg();
  155.       GetMsg(rp);
  156.       DeleteMsgPort(rp);
  157.       exit(0);
  158.     }
  159.   }
  160.   // Port not found so create and add our port.
  161.  
  162.   if ((portptr = CreateMsgPort()) == NULL)
  163.     exit(0);
  164.  
  165.   portptr->mp_Node.ln_Pri = 1;         // Will be searched for.
  166.   portptr->mp_Node.ln_Name = portname;
  167.  
  168.   AddPort(portptr);
  169.   sigmask = (1 << portptr->mp_SigBit) | SIGBREAKF_CTRL_C;
  170.  
  171.   IntuitionBase = (struct IntuitionBase *) OpenLibrary((UBYTE*)"intuition.library",0L);
  172.   GfxBase = (struct GfxBase *) OpenLibrary((UBYTE*)"graphics.library",0L);
  173.  
  174.   for (;;) {
  175.     signals = Wait(sigmask); // Waiting for BLANK or QUIT or SIGBREAKF_CTRL_C
  176.  
  177.     if (signals & SIGBREAKF_CTRL_C)
  178.       die();
  179.  
  180.     while (imsg = (struct IntuiMessage *)GetMsg(portptr)) {
  181.       action = imsg->Class;
  182.       ReplyMsg((struct Message *)imsg);
  183.  
  184.       switch (action) {
  185.         case BLANK:
  186.           if ((s = OpenScreen(&newScreen)) == NULL) {
  187.             break;
  188.           }
  189.           LoadRGB4(&s->ViewPort,CMap,16L);
  190.           srand(IntuitionBase->Micros);
  191.  
  192.           wipe = 1;
  193.           while ((imsg = (struct IntuiMessage *)GetMsg(portptr)) == NULL) {
  194.             Move(&s->RastPort,XCENTER,YCENTER);
  195.             a = b = t = 1;
  196.             if (wipe) {
  197.               wipe = 0;
  198.               SetRast(&s->RastPort,0L);
  199.               mode = (rand() % 5) + 1;
  200.             }
  201.             else {
  202.               wipe = 1;
  203.               SetAPen(&s->RastPort,0L);
  204.             }
  205.             drawlissa();
  206.           }
  207.           // Got a message.
  208.           action = imsg->Class;
  209.           ReplyMsg((struct Message *)imsg);
  210.           if (action == QUIT)
  211.             die();      // Will never return.
  212.           else if (action != UNBLANK)
  213.             break;                   // UNBLANK falls through!!
  214.         case UNBLANK:
  215.           if (s)
  216.             CloseScreen(s);
  217.           s = NULL;
  218.           break;
  219.         case QUIT:
  220.           die();
  221.           break;
  222.       }
  223.     }
  224.   }
  225. }
  226.  
  227. static void drawlissa(void)
  228. {
  229.   WORD i;
  230.  
  231.   for (i=0;i<1500;i++) {
  232.     at = a * t;
  233.     bt = b * t;
  234.     sat = sin(at);
  235.     x=XRADIUS*sat*cos(bt);
  236.     y=YRADIUS*sat*sin(bt);
  237.  
  238.     z=YRADIUS*cos(at);
  239.     z2=z*.1;
  240.  
  241.     switch (mode) {
  242.       case 1:
  243.         t = t + DEGREE;
  244.         a=a+.003;
  245.         b=b- .003;
  246.         break;
  247.       case 2:
  248.         t=t + DEGREE + .01;
  249.         a=a+.2;
  250.         b=b-.2;
  251.         break;
  252.       case 3:
  253.         t = t + DEGREE + .02;
  254.         a=a+.01;
  255.         b=b-.01;
  256.         break;
  257.       case 4:
  258.         t = t + 4;
  259.         a=a+.0005;
  260.         b=b-.0005;
  261.         break;
  262.       case 5:
  263.         t = t + .09;
  264.         a=a+.0001;
  265.         b=b-.0001;
  266.         break;
  267.     }
  268.     if (!wipe && ((i % 50) == 0))     // change colors every 50 times
  269.       SetAPen(&s->RastPort,(z2 % 15)+1);
  270.  
  271.     Draw(&s->RastPort,(long)(z+x+XCENTER),(long)(y+YCENTER));
  272.  
  273.     if (SetSignal(0L,0L) & sigmask) // check for any signals
  274.       break;
  275.   }
  276. }
  277.  
  278. // This deletes and closes up everything.
  279.  
  280. static void die(void)
  281. {
  282.   RemPort(portptr);  // portptr must be valid or we wouldn't be here.
  283.  
  284.   DeleteMsgPort(portptr);
  285.  
  286.   if (s)
  287.     CloseScreen(s);
  288.  
  289.   if (IntuitionBase)
  290.     CloseLibrary((struct Library*)IntuitionBase);
  291.   if (GfxBase)
  292.     CloseLibrary((struct Library*)GfxBase);
  293.   exit(0);
  294. }
  295.