Files

95 lines
2.4 KiB
C++

#include "Window.h"
INT Window::init(HINSTANCE hInstance, UINT width, UINT height)
{
// 1. describe window class
WNDCLASS wc = {};
wc.hInstance = hInstance;
wc.lpszClassName = TEXT("D3D9");
wc.lpfnWndProc = WindowProc; // callback function for windows to communicate with our application
wc.hbrBackground = CreateSolidBrush(RGB(0, 255, 0));
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // technical behaviour
// 2. register window class
if (!RegisterClass(&wc)) return 100;
// 3. adjust window size (optional)
DWORD style = {};
style = WS_OVERLAPPEDWINDOW; // window with border & title
//style = WS_POPUP; // plain window without any decorations
// fullscreen
//width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
//height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
RECT rect = { 100, 100, 100 + width, 100 + height }; // left-top corner, right-bottom corner
AdjustWindowRect(&rect, style, FALSE);
// 4. create window instance
_hWnd = CreateWindow(
wc.lpszClassName, // class name of window class to be instantiated
wc.lpszClassName, // window title
style, // visual style
rect.left, rect.top, // left-top corner position
rect.right - rect.left, rect.bottom - rect.top, // window size
nullptr, // handle to parent window
nullptr, // handle to menu instance
hInstance,
nullptr // pointer to optional parameter
);
if (_hWnd == nullptr) return 110;
// 5. show window
ShowWindow(_hWnd, SW_SHOW);
SetFocus(_hWnd);
return 0;
}
BOOL Window::run()
{
// 6. run window
// GetMessage - synchronous call which holds until a new message arrives
// PeekMessage - get last message from message queue or nothing if there is no one left
// TranslateMessage - translates keyboard input (scan code -> virtual key code)
// DispatchMessage - send message to WindowProcedure
MSG msg = {};
if (PeekMessage(&msg, nullptr, 0, UINT_MAX, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.message != WM_QUIT;
}
void Window::deInit()
{
}
LRESULT WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_KEYDOWN:
if (wParam == VK_ESCAPE) DestroyWindow(hWnd);
return 0;
default:
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}