home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / util / blank / gblanker / source / blankers / worms / blank.c next >
C/C++ Source or Header  |  1994-10-18  |  3KB  |  166 lines

  1. /*
  2.  *    Copyright (c) 1994 Michael D. Bayne.
  3.  *    All rights reserved.
  4.  *
  5.  *    Please see the documentation accompanying the distribution for distribution
  6.  *  and disclaimer information.
  7.  */
  8.  
  9. #include <exec/memory.h>
  10. #include <graphics/gfxmacros.h>
  11. #include <math.h>
  12.  
  13. #include <clib/exec_protos.h>
  14. #include <clib/dos_protos.h>
  15. #include <clib/intuition_protos.h>
  16. #include <clib/graphics_protos.h>
  17. #include <clib/alib_protos.h>
  18.  
  19. #include "/Garshnelib/Garshnelib_protos.h"
  20. #include "/Garshnelib/Garshnelib_pragmas.h"
  21.  
  22. #include "Worms.h"
  23. #include "//defs.h"
  24. #include "/main.h"
  25.  
  26. struct ModulePrefs
  27. {
  28.     LONG Number;
  29.     LONG Length;
  30.     LONG FadePct;
  31.     LONG TopScr;
  32. };
  33.  
  34. typedef struct _Worm
  35. {
  36.     LONG Current;
  37.     LONG X[25];
  38.     LONG Y[25];
  39.     LONG Length;
  40.     LONG Color;
  41.     double Direction;
  42. } Worm;
  43.  
  44. #define ExecBase ( *(( struct Library ** )4L ))
  45.  
  46. extern struct ModulePrefs nP;
  47. LONG BGColor;
  48.  
  49. VOID IterateWorm( struct Screen *Scr, Worm *Jim )
  50. {
  51.     LONG x, y, xp, yp;
  52.     double prev;
  53.     struct RastPort *Rast = &Scr->RastPort;
  54.  
  55.     xp = Jim->X[Jim->Current];
  56.     yp = Jim->Y[Jim->Current];
  57.     prev = Jim->Direction;
  58.     
  59.     Jim->Current = ( Jim->Current + 1 ) % Jim->Length;
  60.  
  61.     x = Jim->X[Jim->Current];
  62.     y = Jim->Y[Jim->Current];
  63.  
  64.     SetAPen( Rast, BGColor );
  65.     RectFill( Rast, x-3, y-3, x+2, y+2 );
  66.  
  67.     Jim->Direction = prev + (( double )RangeRand( 100 ) / 100.0 - 0.5 ) * PID2;
  68.     x = xp + ( LONG )( 5.0 * cos( Jim->Direction ));
  69.     y = yp + ( LONG )( 5.0 * sin( Jim->Direction ));
  70.     if(( x < 4 )||( x > Scr->Width-4 ))
  71.         x = xp;
  72.     if(( y < 4 )||( y > Scr->Height-4 ))
  73.         y = yp;
  74.     
  75.     SetAPen( Rast, Jim->Color );
  76.     RectFill( Rast, x-4, y-4, x+3, y+3 );
  77.  
  78.     Jim->X[Jim->Current] = x;
  79.     Jim->Y[Jim->Current] = y;
  80. }
  81.                  
  82. LONG Blank( VOID *Prefs )
  83. {
  84.     LONG i, j, nw, RetVal = OK, Colors, ToFrontCount = 0;
  85.     struct ModulePrefs *wP;
  86.     struct Screen *Scr;
  87.     struct Window *Wnd;
  88.     Worm *Worms;
  89.     
  90.     if( WormsWnd )
  91.         wP = &nP;
  92.     else
  93.         wP = ( struct ModulePrefs * )Prefs;
  94.     
  95.     nw = wP->Number;
  96.  
  97.     Scr = cloneTopScreen( FALSE, wP->TopScr );
  98.     Worms = AllocVec( sizeof( Worm ) * nw, MEMF_CLEAR );
  99.     
  100.     if( Scr && Worms )
  101.     {
  102.         Colors = 1L << Scr->RastPort.BitMap->Depth;
  103.  
  104.         if( wP->FadePct )
  105.         {
  106.             ULONG *ColorTable, PctCount, BPG;
  107.  
  108.             ColorTable = GetColorTable( Scr );
  109.             BPG = AvgBitsPerGun( getTopScreenMode());
  110.             PctCount = ( 1L << BPG ) * wP->FadePct / 100;
  111.             for( i = 0; i < PctCount; i++ )
  112.                 FadeAndLoadTable( Scr, BPG, ColorTable );
  113.         }
  114.         
  115.         if( ExecBase->lib_Version < 39 )
  116.         {
  117.             BGColor = 0;
  118.             SetOPen(&( Scr->RastPort ), BGColor );
  119.         }
  120.         else
  121.         {
  122.             BGColor = FindColor( Scr->ViewPort.ColorMap, 0, 0, 0, -1 );
  123.             SetOutlinePen(&( Scr->RastPort ), BGColor );
  124.         }
  125.         
  126.         for( i = 0; i < nw; i++ )
  127.         {
  128.             Worms[i].Color = RangeRand( Colors - 1 ) + 1;
  129.             if( Worms[i].Color == BGColor )
  130.                 Worms[i].Color = ( Worms[i].Color + 1 ) % Colors;
  131.             Worms[i].Length = wP->Length;
  132.             Worms[i].Direction = ( double )RangeRand( 360 );
  133.             for( j = 0; j < Worms[i].Length; j++ )
  134.             {
  135.                 Worms[i].X[j] = Scr->Width/2;
  136.                 Worms[i].Y[j] = Scr->Height/2;
  137.             }
  138.         }
  139.         
  140.         Wnd = BlankMousePointer( Scr );
  141.         
  142.         while( RetVal == OK )
  143.         {
  144.             for( i = 0; i < nw; i++ )
  145.                 IterateWorm( Scr, &( Worms[i] ));
  146.  
  147.             if(!( ++ToFrontCount % 60 ))
  148.                 ScreenToFront( Scr );
  149.             
  150.             RetVal = ContinueBlanking();
  151.             WaitTOF();
  152.         }
  153.         
  154.         UnblankMousePointer( Wnd );
  155.     }
  156.     else
  157.         RetVal = FAILED;
  158.         
  159.     if( Worms )
  160.         FreeVec( Worms );
  161.     if( Scr )
  162.         CloseScreen( Scr );
  163.  
  164.     return RetVal;
  165. }
  166.