Skip to content

Commit c13454c

Browse files
committed
Feat: init Win32 window plugin
1 parent 9b1c63c commit c13454c

5 files changed

Lines changed: 223 additions & 0 deletions

File tree

plugins/Window/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
add_subdirectory(GLFW)
2+
add_subdirectory(Win32)
23
add_subdirectory(X11)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
project(cae-window-win32
2+
DESCRIPTION "CAE Win32 Window Plugin"
3+
LANGUAGES C CXX
4+
)
5+
6+
if (NOT WIN32)
7+
message(WARNING "${PROJECT_NAME} can only be build on windows")
8+
return()
9+
endif ()
10+
11+
find_library(USER32_LIB user32)
12+
find_library(GDI32_LIB gdi32)
13+
14+
file(GLOB_RECURSE SOURCE "${PROJECT_SOURCE_DIR}/src/*.cpp")
15+
file(GLOB_RECURSE HEADERS "${PROJECT_SOURCE_DIR}/include/*.hpp")
16+
17+
add_library(${PROJECT_NAME} SHARED
18+
${SOURCE}
19+
${HEADERS}
20+
)
21+
22+
target_include_directories(${PROJECT_NAME} PRIVATE "${PROJECT_SOURCE_DIR}/include")
23+
target_compile_options(${PROJECT_NAME} PRIVATE ${WARNING_FLAGS})
24+
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_23)
25+
target_link_libraries(${PROJECT_NAME} PRIVATE
26+
cae-modules
27+
${USER32_LIB}
28+
${GDI32_LIB}
29+
)
30+
set_target_properties(${PROJECT_NAME} PROPERTIES
31+
LIBRARY_OUTPUT_DIRECTORY "${PLUGIN_DIR}"
32+
RUNTIME_OUTPUT_DIRECTORY "${PLUGIN_DIR}"
33+
)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
///
2+
/// @file Win32.hpp
3+
/// @brief This file contains the Win32 class declaration
4+
/// @namespace cae
5+
///
6+
7+
#pragma once
8+
9+
#include <windows.h>
10+
11+
#include "Interfaces/IWindow.hpp"
12+
13+
namespace cae
14+
{
15+
16+
///
17+
/// @class Win32
18+
/// @brief Class for the Win32 plugin
19+
/// @namespace cae
20+
///
21+
class Win32 final : public IWindow
22+
{
23+
24+
public:
25+
Win32() = default;
26+
~Win32() override = default;
27+
28+
Win32(const Win32 &) = delete;
29+
Win32 &operator=(const Win32 &) = delete;
30+
Win32(Win32 &&) = delete;
31+
Win32 &operator=(Win32 &&) = delete;
32+
33+
[[nodiscard]] std::string getName() const override { return "Win32"; }
34+
[[nodiscard]] utl::PluginType getType() const override { return utl::PluginType::WINDOW; }
35+
[[nodiscard]] utl::PluginPlatform getPlatform() const override { return utl::PluginPlatform::WINDOWS; }
36+
37+
bool create(const std::string &name, WindowSize size) override;
38+
void close() override;
39+
40+
[[nodiscard]] NativeWindowHandle getNativeHandle() const override;
41+
[[nodiscard]] WindowSize getWindowSize() const override;
42+
43+
[[nodiscard]] bool setIcon(const std::string &path) const override;
44+
45+
[[nodiscard]] bool shouldClose() const override { return m_shouldClose; }
46+
void pollEvents() override;
47+
48+
bool wasResized() const override { return m_frameBufferResized; }
49+
void resetResizedFlag() override { m_frameBufferResized = false; }
50+
51+
private:
52+
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
53+
54+
HWND m_hwnd = nullptr;
55+
HINSTANCE m_hInstance = nullptr;
56+
WindowSize m_frameBufferSize;
57+
bool m_frameBufferResized = false;
58+
bool m_shouldClose = false;
59+
60+
}; // class Win32
61+
} // namespace cae
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <memory>
2+
3+
#include "Win32/Win32.hpp"
4+
5+
extern "C"
6+
{
7+
PLUGIN_EXPORT cae::IWindow *entryPoint() { return std::make_unique<cae::Win32>().release(); }
8+
}

plugins/Window/Win32/src/win32.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#include <string>
2+
3+
#include <Utils/Logger.hpp>
4+
5+
#include "Win32/Win32.hpp"
6+
7+
LRESULT CALLBACK cae::Win32::WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
8+
{
9+
auto* self = reinterpret_cast<Win32*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
10+
11+
switch (msg)
12+
{
13+
case WM_CLOSE:
14+
if (self) self->m_shouldClose = true;
15+
return 0;
16+
case WM_SIZE:
17+
if (self)
18+
{
19+
self->m_frameBufferResized = true;
20+
self->m_frameBufferSize.width = LOWORD(lParam);
21+
self->m_frameBufferSize.height = HIWORD(lParam);
22+
}
23+
return 0;
24+
case WM_DESTROY:
25+
PostQuitMessage(0);
26+
return 0;
27+
default:
28+
return DefWindowProc(hwnd, msg, wParam, lParam);
29+
}
30+
}
31+
32+
bool cae::Win32::create(const std::string &name, WindowSize size)
33+
{
34+
m_hInstance = GetModuleHandle(nullptr);
35+
m_frameBufferSize = size;
36+
m_shouldClose = false;
37+
m_frameBufferResized = false;
38+
39+
WNDCLASSW wc{};
40+
wc.lpfnWndProc = WindowProc;
41+
wc.hInstance = m_hInstance;
42+
wc.lpszClassName = L"CAE_WindowsWindowClass";
43+
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
44+
wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
45+
wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
46+
47+
if (!RegisterClassW(&wc) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
48+
{
49+
utl::Logger::log("Failed to register window class", utl::LogLevel::WARNING);
50+
return false;
51+
}
52+
53+
m_hwnd = CreateWindowEx(
54+
0,
55+
reinterpret_cast<LPCSTR>(L"CAE_WindowsWindowClass"),
56+
reinterpret_cast<LPCSTR>(std::wstring(name.begin(), name.end()).c_str()),
57+
WS_OVERLAPPEDWINDOW,
58+
CW_USEDEFAULT, CW_USEDEFAULT,
59+
size.width, size.height,
60+
nullptr,
61+
nullptr,
62+
m_hInstance,
63+
nullptr
64+
);
65+
66+
if (!m_hwnd)
67+
{
68+
utl::Logger::log("Failed to create Win32 window", utl::LogLevel::WARNING);
69+
return false;
70+
}
71+
72+
SetWindowLongPtr(m_hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
73+
ShowWindow(m_hwnd, SW_SHOW);
74+
UpdateWindow(m_hwnd);
75+
76+
return true;
77+
}
78+
79+
void cae::Win32::close()
80+
{
81+
if (m_hwnd)
82+
{
83+
DestroyWindow(m_hwnd);
84+
m_hwnd = nullptr;
85+
}
86+
UnregisterClass(reinterpret_cast<LPCSTR>(L"CAE_WindowsWindowClass"), m_hInstance);
87+
}
88+
89+
cae::NativeWindowHandle cae::Win32::getNativeHandle() const
90+
{
91+
return {.window = m_hwnd, .display = m_hInstance};
92+
93+
}
94+
95+
cae::WindowSize cae::Win32::getWindowSize() const
96+
{
97+
RECT rect{};
98+
GetClientRect(m_hwnd, &rect);
99+
return {.width = static_cast<uint16_t>(rect.right - rect.left),
100+
.height = static_cast<uint16_t>(rect.bottom - rect.top)};
101+
}
102+
103+
bool cae::Win32::setIcon(const std::string &path) const
104+
{
105+
return false;
106+
}
107+
108+
void cae::Win32::pollEvents()
109+
{
110+
MSG msg{};
111+
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
112+
{
113+
if (msg.message == WM_QUIT)
114+
{
115+
m_shouldClose = true;
116+
}
117+
TranslateMessage(&msg);
118+
DispatchMessage(&msg);
119+
}
120+
}

0 commit comments

Comments
 (0)