diff --git a/Engine/premake5.lua b/Engine/premake5.lua index 0aa4b16..efb146f 100644 --- a/Engine/premake5.lua +++ b/Engine/premake5.lua @@ -26,7 +26,10 @@ project "Engine" includedirs { -- Engine + "%{prj.location}/src/", "%{prj.location}/src/Engine/", + "%{prj.location}/src/Platform/GraphicsAPI", + "%{prj.location}/src/Platform/OS", -- 3rd party (dependenciesdir .. "spdlog/include/"), diff --git a/Engine/src/Engine/Core/Application.cpp b/Engine/src/Engine/Core/Application.cpp index d04ea5c..689d1cb 100644 --- a/Engine/src/Engine/Core/Application.cpp +++ b/Engine/src/Engine/Core/Application.cpp @@ -17,9 +17,6 @@ namespace Light { m_Window = std::unique_ptr(Window::Create({ "Title", 800u, 600u, false }, std::bind(&Application::OnEvent, this , std::placeholders::_1))); - TestLayer* layer = new TestLayer("Test Layer"); - m_LayerStack.PushLayer(layer); - LT_ENGINE_INFO("Initialized Logger"); } @@ -31,7 +28,10 @@ namespace Light { { while (m_Window->IsOpen()) { - m_Window->OnUpdate(); + m_Window->PollEvents(); + + m_Window->GetGfxContext()->GetRenderCommand()->SwapBuffers(); + m_Window->GetGfxContext()->GetRenderCommand()->ClearBackBuffer(); } } diff --git a/Engine/src/Engine/Core/Window.h b/Engine/src/Engine/Core/Window.h index 84bad4d..bf133e1 100644 --- a/Engine/src/Engine/Core/Window.h +++ b/Engine/src/Engine/Core/Window.h @@ -4,6 +4,8 @@ #include "Events/Event.h" +#include "Graphics/GraphicsContext.h" + #include namespace Light { @@ -18,13 +20,16 @@ namespace Light { class Window { protected: + std::unique_ptr m_GraphicsContext; bool b_Open; public: virtual ~Window() = default; + inline GraphicsContext* GetGfxContext() { return m_GraphicsContext.get(); } + inline bool IsOpen() const { return b_Open; } - virtual void OnUpdate() = 0; + virtual void PollEvents() = 0; virtual void OnEvent(const Event& event) = 0; virtual unsigned int GetHeight() = 0; diff --git a/Engine/src/Engine/Graphics/GraphicsContext.cpp b/Engine/src/Engine/Graphics/GraphicsContext.cpp new file mode 100644 index 0000000..9b3d2f6 --- /dev/null +++ b/Engine/src/Engine/Graphics/GraphicsContext.cpp @@ -0,0 +1,28 @@ +#include "ltpch.h" +#include "GraphicsContext.h" + +#include "OpenGL/glGraphicsContext.h" + +namespace Light { + + GraphicsContext* GraphicsContext::s_Context; + + GraphicsContext* GraphicsContext::Create(GraphicsAPI api, GLFWwindow* windowHandle) + { + switch (api) + { + case Light::GraphicsAPI::Default: + case Light::GraphicsAPI::OpenGL: + s_Context = new glGraphicsContext(windowHandle); + s_Context->m_RenderCommand = std::unique_ptr(RenderCommand::Create(windowHandle)); + // ...Renderer + // ...UserInterface... + + return s_Context; + + default: + return nullptr; // TODO: Add ASSERT + } + } + +} \ No newline at end of file diff --git a/Engine/src/Engine/Graphics/GraphicsContext.h b/Engine/src/Engine/Graphics/GraphicsContext.h new file mode 100644 index 0000000..3d4176d --- /dev/null +++ b/Engine/src/Engine/Graphics/GraphicsContext.h @@ -0,0 +1,35 @@ +#pragma once + +#include "Base.h" + +#include "RenderCommand.h" + +struct GLFWwindow {}; + +namespace Light { + + enum class GraphicsAPI + { + Default, OpenGL // TODO: Add DirectX, Vulkan, Metal. + }; + + class GraphicsContext + { + private: + static GraphicsContext* s_Context; + + std::unique_ptr m_RenderCommand; + protected: + GraphicsAPI m_GraphicsAPI; + + public: + virtual ~GraphicsContext() = default; + + static GraphicsContext* Create(GraphicsAPI api, GLFWwindow* windowHandle); + + static inline GraphicsAPI GetGraphicsAPI() { return s_Context->m_GraphicsAPI; } + + virtual RenderCommand* GetRenderCommand() { return m_RenderCommand.get(); } + }; + +} \ No newline at end of file diff --git a/Engine/src/Engine/Graphics/RenderCommand.cpp b/Engine/src/Engine/Graphics/RenderCommand.cpp new file mode 100644 index 0000000..32de053 --- /dev/null +++ b/Engine/src/Engine/Graphics/RenderCommand.cpp @@ -0,0 +1,22 @@ +#include "ltpch.h" +#include "RenderCommand.h" + +#include "GraphicsContext.h" + +#include "OpenGL/glRenderCommand.h" + +namespace Light { + + RenderCommand* RenderCommand::Create(GLFWwindow* windowHandle) + { + switch (GraphicsContext::GetGraphicsAPI()) + { + case GraphicsAPI::OpenGL: + return new glRenderCommand(windowHandle); + + default: + return nullptr; // TODO: Add ASSERT + } + } + +} \ No newline at end of file diff --git a/Engine/src/Engine/Graphics/RenderCommand.h b/Engine/src/Engine/Graphics/RenderCommand.h new file mode 100644 index 0000000..285134d --- /dev/null +++ b/Engine/src/Engine/Graphics/RenderCommand.h @@ -0,0 +1,20 @@ +#pragma once + +struct GLFWwindow; + +namespace Light { + + class RenderCommand + { + private: + public: + static RenderCommand* Create(GLFWwindow* windowHandle); + + virtual void SwapBuffers() = 0; + virtual void ClearBackBuffer() = 0; + + virtual void Draw(unsigned int count) = 0; + virtual void DrawIndexed(unsigned int count) = 0; + }; + +} \ No newline at end of file diff --git a/Engine/src/Engine/UserInterface/UserInterface.cpp b/Engine/src/Engine/UserInterface/UserInterface.cpp new file mode 100644 index 0000000..5893a86 --- /dev/null +++ b/Engine/src/Engine/UserInterface/UserInterface.cpp @@ -0,0 +1,11 @@ +#include "ltpch.h" + +namespace Light { + + class UserInterface + { + private: + public: + }; + +} \ No newline at end of file diff --git a/Engine/src/Engine/UserInterface/UserInterface.h b/Engine/src/Engine/UserInterface/UserInterface.h new file mode 100644 index 0000000..e69de29 diff --git a/Engine/src/LightEngine.h b/Engine/src/LightEngine.h index 0bbce6f..0183dfd 100644 --- a/Engine/src/LightEngine.h +++ b/Engine/src/LightEngine.h @@ -1,11 +1,11 @@ #pragma once -#include "Engine/Events/Event.h" -#include "Engine/Events/KeyboardEvents.h" -#include "Engine/Events/MouseEvents.h" +#include "Events/Event.h" +#include "Events/KeyboardEvents.h" +#include "Events/MouseEvents.h" -#include "Engine/Core/Application.h" -#include "Engine/Core/Logger.h" +#include "Core/Application.h" +#include "Core/Logger.h" -#include "Engine/Base.h" -#include "Engine/EntryPoint.h" \ No newline at end of file +#include "Base.h" +#include "EntryPoint.h" \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glGraphicsContext.cpp b/Engine/src/Platform/GraphicsAPI/OpenGL/glGraphicsContext.cpp new file mode 100644 index 0000000..448ef64 --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glGraphicsContext.cpp @@ -0,0 +1,22 @@ +#include "ltpch.h" +#include "glGraphicsContext.h" + +#include +#include + +namespace Light { + + glGraphicsContext::glGraphicsContext(GLFWwindow* windowHandle) + : m_WindowHandle(windowHandle) + { + m_GraphicsAPI = GraphicsAPI::OpenGL; + + glfwMakeContextCurrent(windowHandle); + gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); + + LT_ENGINE_INFO("glGraphicsContext:"); + LT_ENGINE_INFO(" Renderer: {}", glGetString(GL_RENDERER)); + LT_ENGINE_INFO(" Version: {}", glGetString(GL_VERSION)); + } + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glGraphicsContext.h b/Engine/src/Platform/GraphicsAPI/OpenGL/glGraphicsContext.h new file mode 100644 index 0000000..1d9329a --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glGraphicsContext.h @@ -0,0 +1,19 @@ +#pragma once + +#include "Base.h" +#include "Graphics/GraphicsContext.h" + +struct GLFWwindow; + +namespace Light { + + class glGraphicsContext : public GraphicsContext + { + private: + GLFWwindow* m_WindowHandle; + + public: + glGraphicsContext(GLFWwindow* windowHandle); + }; + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glRenderCommand.cpp b/Engine/src/Platform/GraphicsAPI/OpenGL/glRenderCommand.cpp new file mode 100644 index 0000000..286b63f --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glRenderCommand.cpp @@ -0,0 +1,35 @@ +#include "ltpch.h" +#include "glRenderCommand.h" + +#include +#include + +namespace Light { + + glRenderCommand::glRenderCommand(GLFWwindow* windowHandle) + : m_WindowHandle(windowHandle) + { + } + + void glRenderCommand::SwapBuffers() + { + glfwSwapBuffers(m_WindowHandle); + } + + void glRenderCommand::ClearBackBuffer() + { + glClear(GL_COLOR_BUFFER_BIT); + glClearColor(0.32f, 0.65f, 0.892f, 1.0f); + } + + void glRenderCommand::Draw(unsigned int count) + { + glDrawArrays(GL_TRIANGLES, 0, count); + } + + void glRenderCommand::DrawIndexed(unsigned int count) + { + glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, nullptr); + } + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glRenderCommand.h b/Engine/src/Platform/GraphicsAPI/OpenGL/glRenderCommand.h new file mode 100644 index 0000000..789992b --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glRenderCommand.h @@ -0,0 +1,24 @@ +#pragma once + +#include "Base.h" +#include "Graphics/RenderCommand.h" + + +namespace Light { + + class glRenderCommand : public RenderCommand + { + private: + GLFWwindow* m_WindowHandle; + public: + glRenderCommand(GLFWwindow* windowHandle); + + void SwapBuffers() override; + void ClearBackBuffer() override; + + void Draw(unsigned int count) override; + void DrawIndexed(unsigned int count) override; + }; + + +} \ No newline at end of file diff --git a/Engine/src/Engine/Platforms/OS/Windows/wWindow.cpp b/Engine/src/Platform/OS/Windows/wWindow.cpp similarity index 92% rename from Engine/src/Engine/Platforms/OS/Windows/wWindow.cpp rename to Engine/src/Platform/OS/Windows/wWindow.cpp index b5550f7..0d8a1db 100644 --- a/Engine/src/Engine/Platforms/OS/Windows/wWindow.cpp +++ b/Engine/src/Platform/OS/Windows/wWindow.cpp @@ -5,7 +5,7 @@ #include "Events/MouseEvents.h" #include "Events/WindowEvents.h" -#include +#include "Graphics/GraphicsContext.h" namespace Light { @@ -21,11 +21,10 @@ namespace Light { m_Handle = glfwCreateWindow(properties.width, properties.height, properties.title.c_str(), nullptr, nullptr); - glfwMakeContextCurrent(m_Handle); - LT_ENGINE_WARN(gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)); glfwSetWindowUserPointer(m_Handle, &m_EventCallback); - BindGlfwEvents(); + + m_GraphicsContext = std::unique_ptr(GraphicsContext::Create(GraphicsAPI::OpenGL, m_Handle)); } wWindow::~wWindow() @@ -33,12 +32,9 @@ namespace Light { glfwDestroyWindow(m_Handle); } - void wWindow::OnUpdate() + void wWindow::PollEvents() { - glClear(GL_COLOR_BUFFER_BIT); - glClearColor(0.23f, 0.47f, 0.84f, 1.0f); glfwPollEvents(); - glfwSwapBuffers(m_Handle); } void wWindow::OnEvent(const Event& event) diff --git a/Engine/src/Engine/Platforms/OS/Windows/wWindow.h b/Engine/src/Platform/OS/Windows/wWindow.h similarity index 94% rename from Engine/src/Engine/Platforms/OS/Windows/wWindow.h rename to Engine/src/Platform/OS/Windows/wWindow.h index 959ac8e..7a0fd26 100644 --- a/Engine/src/Engine/Platforms/OS/Windows/wWindow.h +++ b/Engine/src/Platform/OS/Windows/wWindow.h @@ -25,7 +25,7 @@ namespace Light { ~wWindow(); - virtual void OnUpdate() override; + virtual void PollEvents() override; virtual void OnEvent(const Event& event) override; virtual unsigned int GetWidth() override; diff --git a/Sandbox/premake5.lua b/Sandbox/premake5.lua index 93b7404..b8bbed4 100644 --- a/Sandbox/premake5.lua +++ b/Sandbox/premake5.lua @@ -25,6 +25,8 @@ project "Sandbox" -- Engine "%{wks.location}/Engine/src", "%{wks.location}/Engine/src/Engine", + "%{wks.location}/Engine/src/Platform/GraphicsAPI", + "%{wks.location}/Engine/src/Platform/OS", -- 3rd party (dependenciesdir .. "spdlog/include/"), diff --git a/Sandbox/src/SandboxLayer.h b/Sandbox/src/SandboxLayer.h new file mode 100644 index 0000000..5a805a0 --- /dev/null +++ b/Sandbox/src/SandboxLayer.h @@ -0,0 +1,28 @@ +#include + +class TestLayer : public Light::Layer +{ +public: + TestLayer(const std::string& name) : Light::Layer(name) {} + + // Mouse events + virtual bool OnMouseMoved(const Light::MouseMovedEvent& event) override { LT_ENGINE_TRACE("{}", event.GetInfoLog()); return false; } + virtual bool OnButtonPressed(const Light::ButtonPressedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + virtual bool OnButtonReleased(const Light::ButtonReleasedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + virtual bool OnWheelScrolled(const Light::WheelScrolledEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + + // Keyboard events + virtual bool OnKeyPressed(const Light::KeyPressedEvent& event) override + { + LT_ENGINE_TRACE(event.GetInfoLog()); + return true; + } + virtual bool OnKeyReleased(const Light::KeyReleasedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + + // Window events + virtual bool OnWindowClosed(const Light::WindowClosedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + virtual bool OnWindowResized(const Light::WindowResizedEvent& event) { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + virtual bool OnWindowMoved(const Light::WindowMovedEvent& event) { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + virtual bool OnWindowLostFocus(const Light::WindowLostFocusEvent& event) { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + virtual bool OnWindowGainFocus(const Light::WindowGainFocusEvent& event) { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } +};