From 7998d53c3178e727b41ca505aee81ee9f149c687 Mon Sep 17 00:00:00 2001 From: Light3039 Date: Fri, 21 May 2021 20:33:37 +0430 Subject: [PATCH] Added Window --- Engine/premake5.lua | 1 + Engine/src/Engine/Core/Application.cpp | 5 ++- Engine/src/Engine/Core/Application.h | 16 ++++++-- Engine/src/Engine/Core/Window.h | 31 ++++++++++++++ Engine/src/Engine/EntryPoint.h | 1 + .../Engine/Platforms/OS/Windows/wWindow.cpp | 41 +++++++++++++++++++ .../src/Engine/Platforms/OS/Windows/wWindow.h | 31 ++++++++++++++ 7 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 Engine/src/Engine/Core/Window.h create mode 100644 Engine/src/Engine/Platforms/OS/Windows/wWindow.cpp create mode 100644 Engine/src/Engine/Platforms/OS/Windows/wWindow.h diff --git a/Engine/premake5.lua b/Engine/premake5.lua index 50baf6b..8cd5c70 100644 --- a/Engine/premake5.lua +++ b/Engine/premake5.lua @@ -26,6 +26,7 @@ project "Engine" "%{prj.location}/src/Engine/", (dependenciesdir .. "spdlog/include/"), + (dependenciesdir .. "glfw/include/"), } links diff --git a/Engine/src/Engine/Core/Application.cpp b/Engine/src/Engine/Core/Application.cpp index 7e29721..b3ca33e 100644 --- a/Engine/src/Engine/Core/Application.cpp +++ b/Engine/src/Engine/Core/Application.cpp @@ -1,6 +1,7 @@ #include "Application.h" #include "Logger.h" +#include "Window.h" namespace Light { @@ -8,6 +9,8 @@ namespace Light { { Logger::Initialize(); + m_Window = std::unique_ptr(Window::Create({ "Title", 800u, 600u, false })); + LT_ENGINE_INFO("Initialized Logger"); } @@ -17,7 +20,7 @@ namespace Light { void Application::GameLoop() { - while (true); + while (true) { m_Window->OnUpdate(); } } } \ No newline at end of file diff --git a/Engine/src/Engine/Core/Application.h b/Engine/src/Engine/Core/Application.h index 97ffe58..e6171c2 100644 --- a/Engine/src/Engine/Core/Application.h +++ b/Engine/src/Engine/Core/Application.h @@ -2,17 +2,27 @@ #include "Base.h" +#include + namespace Light { + class Window; + class Application { + private: + std::unique_ptr m_Window = nullptr; + public: - Application(); virtual ~Application(); void GameLoop(); + + // To be defined in client project + friend Application* CreateApplication(); + + protected: + Application(); }; - Application* CreateApplication(); - } \ No newline at end of file diff --git a/Engine/src/Engine/Core/Window.h b/Engine/src/Engine/Core/Window.h new file mode 100644 index 0000000..3d2659f --- /dev/null +++ b/Engine/src/Engine/Core/Window.h @@ -0,0 +1,31 @@ +#pragma once + +#include "Base.h" + +#include + +namespace Light { + + struct WindowProperties + { + std::string title; + unsigned int width, height; + bool vsync; + }; + + class Window + { + public: + virtual ~Window() = default; + + virtual void OnUpdate() = 0; + + virtual unsigned int GetHeight() = 0; + virtual unsigned int GetWidth() = 0; + + virtual inline void* GetNativeHandle() = 0; + + static Window* Create(const WindowProperties& properties); + }; + +} \ No newline at end of file diff --git a/Engine/src/Engine/EntryPoint.h b/Engine/src/Engine/EntryPoint.h index dd72b8a..8c2b948 100644 --- a/Engine/src/Engine/EntryPoint.h +++ b/Engine/src/Engine/EntryPoint.h @@ -2,6 +2,7 @@ #ifdef LT_PLATFORM_WINDOWS +// To be defined in client project extern Light::Application* Light::CreateApplication(); int main(int argc, char** argv) diff --git a/Engine/src/Engine/Platforms/OS/Windows/wWindow.cpp b/Engine/src/Engine/Platforms/OS/Windows/wWindow.cpp new file mode 100644 index 0000000..b1def44 --- /dev/null +++ b/Engine/src/Engine/Platforms/OS/Windows/wWindow.cpp @@ -0,0 +1,41 @@ +#include "wWindow.h" + +namespace Light { + + Window* Window::Create(const WindowProperties& properties) + { + return new wWindow(properties); + } + + wWindow::wWindow(const WindowProperties& properties) + : m_Properties(properties) + { + if (!glfwInit()) __debugbreak(); + + m_Handle = glfwCreateWindow(properties.width, properties.height, properties.title.c_str(), nullptr, nullptr); + + glfwMakeContextCurrent(m_Handle); + } + + wWindow::~wWindow() + { + glfwDestroyWindow(m_Handle); + } + + void wWindow::OnUpdate() + { + glfwPollEvents(); + glfwSwapBuffers(m_Handle); + } + + unsigned int wWindow::GetWidth() + { + return m_Properties.width; + } + + unsigned int wWindow::GetHeight() + { + return m_Properties.height; + } + +} \ No newline at end of file diff --git a/Engine/src/Engine/Platforms/OS/Windows/wWindow.h b/Engine/src/Engine/Platforms/OS/Windows/wWindow.h new file mode 100644 index 0000000..286e9b7 --- /dev/null +++ b/Engine/src/Engine/Platforms/OS/Windows/wWindow.h @@ -0,0 +1,31 @@ +#pragma once + +#include "Base.h" + +#include "Core/Window.h" + +#include + +#include + +namespace Light { + + class wWindow : public Window + { + private: + GLFWwindow* m_Handle = nullptr; + WindowProperties m_Properties; + public: + wWindow(const WindowProperties& properties); + + ~wWindow(); + + virtual void OnUpdate() override; + + virtual unsigned int GetWidth() override; + virtual unsigned int GetHeight() override; + + virtual inline void* GetNativeHandle() override { return m_Handle; } + }; + +} \ No newline at end of file