Compare commits

...
2 Commits
Author SHA1 Message Date
david ed7b1816e9 add window message queue 2025-09-10 13:34:42 +02:00
david f98cacdde3 add window (WIP) 2025-09-03 16:10:19 +02:00
2 changed files with 57 additions and 1 deletions
+56 -1
View File
@@ -1,16 +1,40 @@
#include "Window.h" #include "Window.h"
// https://learn.microsoft.com/en-us/windows/win32/learnwin32/creating-a-window
INT Window::init(HINSTANCE hInstance, UINT width, UINT height) INT Window::init(HINSTANCE hInstance, UINT width, UINT height)
{ {
// 1. describe window class // 1. describe window class
WNDCLASS wc = {};
wc.hInstance = hInstance;
wc.lpszClassName = TEXT("D3D9"); // class name
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);
// 2. register window class // 2. register window class
if (!RegisterClass(&wc)) return 100;
// 3. adjust window size (optional) // 3. adjust window size (optional)
// 4. create window instance // 4. create window instance
_hWnd = CreateWindow(
wc.lpszClassName, // class name of window class to be instantiated
wc.lpszClassName, // window title
WS_OVERLAPPEDWINDOW, // visual style
100, 100, // window position (left-top corner)
width, height, // 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 // 5. show window
ShowWindow(_hWnd, SW_SHOW);
SetFocus(_hWnd);
return 0; return 0;
} }
@@ -18,10 +42,41 @@ INT Window::init(HINSTANCE hInstance, UINT width, UINT height)
BOOL Window::run() BOOL Window::run()
{ {
// 6. run window // 6. run window
// GetMessage - syncronous call which holds until a new message arrived
// PeekMessage - get last message from message queue or nothing if there is no message
// TranslateMessage - translates keyboard input
// DispatchMessage - send message to WindowProcedure
return 0; MSG msg = {};
if (PeekMessage(&msg, nullptr, 0, UINT_MAX, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.message != WM_QUIT;
} }
void Window::deInit() 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);
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
+1
View File
@@ -14,3 +14,4 @@ private:
HWND _hWnd = nullptr; // handle to window instance HWND _hWnd = nullptr; // handle to window instance
}; };
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);