home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Life 1.0d2 / Control.c next >
Encoding:
Text File  |  1995-07-11  |  3.3 KB  |  200 lines  |  [TEXT/MPCC]

  1. // Control.c
  2. // Control the progress of Mike's Life
  3. // Copyright ©1995 Michael D. Crawford.  All Rights Reserved.
  4. // 11 Jun 95 Mike Crawford crawford@scruznet.com
  5. //
  6. // Revision History:
  7. // 11 Jun 95    MDC    New today
  8.  
  9. #include <QDOffscreen.h>
  10. #include "Control.h"
  11. #include "LifeModules.h"
  12.  
  13. void ZeroBytes( char *p, unsigned long theSize );
  14.  
  15. static Boolean    gLifeMarchesOn;
  16.  
  17. short    gNumWindows;
  18.  
  19. tLifeWorldRecord gLifeWorlds[ kMaxWindows ];
  20. Boolean gLifeSlots[ kMaxWindows ];
  21.  
  22. void DoLifeNull( void )
  23. {
  24.     tLifeWorldPtr    worldPtr;
  25.     WindowPtr        winPtr;
  26.  
  27.     if ( !gLifeMarchesOn )
  28.         return;
  29.         
  30.     winPtr = FrontWindow();
  31.     
  32.     if (!winPtr )
  33.         return;
  34.     
  35.     worldPtr = WinToLifeWorld( winPtr );
  36.     if ( !worldPtr )
  37.         return;
  38.     
  39.     LifeStep( worldPtr );
  40.  
  41.     DrawLifeWorld( worldPtr );
  42.  
  43.     return;
  44. }
  45.  
  46. void InitControl( void )
  47. {
  48.     short    i;
  49.  
  50.     gLifeMarchesOn = false;
  51.     
  52.     gNumWindows = 0;
  53.  
  54.     for ( i = 0; i < kMaxWindows; i++ ){
  55.         ZeroBytes( (char*)&gLifeWorlds[ i ], sizeof( tLifeWorldRecord ) );
  56.         gLifeSlots[ i ] = false;
  57.     }
  58.     
  59.     return;
  60. }
  61.  
  62. tLifeWorldPtr NewLifeWorld( void )
  63. {
  64.     tLifeWorldPtr    worldPtr;
  65.     short            i;
  66.     
  67.     if ( gNumWindows >= kMaxWindows )
  68.         return (tLifeWorldPtr)NULL;
  69.     
  70.     gNumWindows++;
  71.     
  72.     // Search for a free slot
  73.     // This is hokey... should have an array of pointers rather than a static array of elements
  74.  
  75.     for ( i = 0; i < kMaxWindows; i++ ){
  76.         if ( !gLifeSlots[ i ] ){
  77.             worldPtr = &(gLifeWorlds[ i ] );
  78.             ZeroBytes( (char*)worldPtr, sizeof( tLifeWorldRecord ) );
  79.             gLifeSlots[ i ] = true;
  80.             return worldPtr;
  81.         }
  82.     }
  83.     
  84.     DebugStr( "\pOut of slots... should never get here" );
  85.  
  86.     return (tLifeWorldPtr)NULL;
  87. }
  88.  
  89. void FreeLifeWorld( tLifeWorldPtr worldPtr )
  90. {
  91.     short i;
  92.  
  93.     for ( i = 0; i < kMaxWindows; i++ ){
  94.         if ( &(gLifeWorlds[ i ] ) == worldPtr ){
  95.             gLifeSlots[ i ] = false;
  96.             return;
  97.         }
  98.     }
  99.  
  100.     DebugStr( "\pFreeLifeWorld got bogus worldPtr" );
  101.  
  102.     return;
  103. }
  104.  
  105. tLifeWorldPtr WinToLifeWorld( WindowPtr win )
  106. {
  107.     tLifeWorldPtr    worldPtr;
  108.     short            i;
  109.  
  110.     for ( i = 0; i < kMaxWindows; i++ ){
  111.         if ( gLifeSlots[ i ] ){
  112.             if ( gLifeWorlds[ i ].win == win ){
  113.                 worldPtr = &( gLifeWorlds[ i ] );
  114.                 return worldPtr;
  115.             }
  116.         }
  117.     }
  118.  
  119.     return (tLifeWorldPtr)NULL;
  120. }
  121.  
  122. void ToggleCell( tLifeWorldPtr worldPtr, long h, long v )
  123. {
  124. #ifdef NEVER
  125.     GWorldPtr    gPtr;
  126.     short        rowBytes;
  127.     PixMapHandle    pHdl;
  128.     unsigned char    *pixPtr;
  129.  
  130.     if ( worldPtr->isHip )
  131.         gPtr = worldPtr->hip;
  132.     else
  133.         gPtr = worldPtr->hop;
  134.     
  135.     pHdl = GetGWorldPixMap( gPtr );
  136.     rowBytes = (*pHdl)->rowBytes & 0x3ff;
  137.     
  138.     pixPtr = ( (unsigned char*)(*pHdl)->baseAddr ) + h + rowBytes * v;
  139.     
  140.     if ( *pixPtr )
  141.         *pixPtr = (unsigned char)0;
  142.     else
  143.         *pixPtr = (unsigned char)0xff;
  144. #endif
  145.     
  146.     return;
  147. }
  148.  
  149. void DrawCell( WindowPtr winPtr, tLifeWorldPtr worldPtr, long h, long v )
  150. {
  151. #ifdef NEVER
  152.     GWorldPtr    gPtr;
  153.     short        rowBytes;
  154.     PixMapHandle    pHdl;
  155.     unsigned char    *pixPtr;
  156.     Rect            cellRect;
  157.  
  158.     if ( worldPtr->isHip )
  159.         gPtr = worldPtr->hip;
  160.     else
  161.         gPtr = worldPtr->hop;
  162.     
  163.     SetRect( &cellRect, (short)h, (short)v, (short)(h + 1), (short)(v + 1) );
  164.     
  165.     SetGWorld( (GWorldPtr)winPtr, (GDHandle)NULL );
  166.     
  167.     pHdl = GetGWorldPixMap( gPtr );
  168.     
  169.     CopyBits( &( (GrafPtr)gPtr )->portBits,
  170.                 &winPtr->portBits,
  171.                 &cellRect,
  172.                 &cellRect,
  173.                 srcCopy,
  174.                 (RgnHandle)NULL );
  175.  
  176. #endif
  177.      return;
  178. }
  179.  
  180. void ZeroBytes( char *p, unsigned long theSize )
  181. {
  182.     while( theSize-- )
  183.         *p++ = 0;
  184.  
  185.     return;
  186. }
  187.  
  188. void StartRunning( void )
  189. {
  190.     gLifeMarchesOn = true;
  191.     return;
  192. }
  193.  
  194. void StopRunning( void )
  195. {
  196.     gLifeMarchesOn = false;
  197.     return;
  198. }
  199.  
  200.