Добавил:
Вуз:
Предмет:
Файл:
#include "App.h"
#ifdef WIN32
#include <chrono>
int APIENTRY __stdcall wWinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
if (SUCCEEDED(CoInitialize(NULL)))
{
EntryPoint app;
if (SUCCEEDED(app.Init()))
{
app.RunMessageLoop();
}
CoUninitialize();
}
}
#else
#include <iostream>
int main(int argc, char* argv[])
{
std::cout << "WinAPI connection problem";
return 0;
}
#endif
EntryPoint::EntryPoint() : mHwnd(NULL)
{
engine = new Engine();
}
EntryPoint::~EntryPoint()
{
}
void EntryPoint::RunMessageLoop()
{
MSG msg;
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
int frames = 0;
double framesTime = 0;
boolean isRunning = true;
while (isRunning)
{
end = std::chrono::steady_clock::now();
double elapsed_secs = std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() / 1000000.0;
begin = end;
// MESSAGE AND USER INPUT
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT) {
isRunning = false;
}
}
// APP LOGIC
if (elapsed_secs > 0.1)
{
elapsed_secs = 0.1;
}
engine->Logic(elapsed_secs);
// DRAW
engine->Draw();
}
}
HRESULT EntryPoint::Init()
{
HRESULT hr = S_OK;
//Windows class registration
WNDCLASSEX wdex;
ZeroMemory(&wdex, sizeof(WNDCLASSEX));
wdex.cbSize = sizeof(WNDCLASSEX);
wdex.hbrBackground = NULL;
wdex.hInstance = HINST_THISCOMPONENT;
wdex.lpfnWndProc = EntryPoint::WndProc;
wdex.lpszClassName = L"WndClassEx";
wdex.cbClsExtra = 0;
wdex.cbWndExtra = 0;
wdex.hCursor = LoadCursor(NULL, IDI_APPLICATION);
wdex.style = CS_HREDRAW | CS_VREDRAW;
ATOM x = RegisterClassEx(&wdex);
mHwnd = CreateWindowEx(
NULL,
L"WndClassEx",
L"Maze",
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
X_RES,
Y_RES,
NULL,
NULL,
HINST_THISCOMPONENT,
0
);
hr = mHwnd ? S_OK : E_FAIL;
if (mHwnd != 0)
{
RECT lWndRect;
GetWindowRect(mHwnd, &lWndRect);
RECT lClntRect;
GetWindowRect(mHwnd, &lClntRect);
SetWindowPos(
mHwnd,
NULL,
lWndRect.left,
lWndRect.top,
X_RES + ((lWndRect.right - lWndRect.left) - (lClntRect.right - lClntRect.left)),
Y_RES + ((lWndRect.bottom - lWndRect.top) - (lClntRect.bottom - lClntRect.top)),
NULL
);
if (SUCCEEDED(hr)) {
engine->InitializeD2D(mHwnd);
ShowWindow(mHwnd, SW_SHOWNORMAL);
UpdateWindow(mHwnd);
}
return hr;
}
return -1;
}
LRESULT CALLBACK EntryPoint::WndProc(
_In_ HWND hwnd,
_In_ UINT message,
_In_ WPARAM wParam,
_In_ LPARAM lParam)
{
LRESULT result = 0;
if (message == WM_CREATE)
{
LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
EntryPoint* pMainApp = (EntryPoint*)pcs->lpCreateParams;
::SetWindowLongPtrW(
hwnd,
GWLP_USERDATA,
reinterpret_cast<LONG_PTR>(pMainApp)
);
result = 1;
}
else
{
EntryPoint* pMainApp = reinterpret_cast<EntryPoint*>(static_cast<LONG_PTR>(
::GetWindowLongW(
hwnd,
GWLP_USERDATA
)));
boolean wasHendled = false;
if (pMainApp)
{
switch (message)
{
case WM_DISPLAYCHANGE:
{
InvalidateRect(hwnd, NULL, FALSE);
}
result = 0;
wasHendled = true;
break;
case WM_DESTROY:
{
PostQuitMessage(0);
}
result = 1;
wasHendled = true;
break;
}
}
if (!wasHendled)
{
result = DefWindowProc(hwnd, message, wParam, lParam);
}
}
return result;
} 