68 lines
2.3 KiB
C++
68 lines
2.3 KiB
C++
|
|
#include "directx_util.hpp"
|
||
|
|
#include <stdexcept>
|
||
|
|
#include <string>
|
||
|
|
|
||
|
|
namespace Basalt::Shared::DirectX {
|
||
|
|
|
||
|
|
// Window procedure for the render window
|
||
|
|
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
|
||
|
|
switch (msg) {
|
||
|
|
case WM_CLOSE:
|
||
|
|
PostQuitMessage(0);
|
||
|
|
return 0;
|
||
|
|
default:
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
return DefWindowProcW(hwnd, msg, wParam, lParam);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Create a render window for DirectX
|
||
|
|
HWND CreateRenderWindow(std::uint32_t width, std::uint32_t height, const std::wstring_view& title) {
|
||
|
|
static bool g_CLSREG = false;
|
||
|
|
constexpr wchar_t class_name[] = L"DirectXRenderWindowClass";
|
||
|
|
std::wstring c_title(title);
|
||
|
|
|
||
|
|
if (!g_CLSREG) {
|
||
|
|
WNDCLASSEXW wc = {0};
|
||
|
|
wc.cbSize = sizeof(WNDCLASSEXW);
|
||
|
|
wc.style = CS_HREDRAW | CS_VREDRAW;
|
||
|
|
wc.lpfnWndProc = WndProc;
|
||
|
|
wc.hInstance = GetModuleHandleW(nullptr);
|
||
|
|
wc.hCursor = LoadCursorW(nullptr, IDC_ARROW);
|
||
|
|
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
|
||
|
|
wc.lpszClassName = class_name;
|
||
|
|
|
||
|
|
if (!RegisterClassExW(&wc)) {
|
||
|
|
throw std::runtime_error("Failed to register window class");
|
||
|
|
}
|
||
|
|
g_CLSREG = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Calculate window size including borders
|
||
|
|
RECT rect = {0, 0, static_cast<LONG>(width), static_cast<LONG>(height)};
|
||
|
|
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
|
||
|
|
|
||
|
|
HWND hwnd = CreateWindowExW(0,
|
||
|
|
class_name,
|
||
|
|
c_title.c_str(),
|
||
|
|
WS_OVERLAPPEDWINDOW,
|
||
|
|
CW_USEDEFAULT,
|
||
|
|
CW_USEDEFAULT,
|
||
|
|
rect.right - rect.left,
|
||
|
|
rect.bottom - rect.top,
|
||
|
|
nullptr,
|
||
|
|
nullptr,
|
||
|
|
GetModuleHandleW(nullptr),
|
||
|
|
nullptr);
|
||
|
|
if (!hwnd) {
|
||
|
|
throw std::runtime_error("Failed to create render window");
|
||
|
|
}
|
||
|
|
|
||
|
|
ShowWindow(hwnd, SW_SHOW);
|
||
|
|
UpdateWindow(hwnd);
|
||
|
|
|
||
|
|
return hwnd;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|