home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / util / blank / gblanker / source / blankers / flyingtoaster / blank.c next >
C/C++ Source or Header  |  1994-10-08  |  8KB  |  313 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/types.h>
  10. #include <exec/memory.h>
  11.  
  12. #include <intuition/intuition.h>
  13. #include <intuition/intuitionbase.h>
  14. #include <intuition/screens.h>
  15.  
  16. #include <graphics/gfxbase.h>
  17. #include <dos/dos.h>
  18.  
  19. #include <clib/exec_protos.h>
  20. #include <clib/dos_protos.h>
  21. #include <clib/intuition_protos.h>
  22. #include <clib/graphics_protos.h>
  23. #include <clib/alib_protos.h>
  24.  
  25. #include "/Garshnelib/Garshnelib_protos.h"
  26. #include "/Garshnelib/Garshnelib_pragmas.h"
  27.  
  28. #define    IM_WIDTH  64
  29. #define    IM_HEIGHT 64
  30. #define IM_WIDBUF 84
  31. #define IM_HEIBUF 84
  32.  
  33. #include "FlyingToaster.h"
  34. #include "FlyingToaster_rev.h"
  35. #include "/main.h"
  36. #include "//defs.h"
  37.  
  38. struct ModulePrefs
  39. {
  40.     LONG Mode;
  41.     LONG Objects;
  42.     LONG Speed;
  43. };
  44.  
  45. typedef struct _Toaster
  46. {
  47.     LONG delay;
  48.     LONG x, y;
  49.     LONG old_x, old_y;
  50.     LONG xspeed, yspeed;
  51.     LONG phase;
  52.     LONG xcol, ycol;
  53. } Toaster;
  54.  
  55. extern struct ModulePrefs nP;
  56. Toaster *Toasters;
  57. LONG NumToasters;
  58.  
  59. #include "images.h"
  60.  
  61. ULONG cmap[] = {
  62.     0x00, 0x00, 0x00, 0x33, 0x33, 0x33, 0x55, 0x55, 0x55, 0x88, 0x55, 0x22,
  63.     0x66, 0x66, 0x66, 0x77, 0x77, 0x44, 0x55, 0x55, 0x66, 0xEE, 0x55, 0x22,
  64.     0x88, 0x88, 0x88, 0x99, 0x99, 0x99, 0xBB, 0xBB, 0xBB, 0xCC, 0xCC, 0xCC,
  65.     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF };
  66.  
  67. #define abs( x ) ( (x) < 0 ? -(x) : (x) )
  68.  
  69. LONG TestExtent( LONG Start, LONG Me, LONG x, LONG y )
  70. {
  71.     LONG i;
  72.     
  73.     for( i = Start; i < NumToasters; i++ )
  74.         if(( i != Me )&& !Toasters[i].delay &&
  75.            ( abs( x - Toasters[i].x ) < IM_WIDBUF )&&
  76.            ( abs( y - Toasters[i].y ) < IM_HEIBUF ))
  77.             return i;
  78.     
  79.     return -1L;
  80. }
  81.  
  82. BOOL FindLaunchPos( LONG i, LONG wid, LONG hei )
  83. {
  84.     LONG x, y;
  85.     
  86.     x = wid;
  87.     y = RangeRand( hei );
  88.     
  89.     FOREVER
  90.     {
  91.         if( x <= 2 * IM_WIDTH )
  92.             return FALSE;
  93.         
  94.         if( TestExtent( 0, i, x, y ) == -1 )
  95.         {
  96.             Toasters[i].x = Toasters[i].old_x = x;
  97.             Toasters[i].y = Toasters[i].old_y = y;
  98.             Toasters[i].xspeed = RangeRand( 3 ) + 1;
  99.             Toasters[i].yspeed = RangeRand( 2 ) + 1;
  100.             Toasters[i].phase = RangeRand( IMAGEMAX );
  101.  
  102.             return TRUE;
  103.         }
  104.         
  105.         if( y > -IM_HEIGHT )
  106.             y -= IM_HEIGHT;
  107.         else
  108.             x -= IM_WIDTH;
  109.     }
  110. }
  111.  
  112. LONG ActXSpeed( LONG i )
  113. {
  114.     if( Toasters[i].xcol == -1 )
  115.         return Toasters[i].xspeed;
  116.     else
  117.         return ActXSpeed( Toasters[i].xcol );
  118. }
  119.  
  120. LONG ActYSpeed( LONG i )
  121. {
  122.     if( Toasters[i].ycol == -1 )
  123.         return Toasters[i].yspeed;
  124.     else
  125.         return ActXSpeed( Toasters[i].ycol );
  126. }
  127.  
  128. LONG Blank( VOID *Prefs )
  129. {
  130.     LONG delay_rate, i, j, counter = 0, RetVal = OK;
  131.     struct ModulePrefs *mP;
  132.     struct RastPort *Rast;
  133.     struct Screen *FTScr;
  134.     struct Window *FTWin;
  135.     struct Window *Wnd;
  136.     
  137.     if( FlyingToasterWnd )
  138.         mP = &nP;
  139.     else
  140.         mP = ( struct ModulePrefs * )Prefs;
  141.     
  142.     NumToasters = mP->Objects;
  143.     delay_rate = 11 - mP->Speed;
  144.     
  145.     Toasters = AllocVec( sizeof( Toaster ) * NumToasters, MEMF_CLEAR );
  146.     FTScr = OpenScreenTags( NULL, SA_DisplayID, mP->Mode, SA_Depth, 4,
  147.                            SA_Overscan, OSCAN_STANDARD, SA_Type, CUSTOMSCREEN,
  148.                            SA_Quiet, TRUE, SA_Behind, TRUE, TAG_DONE );
  149.     if( Toasters && FTScr )
  150.     {
  151.         for( i = 0; i < 48; i += 3 )
  152.             if( GfxBase->LibNode.lib_Version < 39 )
  153.                 SetRGB4(&( FTScr->ViewPort ), i/3, ( cmap[i] * 16 ) / 256,
  154.                         ( cmap[i+1] * 16 ) / 256, ( cmap[i+2] * 16 ) / 256 );
  155.             else
  156.                 SetRGB32(&( FTScr->ViewPort ), i/3, cmap[i]<<24, cmap[i+1]<<24,
  157.                          cmap[i+2]<<24 );
  158.         FTWin = OpenWindowTags( 0L, WA_Width, FTScr->Width, WA_Height,
  159.                                FTScr->Height, WA_IDCMP, 0L, WA_Flags,
  160.                                WFLG_SIMPLE_REFRESH|WFLG_BORDERLESS,
  161.                                WA_CustomScreen, FTScr, TAG_DONE );
  162.         if( FTWin )
  163.         {
  164.             Rast = FTWin->RPort;
  165.             SetAPen( Rast, 0 );
  166.             
  167.             for( i = 0; i < NumToasters; i++ )
  168.                 if( !FindLaunchPos( i, ( LONG )FTWin->Width,
  169.                                    ( LONG )FTWin->Height ))
  170.                     Toasters[i].delay = 30;
  171.             
  172.             Wnd = BlankMousePointer( FTScr );
  173.             ScreenToFront( FTScr );
  174.             
  175.             while( RetVal == OK )
  176.             {
  177.                 WaitTOF();
  178.                 
  179.                 if(!( ++counter % 60 ))
  180.                     ScreenToFront( FTScr );
  181.                 
  182.                 if(!( counter % delay_rate ))
  183.                 {
  184.                     for( i = 0; i < NumToasters; i++ )
  185.                     {
  186.                         if( !Toasters[i].delay )
  187.                         {
  188.                             Toasters[i].old_x = Toasters[i].x;
  189.                             Toasters[i].old_y = Toasters[i].y;
  190.                             Toasters[i].x -= Toasters[i].xspeed;
  191.                             Toasters[i].y += Toasters[i].yspeed;
  192.                             Toasters[i].xcol = -1;
  193.                             Toasters[i].ycol = -1;
  194.                         }
  195.                     }
  196.  
  197.                     for( i = 0; i < NumToasters; i++ )
  198.                     {
  199.                         if( !Toasters[i].delay )
  200.                         {
  201.                             j = -1;
  202.                             while(( j = TestExtent( j+1, i, Toasters[i].x,
  203.                                                    Toasters[i].y )) >= 0 )
  204.                             {
  205.                                 if( abs( Toasters[j].old_x -
  206.                                         Toasters[i].old_x ) < IM_WIDBUF )
  207.                                 {
  208.                                     if( Toasters[i].y < Toasters[j].y )
  209.                                         Toasters[i].ycol = j;
  210.                                     if( Toasters[i].xspeed ==
  211.                                        Toasters[j].xspeed )
  212.                                         Toasters[i].xspeed++;
  213.                                 }
  214.                                 else
  215.                                 {
  216.                                     if( Toasters[i].x > Toasters[j].x )
  217.                                         Toasters[i].xcol = j;
  218.                                     if( Toasters[i].yspeed ==
  219.                                        Toasters[j].yspeed )
  220.                                         Toasters[i].yspeed++;
  221.                                 }
  222.                                 if( abs( Toasters[j].old_y -
  223.                                         Toasters[i].old_y ) < IM_HEIBUF )
  224.                                 {
  225.                                     if( Toasters[i].x > Toasters[j].x )
  226.                                         Toasters[i].xcol = j;
  227.                                     if( Toasters[i].yspeed ==
  228.                                        Toasters[j].yspeed )
  229.                                         Toasters[i].yspeed++;
  230.                                 }
  231.                             }
  232.                         }
  233.                     }
  234.  
  235.                     for( i = 0; i < NumToasters; i++ )
  236.                     {
  237.                         if( !Toasters[i].delay )
  238.                         {
  239.                             Toasters[i].x = Toasters[i].old_x - ActXSpeed( i );
  240.                             Toasters[i].y = Toasters[i].old_y + ActYSpeed( i );
  241.                         }
  242.                     }
  243.                     
  244.                     for( i = 0; i < NumToasters; i++ )
  245.                     {
  246.                         if( !Toasters[i].delay )
  247.                         {
  248.                             j = -1;
  249.                             while(( j = TestExtent( j+1, i, Toasters[i].x,
  250.                                                    Toasters[i].y )) >= 0 )
  251.                             {
  252.                                 if( abs( Toasters[j].old_x -
  253.                                         Toasters[i].old_x ) < IM_WIDBUF )
  254.                                     if( Toasters[i].x > Toasters[j].x )
  255.                                         Toasters[i].x = Toasters[i].old_x;
  256.                                 else
  257.                                     if( Toasters[i].y < Toasters[j].y )
  258.                                         Toasters[i].y = Toasters[i].old_y;
  259.                                 if( abs( Toasters[j].old_y -
  260.                                         Toasters[i].old_y ) < IM_HEIBUF )
  261.                                     if( Toasters[i].y < Toasters[j].y )
  262.                                         Toasters[i].y = Toasters[i].old_y;
  263.                             }
  264.                         }
  265.                     }
  266.  
  267.                     for( i = 0; i < NumToasters; i++ )
  268.                     {
  269.                         if( !Toasters[i].delay )
  270.                         {
  271.                             Toasters[i].phase =
  272.                                 ( Toasters[i].phase + 1 ) % IMAGEMAX;
  273.                             EraseRect( Rast, Toasters[i].x + IM_WIDTH,
  274.                                       Toasters[i].old_y, Toasters[i].x +
  275.                                       IM_WIDTH + Toasters[i].xspeed,
  276.                                       Toasters[i].old_y + IM_HEIGHT );
  277.                             EraseRect( Rast, Toasters[i].old_x,
  278.                                       Toasters[i].old_y,
  279.                                       Toasters[i].old_x + IM_WIDTH,
  280.                                       Toasters[i].old_y + Toasters[i].yspeed );
  281.                             DrawImage( Rast, img[Toasters[i].phase],
  282.                                       Toasters[i].x, Toasters[i].y );
  283.                             if(( Toasters[i].x < -IM_WIDTH-1 )||
  284.                                ( Toasters[i].y > FTWin->Height ))
  285.                                 Toasters[i].delay = RangeRand( 50 );
  286.                         }
  287.                         else
  288.                             if(!( --Toasters[i].delay ))
  289.                                 Toasters[i].delay =
  290.                                     FindLaunchPos( i, ( LONG )FTWin->Width,
  291.                                                   ( LONG )FTWin->Height )
  292.                                         ? 0 : 30;
  293.                     }
  294.                 }
  295.                 RetVal = ContinueBlanking();
  296.             }
  297.  
  298.             SetSignal( 0L, SIGBREAKF_CTRL_C );
  299.             UnblankMousePointer( Wnd );
  300.             CloseWindow( FTWin );
  301.         }
  302.         else
  303.             RetVal = FAILED;
  304.         CloseScreen( FTScr );
  305.     }
  306.     else
  307.         RetVal = FAILED;
  308.     
  309.     FreeVec( Toasters );
  310.  
  311.     return RetVal;
  312. }
  313.