home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / util / blank / gblanker / source / blankers / puzzle / blank.c next >
C/C++ Source or Header  |  1994-10-18  |  5KB  |  247 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/screens.h>
  14.  
  15. #include <graphics/gfx.h>
  16. #include <dos/dos.h>
  17.  
  18. #include <clib/exec_protos.h>
  19. #include <clib/dos_protos.h>
  20. #include <clib/intuition_protos.h>
  21. #include <clib/graphics_protos.h>
  22. #include <clib/alib_protos.h>
  23.  
  24. #include "/Garshnelib/Garshnelib_protos.h"
  25. #include "/Garshnelib/Garshnelib_pragmas.h"
  26.  
  27. #include "Puzzle.h"
  28. #include "//defs.h"
  29. #include "/main.h"
  30.  
  31. #define STEP_DELAY pP->Delay
  32.  
  33. struct ModulePrefs
  34. {
  35.     LONG Horiz;
  36.     LONG Vert;
  37.     LONG Divisions;
  38.     LONG Delay;
  39.     LONG FadePct;
  40.     LONG TopScr;
  41. };
  42.  
  43. typedef struct _mPoint
  44. {
  45.     LONG x;
  46.     LONG y;
  47. } mPoint;
  48.  
  49. extern struct ModulePrefs nP;
  50.  
  51. #define UP      0
  52. #define DOWN    1
  53. #define LEFT    2
  54. #define RIGHT   3
  55.  
  56. LONG getNewDir( LONG x, LONG y, LONG xmax, LONG ymax, LONG olddir )
  57. {
  58.     LONG a1[] = { UP, DOWN, LEFT, RIGHT }, dir;
  59.     LONG a2[] = { DOWN, UP, RIGHT, LEFT };
  60.  
  61.     do
  62.     {
  63.         switch( dir = RangeRand( 4 ))
  64.         {
  65.         case RIGHT: if( !x ) dir = LEFT; break;
  66.         case LEFT: if( x > xmax ) dir = RIGHT; break;
  67.         case DOWN: if( !y ) dir = UP; break;
  68.         case UP: if( y > ymax ) dir = DOWN; break;
  69.         }
  70.     }
  71.     while( a1[dir] == a2[olddir] );
  72.     
  73.     return dir;
  74. }
  75.  
  76. LONG Blank( VOID *Prefs )
  77. {
  78.     LONG i, dir = UP, xmax, ymax, wid, hei, Width, Height, Divs;
  79.     LONG RetVal = OK, ToFrontCount = 0;
  80.     struct ModulePrefs *pP;
  81.     struct Screen *PScr;
  82.     struct Window *Wnd;
  83.     struct RastPort *Rast;
  84.     mPoint c = { 0, 0 }, n = { 0, 0 }, t;
  85.     
  86.     if( PuzzleWnd )
  87.         pP = &nP;
  88.     else
  89.         pP = ( struct ModulePrefs * )Prefs;
  90.     
  91.     if( PScr = cloneTopScreen( FALSE, pP->TopScr ))
  92.     {
  93.         if( pP->FadePct )
  94.         {
  95.             ULONG *ColorTable, PctCount, BPG;
  96.  
  97.             ColorTable = GetColorTable( PScr );
  98.             BPG = AvgBitsPerGun( getTopScreenMode());
  99.             PctCount = ( 1L << BPG ) * pP->FadePct / 100;
  100.             for( i = 0; i < PctCount; i++ )
  101.                 FadeAndLoadTable( PScr, BPG, ColorTable );
  102.         }
  103.         
  104.         Rast = &( PScr->RastPort );
  105.         Width = PScr->Width;
  106.         Height = PScr->Height;
  107.         if( pP->Horiz )
  108.             wid = Width / pP->Horiz;
  109.         else
  110.             wid = Width / 3;
  111.         if( pP->Vert )
  112.             hei = Height / pP->Vert;
  113.         else
  114.             hei = Height / 3;
  115.         if( pP->Divisions )
  116.             Divs = pP->Divisions;
  117.         else
  118.             Divs = 4;
  119.         
  120.         SetAPen( Rast, 2 );
  121.         for( i = 0; i < Width; i += wid )
  122.         {
  123.             Move( Rast, i, 0 );
  124.             Draw( Rast, i, Height-1 );
  125.         }
  126.         for( i = 0; i < Height; i += hei )
  127.         {
  128.             Move( Rast, 0, i );
  129.             Draw( Rast, Width-1, i );
  130.         }
  131.         
  132.         SetAPen( Rast, 1 );
  133.         for( i = wid-1; i < Width; i += wid )
  134.         {
  135.             Move( Rast, i, 0 );
  136.             Draw( Rast, i, Height-1 );
  137.         }
  138.         for( i = hei-1; i < Height; i += hei )
  139.         {
  140.             Move( Rast, 0, i );
  141.             Draw( Rast, Width-1, i );
  142.         }
  143.         
  144.         xmax = Width - 2 * wid;
  145.         ymax = Height - 2 * hei;
  146.         
  147.         SetAPen( Rast, 3 );
  148.         
  149.         Wnd = BlankMousePointer( PScr );
  150.         
  151.         while( RetVal == OK )
  152.         {
  153.             LONG step, i;
  154.  
  155.             if(!( ++ToFrontCount % 60 ))
  156.                 ScreenToFront( PScr );
  157.             
  158.             switch( dir = getNewDir( c.x, c.y, xmax, ymax, dir ))
  159.             {
  160.             case UP:
  161.                 n.y += hei;
  162.                 t = n;
  163.                 step = ( n.y - c.y ) / Divs;
  164.                 do
  165.                 {
  166.                     if( step > t.y - c.y )
  167.                         step = t.y - c.y;
  168.                     t.y -= step;
  169.                     for( i = 0; i < STEP_DELAY; i++ )
  170.                         WaitTOF();
  171.                     BltBitMap( Rast->BitMap, t.x, t.y + step, Rast->BitMap,
  172.                               t.x, t.y, wid, hei, 0xC0, 0xFF, 0L );
  173.                     RectFill( Rast, t.x, t.y + hei, t.x + wid - 1,
  174.                              t.y + hei + step - 1 );
  175.                     RetVal = ContinueBlanking();
  176.                 }
  177.                 while( t.y > c.y && RetVal == OK );
  178.                 break;
  179.             case DOWN:
  180.                 n.y -= hei;
  181.                 t = n;
  182.                 step = ( c.y - n.y ) / Divs;
  183.                 do
  184.                 {
  185.                     if( step > c.y - t.y )
  186.                         step = c.y - t.y;
  187.                     t.y += step;
  188.                     for( i = 0; i < STEP_DELAY; i++ )
  189.                         WaitTOF();
  190.                     BltBitMap( Rast->BitMap, t.x, t.y - step, Rast->BitMap,
  191.                               t.x, t.y, wid, hei, 0xC0, 0xFF, 0L );
  192.                     RectFill( Rast, t.x, t.y - step, t.x + wid - 1, t.y - 1 );
  193.                     RetVal = ContinueBlanking();
  194.                 }
  195.                 while( t.y < c.y && RetVal == OK );
  196.                 break;
  197.             case LEFT:
  198.                 n.x += wid;
  199.                 t = n;
  200.                 step = ( n.x - c.x ) / Divs;
  201.                 do
  202.                 {
  203.                     if( step > t.x - c.x )
  204.                         step = t.x - c.x;
  205.                     t.x -= step;
  206.                     for( i = 0; i < STEP_DELAY; i++ )
  207.                         WaitTOF();
  208.                     BltBitMap( Rast->BitMap, t.x + step, t.y, Rast->BitMap,
  209.                               t.x, t.y, wid, hei, 0xC0, 0xFF, 0L );
  210.                     RectFill( Rast, t.x + wid, t.y, t.x + wid + step - 1,
  211.                              t.y + hei - 1 );
  212.                     RetVal = ContinueBlanking();
  213.                 }
  214.                 while( t.x > c.x && RetVal == OK );
  215.                 break;
  216.             case RIGHT:
  217.                 n.x -= wid;
  218.                 t = n;
  219.                 step = ( c.x - n.x ) / Divs;
  220.                 do
  221.                 {
  222.                     if( step > c.x - t.x )
  223.                         step = c.x - t.x;
  224.                     t.x += step;
  225.                     for( i = 0; i < STEP_DELAY; i++ )
  226.                         WaitTOF();
  227.                     BltBitMap( Rast->BitMap, t.x - step, t.y, Rast->BitMap,
  228.                               t.x, t.y, wid, hei, 0xC0, 0xFF, 0L );
  229.                     RectFill( Rast, t.x - step, t.y, t.x - 1, t.y + hei - 1 );
  230.                     RetVal = ContinueBlanking();
  231.                 }
  232.                 while( t.x < c.x && RetVal == OK );
  233.                 break;
  234.             }
  235.  
  236.             c = n;
  237.         }
  238.         
  239.         UnblankMousePointer( Wnd );
  240.         CloseScreen( PScr );
  241.     }
  242.     else
  243.         RetVal = FAILED;
  244.         
  245.     return RetVal;
  246. }
  247.