evan 2026.1.4
Evan
Loading...
Searching...
No Matches
IDesktopPlatform.cpp
1/*
2** ETIB PROJECT, 2026
3** evan
4** File description:
5** IDesktopPlatform
6*/
7
8#include <cmath>
9#include <math.h>
10
11#include "evan/glfw/IDesktopPlatform.hpp"
12
14{
15 this->getLogger().info()
16 << "Destroying IDesktopPlatform and releasing associated resources...";
17
18 if (_window != nullptr) {
20 _window = nullptr;
21 }
22 // _glfwContext releases here; the last instance calls glfwTerminate().
23}
24
26// Public Methods //
28
29std::shared_ptr<evan::ADeviceBackend>
31{
32 this->getLogger().info()
33 << "Creating device backend for Desktop platform...";
34 return std::make_shared<DesktopBackend>(*this);
35}
36
37std::shared_ptr<evan::ASwapchainContext>
39 const DeviceContext &deviceContext) const
40{
41 this->getLogger().info()
42 << "Creating swapchain context for Desktop platform...";
43 auto swapchainContext =
44 std::make_shared<DesktopSwapchainContext>(deviceContext, _window);
45
46 auto viewportSize = swapchainContext->getViewportSize();
47 VkExtent2D extent { static_cast<uint32_t>(viewportSize.x),
48 static_cast<uint32_t>(viewportSize.y) };
49
50 utility::graphic::ViewF view;
52 view.setClippingPlanes(1.0f, 4000.0f);
53
54 this->getLogger().info()
55 << "Setting up view for Desktop platform with viewport size: "
56 << viewportSize;
57
58 this->getLogger().info() << "Created view for Desktop platform: " << view;
59
60 swapchainContext->setView(0, view);
61 return swapchainContext;
62}
63
64std::vector<std::shared_ptr<utility::event::Event>>
66{
67 this->getLogger().info() << "Polling events for Desktop platform...";
68 _lastError = Error::Ok;
70
71 // Surface GLFW errors into the normalized taxonomy.
72 const char *glfwErrorDescription = nullptr;
74 this->getLogger().error()
75 << "GLFW error: "
77 _lastError = Error::RuntimeError;
78 }
79
80 std::vector<std::shared_ptr<utility::event::Event>> events;
81
82 // Poll current key states for continuous movement
83 this->getLogger().info()
84 << "Polling keyboard state for continuous movement...";
85 auto pressedKeys = getPressedMovementKeys();
86 for (auto &keyEvent: pressedKeys) {
87 events.push_back(std::move(keyEvent));
88 }
89
90 this->getLogger().info()
91 << "Processing keyboard events for Desktop platform...";
92 events.insert(events.end(),
93 std::make_move_iterator(_keyboardEvents.begin()),
94 std::make_move_iterator(_keyboardEvents.end()));
95 // Clear keyboard events after processing them
96 _keyboardEvents.clear();
97
98 this->getLogger().info()
99 << "Processing mouse button events for Desktop platform...";
100 events.insert(events.end(),
101 std::make_move_iterator(_mouseButtonEvents.begin()),
102 std::make_move_iterator(_mouseButtonEvents.end()));
103
104 // Clear mouse button events after processing them
105 _mouseButtonEvents.clear();
106
107 this->getLogger().info()
108 << "Processing mouse motion events for Desktop platform...";
109 events.insert(events.end(),
110 std::make_move_iterator(_mouseMotionEvents.begin()),
111 std::make_move_iterator(_mouseMotionEvents.end()));
112
113 // Clear mouse motion events after processing them
114 _mouseMotionEvents.clear();
115
116 this->getLogger().info()
117 << "Processing mouse wheel events for Desktop platform...";
118 events.insert(events.end(),
119 std::make_move_iterator(_mouseWheelEvents.begin()),
120 std::make_move_iterator(_mouseWheelEvents.end()));
121
122 // Clear mouse wheel events after processing them
123 _mouseWheelEvents.clear();
124
125 this->getLogger().info()
126 << "Processing cursor enter/leave events for Desktop platform...";
127 events.insert(events.end(),
128 std::make_move_iterator(_cursorEnterEvents.begin()),
129 std::make_move_iterator(_cursorEnterEvents.end()));
130
131 // Clear cursor enter/leave events after processing them
132 _cursorEnterEvents.clear();
133
134 this->getLogger().info()
135 << "Processing file drop events for Desktop platform...";
136 events.insert(events.end(),
137 std::make_move_iterator(_fileDropEvents.begin()),
138 std::make_move_iterator(_fileDropEvents.end()));
139
140 // Clear file drop events after processing them
141 _fileDropEvents.clear();
142
143 this->getLogger().info()
144 << "Processing text input events for Desktop platform...";
145 events.insert(events.end(),
146 std::make_move_iterator(_textInputEvents.begin()),
147 std::make_move_iterator(_textInputEvents.end()));
148
149 // Clear text input events after processing them
150 _textInputEvents.clear();
151
152 return events;
153}
154
155std::vector<std::string>
157{
158 this->getLogger().info()
159 << "Getting required instance extensions for Desktop platform...";
160
162 const char **glfwExtensions =
164 std::vector<const char *> extensions(glfwExtensions,
166
168 if (enableValidationLayers == true) {
169 this->getLogger().info() << "Validation layers are enabled. Adding "
170 "VK_EXT_DEBUG_UTILS_EXTENSION_NAME to "
171 "required instance extensions.";
173 }
174 return std::vector<std::string>(extensions.begin(), extensions.end());
175}
176
178{
179 this->getLogger().info() << "Checking if Desktop window should close...";
180 return glfwWindowShouldClose(_window);
181}
182
183utility::event::KeyboardEvent::KeyCode
185{
186 return static_cast<utility::event::KeyboardEvent::KeyCode>(glfwKey);
187}
188
189utility::event::MouseButtonEvent::Button
191{
192 this->getLogger().info()
193 << "Converting GLFW mouse button to event button...";
194 switch (glfwButton) {
196 return utility::event::MouseButtonEvent::Button::Left;
198 return utility::event::MouseButtonEvent::Button::Right;
200 return utility::event::MouseButtonEvent::Button::Middle;
202 return utility::event::MouseButtonEvent::Button::X1;
204 return utility::event::MouseButtonEvent::Button::X2;
205 default:
206 return utility::event::MouseButtonEvent::Button::Unknown;
207 }
208}
209
210utility::event::KeyboardEvent::KeyModifiers
212{
213 std::uint16_t result = 0;
214
215 if (glfwMods & GLFW_MOD_SHIFT) {
216 result |= static_cast<std::uint16_t>(
217 utility::event::KeyboardEvent::KeyModifiers::Shift);
218 }
220 result |= static_cast<std::uint16_t>(
221 utility::event::KeyboardEvent::KeyModifiers::Ctrl);
222 }
223 if (glfwMods & GLFW_MOD_ALT) {
224 result |= static_cast<std::uint16_t>(
225 utility::event::KeyboardEvent::KeyModifiers::Alt);
226 }
227 if (glfwMods & GLFW_MOD_SUPER) {
228 result |= static_cast<std::uint16_t>(
229 utility::event::KeyboardEvent::KeyModifiers::Gui);
230 }
231
232 return static_cast<utility::event::KeyboardEvent::KeyModifiers>(result);
233}
234
235utility::event::MouseMotionEvent::MousePosition
237{
238 double xPos = 0.0;
239 double yPos = 0.0;
240
241 glfwGetCursorPos(_window, &xPos, &yPos);
242 return convertCursorPosition(xPos, yPos);
243}
244
245utility::event::MouseMotionEvent::MousePosition
247{
248 int windowWidth = 0;
249 int windowHeight = 0;
250 int framebufferWidth = 0;
251 int framebufferHeight = 0;
252
255
256 const float scaleX =
257 windowWidth > 0
258 ? static_cast<float>(framebufferWidth)
259 / static_cast<float>(windowWidth)
260 : 1.0f;
261 const float scaleY =
262 windowHeight > 0
263 ? static_cast<float>(framebufferHeight)
264 / static_cast<float>(windowHeight)
265 : 1.0f;
266
267 return utility::event::MouseMotionEvent::MousePosition {
268 static_cast<float>(xPos * scaleX), static_cast<float>(yPos * scaleY)
269 };
270}
271
272std::vector<std::shared_ptr<utility::event::KeyboardEvent>>
274{
275 std::vector<std::shared_ptr<utility::event::KeyboardEvent>> events;
280
281 for (int key: movementKeys) {
282 if (glfwGetKey(_window, key) == GLFW_PRESS) {
283 auto event = std::make_shared<utility::event::KeyboardEvent>();
284 event->setKeycode(convertGlfwKeyToKeyCode(key));
285 event->setIsDownEvent(true);
286 events.push_back(std::move(event));
287 }
288 }
289
290 return events;
291}
Abstract base class for Vulkan device backends.
static void updateViewForExtent(utility::graphic::ViewF &view, VkExtent2D extent)
Updates a view's viewport size and perspective for a new swapchain extent.
Represents the device context for the engine, managing Vulkan-related resources and operations.
~IDesktopPlatform() override
Destructor for DesktopPlatform.
bool shouldClose() const override
Check if the Desktop platform should close.
std::shared_ptr< ASwapchainContext > createSwapchainContext(const DeviceContext &deviceContext) const override
Create a swapchain context for the Desktop platform.
utility::event::KeyboardEvent::KeyCode convertGlfwKeyToKeyCode(int glfwKey) const
Convert a GLFW key code to a KeyboardEvent::KeyCode.
utility::event::MouseButtonEvent::Button convertGlfwMouseButtonToButton(int glfwMouseButton) const
Convert a GLFW mouse button code to a MouseButtonEvent::Button.
GLFWwindow * _window
Currently active GLFW window for the Desktop platform.
utility::event::KeyboardEvent::KeyModifiers convertGlfwModsToKeyModifiers(int glfwMods) const
Convert GLFW modifier flags to KeyModifiers.
utility::event::MouseMotionEvent::MousePosition convertCursorPosition(double xPos, double yPos) const
Convert a GLFW cursor position to framebuffer coordinates.
virtual std::vector< std::shared_ptr< utility::event::Event > > pollEvents(ADeviceBackend &deviceBackend) override
Poll events for the Desktop platform.
virtual std::vector< std::string > getRequiredInstanceExtensions() const override=0
Get the required instance extensions for the Desktop platform.
utility::event::MouseMotionEvent::MousePosition getMousePosition() const
Get the current mouse position.
std::vector< std::shared_ptr< utility::event::KeyboardEvent > > getPressedMovementKeys() const
Get the current state of movement keys.
std::shared_ptr< ADeviceBackend > createDeviceBackend() const override
Create a device context for the Desktop platform.
Small result wrapper used by helpers that must return both a status and a value (e....
Definition Error.hpp:89