📄 fallingblocks.cpp
字号:
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_1, MF_CHECKED);
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_2, MF_UNCHECKED);
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_3, MF_UNCHECKED);
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_4, MF_UNCHECKED);
return 0L;
case ID_LEVEL_2:
g_nDelayToMoveDown = DELAY_TO_MOVE_DOWN_LEVEL2;
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_1, MF_UNCHECKED);
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_2, MF_CHECKED);
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_3, MF_UNCHECKED);
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_4, MF_UNCHECKED);
return 0L;
case ID_LEVEL_3:
g_nDelayToMoveDown = DELAY_TO_MOVE_DOWN_LEVEL3;
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_1, MF_UNCHECKED);
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_2, MF_UNCHECKED);
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_3, MF_CHECKED);
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_4, MF_UNCHECKED);
return 0L;
case ID_LEVEL_4:
g_nDelayToMoveDown = DELAY_TO_MOVE_DOWN_LEVEL4;
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_1, MF_UNCHECKED);
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_2, MF_UNCHECKED);
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_3, MF_UNCHECKED);
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_4, MF_CHECKED);
return 0L;
case ID_LEVEL_NORMAL:
CShape::SetMaxNoOfShapesAllowed(NORMAL_GAME);
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_CRAZY, MF_UNCHECKED);
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_NORMAL, MF_CHECKED);
return 0L;
case ID_LEVEL_CRAZY:
CShape::SetMaxNoOfShapesAllowed(CRAZY_GAME);
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_NORMAL, MF_UNCHECKED);
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_CRAZY, MF_CHECKED);
return 0L;
case ID_HELP_ABOUT:
DialogBox(g_hInstance,
MAKEINTRESOURCE(IDD_ABOUT), hWnd, (DLGPROC)AboutDlgProc);
break;
}
break; // Continue with default processing
case WM_ACTIVATE:
if (wParam == WA_ACTIVE || wParam == WA_CLICKACTIVE)
g_bIsPaused = false;
if (wParam == WA_INACTIVE)
g_bIsPaused = true;
break;
case WM_PAINT:
// Update the screen if we need to refresh. This case occurs
// when in windowed mode and the window is behind others.
// The app will not be active, but it will be visible.
if( g_pDisplay )
{
// Display the new position of the sprite
if( DisplayFrame() == DDERR_SURFACELOST )
{
// If the surfaces were lost, then restore and try again
RestoreSurfaces();
DisplayFrame();
}
}
break; // Continue with default processing to validate the region
case WM_QUERYNEWPALETTE:
if( g_pDisplay )
{
// If we are in windowed mode with a desktop resolution in 8 bit
// color, then the palette we created during init has changed
// since then. So get the palette back from the primary
// DirectDraw surface, and set it again so that DirectDraw
// realises the palette, then release it again.
LPDIRECTDRAWPALETTE pDDPal = NULL;
g_pDisplay->GetFrontBuffer()->GetPalette( &pDDPal );
g_pDisplay->GetFrontBuffer()->SetPalette( pDDPal );
SAFE_RELEASE( pDDPal );
}
break;
case WM_GETMINMAXINFO:
{
// Don't allow resizing in windowed mode.
// Fix the size of the window to 640x480 (client size)
MINMAXINFO* pMinMax = (MINMAXINFO*) lParam;
DWORD dwFrameWidth = GetSystemMetrics( SM_CXSIZEFRAME );
DWORD dwFrameHeight = GetSystemMetrics( SM_CYSIZEFRAME );
DWORD dwMenuHeight = GetSystemMetrics( SM_CYMENU );
DWORD dwCaptionHeight = GetSystemMetrics( SM_CYCAPTION );
pMinMax->ptMinTrackSize.x = WINDOW_WIDTH + dwFrameWidth * 2;
pMinMax->ptMinTrackSize.y = WINDOW_HEIGHT + dwFrameHeight * 2 +
dwMenuHeight + dwCaptionHeight;
pMinMax->ptMaxTrackSize.x = pMinMax->ptMinTrackSize.x;
pMinMax->ptMaxTrackSize.y = pMinMax->ptMinTrackSize.y;
}
return 0L;
case WM_MOVE:
if( g_pDisplay )
g_pDisplay->UpdateBounds();
return 0L;
case WM_EXITMENULOOP:
// Ignore time spent in menu
g_dwLastTick = timeGetTime();
break;
case WM_EXITSIZEMOVE:
// Ignore time spent resizing
g_dwLastTick = timeGetTime();
break;
case WM_SIZE:
// Check to see if we are losing our window...
if( SIZE_MAXHIDE==wParam || SIZE_MINIMIZED==wParam )
g_bActive = FALSE;
else
g_bActive = TRUE;
if( g_pDisplay )
g_pDisplay->UpdateBounds();
break;
case WM_KEYDOWN:
if (g_bIsGameOver == false) {
switch (wParam)
{
case VK_LEFT:
// Process the LEFT ARROW key.
g_pCurrShape->MoveLeft();
// ::PlaySound(MAKEINTRESOURCE( IDR_SOUNDMOVE), g_hInstance, SND_RESOURCE | SND_ASYNC);
break;
case VK_RIGHT:
// Process the RIGHT ARROW key.
g_pCurrShape->MoveRight();
// ::PlaySound(MAKEINTRESOURCE( IDR_SOUNDMOVE), g_hInstance, SND_RESOURCE | SND_ASYNC);
break;
case VK_DOWN:
// Process the DOWN ARROW key.
MoveDownShape();
//::PlaySound(MAKEINTRESOURCE( IDR_SOUNDMOVE), g_hInstance, SND_RESOURCE | SND_ASYNC);
break;
case VK_UP:
// Process the DOWN ARROW key.
//g_pCurrShape->MoveUp();
g_pCurrShape->Rotate();
//::PlaySound(MAKEINTRESOURCE( IDR_SOUNDMOVE), g_hInstance, SND_RESOURCE | SND_ASYNC);
break;
case VK_CLEAR:
// Process the DOWN ARROW key.
while(g_pCurrShape->MoveDown());
MoveDownShape();
//::PlaySound(MAKEINTRESOURCE( IDR_SOUNDMOVE), g_hInstance, SND_RESOURCE | SND_ASYNC);
break;
default:
break;
}
}
break;
case WM_CHAR:
switch (wParam)
{
case 'R':
case 'r':
g_pCurrShape->Rotate();
break;
case ' ':
if (g_bIsGameOver)
NewGame();
break;
case '5':
while (g_pCurrShape->MoveDown());
break;
/*
case 'N':
case 'n':
g_pCurrShape->NextShape();
break;
*/
default:
break;
}
break;
case WM_DESTROY:
// Cleanup and close the app
FreeDirectDraw();
PostQuitMessage( 0 );
return 0L;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
//-----------------------------------------------------------------------------
// Name: ProcessNextFrame()
// Desc: Move the sprites, blt them to the back buffer, then
// flip or blt the back buffer to the primary buffer
//-----------------------------------------------------------------------------
HRESULT ProcessNextFrame( HWND hWnd )
{
HRESULT hr;
// Figure how much time has passed since the last time
DWORD dwCurrTick = timeGetTime();
DWORD dwTickDiff = dwCurrTick - g_dwLastTick;
Sleep(50);
// Don't update if no time has passed
if( dwTickDiff < 100 )
return S_OK;
g_nPulse++;
g_dwLastTick = dwCurrTick;
if (! g_bIsPaused) {
if ((g_nPulse % g_nDelayToMoveDown) == 0)
MoveDownShape();
}
// Check the cooperative level before rendering
if( FAILED( hr = g_pDisplay->GetDirectDraw()->TestCooperativeLevel() ) )
{
switch( hr )
{
case DDERR_EXCLUSIVEMODEALREADYSET:
// Do nothing because some other app has exclusive mode
Sleep(10);
return S_OK;
case DDERR_WRONGMODE:
// The display mode changed on us. Update the
// DirectDraw surfaces accordingly
FreeDirectDraw();
return InitDirectDraw( hWnd );
}
return hr;
}
// Display the sprites on the screen
if( FAILED( hr = DisplayFrame() ) )
{
if( hr != DDERR_SURFACELOST )
return hr;
// The surfaces were lost so restore them
RestoreSurfaces();
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DisplayFrame()
// Desc: Blts a the sprites to the back buffer, then it blts or flips the
// back buffer onto the primary buffer.
//-----------------------------------------------------------------------------
HRESULT DisplayFrame()
{
HRESULT hr;
// Blt the help text on the backbuffer, ignoring errors until the flip
g_pDisplay->Blt( 0, 0, g_pSecondarySurface, &g_rcBackground );
// Blt all the sprites onto the back buffer using color keying,
// ignoring errors until the last blt. Note that all of these sprites
// use the same DirectDraw surface.
g_FlooredBlocks.Display();
g_pCurrShape->Display();
g_pNextShape->Display();
if (g_bIsGameOver) {
// Blt the help text on the backbuffer, ignoring errors until the flip
g_pDisplay->Blt( 80, 150, g_pGameOverSurface, NULL );
} else
if (g_bIsPaused) {
g_pDisplay->Blt(80, 150, g_pPauseSurface, NULL);
}
char szScore[50];
wsprintf(szScore,"%06d",g_nScore);
if( FAILED( hr = g_pScoreSurface->DrawText( 0/*hFont*/, szScore, 0,0,0, RGB(255,255,0) ) ) )
return hr;
g_pDisplay->Blt( SCORE_X, SCORE_Y, g_pScoreSurface, NULL );
// We are in windowed mode so perform a blt from the backbuffer
// to the primary, returning any errors like DDERR_SURFACELOST
if( FAILED( hr = g_pDisplay->Present() ) )
return hr;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestoreSurfaces()
// Desc: Restore all the surfaces, and redraw the sprite surfaces.
//-----------------------------------------------------------------------------
HRESULT RestoreSurfaces()
{
LPDIRECTDRAWPALETTE pDDPal = NULL;
HRESULT hr;
if( FAILED( hr = g_pDisplay->GetDirectDraw()->RestoreAllSurfaces() ) )
return hr;
// We need to release and re-load, and set the palette again to
// redraw the bitmap on the surface. Otherwise, GDI will not
// draw the bitmap on the surface with the right palette
if( FAILED( hr = g_pDisplay->CreatePaletteFromBitmap( &pDDPal, "IDB_Graphics" ) ) )
return hr;
g_pDisplay->SetPalette( pDDPal );
SAFE_RELEASE( pDDPal );
// No need to re-create the surface, just re-draw it.
if( FAILED( hr = g_pSecondarySurface->DrawBitmap( "IDB_Graphics", BITMAP_WIDTH, BITMAP_HEIGHT )))
return hr;
g_FlooredBlocks.Display();
g_pCurrShape->Display();
g_pNextShape->Display();
if (g_bIsGameOver) {
// No need to re-create the surface, just re-draw it.
if( FAILED( hr = g_pGameOverSurface->DrawText( NULL, GAMEOVERTEXT,
80,150, RGB(0,0,0), RGB(255, 255, 0) ) ) )
return hr;
}
return S_OK;
}
//////////////////////////////////////////////
// Display the Block
//////////////////////////////////////////////
void DisplayBlock(SBlock Block)
{
if (Block.nY < 1) return;
RECT rcBlock = g_rcBlock;
rcBlock.left = Block.nColor * BLOCK_DIAMETER;
rcBlock.right = Block.nColor * BLOCK_DIAMETER + BLOCK_DIAMETER;
g_pDisplay->Blt( (DWORD)Block.nX * BLOCK_DIAMETER - 2 ,
(DWORD)Block.nY * BLOCK_DIAMETER ,
g_pSecondarySurface, &rcBlock );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -