📄 fallingblocks.cpp
字号:
//-----------------------------------------------------------------------------
// File: FallingBlocks.cpp
//-----------------------------------------------------------------------------
#define STRICT
#include <windows.h>
#include <ddraw.h>
#include <mmsystem.h>
#include "resource.h"
#include "ddutil.h"
#include "BlockList.h"
#include "FlooredBlocks.h"
#include "Shape.h"
//-----------------------------------------------------------------------------
// Defines, constants, and global variables
//-----------------------------------------------------------------------------
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
#define WINDOW_WIDTH 320
#define WINDOW_HEIGHT 375
#define BITMAP_WIDTH 329
#define BITMAP_HEIGHT 400
#define BLOCK_START 379
#define BLOCK_DIAMETER 21
#define CURR_SHAPE_X 3
#define CURR_SHAPE_Y -3
#define NEXT_SHAPE_X 10
#define NEXT_SHAPE_Y 6
#define SCORE_X 230
#define SCORE_Y 105
#define DELAY_TO_MOVE_DOWN_LEVEL1 10
#define DELAY_TO_MOVE_DOWN_LEVEL2 6
#define DELAY_TO_MOVE_DOWN_LEVEL3 3
#define DELAY_TO_MOVE_DOWN_LEVEL4 1
#define ROWS 15
#define COLS 9
#define NORMAL_GAME 7
#define CRAZY_GAME 11
#define GAMEOVERTEXT TEXT("G A M E O V E R")
CDisplay* g_pDisplay = NULL;
CSurface* g_pSecondarySurface = NULL;
CSurface* g_pGameOverSurface = NULL;
CSurface* g_pScoreSurface = NULL;
CSurface* g_pPauseSurface = NULL;
RECT g_rcViewport;
RECT g_rcScreen;
RECT g_rcBackground = { 0 , 0, WINDOW_WIDTH, WINDOW_HEIGHT };
RECT g_rcBlock = {0, BLOCK_START, BLOCK_DIAMETER, BLOCK_START+BLOCK_DIAMETER};
RECT g_rcBoundary = { 1,-5, 9,16 };
CFlooredBlocks g_FlooredBlocks(g_rcBoundary);
BOOL g_bActive = FALSE;
DWORD g_dwLastTick;
CShape* g_pCurrShape;
CShape* g_pNextShape;
bool g_bIsGameOver=false;
bool g_bIsPaused = false;
long g_nScore =0;
long g_nPulse=0;
long g_nDelayToMoveDown = 10;
HINSTANCE g_hInstance =0;
//-----------------------------------------------------------------------------
// Function-prototypes
//-----------------------------------------------------------------------------
LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
HRESULT WinInit( HINSTANCE hInst, int nCmdShow, HWND* phWnd, HACCEL* phAccel );
HRESULT InitDirectDraw( HWND hWnd );
VOID FreeDirectDraw();
HRESULT ProcessNextFrame( HWND hWnd );
HRESULT DisplayFrame();
HRESULT RestoreSurfaces();
BOOL CALLBACK AboutDlgProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
case IDCANCEL:
EndDialog(hwndDlg, wParam);
return TRUE;
}
}
return FALSE;
}
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: Entry point to the program. Initializes everything and calls
// UpdateFrame() when idle from the message pump.
//-----------------------------------------------------------------------------
int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, int nCmdShow )
{
MSG msg;
HWND hWnd;
HACCEL hAccel;
srand( GetTickCount() );
if( FAILED( WinInit( hInst, nCmdShow, &hWnd, &hAccel ) ) )
return FALSE;
if( FAILED( InitDirectDraw( hWnd ) ) )
{
MessageBox( hWnd, TEXT("DirectDraw init failed. ")
TEXT("The sample will now exit. "), TEXT("DirectDraw Sample"),
MB_ICONERROR | MB_OK );
return FALSE;
}
g_dwLastTick = timeGetTime();
while( TRUE )
{
// Look for messages, if none are found then
// update the state and display it
if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
{
if( 0 == GetMessage(&msg, NULL, 0, 0 ) )
{
// WM_QUIT was posted, so exit
return (int)msg.wParam;
}
// Translate and dispatch the message
if( 0 == TranslateAccelerator( hWnd, hAccel, &msg ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
else
{
if( g_bActive )
{
// Move the sprites, blt them to the back buffer, then
// flip or blt the back buffer to the primary buffer
if( FAILED( ProcessNextFrame( hWnd ) ) )
{
SAFE_DELETE( g_pDisplay );
MessageBox( hWnd, TEXT("Displaying the next frame failed. ")
TEXT("The sample will now exit. "), TEXT("DirectDraw Sample"),
MB_ICONERROR | MB_OK );
return FALSE;
}
}
else
{
// Make sure we go to sleep if we have nothing else to do
WaitMessage();
// Ignore time spent inactive
g_dwLastTick = timeGetTime();
}
}
}
}
//-----------------------------------------------------------------------------
// Name: WinInit()
// Desc: Init the window
//-----------------------------------------------------------------------------
HRESULT WinInit( HINSTANCE hInst, int nCmdShow, HWND* phWnd, HACCEL* phAccel )
{
WNDCLASSEX wc;
HWND hWnd;
HACCEL hAccel;
g_hInstance = hInst;
// Register the Window Class
wc.cbSize = sizeof(wc);
wc.lpszClassName = TEXT("WindowedMode");
wc.lpfnWndProc = MainWndProc;
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.hInstance = hInst;
wc.hIcon = LoadIcon( hInst, MAKEINTRESOURCE(IDI_MAIN) );
wc.hIconSm = LoadIcon( hInst, MAKEINTRESOURCE(IDI_MAIN) );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
if( RegisterClassEx( &wc ) == 0 )
return E_FAIL;
// Load keyboard accelerators
hAccel = LoadAccelerators( hInst, MAKEINTRESOURCE(IDR_MAIN_ACCEL) );
// Calculate the proper size for the window given a client of 640x480
DWORD dwFrameWidth = GetSystemMetrics( SM_CXSIZEFRAME );
DWORD dwFrameHeight = GetSystemMetrics( SM_CYSIZEFRAME );
DWORD dwMenuHeight = GetSystemMetrics( SM_CYMENU );
DWORD dwCaptionHeight = GetSystemMetrics( SM_CYCAPTION );
DWORD dwWindowWidth = WINDOW_WIDTH + dwFrameWidth * 2;
DWORD dwWindowHeight = WINDOW_HEIGHT + dwFrameHeight * 2 +
dwMenuHeight + dwCaptionHeight;
// Create and show the main window
DWORD dwStyle = WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX;
hWnd = CreateWindowEx( 0, TEXT("WindowedMode"), TEXT("Falling Blocks"),
dwStyle, CW_USEDEFAULT, CW_USEDEFAULT,
dwWindowWidth, dwWindowHeight, NULL, NULL, hInst, NULL );
if( hWnd == NULL )
return E_FAIL;
ShowWindow( hWnd, nCmdShow );
UpdateWindow( hWnd );
*phWnd = hWnd;
*phAccel = hAccel;
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_1, MF_CHECKED);
::CheckMenuItem(::GetMenu(hWnd), ID_LEVEL_NORMAL, MF_CHECKED);
return S_OK;
}
void NewGame()
{
g_FlooredBlocks.Destroy();
g_nScore = 0;
g_bIsGameOver= false;
if (g_pCurrShape)
delete g_pCurrShape;
g_pCurrShape = new CShape(&g_FlooredBlocks);
g_pCurrShape->CreateRandShape();
g_pCurrShape->MoveTo(CURR_SHAPE_X,CURR_SHAPE_Y);
if (g_pNextShape)
delete g_pNextShape;
g_pNextShape = new CShape(&g_FlooredBlocks);
g_pNextShape->CreateRandShape();
g_pNextShape->MoveTo(NEXT_SHAPE_X, NEXT_SHAPE_Y );
}
//-----------------------------------------------------------------------------
// Name: InitDirectDraw()
// Desc: Create the DirectDraw object, and init the surfaces
//-----------------------------------------------------------------------------
HRESULT InitDirectDraw( HWND hWnd )
{
LPDIRECTDRAWPALETTE pDDPal = NULL;
HRESULT hr;
g_pDisplay = new CDisplay();
if( FAILED( hr = g_pDisplay->CreateWindowedDisplay( hWnd, WINDOW_WIDTH, WINDOW_HEIGHT ) ) )
{
MessageBox( hWnd, TEXT("Failed initializing DirectDraw."),
TEXT("DirectDraw Sample"), MB_ICONERROR | MB_OK );
return hr;
}
// Create and set the palette when in palettized color
if( FAILED( hr = g_pDisplay->CreatePaletteFromBitmap( &pDDPal, "IDB_Graphics" ) ) )
return hr;
g_pDisplay->SetPalette( pDDPal );
SAFE_RELEASE( pDDPal );
// Create a surface, and draw a bitmap resource on it.
if( FAILED( hr = g_pDisplay->CreateSurfaceFromBitmap( &g_pSecondarySurface, "IDB_Graphics",
BITMAP_WIDTH, BITMAP_HEIGHT ) ) )
return hr;
// Create a surface, and draw text to it.
if( FAILED( hr = g_pDisplay->CreateSurfaceFromText( &g_pGameOverSurface, NULL, GAMEOVERTEXT,
RGB(0,0,0), RGB(255, 255, 0) ) ) )
return hr;
if( FAILED( hr = g_pDisplay->CreateSurfaceFromText( &g_pScoreSurface, NULL, "xxxxxx",
RGB(0,0,0), RGB(255, 255, 0) ) ) )
return hr;
if( FAILED( hr = g_pDisplay->CreateSurfaceFromText( &g_pPauseSurface, NULL, "Paused...",
RGB(0,0,0), RGB(255, 255, 0) ) ) )
return hr;
CShape::SetMaxNoOfShapesAllowed(NORMAL_GAME);
NewGame();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: FreeDirectDraw()
// Desc: Release all the DirectDraw objects
//-----------------------------------------------------------------------------
VOID FreeDirectDraw()
{
SAFE_DELETE( g_pSecondarySurface );
SAFE_DELETE( g_pGameOverSurface );
SAFE_DELETE( g_pScoreSurface );
SAFE_DELETE( g_pPauseSurface);
SAFE_DELETE( g_pDisplay );
SAFE_DELETE( g_pCurrShape);
SAFE_DELETE( g_pNextShape);
}
void MoveDownShape()
{
if (g_bIsGameOver)
return;
if (g_pCurrShape->MoveDown() == false) {
g_pCurrShape->ConvertToSpaceCoord();
// ::PlaySound(MAKEINTRESOURCE( IDR_SOUNDPLACED), g_hInstance, SND_RESOURCE | SND_ASYNC);
g_FlooredBlocks.Insert(*g_pCurrShape);
delete g_pCurrShape;
g_pCurrShape = g_pNextShape;
g_pCurrShape->MoveTo(CURR_SHAPE_X,CURR_SHAPE_Y);
g_pNextShape = new CShape(&g_FlooredBlocks);
if (g_pNextShape->CreateRandShape(3,-4) == false) {
OutputDebugString("Creation of New Shape Failed\n");
}
g_pNextShape->MoveTo(NEXT_SHAPE_X, NEXT_SHAPE_Y);
}
if (g_FlooredBlocks.IsGameOver()) {
g_bIsGameOver=true;
::PlaySound(MAKEINTRESOURCE( IDR_SOUNDGAMEOVER), g_hInstance, SND_RESOURCE | SND_ASYNC);
}
short nRows;
nRows = g_FlooredBlocks.CheckAndRemoveContinuousBlocks();
g_nScore += 10 * nRows * nRows * ( 11 - g_nDelayToMoveDown);
}
//-----------------------------------------------------------------------------
// Name: MainWndProc()
// Desc: The main window procedure
//-----------------------------------------------------------------------------
LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch (msg)
{
case WM_COMMAND:
switch( LOWORD(wParam) )
{
case IDM_EXIT:
// Received key/menu command to exit app
PostMessage( hWnd, WM_CLOSE, 0, 0 );
return 0L;
case ID_FILE_NEWGAME:
NewGame();
return 0L;
case ID_LEVEL_1:
g_nDelayToMoveDown = DELAY_TO_MOVE_DOWN_LEVEL1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -