1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
// GameEngine.cpp - Game Engine Source
#include "Bitmap.hpp"
#include "GameEngine.hpp"
GameEngine* GameEngine::m_pGameEngine = NULL;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
if (GameInitialize(hInstance) == S_OK)
{
if (GameEngine::GetEngine()->Initialize(iCmdShow) != S_OK)
{
return E_FAIL;
}
HACCEL hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCELERATORS));
if (hAccel == NULL)
{
MessageBox(NULL, TEXT("Unable to Load the Accelerators!"), GameEngine::GetEngine()->GetTitle(), MB_OK | MB_ICONERROR);
return E_FAIL;
}
MSG msg;
while (TRUE)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != 0)
{
if (msg.message == WM_QUIT)
{
break;
}
if (0 == TranslateAccelerator(GameEngine::GetEngine()->GetWindow(), hAccel, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
if (GameEngine::GetEngine()->GetSleep() == FALSE)
{
static UINT iTickTrigger = 0;
UINT iTickCount = GetTickCount();
if (iTickCount > iTickTrigger)
{
iTickTrigger = iTickCount + GameEngine::GetEngine()->GetFrameDelay();
GameCycle();
}
}
}
}
return (int) msg.wParam;
}
GameEnd();
return S_OK;
}
LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
return GameEngine::GetEngine()->HandleEvent(hWindow, msg, wParam, lParam);
}
BOOL CALLBACK DlgProc(HWND hDialog, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
EndDialog(hDialog, 0);
return TRUE;
}
}
return FALSE;
}
GameEngine::GameEngine(HINSTANCE hInstance, LPCTSTR szWindowClass, LPCTSTR szTitle,
WORD wIcon, WORD wSmallIcon, UINT iWidth, UINT iHeight)
{
m_pGameEngine = this;
m_hInstance = hInstance;
m_hWindow = NULL;
m_wIcon = wIcon;
m_wSmallIcon = wSmallIcon;
m_iWidth = iWidth;
m_iHeight = iHeight;
m_iFrameDelay = 50;
m_bSleep = TRUE;
size_t pcch = 0;
HRESULT hRes = StringCchLength(szWindowClass, str_length, &pcch);
if (pcch > 0)
{
StringCchCopy(m_szWindowClass, str_length, szWindowClass);
}
else
{
StringCchCopy(m_szWindowClass, str_length,TEXT(""));
}
hRes = StringCchLength(szTitle, str_length, &pcch);
if (pcch > 0)
{
StringCchCopy(m_szTitle, str_length, szTitle);
}
else
{
StringCchCopy(m_szTitle, str_length, TEXT(""));
}
}
GameEngine::~GameEngine()
{
}
HRESULT GameEngine::Initialize(int iCmdShow)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = m_hInstance;
wc.hIcon = (HICON) LoadImage(m_hInstance, MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
wc.hIconSm = (HICON) LoadImage(m_hInstance, MAKEINTRESOURCE(IDI_ICON_SM), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);;
wc.hCursor = (HCURSOR) LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
wc.lpszClassName = m_szWindowClass;
if (RegisterClassEx(&wc) == 0)
{
MessageBox(NULL, TEXT("Unable to initialize Main Window!"), TEXT("ERROR"), MB_ICONERROR | MB_OK);
return E_FAIL;
}
UINT iWindowWidth = m_iWidth + GetSystemMetrics(SM_CXFIXEDFRAME) * 2;
UINT iWindowHeight = m_iHeight + GetSystemMetrics(SM_CYFIXEDFRAME) * 2 + GetSystemMetrics(SM_CYCAPTION);
iWindowWidth += 10;
iWindowHeight += 10;
if (wc.lpszMenuName != NULL)
{
iWindowHeight += GetSystemMetrics(SM_CYMENU);
}
UINT iWindowPosX = (GetSystemMetrics(SM_CXSCREEN) - iWindowWidth) / 2;
UINT iWindowPosY = (GetSystemMetrics(SM_CYSCREEN) - iWindowHeight) / 2;
m_hWindow = CreateWindow(m_szWindowClass, m_szTitle,
WS_POPUPWINDOW | WS_CAPTION | WS_MINIMIZEBOX,
iWindowPosX, iWindowPosY,
iWindowWidth, iWindowHeight,
NULL, NULL, m_hInstance, NULL);
if (m_hWindow == NULL)
{
MessageBox(NULL, TEXT("Unable to create Main Window!"), TEXT("ERROR"), MB_ICONERROR | MB_OK);
return E_FAIL;
}
ShowWindow(m_hWindow, iCmdShow);
UpdateWindow(m_hWindow);
return S_OK;
}
LRESULT GameEngine::HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
SetWindow(hWindow);
GameStart(hWindow);
return 0;
case WM_ACTIVATE:
if (wParam != WA_INACTIVE)
{
GameActivate(hWindow);
SetSleep(FALSE);
}
else
{
GameDeactivate(hWindow);
SetSleep(TRUE);
}
return 0;
case WM_COMMAND:
GameMenu(wParam);
return 0;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hWindow, &ps);
GamePaint(hDC);
EndPaint(hWindow, &ps);
return 0;
case WM_DESTROY:
GameEnd();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWindow, msg, wParam, lParam);
}
| |