home *** CD-ROM | disk | FTP | other *** search
- #include "AppTypes.h"
- #include "AppConstants.h"
- #include "AppGlobals.h"
- #include "AppMacros.h"
- #include "GridCopyPr.h"
-
- #include "TablesUtilPr.h"
-
- static Boolean CreatePositionTable ( GridDataPtr );
- static Boolean CreateGridArray ( GridDataPtr );
- static Boolean CreateNeighbourArray ( GridDataPtr );
-
- // InitWindowTables - call to initialize the window's tables
- Boolean InitWindowTables( WindowRef theWindow )
- {
- GridDataPtr refCon = (GridDataPtr)MGetWRefCon( theWindow );
-
- refCon->oldGrid = nil;
- refCon->newGrid = nil;
- refCon->gridMove = nil;
- refCon->neighbours = nil;
- refCon->position = nil;
-
- if( !CreateGridArray( refCon ) )
- return( false );
- if( !CreatePositionTable( refCon ) )
- {
- DisposeTables( refCon );
- return( false );
- }
- if( !CreateNeighbourArray( refCon ) )
- {
- DisposeTables( refCon );
- return( false );
- }
- return( true );
- }
-
- // DisposeTables - call to dispose of a grid's tables
- void DisposeTables( GridDataPtr refCon )
- {
- if( refCon->oldGrid )
- DisposePtr( (Ptr)refCon->oldGrid );
- if( refCon->newGrid )
- DisposePtr( (Ptr)refCon->newGrid );
- if( refCon->gridMove )
- DisposePtr( (Ptr)refCon->gridMove );
- if( refCon->neighbours )
- DisposePtr( (Ptr)refCon->neighbours );
- if( refCon->position )
- DisposePtr( (Ptr)refCon->position );
- }
-
- // CreatePositionTable - call to create the position offset table for the PixMap hex positions
- static Boolean CreatePositionTable( GridDataPtr refCon )
- {
- long rowBytes = (long)((**MGetGWorldPixMap( refCon->theGWorld )).rowBytes & 0x3FFF);
- long countx, county, offsetx, offsety, total = 0;
- Boolean even = true;
- short hexMidPoint, hexSize;
- unsigned char *baseAddr = (unsigned char *)GetPixBaseAddr( MGetGWorldPixMap( refCon->theGWorld ) );
-
- if( !(refCon->position = (unsigned char **)NewPtr( sizeof( unsigned char * ) * refCon->gridTotalSize )) )
- return( false );
-
- refCon->rowBytes = rowBytes;
- switch( refCon->scale )
- {
- case kHexSize:
- hexMidPoint = kHexMidPoint;
- hexSize = kHexSize;
- break;
- case kMiniHexSize:
- hexMidPoint = kMiniHexMidPoint;
- hexSize = kMiniHexSize;
- break;
- case kTinyHexSize:
- hexMidPoint = kTinyHexMidPoint;
- hexSize = kTinyHexSize;
- break;
- }
-
- offsetx = -(**MGetGWorldPixMap( refCon->theGWorld )).bounds.left + (refCon->theGWorld->portRect.right - refCon->gridHSize * hexSize - hexMidPoint + 1) / 2;
- offsety = -(**MGetGWorldPixMap( refCon->theGWorld )).bounds.top + (refCon->theGWorld->portRect.bottom - refCon->gridVSize * (hexSize - hexMidPoint / 2 + 1) - hexMidPoint + hexMidPoint / 2 + 1) / 2;
-
- for( county = 0; county < refCon->gridVSize; county++ )
- {
- for( countx = 0; countx < refCon->gridHSize; countx++, total++ )
- *(refCon->position + total) = baseAddr + offsetx + countx * hexSize + rowBytes * (offsety + county * (hexSize - hexMidPoint / 2 + 1)) + (even ? hexMidPoint : 0);
- even = !even;
- }
-
- return( true );
- }
-
- // CreateGridArray - call to create a grid array and the asociated data
- static Boolean CreateGridArray( GridDataPtr refCon )
- {
- if( !(refCon->newGrid = (Boolean *)NewPtr( sizeof( Boolean ) * refCon->gridTotalSize )) )
- return( false );
- if( !(refCon->oldGrid = (Boolean *)NewPtrClear( sizeof( Boolean ) * refCon->gridTotalSize )) )
- return( false );
- if( !(refCon->gridMove = (RBlockDataPtr)NewPtr( sizeof( RBlockData ) )) )
- return( false );
- SetRBlockData( sizeof( Boolean ) * refCon->gridTotalSize, refCon->gridMove );
- return( true );
- }
-
- // CreateNeighbourArray - creates and sets the look-up tables for neighbour cells (must be called _after_ the gids are created!)
- static Boolean CreateNeighbourArray( GridDataPtr refCon )
- {
- long countx, county, totalx = refCon->gridHSize, totaly = refCon->gridVSize;
- Boolean *oldSrc = refCon->oldGrid, *oldGrid = refCon->oldGrid;
- NeighbourPtr neighbours;
- Boolean even = true;
-
- if( !(refCon->neighbours = (NeighbourPtr)NewPtr( sizeof( NeighbourRec ) * refCon->gridTotalSize )) )
- return( false );
-
- neighbours = refCon->neighbours;
- for( county = 0; county < totaly; county++ )
- {
- for( countx = 0; countx < totalx; countx++, oldSrc++, neighbours++ )
- {
- if( county != 0 && county != totaly - 1 && countx != 0 && countx != totalx - 1 )
- {
- neighbours->neighbour[0] = oldSrc - 1;
- neighbours->neighbour[1] = oldSrc + 1;
- if( even )
- {
- neighbours->neighbour[2] = oldSrc - totalx;
- neighbours->neighbour[3] = oldSrc - totalx + 1;
- neighbours->neighbour[4] = oldSrc + totalx;
- neighbours->neighbour[5] = oldSrc + totalx + 1;
- }
- else
- {
- neighbours->neighbour[2] = oldSrc - totalx - 1;
- neighbours->neighbour[3] = oldSrc - totalx;
- neighbours->neighbour[4] = oldSrc + totalx - 1;
- neighbours->neighbour[5] = oldSrc + totalx;
- }
- }
- else
- {
- long offxn, offxp, offyn, offyp, offym = county * totalx;
-
- if( countx != 0 ) offxn = countx - 1; else offxn = totalx - 1;
- if( countx != totalx - 1 ) offxp = countx + 1; else offxp = 0;
- if( county != 0 ) offyn = offym - totalx; else offyn = (totaly - 1) * totalx;
- if( county != totaly - 1 ) offyp = offym + totalx; else offyp = 0;
-
- neighbours->neighbour[0] = oldGrid + offym + offxn;
- neighbours->neighbour[1] = oldGrid + offym + offxp;
- if( even )
- {
- neighbours->neighbour[2] = oldGrid + offyn + countx;
- neighbours->neighbour[3] = oldGrid + offyn + offxp;
- neighbours->neighbour[4] = oldGrid + offyp + countx;
- neighbours->neighbour[5] = oldGrid + offyp + offxp;
- }
- else
- {
- neighbours->neighbour[2] = oldGrid + offyn + countx;
- neighbours->neighbour[3] = oldGrid + offyn + offxn;
- neighbours->neighbour[4] = oldGrid + offyp + countx;
- neighbours->neighbour[5] = oldGrid + offyp + offxn;
- }
- }
- }
- even = !even;
- }
- return( true );
- }