Win32 SDK 打砖块游戏(八)

2014-11-24 09:00:43 · 作者: · 浏览: 10
gisterClass(&winclass))
return 0;


int cxNonClient = GetSystemMetrics(SM_CXBORDER)*2 + 10;
int cyNonClient = GetSystemMetrics(SM_CYBORDER) + GetSystemMetrics(SM_CYCAPTION) + 10;

// Create the window, note the use of WS_POPUP
hwnd = CreateWindow(
WINDOW_CLASS_NAME, // class
TEXT("WIN32 Game Console"), // title
WS_OVERLAPPEDWINDOW, // style
0, 0, // initial x, y
WINDOW_WIDTH + cxNonClient, // initial width
WINDOW_HEIGHT+ cyNonClient, // initial height
NULL, // handle to parent
NULL, // handle to menu
hinstance, // instance
NULL); // creation parms

if (! hwnd)
return 0;

ShowWindow(hwnd, ncmdshow);
UpdateWindow(hwnd);

// hide mouse
//ShowCursor(FALSE);

// save the window handle and instance in a global
main_window_handle = hwnd;
main_instance = hinstance;

// perform all game console specific initialization
Game_Init();

// enter main event loop
while (1)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// test if this is a quit msg
if (msg.message == WM_QUIT)
break;

// translate any accelerator keys
TranslateMessage(&msg);

// send the message to the window proc
DispatchMessage(&msg);
}

// main game processing goes here
Game_Main();
Sleep(10);
}

// shutdown game and release all resources
Game_Shutdown();

// show mouse
//ShowCursor(TRUE);

// return to windows like this
return (msg.wParam);
}


/* DRAW FUNCTION **************************************************************************/
int Draw_Rectangle(int x1, int y1, int x2, int y2, int color)
{
// this function uses Win32 API to draw a filled rectangle
HBRUSH hbrush;
HDC hdc;
RECT rect;

SetRect(&rect, x1, y1, x2, y2);

hbrush = CreateSolidBrush(color);
hdc = GetDC(main_window_handle);

FillRect(hdc, &rect, hbrush);
ReleaseDC(main_window_handle, hdc);
DeleteObject(hbrush);

return 1;
}

int DrawText_GUI(TCHAR *text, int x, int y, int color)
{
HDC hdc;

hdc = GetDC(main_window_handle);

// set the colors for the text up
SetTextColor(hdc, color);

// set background mode to transparent so black isn't copied
SetBkMode(hdc, TRANSPARENT);

// draw the text
TextOut(hdc, x, y, text, lstrlen(text));

// release the dc
ReleaseDC(main_window_handle, hdc);

return 1;
}


/* GAME PROGRAMMING CONSOLE FUNCTIONS *****************************************************/
void Init_Blocks(void)
{
// initialize the block field
for (int row = 0; row < NUM_BLOCK_ROWS; row++)
for (int col = 0; col < NUM_BLOCK_COLUMNS; col++)
blocks[row][col] = 1;
}

void Draw_Blocks(void)
{
// this function draws all the blocks in row major form
int x1 = BLOCK_ORIGIN_X;
int y1 = BLOCK_ORIGIN_Y;

// draw all the blocks
for (int row = 0; row < NUM_BLOCK_ROWS; row++)
{
// reset column position
x1 = BLOCK_ORIGIN_X;

for (int col = 0; col < NUM_BLOCK_COLUMNS; col++)
{
if (blocks[row][col] != 0)
{
Draw_Rectangle(