From 933ac514a00291850e8965126a8207ca9a6c4c35 Mon Sep 17 00:00:00 2001 From: light7734 Date: Sat, 12 Jul 2025 14:40:10 +0330 Subject: [PATCH] build: fix build on Windows using msvc compiler --- modules/base/include/base/base.hpp | 16 +- modules/base/src/pch.hpp | 1 + modules/ecs/src/scene.cpp | 4 +- .../include/renderer/dx/shared_context.hpp | 8 +- .../renderer/programs/tinted_texture.hpp | 1 + modules/renderer/src/gl/shader.cpp | 8 +- modules/renderer/src/renderer.cpp | 1 + .../window/include/window/windows/window.hpp | 51 ++++ modules/window/src/windows/window.cpp | 228 ++++++++++++++++++ 9 files changed, 295 insertions(+), 23 deletions(-) diff --git a/modules/base/include/base/base.hpp b/modules/base/include/base/base.hpp index 76fd710..63bcf21 100644 --- a/modules/base/include/base/base.hpp +++ b/modules/base/include/base/base.hpp @@ -55,10 +55,12 @@ enum class Platform : uint8_t namespace constants { #if defined(LIGHT_PLATFORM_WINDOWS) - #define lt_win(x) x + #define lt_win(x) constexpr auto platform = Platform::windows; constexpr auto platform_name = "windows"; +#undef LIGHT_PLATFORM_WINDOWS + #elif defined(LIGHT_PLATFORM_LINUX) #define lt_lin(x) x constexpr auto platform = Platform::gnu; @@ -77,18 +79,6 @@ constexpr auto platform_name = "mac"; } // namespace constants -template -concept is_linux = true; - -auto linux_only(auto value) - requires is_linux -{ - if constexpr (is_linux) - { - return value; - } -} - /* bit-wise */ constexpr auto bit(auto x) { diff --git a/modules/base/src/pch.hpp b/modules/base/src/pch.hpp index 7257c69..20aa083 100644 --- a/modules/base/src/pch.hpp +++ b/modules/base/src/pch.hpp @@ -11,6 +11,7 @@ /** Stdlib */ #include +#include #include #include #include diff --git a/modules/ecs/src/scene.cpp b/modules/ecs/src/scene.cpp index a202ef7..2e0c087 100644 --- a/modules/ecs/src/scene.cpp +++ b/modules/ecs/src/scene.cpp @@ -33,8 +33,8 @@ void Scene::on_update(float deltaTime) void Scene::on_render(const Ref &targetFrameBuffer /* = nullptr */) { - auto *sceneCamera = (Camera *) {}; - auto *sceneCameraTransform = (TransformComponent *) {}; + auto *sceneCamera = (Camera *) nullptr; + auto *sceneCameraTransform = (TransformComponent *) nullptr; /* scene camera */ { diff --git a/modules/renderer/include/renderer/dx/shared_context.hpp b/modules/renderer/include/renderer/dx/shared_context.hpp index ff1084b..74febbb 100644 --- a/modules/renderer/include/renderer/dx/shared_context.hpp +++ b/modules/renderer/include/renderer/dx/shared_context.hpp @@ -29,22 +29,22 @@ public: return m_render_target_view; } - [[nodiscard]] auto &GetDeviceRef() -> Microsoft::WRL::ComPtr + [[nodiscard]] auto GetDeviceRef() -> Microsoft::WRL::ComPtr { return m_device; } - [[nodiscard]] auto &GetDeviceContextRef() -> Microsoft::WRL::ComPtr + [[nodiscard]] auto GetDeviceContextRef() -> Microsoft::WRL::ComPtr { return m_deviceContext; } - [[nodiscard]] auto &GetSwapChainRef() -> Microsoft::WRL::ComPtr + [[nodiscard]] auto GetSwapChainRef() -> Microsoft::WRL::ComPtr { return m_swap_chain; } - [[nodiscard]] auto &GetRenderTargetViewRef() -> Microsoft::WRL::ComPtr + [[nodiscard]] auto GetRenderTargetViewRef() -> Microsoft::WRL::ComPtr { return m_render_target_view; } diff --git a/modules/renderer/include/renderer/programs/tinted_texture.hpp b/modules/renderer/include/renderer/programs/tinted_texture.hpp index 28999f1..1c2a2e2 100644 --- a/modules/renderer/include/renderer/programs/tinted_texture.hpp +++ b/modules/renderer/include/renderer/programs/tinted_texture.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include diff --git a/modules/renderer/src/gl/shader.cpp b/modules/renderer/src/gl/shader.cpp index d1b4710..295159b 100644 --- a/modules/renderer/src/gl/shader.cpp +++ b/modules/renderer/src/gl/shader.cpp @@ -23,13 +23,13 @@ glShader::glShader( pixel_asset->unpack_blob(pixel_blob_metadata.tag, pixel_blob.data(), pixel_blob.size()); auto vertex_source = std::string { - vertex_blob.data(), - vertex_blob.data() + vertex_blob.size(), // NOLINT + reinterpret_cast(vertex_blob.data()), + reinterpret_cast(vertex_blob.data()) + vertex_blob.size(), // NOLINT }; auto pixel_source = std::string { - pixel_blob.data(), - pixel_blob.data() + pixel_blob.size(), // NOLINT + reinterpret_cast(pixel_blob.data()), + reinterpret_cast(pixel_blob.data()) + pixel_blob.size(), // NOLINT }; const auto vertex_shader = compile_shader(vertex_source, Shader::Stage::vertex); diff --git a/modules/renderer/src/renderer.cpp b/modules/renderer/src/renderer.cpp index aa4dd3c..fbf9593 100644 --- a/modules/renderer/src/renderer.cpp +++ b/modules/renderer/src/renderer.cpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace lt { diff --git a/modules/window/include/window/windows/window.hpp b/modules/window/include/window/windows/window.hpp index e69de29..a2c9e27 100644 --- a/modules/window/include/window/windows/window.hpp +++ b/modules/window/include/window/windows/window.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include + +struct GLFWwindow; + +namespace lt { + +class Event; +class WindowResizedEvent; + +class wWindow: public Window +{ +public: + wWindow(std::function callback); + + ~wWindow() override; + + void poll_events() override; + + void on_event(const Event &event) override; + + void set_properties( + const WindowProperties &properties, + bool overrideVisibility = false + ) override; + + void set_title(const std::string &title) override; + + void set_size(const glm::uvec2 &size, bool additive = false) override; + + void set_v_sync(bool vsync, bool toggle = false) override; + + void set_visibility(bool visible, bool toggle = false) override; + + [[nodiscard]] auto get_handle() -> void * override + { + return m_handle; + } + +private: + GLFWwindow *m_handle { nullptr }; + + std::function m_event_callback; + + void on_window_resize(const WindowResizedEvent &event); + + void bind_glfw_events(); +}; + +} // namespace lt diff --git a/modules/window/src/windows/window.cpp b/modules/window/src/windows/window.cpp index e69de29..c3f960a 100644 --- a/modules/window/src/windows/window.cpp +++ b/modules/window/src/windows/window.cpp @@ -0,0 +1,228 @@ +#include +#include +#include +#include +#include +#include +#include + +namespace lt { + +Window::~Window() +{ +} + +auto Window::create(const std::function &callback) -> Scope +{ + return create_scope(callback); +} + +wWindow::wWindow(std::function callback) + : m_event_callback(std::move(std::move(callback))) +{ + // init glfw + ensure(glfwInit(), "wWindow::wWindow: failed to initialize 'glfw'"); + + // create window + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); + + m_handle = glfwCreateWindow(1u, 1u, "", nullptr, nullptr); + ensure(m_handle, "wWindow::wWindow: failed to create 'GLFWwindow'"); + + glfwSetWindowUserPointer(m_handle, &m_event_callback); + bind_glfw_events(); +} + +wWindow::~wWindow() +{ + glfwDestroyWindow(m_handle); +} + +void wWindow::poll_events() +{ + glfwPollEvents(); +} + +void wWindow::on_event(const Event &event) +{ + switch (event.get_event_type()) + { + /* closed */ + case EventType::WindowClosed: b_Closed = true; break; + + /* resized */ + case EventType::WindowResized: + on_window_resize(dynamic_cast(event)); + break; + } +} + +void wWindow::on_window_resize(const WindowResizedEvent &event) +{ + m_properties.size = event.get_size(); +} + +void wWindow:: + set_properties(const WindowProperties &properties, bool overrideVisibility /* = false */) +{ + // save the visibility status and re-assign if 'overrideVisibility' is false + auto visible = overrideVisibility ? properties.visible : m_properties.visible; + m_properties = properties; + m_properties.visible = visible; + + // set properties + set_title(properties.title); + set_size(properties.size); + set_v_sync(properties.vsync); + set_visibility(visible); +} + +void wWindow::set_title(const std::string &title) +{ + m_properties.title = title; + + glfwSetWindowTitle(m_handle, title.c_str()); +} + +void wWindow::set_size(const glm::uvec2 &size, bool additive /* = false */) +{ + m_properties.size.x = size.x == 0u ? m_properties.size.x : + additive ? m_properties.size.x + size.x : + size.x; + m_properties.size.y = size.y == 0u ? m_properties.size.y : + additive ? m_properties.size.y + size.y : + size.y; + + + glfwSetWindowSize(m_handle, size.x, size.y); +} + +void wWindow::set_v_sync(bool vsync, bool toggle /* = false */) +{ + m_properties.vsync = toggle ? !m_properties.vsync : vsync; + + glfwSwapInterval(m_properties.vsync); +} + +void wWindow::set_visibility(bool visible, bool toggle) +{ + m_properties.visible = toggle ? !m_properties.visible : visible; + + if (m_properties.visible) + { + glfwShowWindow(m_handle); + } + else + { + glfwHideWindow(m_handle); + } +} + +void wWindow::bind_glfw_events() +{ + glfwSetCursorPosCallback(m_handle, [](GLFWwindow *window, double xpos, double ypos) { + auto callback = *(std::function *)glfwGetWindowUserPointer(window); + + auto event = MouseMovedEvent { + static_cast(xpos), + static_cast(ypos), + }; + callback(event); + }); + + glfwSetMouseButtonCallback( + m_handle, + [](GLFWwindow *window, int button, int action, int /*mods*/) { + std::function const callback = *( + std::function * + )glfwGetWindowUserPointer(window); + + if (action == GLFW_PRESS) + { + auto event = ButtonPressedEvent { button }; + callback(event); + } + else if (action == GLFW_RELEASE) + { + auto event = ButtonReleasedEvent { button }; + callback(event); + } + } + ); + + glfwSetScrollCallback(m_handle, [](GLFWwindow *window, double /*xoffset*/, double yoffset) { + auto callback = *(std::function *)glfwGetWindowUserPointer(window); + + auto event = WheelScrolledEvent { static_cast(yoffset) }; + callback(event); + }); + + glfwSetKeyCallback( + m_handle, + [](GLFWwindow *window, int key, int /*scancode*/, int action, int /*mods*/) { + auto callback = *(std::function *)glfwGetWindowUserPointer(window); + + if (action == GLFW_PRESS) + { + auto event = KeyPressedEvent { key }; + callback(event); + } + else if (action == GLFW_RELEASE) + { + auto event = KeyReleasedEvent { key }; + callback(event); + } + } + ); + + glfwSetCharCallback(m_handle, [](GLFWwindow *window, unsigned int character) { + auto callback = *(std::function *)glfwGetWindowUserPointer(window); + + auto event = SetCharEvent { character }; + callback(event); + }); + + glfwSetWindowPosCallback(m_handle, [](GLFWwindow *window, int xpos, int ypos) { + auto callback = *(std::function *)glfwGetWindowUserPointer(window); + auto event = WindowMovedEvent { xpos, ypos }; + + callback(event); + }); + + glfwSetWindowSizeCallback(m_handle, [](GLFWwindow *window, int width, int height) { + auto callback = *(std::function *)glfwGetWindowUserPointer(window); + auto event = WindowResizedEvent { + static_cast(width), + static_cast(height), + }; + + callback(event); + }); + + glfwSetWindowCloseCallback(m_handle, [](GLFWwindow *window) { + auto callback = *(std::function *)glfwGetWindowUserPointer(window); + auto event = WindowClosedEvent {}; + + callback(event); + }); + + glfwSetWindowFocusCallback(m_handle, [](GLFWwindow *window, int focus) { + auto callback = *(std::function *)glfwGetWindowUserPointer(window); + + if (focus == GLFW_TRUE) + { + auto event = WindowGainFocusEvent {}; + callback(event); + } + else + { + auto event = WindowLostFocusEvent {}; + callback(event); + } + }); +} + +} // namespace lt