游戏⼊门引擎代码
头⽂件 GameEngine.h
源⽂件
GameEngine.cpp
#include "stdafx.h"
#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;
}javascript游戏引擎
while (true)
{
if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
if (ssage == 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;
}
//根据game⼤⼩计算窗⼝⼤⼩和位置
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, iW 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);
return0;
case WM_SETFOCUS:
GameActive(hWnd);
SetSleep(FALSE);
return0;
case WM_KILLFOCUS:
GameDeactive(hWnd);
SetSleep(TRUE);
return0;
case WM_PAINT:
HDC        hdc;
PAINTSTRUCT ps;
__declspec(selectany) GameEngine* GameEngine::m_pGameEngine =

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。