From 84062873e162bc4a7c799fb67f72dbd055eb15ca Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Wed, 1 May 2024 01:23:05 +0200 Subject: Some work on WSI --- deimos/core/os_win32.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 5 deletions(-) (limited to 'deimos/core/os_win32.cpp') diff --git a/deimos/core/os_win32.cpp b/deimos/core/os_win32.cpp index d848530..fe904cd 100644 --- a/deimos/core/os_win32.cpp +++ b/deimos/core/os_win32.cpp @@ -8,6 +8,17 @@ namespace deimos { +gsl::cwzstring Utf8ToUtf16Z(StringView src, Span buffer) +{ + Expects(buffer.size() > 0 && buffer.size() > src.size()); + + int res = ::MultiByteToWideChar(CP_UTF8, 0, src.data(), (int)src.size_bytes(), buffer.data(), (int)buffer.size() - 1); + if (res < 0) { return L"< MBTWC ERROR >"; } + + buffer[res] = L'\0'; + return buffer.data(); +} + class Win32ConsoleApiImpl : public OsConsoleApi { HANDLE m_stdout; @@ -73,18 +84,68 @@ public: } }; +class Win32WindowApiImpl : public OsWindowApi +{ + static constexpr wchar_t kClassName[] = L"Deimos window class"; + + bool m_class_registered = false; + + void RegisterClass() + { + Expects(!m_class_registered); + + WNDCLASSW wnd_class{}; + wnd_class.lpfnWndProc = ::DefWindowProcW; + wnd_class.hInstance = ::GetModuleHandleW(nullptr); + wnd_class.lpszClassName = kClassName; + + ::RegisterClassW(&wnd_class); + + m_class_registered = true; + } + +public: + StatusOr> Create(gsl::czstring title, int32_t width, int32_t height) override + { + if (!m_class_registered) + { + RegisterClass(); + } + Ensures(m_class_registered); + + wchar_t title_w_buffer[128]{}; + gsl::cwzstring title_w = Utf8ToUtf16Z(title, title_w_buffer); + + HWND hwnd = ::CreateWindowExW( + 0, kClassName, title_w, WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, width, height, + nullptr, nullptr, ::GetModuleHandle(nullptr), nullptr); + + if (hwnd == nullptr) + { + return InternalError("Error while creating Win32 window"); + } + + ::ShowWindow(hwnd, SW_SHOW); + + return nullptr; + } +}; + class Win32OsApiImpl : public OsApi { - Win32ConsoleApiImpl m_console_api; - Win32DllApiImpl m_dll_api; - Win32VirtualMemoryApiImpl m_virtual_memory_api; + Win32ConsoleApiImpl m_console_api; + Win32DllApiImpl m_dll_api; + Win32VirtualMemoryApiImpl m_virtual_memory_api; + Win32WindowApiImpl m_window_api; public: Win32OsApiImpl() { - console = &m_console_api; - dll = &m_dll_api; + console = &m_console_api; + dll = &m_dll_api; virtual_memory = &m_virtual_memory_api; + window = &m_window_api; } }; -- cgit