home *** CD-ROM | disk | FTP | other *** search
- Save Format v1.3
- @begin ClassFile "CellArray"
- Exported 0;
-
- @begin-code BaseClassList
-
- public WObject
-
- @end-code;
-
- @begin UserFunction "CellArray()"
- GencodeSrcLine 16;
- FunctionName "CellArray::CellArray()";
- @end;
-
- @begin UserFunction "Prototype for CellArray()"
- Private 1;
- GencodeSrcLine 54;
- FunctionName "CellArray::Prototype for CellArray()";
- @end;
-
- @begin UserFunction "~CellArray()"
- GencodeSrcLine 21;
- FunctionName "CellArray::~CellArray()";
- @end;
-
- @begin UserFunction "Prototype for ~CellArray()"
- Private 1;
- GencodeSrcLine 56;
- FunctionName "CellArray::Prototype for ~CellArray()";
- @end;
-
- @begin UserFunction "Resize( WSize const & size )"
- GencodeSrcLine 28;
- FunctionName "CellArray::Resize( WSize const & size )";
- @end;
-
- @begin UserFunction "Prototype for Resize( WSize const & size )"
- Private 1;
- GencodeSrcLine 58;
- FunctionName "CellArray::Prototype for Resize( WSize const & size )";
- @end;
-
- @begin UserFunction "Randomize()"
- GencodeSrcLine 56;
- FunctionName "CellArray::Randomize()";
- @end;
-
- @begin UserFunction "Prototype for Randomize()"
- Private 1;
- GencodeSrcLine 60;
- FunctionName "CellArray::Prototype for Randomize()";
- @end;
-
- @begin UserFunction "NextGeneration( Rules const &rules )"
- GencodeSrcLine 64;
- FunctionName "CellArray::NextGeneration( Rules const &rules )";
- @end;
-
- @begin UserFunction "Prototype for NextGeneration( Rules const &rules )"
- Private 1;
- GencodeSrcLine 62;
- FunctionName "CellArray::Prototype for NextGeneration( Rules const &rules )";
- @end;
-
- @begin UserFunction "ToggleCell( int x, int y )"
- GencodeSrcLine 127;
- FunctionName "CellArray::ToggleCell( int x, int y )";
- @end;
-
- @begin UserFunction "Prototype for ToggleCell( int x, int y )"
- Private 1;
- GencodeSrcLine 64;
- FunctionName "CellArray::Prototype for ToggleCell( int x, int y )";
- @end;
-
- @begin UserFunction "Clear()"
- GencodeSrcLine 142;
- FunctionName "CellArray::Clear()";
- @end;
-
- @begin UserFunction "Prototype for Clear()"
- Private 1;
- GencodeSrcLine 66;
- FunctionName "CellArray::Prototype for Clear()";
- @end;
-
- @begin UserFunction "Count()"
- GencodeSrcLine 150;
- FunctionName "CellArray::Count()";
- @end;
-
- @begin UserFunction "Prototype for Count()"
- Private 1;
- GencodeSrcLine 68;
- FunctionName "CellArray::Prototype for Count()";
- @end;
-
- @begin HPPPrefixBlock
- @begin-code HPPPrefix
-
- // Declarations added here will be included at the top of the .HPP file
- class Rules;
-
- @end-code;
- GencodeSrcLine 11;
- @end;
-
- @begin CPPPrefixBlock
- @begin-code CPPPrefix
-
- // Code added here will be included at the top of the .CPP file
- #include "Rules.hpp"
-
- // Include definitions for resources.
- #include "WRes.h"
-
- @end-code;
- GencodeSrcLine 11;
- @end;
-
- @begin ClassContentsBlock
- @begin-code ClassContents
-
- enum {
- OFF = 0,
- ON = 1,
- NEXT = 2,
- PREV = 4,
- };
-
- char *First()
- {
- return( _array );
- }
- char *Next( char *p ) {
- return( p + 1 );
- }
- bool IsOn( char *p ) {
- return( ( *p & ON ) != 0 );
- }
- bool WasOn( char *p ) {
- return( ( *p & PREV ) != 0 );
- }
- bool FlippedOn( char *p ) {
- return( ( *p & (ON+PREV) ) == ON );
- }
- bool FlippedOff( char *p ) {
- return( ( *p & (ON+PREV) ) == PREV );
- }
-
- public:
- // add your public instance data here
- private:
- WSize _size;
- char *_array;
- int _count;
- // add your private instance data here
- protected:
- // add your protected instance data here
-
-
- @end-code;
- GencodeSrcLine 17;
- @end;
-
- @begin-code GeneratedClassContents
-
-
- @end-code;
-
- @begin-code Code "CellArray::CellArray()"
-
- CellArray::CellArray()
- {
- _count = 0;
- _array = NULL;
- }
-
- @end-code;
-
- @begin-code Code "CellArray::Prototype for CellArray()"
-
- public:
- CellArray();
-
- @end-code;
-
- @begin-code Code "CellArray::~CellArray()"
-
- CellArray::~CellArray()
- {
- if( _array != NULL ) {
- _array -= _size.w;
- delete [] _array;
- }
- }
-
- @end-code;
-
- @begin-code Code "CellArray::Prototype for ~CellArray()"
-
- public:
- ~CellArray();
-
- @end-code;
-
- @begin-code Code "CellArray::Resize( WSize const & size )"
-
- void CellArray::Resize( WSize const & size )
- {
- if( _size == size ) return;
- char *oldArray = _array;
- WSize oldSize = _size;
- _size = size;
- // allocate an extra row above the top and below the bottom
- _array = new char[ ( _size.h + 2 ) * _size.w ];
- memset( _array, 0, ( _size.h + 2 ) * _size.w );
- _array += _size.w;
- if( oldArray != NULL ) {
- WSize min;
- char *op, *np;
- min.w = _size.w < oldSize.w ? _size.w : oldSize.w;
- min.h = _size.h < oldSize.h ? _size.h : oldSize.h;
- op = oldArray;
- np = _array;
- for( int i = 0; i < min.h; ++i ) {
- for( int j = 0; j < min.w; ++j ) {
- *np++ = *op++;
- }
- op += oldSize.w - min.w;
- np += _size.w - min.w;
- }
- oldArray -= oldSize.w;
- delete [] oldArray;
- }
- }
-
- @end-code;
-
- @begin-code Code "CellArray::Prototype for Resize( WSize const & size )"
-
- public:
- void Resize( WSize const & size );
-
- @end-code;
-
- @begin-code Code "CellArray::Randomize()"
-
- void CellArray::Randomize()
- {
- char *end = _array + _size.w * _size.h;
- for( char *p = _array; p < end; ++p ) {
- *p = rand() < RAND_MAX / 4 ? ON : OFF;
- if( *p ) ++_count;
- }
- }
-
- @end-code;
-
- @begin-code Code "CellArray::Prototype for Randomize()"
-
- public:
- void Randomize();
-
- @end-code;
-
- @begin-code Code "CellArray::NextGeneration( Rules const &rules )"
-
- void CellArray::NextGeneration( Rules const &rules )
- {
-
- // notice that we allocated an extra row on the top and bottom to allow us to
- // wrap the universe around without worrying about going off the start/end of memory
-
- char *p;
- char *end = _array + _size.w * _size.h;
-
- // copy the first row after the last row and the last row before the first row to simulate wrap
- char *lastRow = end - _size.w;
- char *preTopRow = _array - _size.w;
- char *topRow = _array;
- char *postLastRow = end;
- for( int i = 0; i < _size.w; ++i ) {
- *preTopRow++ = *lastRow++;
- *postLastRow++ = *topRow++;
- }
-
- // make a pointer for each neighbour of the cell, and march them along
- // the array simultaneously. The avoids the MANY multiplies involved
- // in the 8 double indexes for each neighbour calculation
- char *a = _array - 1;
- char *b = _array + 1;
- char *c = _array - _size.w - 1;
- char *d = _array - _size.w;
- char *e = _array - _size.w + 1;
- char *f = _array + _size.w - 1;
- char *g = _array + _size.w;
- char *h = _array + _size.w + 1;
- for( p = _array; p < end; ++p ) {
- int neighbors = (*a&ON)+(*b&ON)+(*c&ON)+(*d&ON)+(*e&ON)+(*f&ON)+(*g&ON)+(*h&ON);
- if( *p & ON ) {
- if( rules.Death( neighbors ) ) {
- *p &= ~NEXT;
- } else {
- *p |= NEXT;
- }
- } else {
- if( rules.Birth( neighbors ) ) {
- *p |= NEXT;
- } else {
- *p &= ~NEXT;
- }
- }
- ++a;++b;++c;++d;++e;++f;++g;++h;
- }
- _count = 0;
- for( p = _array; p < end; ++p ) {
- if( *p & ON ) {
- *p |= PREV;
- } else {
- *p &= ~PREV;
- }
- if( *p & NEXT ) {
- ++_count;
- *p |= ON;
- } else {
- *p &= ~ON;
- }
- *p &= ~NEXT;
- }
- }
-
- @end-code;
-
- @begin-code Code "CellArray::Prototype for NextGeneration( Rules const &rules )"
-
- public:
- void NextGeneration( Rules const &rules );
-
- @end-code;
-
- @begin-code Code "CellArray::ToggleCell( int x, int y )"
-
- bool CellArray::ToggleCell( int x, int y )
- {
- char *p = _array + y * _size.w + x;
- bool on;
- *p ^= ON;
- on = ( *p & ON ) != 0;
- if( on ) {
- *p &= ~PREV;
- ++_count;
- } else {
- *p |= PREV;
- --_count;
- }
- return( on );
- }
-
- @end-code;
-
- @begin-code Code "CellArray::Prototype for ToggleCell( int x, int y )"
-
- public:
- bool ToggleCell( int x, int y );
-
- @end-code;
-
- @begin-code Code "CellArray::Clear()"
-
- void CellArray::Clear()
- {
- _count = 0;
- char *end = _array + _size.w * _size.h;
- for( char *p = _array; p < end; ++p ) {
- *p = OFF;
- }
- }
-
- @end-code;
-
- @begin-code Code "CellArray::Prototype for Clear()"
-
- public:
- void Clear();
-
- @end-code;
-
- @begin-code Code "CellArray::Count()"
-
- int CellArray::Count()
- {
- return( _count );
- }
-
- @end-code;
-
- @begin-code Code "CellArray::Prototype for Count()"
-
- public:
- int Count();
-
- @end-code;
- @end;
-