📄 fire.cpp
字号:
///////////////////////////////////////////////////////////////////////////////
// Fire.cpp
// Date: 2004-8-5 21:16
// A moving ball.
//
///////////////////////////////////////////////////////////////////////////////
#include <assert.h>
#include "Fire.h"
#define ID_TIMER 1
///////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, char* cmdParam, int cmdShow )
{
char className[] = "Fire";
MovingBall MovingBallClass( WindowsProcedure, className, hInst );
MovingBallClass.Register();
WinMaker win( "Fire", className, hInst );
win.Show( cmdShow );
MSG msg;
int status;
while( ( status = ::GetMessage( & msg, NULL, 0, 0 ) ) != 0 )
{
if ( status == -1 )
return -1;
::TranslateMessage( & msg );
::DispatchMessage( & msg );
}
return msg.wParam;
}
///////////////////////////////////////////////////////////////////////////////
MovingBall::MovingBall( WNDPROC wndProc, const char* className, HINSTANCE hInstance )
{
_class.style = 0;
_class.lpfnWndProc = wndProc; // Windows procedure: mandatory
_class.cbClsExtra = 0;
_class.cbWndExtra = 0;
_class.hInstance = hInstance;
_class.hIcon = 0; // Owner of class: mandatory
_class.hCursor = ::LoadCursor( 0, IDC_ARROW );
_class.hbrBackground = (HBRUSH) ( COLOR_WINDOW + 1 ); // Optional
_class.lpszMenuName = 0;
_class.lpszClassName = className; // Mandatory
}
WinMaker::WinMaker( const char* szCaption, const char* className, HINSTANCE hInstance )
{
DWORD dwStyle = WS_OVERLAPPEDWINDOW;
dwStyle &= ~WS_SIZEBOX;
dwStyle &= ~WS_MAXIMIZEBOX;
dwStyle &= ~WS_MINIMIZEBOX;
_hWnd = ::CreateWindow(
className, // Name of a registered window class
szCaption, // Window caption
dwStyle, // Window style
CW_USEDEFAULT, // x position
CW_USEDEFAULT, // y position
640, // width
480, // height
0, // Handle to parent window
0, // Handle to menu
hInstance, // Application instance
0 ); // Window creation data
}
LRESULT CALLBACK WindowsProcedure( HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam )
{
static HBITMAP hbmpBkground = NULL,
hbmpFireball = NULL,
hbmpMask = NULL;
static int cxClient, cyClient,
xCenter, yCenter;
static FireBall Fireballs[ 50 ];
static int countFireball = 0;
HINSTANCE hInst = NULL;
HDC hdc = NULL,
hdcMem = NULL;
switch( uMessage )
{
case WM_CREATE:
hInst = ( (LPCREATESTRUCT) lParam )->hInstance;
assert( hInst );
hbmpBkground = ::LoadBitmap( hInst, TEXT( "bground" ) );
assert( hbmpBkground );
hbmpFireball = ::LoadBitmap( hInst, TEXT( "fireball" ) );
assert( hbmpFireball );
hbmpMask = ::LoadBitmap( hInst, TEXT( "mask" ) );
assert( hbmpMask );
::SetTimer( hWnd, ID_TIMER, 30, NULL );
return 0;
case WM_SIZE:
cxClient = LOWORD( lParam );
cyClient = HIWORD( lParam );
return 0;
case WM_TIMER:
int i;
if ( countFireball == 0 ) // Set the broken point.
{
xCenter = rand() % cxClient;
yCenter = rand() % cyClient;
for ( i = 0; i < 50; i++ ) // Set the fireball
{
Fireballs[ i ].xPos = xCenter;
Fireballs[ i ].yPos = yCenter;
Fireballs[ i ].countTime = 0;
if ( i % 2 == 0 )
{
Fireballs[ i ].cxMove = -rand() % 30;
Fireballs[ i ].cyMove = -rand() % 30;
}
if ( i % 2 == 1 )
{
Fireballs[ i ].cxMove = rand() % 30;
Fireballs[ i ].cyMove = rand() % 30;
}
if ( i % 4 == 2 )
{
Fireballs[ i ].cxMove = -rand() % 30;
Fireballs[ i ].cyMove = rand() % 30;
}
if ( i % 4 == 3 )
{
Fireballs[ i ].cxMove = rand() % 30;
Fireballs[ i ].cyMove = -rand() % 30;
}
Fireballs[ i ].bIsExist = TRUE;
}
countFireball = 50;
} // end if ( countFireball == 0 ) //
hdc = ::GetDC( hWnd );
assert( hdc );
hdcMem = ::CreateCompatibleDC( hdc );
assert( hdcMem );
::SelectObject( hdcMem, hbmpBkground );
::BitBlt(
hdc,
0, 0,
cxClient, cyClient,
hdcMem,
0, 0,
SRCCOPY );
for ( i = 0; i < 50; i++ )
{
if ( Fireballs[ i ].bIsExist )
{
::SelectObject( hdcMem, hbmpMask );
::BitBlt(
hdc,
Fireballs[ i ].xPos, Fireballs[ i ].yPos,
10, 10,
hdcMem,
0, 0,
SRCAND );
::SelectObject( hdcMem, hbmpFireball );
::BitBlt(
hdc,
Fireballs[ i ].xPos, Fireballs[ i ].yPos,
10, 10,
hdcMem,
0, 0,
SRCPAINT );
Fireballs[ i ].xPos += Fireballs[ i ].cxMove;
Fireballs[ i ].yPos += Fireballs[ i ].cyMove;
Fireballs[ i ].countTime++;
if ( Fireballs[ i ].xPos <= -10 ||
Fireballs[ i ].xPos > cxClient ||
Fireballs[ i ].yPos < -10 ||
Fireballs[ i ].yPos > cyClient ||
Fireballs[ i ].countTime > 50 )
{
Fireballs[ i ].bIsExist = FALSE;
countFireball--;
}
} // end if ( Fireballs[ i ].bIsExist ) //
} // end for
ReleaseDC( hWnd, hdc );
DeleteDC( hdcMem );
return 0;
case WM_DESTROY:
::KillTimer( hWnd, ID_TIMER );
::PostQuitMessage( 0 );
return 0;
}
return ::DefWindowProc( hWnd, uMessage, wParam, lParam );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -