From a2f4527204f9a9d35b826ad76e9b353194eaa468 Mon Sep 17 00:00:00 2001 From: David Hackbarth Date: Wed, 3 Sep 2025 11:16:24 +0200 Subject: [PATCH] add window --- GDP/D3D9/Window.cpp | 88 +++++++++++++++++++++++++++++++++++++-------- GDP/D3D9/Window.h | 1 + 2 files changed, 75 insertions(+), 14 deletions(-) diff --git a/GDP/D3D9/Window.cpp b/GDP/D3D9/Window.cpp index 1b684a0..dd38e7c 100644 --- a/GDP/D3D9/Window.cpp +++ b/GDP/D3D9/Window.cpp @@ -2,26 +2,86 @@ INT Window::init(HINSTANCE hInstance, UINT width, UINT height) { - // 1. describe window class - - // 2. register window class - - // 3. adjust window size (optional) - - // 4. create window instance - - // 5. show window - - return 0; + // 1. describe window class + WNDCLASS wc = {}; + wc.hInstance = hInstance; + wc.lpszClassName = TEXT("D3D9"); // class name for identify + wc.lpfnWndProc = WindowProc; // function for windows to send messages to our application + wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // technical window properties, here update window if window change horizontally or vertically and window should have its own device context + wc.hbrBackground = CreateSolidBrush(RGB(0, 255, 0)); + wc.hCursor = LoadCursor(nullptr, IDC_ARROW); + wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION); + + // 2. register window class + if (!RegisterClass(&wc)) return 100; + + // 3. adjust window size (optional) + + // 4. create window instance + _hWnd = CreateWindow( + wc.lpszClassName, // class name for window instance + wc.lpszClassName, // window title + WS_OVERLAPPEDWINDOW, // visual window 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 + ShowWindow(_hWnd, SW_SHOW); + SetFocus(_hWnd); + + return 0; } BOOL Window::run() { - // 6. run window - - return 0; + // 6. run window + // message queue + // https://learn.microsoft.com/en-us/windows/win32/winmsg/about-messages-and-message-queues + // GetMessage - wait for a message send by windows (syncronous) + // PeekMessage - get last message from queue or nothing + // TranslateMessage - translates keyboard input (scan (hardware) vs. virtual key code) + // DispatchMessage - send message to window proc + + MSG msg; + if (PeekMessage(&msg, nullptr, 0, UINT_MAX, PM_REMOVE)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + + Sleep(10); + + 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; + } + break; + } + + return DefWindowProc(hWnd, msg, wParam, lParam); +} diff --git a/GDP/D3D9/Window.h b/GDP/D3D9/Window.h index 3ef230c..ea76a26 100644 --- a/GDP/D3D9/Window.h +++ b/GDP/D3D9/Window.h @@ -14,3 +14,4 @@ private: HWND _hWnd = nullptr; // handle to window instance }; +LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);