头文件 GameEngine.h
#pragma once
#include
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow);
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
bool GameInitialize(HINSTANCE hInstance);
void GameStart(HWND hWnd);
void GameEnd();
void GameActive(HWND hWnd);
void GameDeactive(HWND hWnd);
void GamePaint(HDC hdc);
void GameCycle();
class GameEngine
{
public:
GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass, LPTSTR szTitle, WORD wIcon, WORD wSmallIcon, int iWidth = 640, int iHeight = 480);
virtual ~GameEngine();
static GameEngine* GetEngine(){ return m_pGameEngine; }
static void SetEngine(GameEngine* pGameEngine){ m_pGameEngine = pGameEngine; }
BOOL Initialize(int iCmdShow);
LRESULT HandleEvent(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
HINSTANCE GetInstance(){ return m_hInstance; }
HWND GetWnd(){ return m_hWnd; }
void SetWnd(HWND hWnd){ m_hWnd = hWnd; }
LPTSTR GetTitle(){ return m_szTitle; }
WORD GetIcon(){ return m_wIcon; }
WORD GetSmallIcon(){ return m_wSmallIcon; }
int GetWidth(){ return m_iWidth; }
int GetHeight(){ return m_iHeight; }
int GetFrameDelay(){ return m_iFrameDelay; }
void SetFrameRate(int iFrameRate){ m_iFrameDelay = 1000 / iFrameRate; }
BOOL GetSleep(){ return m_bSleep; }
void SetSleep(BOOL bSleep){ m_bSleep = bSleep; }
private:
protected:
static GameEngine* m_pGameEngine;
HINSTANCE m_hInstance;
HWND m_hWnd;
TCHAR m_szWindowClass[32];
TCHAR m_szTitle[32];
WORD m_wIcon, m_wSmallIcon;
int m_iWidth, m_iHeight;
int m_iFrameDelay;
BOOL m_bSleep;
};
源文件GameEngine.cpp
#include "stdafx.h"
#include "GameEngine.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
MSG msg;
static int iTickTrigger = 0;
int iTickCount;
if (GameInitialize(hInstance))
{
if (!GameEngine::GetEngine()->Initialize(iCmdShow))
{
return FALSE;
}
while (true)
{
if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
if (!GameEngine::GetEngine()->GetSleep())
{
iTickCount = GetTickCount();
if (iTickCount > iTickTrigger)
{
iTickTrigger = iTickCount + GameEngine::GetEngine()->GetFrameDelay();
GameCycle();
}
}
}
}
return (int)msg.wParam;
}
GameEnd();
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
return GameEngine::GetEngine()->HandleEvent(hWnd, message, wParam, lParam);
}
GameEngine::GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass, LPTSTR szTitle, WORD wIcon, WORD wSmallIcon, int iWidth, int iHeight) :
m_hInstance(hInstance),
m_wIcon(wIcon),
m_wSmallIcon(wSmallIcon),
m_iWidth(iWidth),
m_iHeight(iHeight),
m_iFrameDelay(50),
m_bSleep(TRUE),
m_hWnd(nullptr)
{
memcpy(m_szWindowClass, szWindowClass, strlen(szWindowClass));
memcpy(m_szTitle, szTitle, strlen(szTitle));
}
GameEngine::~GameEngine()
{
}
BOOL GameEngine::Initialize(int iCmdShow)
{
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = m_hInstance;
wndclass.hIcon = LoadIcon(m_hInstance, MAKEINTRESOURCE(GetIcon()));
wndclass.hIconSm = LoadIcon(m_hInstance, MAKEINTRESOURCE(GetSmallIcon()));
wndclass.hCursor = LoadCursor(nullptr, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndclass.lpszMenuName = nullptr;
wndclass.lpszClassName = m_szWindowClass;
if (!RegisterClassEx(&wndclass))
{
return FALSE;
}
int iWindowWidth = m_iWidth + GetSystemMetrics(SM_CXFIXEDFRAME) * 2;
int iWindowHeight = m_iHeight + GetSystemMetrics(SM_CYFIXEDFRAME) * 2 + GetSystemMetrics(SM_CYCAPTION);
if (wndclass.lpszMenuName != nullptr)
{
iWindowHeight += GetSystemMetrics(SM_CYMENU);
}
int iXWindowPos = (GetSystemMetrics(SM_CXSCREEN) - iWindowWidth) / 2;
int iYWindowPos = (GetSystemMetrics(SM_CYSCREEN) - iWindowHeight) / 2;
m_hWnd = CreateWindow(m_szWindowClass, m_szTitle, WS_POPUPWINDOW | WS_CAPTION | WS_MINIMIZEBOX, iXWindowPos, iYWindowPos, iWindowWidth, iWindowHeight, nullptr, nullptr, m_hInstance, nullptr);
if (!m_hWnd)
{
return FALSE;
}
ShowWindow(m_hWnd, iCmdShow);
UpdateWindow(m_hWnd);
return TRUE;
}
LRESULT GameEngine::HandleEvent(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
SetWnd(hWnd);
GameStart(hWnd);
return 0;
case WM_SETFOCUS:
GameActive(hWnd);
SetSleep(FALSE);
return 0;
case WM_KILLFOCUS:
GameDeactive(hWnd);
SetSleep(TRUE);
return 0;
case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hWnd, &ps);
GamePaint(hdc);
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
GameEnd();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
__declspec(selectany) GameEngine* GameEngine::m_pGameEngine = nullptr;