📄 life.c
字号:
DisplayDialog( "AboutBox", About );
break;
case MENU_WRAP_AROUND:
CurvedSpace = TRUE;
break;
case MENU_BOUNDED_EDGES:
CurvedSpace = FALSE;
break;
case MENU_SINGLE_STEP:
ToSingleStepMode();
break;
case MENU_PAUSE:
ToPauseMode();
break;
case MENU_RESUME:
ToResumeMode();
break;
case MENU_FLIP_PATTERNS:
ToPatternFlipMode();
break;
case MENU_SELECT:
ToSelectMode();
break;
case MENU_SAVE:
WritePatternFile();
break;
case MENU_LOAD:
LoadNewPattern();
break;
case MENU_FASTEST:
TimerTurbo();
ToResumeMode();
break;
case MENU_CLEAR:
Clear();
if( !SelectOn() ) ToPauseMode();
break;
case MENU_GRID:
DrawGrid = !DrawGrid;
RePaint();
break;
case MENU_RANDOM:
Randomize();
break;
case MENU_LOAD_BITMAP:
LoadNewBitmap();
SelectOff();
ReSizeArray( WindowWidth, WindowHeight, 0 );
RePaint();
break;
case MENU_NEW_RULES:
DisplayDialog( "Rules", Rules );
break;
case MENU_ROTATE_M90:
TransformPatterns( ReflectAboutXequalY );
TransformPatterns( ReflectAboutYAxis );
/* fall through */
case MENU_ROTATE_180:
TransformPatterns( ReflectAboutXequalY );
TransformPatterns( ReflectAboutYAxis );
/* fall through */
case MENU_ROTATE_P90:
TransformPatterns( ReflectAboutXequalY );
TransformPatterns( ReflectAboutYAxis );
CreatePatternMenu();
break;
case MENU_REFLECT_X:
TransformPatterns( ReflectAboutXequalY );
TransformPatterns( ReflectAboutYAxis );
TransformPatterns( ReflectAboutXequalY );
CreatePatternMenu();
break;
case MENU_REFLECT_Y:
TransformPatterns( ReflectAboutYAxis );
CreatePatternMenu();
break;
default:
break;
}
}
static int Check[] = { MF_UNCHECKED, MF_CHECKED };
static int Gray[] = { MF_GRAYED, MF_ENABLED };
static void InitMenu( HMENU mh )
/*******************************
Initialize the menu display. Disable any items which are not applicable.
*/
{
int i;
CheckMenuItem( mh, MENU_WRAP_AROUND, Check[ CurvedSpace ] );
CheckMenuItem( mh, MENU_BOUNDED_EDGES, Check[ !CurvedSpace ] );
CheckMenuItem( mh, MENU_PAUSE, Check[ Mode == MENU_PAUSE ] );
CheckMenuItem( mh, MENU_RESUME, Check[ Mode == MENU_RESUME ] );
CheckMenuItem( mh, MENU_SINGLE_STEP, Check[ MouseMode == MENU_SINGLE_STEP ]);
CheckMenuItem( mh, MENU_SELECT, Check[ MouseMode == MENU_SELECT ]);
CheckMenuItem( mh, MENU_FLIP_PATTERNS, Check[ MouseMode == MENU_FLIP_PATTERNS ]);
EnableMenuItem( mh, MENU_SAVE, Gray[ SelectOn() ]);
CheckMenuItem( mh, MENU_GRID, Check[ DrawGrid ]);
for( i = 0; i < NumberPatterns; ++i ) {
CheckMenuItem( mh, MENU_PATTERN+i, Check[ IsCurrPattern( i ) ] );
}
}
static BOOL SingleStep()
/***********************
Process a single step request
*/
{
if( Mode != MENU_SINGLE_STEP ) return( FALSE );
NextGeneration();
return( TRUE );
}
extern void SetCaption()
/***********************
Set the caption to indicate generation number, etc.
*/
{
static BOOL IconCaptionSet = FALSE;
if( IsAnIcon ) {
if( !IconCaptionSet ) {
SetWindowText( WinHandle, "Life" );
IconCaptionSet = TRUE;
}
} else {
IconCaptionSet = FALSE;
sprintf( Buffer, "Life: Pop - %ld; Gen - %ld; Max - %ld",
Population, Generation, (long)ArraySizeX*ArraySizeY);
SetWindowText( WinHandle, Buffer );
}
}
static void Cleanup()
/********************
Free up all our memory, etc
*/
{
DeleteObject( Pen );
DeleteObject( Brush );
FreeArray( CellArray );
FreePatterns();
FiniBitMap();
FiniTimer();
}
LONG _EXPORT FAR PASCAL WindowProc( HWND win_handle, unsigned msg,
UINT wparam, LONG lparam )
/**************************************************************
Handle messages for the main application window.
*/
{
WinHandle = win_handle;
switch( msg ) {
case WM_CREATE:
InitBitMap();
CreatePatternMenu();
break;
case WM_INITMENU:
InitMenu( (HMENU)wparam );
break;
case WM_COMMAND:
MenuItem( LOWORD( wparam ) );
break;
case WM_PAINT:
RePaint();
break;
case WM_TIMER:
if( Mode != MENU_RESUME ) break;
NewTimer();
NextGeneration();
CheckTimerRate();
break;
case WM_SIZE:
SelectOff();
if( ReSizeArray( LOWORD( lparam ), HIWORD( lparam ), wparam ) ) break;
NoMemory();
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return( DefWindowProc( WinHandle, msg, wparam, lparam ) );
break;
case WM_LBUTTONUP:
EndSelect( LOWORD( lparam ), HIWORD( lparam ) );
break;
case WM_LBUTTONDOWN:
if( StartSelect( LOWORD( lparam ), HIWORD( lparam ) ) ) break;
case WM_RBUTTONDOWN:
if( SingleStep() ) break;
/* fall through */
case WM_MOUSEMOVE:
if( MoveSelect( wparam, LOWORD( lparam ), HIWORD( lparam ) ) ) break;
FlipPattern( wparam, LOWORD( lparam ), HIWORD( lparam ) );
break;
case WM_KEYDOWN:
SingleStep();
break;
default:
return( DefWindowProc( WinHandle, msg, wparam, lparam ) );
}
return( 0L );
}
static void UpdateCell( HDC dc, cell_ptr cell, pixels x, pixels y )
/******************************************************************
Update the cell at location (x,y)
*/
{
if( cell->alive && !cell->drawn ) {
BlitBitMap( dc, x, y );
cell->drawn = 1;
} else if( !cell->alive && cell->drawn ) {
UnBlitBitMap( dc, x, y );
cell->drawn = 0;
}
}
extern BOOL TurnOnCell( HDC dc, cell_ptr cell, pixels x, pixels y )
/******************************************************************
Turn on the "cell" at grid location (x,y) if needed
*/
{
if( cell->alive ) return( FALSE );
++Population;
cell->alive = 1;
UpdateCell( dc, cell, x, y );
return( TRUE );
}
extern BOOL TurnOffCell( HDC dc, cell_ptr cell, pixels x, pixels y )
/******************************************************************
Turn off the "cell" at grid location (x,y) if needed
*/
{
cell = CellPointer( x, y );
if( !cell->alive ) return( FALSE );
--Population;
cell->alive = 0;
UpdateCell( dc, cell, x, y );
return( TRUE );
}
static void RePaint()
/********************
Re-draw the entire screen. It's been trashed.
*/
{
PAINTSTRUCT ps;
pixels x,y;
cell_ptr cell;
InvalidateRect( WinHandle, NULL, TRUE );
BeginPaint( WinHandle, &ps );
SelectObject( ps.hdc, Pen );
for( x = 0; x < ArraySizeX; ++x ) {
for( y = 0; y < ArraySizeY; ++y ) {
cell = CellPointer( x, y );
if( cell->alive ) {
BlitBitMap( ps.hdc, x, y );
cell->drawn = 1;
}
}
}
if( DrawGrid && !IsAnIcon ) {
for( x = 0; x < ArraySizeX; ++x ) {
MoveTo( ps.hdc, x*BitInfo.bmWidth, 0 );
LineTo( ps.hdc, x*BitInfo.bmWidth, WindowHeight );
}
for( y = 0; y < ArraySizeX; ++y ) {
MoveTo( ps.hdc, 0, y*BitInfo.bmHeight );
LineTo( ps.hdc, WindowWidth, y*BitInfo.bmHeight );
}
MoveTo( ps.hdc, 0, 0 );
LineTo( ps.hdc, 0, WindowHeight );
LineTo( ps.hdc, WindowWidth, WindowHeight );
LineTo( ps.hdc, WindowWidth, 0 );
LineTo( ps.hdc, 0, 0 );
}
EndPaint( WinHandle, &ps );
FlipSelect();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -