From 612b382f3028ca1b199fcde07a3fd991e18417bb Mon Sep 17 00:00:00 2001 From: Light Date: Mon, 14 Jun 2021 10:01:28 +0430 Subject: [PATCH] Window - Moved setting window properties to ClientSide - Made Application::OnEvent private --- Engine/src/Engine/Core/Application.cpp | 9 +++++--- Engine/src/Engine/Core/Application.h | 8 +++++-- Engine/src/Engine/Core/Window.h | 8 +++++-- Engine/src/Platform/OS/Windows/wWindow.cpp | 25 +++++++++++++++++++--- Engine/src/Platform/OS/Windows/wWindow.h | 8 +++++-- Sandbox/src/SandboxApp.cpp | 12 ++++++++++- 6 files changed, 57 insertions(+), 13 deletions(-) diff --git a/Engine/src/Engine/Core/Application.cpp b/Engine/src/Engine/Core/Application.cpp index 88e557f..92b975f 100644 --- a/Engine/src/Engine/Core/Application.cpp +++ b/Engine/src/Engine/Core/Application.cpp @@ -17,12 +17,12 @@ namespace Light { Application::Application() { Logger::Initialize(); - m_Window = std::unique_ptr(Window::Create({ "Title", 800u, 600u, false }, std::bind(&Application::OnEvent, this , std::placeholders::_1))); + m_Window = std::unique_ptr(Light::Window::Create(std::bind(&Light::Application::OnEvent, this, std::placeholders::_1))); } Application::~Application() { - LT_ENGINE_INFO("Application::~Application: Terminating Application"); + LT_ENGINE_TRACE("Application::~Application()"); } void Application::GameLoop() @@ -30,10 +30,13 @@ namespace Light { // check LT_ENGINE_ASSERT(!m_LayerStack.IsEmpty(), "Application::GameLoop: Layerstack is empty"); - // Log some data + // Log window data m_Window->GetGfxContext()->LogDebugData(); m_Window->GetGfxContext()->GetUserInterface()->LogDebugData(); + // Show window + m_Window->SetVisible(true); + // GAMELOOP // while (m_Window->IsOpen()) { diff --git a/Engine/src/Engine/Core/Application.h b/Engine/src/Engine/Core/Application.h index b7a32e4..c6b32f6 100644 --- a/Engine/src/Engine/Core/Application.h +++ b/Engine/src/Engine/Core/Application.h @@ -12,9 +12,11 @@ namespace Light { class Application { private: - std::unique_ptr m_Window = nullptr; LayerStack m_LayerStack; + protected: + std::unique_ptr m_Window = nullptr; + public: Application(const Application&) = delete; Application& operator=(const Application&) = delete; @@ -22,13 +24,15 @@ namespace Light { virtual ~Application(); void GameLoop(); - void OnEvent(const Event& event); // To be defined in client project friend Application* CreateApplication(); protected: Application(); + + private: + void OnEvent(const Event& event); }; } \ No newline at end of file diff --git a/Engine/src/Engine/Core/Window.h b/Engine/src/Engine/Core/Window.h index 7188a4b..1acd699 100644 --- a/Engine/src/Engine/Core/Window.h +++ b/Engine/src/Engine/Core/Window.h @@ -21,11 +21,17 @@ namespace Light { bool b_Open; public: + static Window* Create(const WindowProperties& properties, std::function callback); + Window(const Window&) = delete; Window& operator=(const Window&) = delete; virtual ~Window() = default; + virtual void SetProperties(const WindowProperties& properties) = 0; + + virtual void SetVisible(bool visible) = 0; + inline GraphicsContext* GetGfxContext() { return m_GraphicsContext.get(); } inline bool IsOpen() const { return b_Open; } @@ -38,8 +44,6 @@ namespace Light { virtual inline void* GetNativeHandle() = 0; - static Window* Create(const WindowProperties& properties, std::function callback); - protected: Window() = default; }; diff --git a/Engine/src/Platform/OS/Windows/wWindow.cpp b/Engine/src/Platform/OS/Windows/wWindow.cpp index ee77820..167ef5b 100644 --- a/Engine/src/Platform/OS/Windows/wWindow.cpp +++ b/Engine/src/Platform/OS/Windows/wWindow.cpp @@ -17,12 +17,14 @@ namespace Light { return new wWindow(properties, callback); } - wWindow::wWindow(const WindowProperties& properties, std::function callback) - : m_Properties(properties), m_EventCallback(callback) + wWindow::wWindow(std::function callback) + : m_EventCallback(callback) { LT_ENGINE_ASSERT(glfwInit(), "wWindow::wWindow: glfwInit: failed to initialize glfw"); - m_Handle = glfwCreateWindow(properties.width, properties.height, properties.title.c_str(), nullptr, nullptr); + glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); + + m_Handle = glfwCreateWindow(1u, 1u, "", nullptr, nullptr); LT_ENGINE_ASSERT(m_Handle, "wWindow::wWindow: glfwCreateWindow: failed to create glfw window"); glfwSetWindowUserPointer(m_Handle, &m_EventCallback); @@ -37,6 +39,23 @@ namespace Light { glfwDestroyWindow(m_Handle); } + void wWindow::SetProperties(const WindowProperties& properties) + { + m_Properties = properties; + + glfwSetWindowSize(m_Handle, properties.width, properties.height); + glfwSetWindowTitle(m_Handle, properties.title.c_str()); + glfwSwapInterval((int)properties.vsync); + } + + void wWindow::SetVisible(bool visible) + { + if (visible) + glfwShowWindow(m_Handle); + else + glfwHideWindow(m_Handle); + } + void wWindow::PollEvents() { glfwPollEvents(); diff --git a/Engine/src/Platform/OS/Windows/wWindow.h b/Engine/src/Platform/OS/Windows/wWindow.h index cd03cb2..cd361e5 100644 --- a/Engine/src/Platform/OS/Windows/wWindow.h +++ b/Engine/src/Platform/OS/Windows/wWindow.h @@ -13,15 +13,19 @@ namespace Light { { private: GLFWwindow* m_Handle = nullptr; - WindowProperties m_Properties; + WindowProperties m_Properties = {}; std::function m_EventCallback; public: - wWindow(const WindowProperties& properties, std::function callback); + wWindow(std::function callback); ~wWindow(); + virtual void SetProperties(const WindowProperties& properties) override; + + virtual void SetVisible(bool visible) override; + virtual void PollEvents() override; virtual void OnEvent(const Event& event) override; diff --git a/Sandbox/src/SandboxApp.cpp b/Sandbox/src/SandboxApp.cpp index 6f15588..01f8873 100644 --- a/Sandbox/src/SandboxApp.cpp +++ b/Sandbox/src/SandboxApp.cpp @@ -1,7 +1,7 @@ #define LIGHT_ENTRY_POINT #include - #include "SandboxLayer.h" +#include "SandboxLayer.h" class Sandbox : public Light::Application { @@ -10,6 +10,16 @@ public: { LT_CLIENT_TRACE("Sandbox::Sandbox"); + // Set window properties + Light::WindowProperties properties; + properties.title = "Sandbox"; + properties.width = 800u; + properties.height = 600u; + properties.vsync = true; + + m_Window->SetProperties(properties); + + // Attach the sandbox layer Light::LayerStack::AttachLayer(new SandboxLayer("SandboxLayer")); }