Brownian tree/C++ animated: Difference between revisions

Content added Content deleted
mNo edit summary
Line 16: Line 16:


//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
class point
struct point
{
{
point() : x(0), y(0) { }
public:
point() { x = y = 0; }
point( int a, int b ) : x(a), y(b) { }
point( int a, int b ) { x = a; y = b; }
void set( int a, int b ) { x = a; y = b; }
void set( int a, int b ) { x = a; y = b; }
int x, y;
int x, y;
Line 26: Line 25:


//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
class movers
struct movers
{
{
public:
point pos;
point pos;
bool moving;
bool moving;
Line 86: Line 84:
BITMAPINFO infoheader;
BITMAPINFO infoheader;
BITMAP bitmap;
BITMAP bitmap;
DWORD* dwpBits;
DWORD wb;
DWORD wb;
HANDLE file;


GetObject( bmp, sizeof( bitmap ), &bitmap );
GetObject( bmp, sizeof( bitmap ), &bitmap );


dwpBits = new DWORD[bitmap.bmWidth * bitmap.bmHeight];
DWORD* dwpBits = new DWORD[bitmap.bmWidth * bitmap.bmHeight];
ZeroMemory( dwpBits, bitmap.bmWidth * bitmap.bmHeight * sizeof( DWORD ) );
ZeroMemory( dwpBits, bitmap.bmWidth * bitmap.bmHeight * sizeof( DWORD ) );
ZeroMemory( &infoheader, sizeof( BITMAPINFO ) );
ZeroMemory( &infoheader, sizeof( BITMAPINFO ) );
Line 111: Line 107:
GetDIBits( hdc, bmp, 0, height, ( LPVOID )dwpBits, &infoheader, DIB_RGB_COLORS );
GetDIBits( hdc, bmp, 0, height, ( LPVOID )dwpBits, &infoheader, DIB_RGB_COLORS );


file = CreateFile( path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
HANDLE file = CreateFile( path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
WriteFile( file, &fileheader, sizeof( BITMAPFILEHEADER ), &wb, NULL );
WriteFile( file, &fileheader, sizeof( BITMAPFILEHEADER ), &wb, NULL );
WriteFile( file, &infoheader.bmiHeader, sizeof( infoheader.bmiHeader ), &wb, NULL );
WriteFile( file, &infoheader.bmiHeader, sizeof( infoheader.bmiHeader ), &wb, NULL );
Line 120: Line 116:
}
}


HDC getDC() { return hdc; }
HDC getDC() const { return hdc; }
int getWidth() { return width; }
int getWidth() const { return width; }
int getHeight() { return height; }
int getHeight() const { return height; }


private:
private:
Line 167: Line 163:
void drawGrid()
void drawGrid()
{
{
DWORD clr;
BYTE g;
_bmp.clear();
_bmp.clear();


Line 175: Line 168:
for( int x = 0; x < MAX_SIDE; x++ )
for( int x = 0; x < MAX_SIDE; x++ )
{
{
g = _grid[x][y];
BYTE g = _grid[x][y];
if( g != NONE )
if( g != NONE )
{
{
clr = g > MOVER ? _color[g - 2] : RGB( 0, 255, 0 );
DWORD clr = g > MOVER ? _color[g - 2] : RGB( 0, 255, 0 );
SetPixel( _bmp.getDC(), x, y, clr );
SetPixel( _bmp.getDC(), x, y, clr );
}
}
Line 200: Line 193:
m->moving = true;
m->moving = true;
int x = MAX_SIDE - MAX_SIDE / 2, y = MAX_SIDE / 4, a, b;
int x = MAX_SIDE - MAX_SIDE / 2, y = MAX_SIDE / 4, a, b;
do
while( true )
{
{
a = rand() % x + y; b = rand() % x + y;
a = rand() % x + y; b = rand() % x + y;
if( _grid[a][b] == NONE ) break;
} while ( _grid[a][b] != NONE )
}


m->pos.set( a, b );
m->pos.set( a, b );
Line 212: Line 204:
void startMovers()
void startMovers()
{
{
movers* m;
for( int c = 0; c < MAX_MOVERS; c++ )
for( int c = 0; c < MAX_MOVERS; c++ )
{
{
m = &_movers[c];
movers* m = &_movers[c];
addMover( m );
addMover( m );
}
}
Line 263: Line 254:
void moveMovers()
void moveMovers()
{
{
movers* mm;
bool found = false;
bool found = false;
for( int m = 0; m < MAX_MOVERS; m++ )
for( int m = 0; m < MAX_MOVERS; m++ )
{
{
mm = &_movers[m];
movers* mm = &_movers[m];
if( !mm->moving ) continue;
if( !mm->moving ) continue;
found = true;
found = true;