GraphicsContext & RenderCommands

This commit is contained in:
Light3039 2021-05-26 18:39:40 +04:30
parent cb44bd35e7
commit 8841216238
18 changed files with 271 additions and 21 deletions

View file

@ -26,7 +26,10 @@ project "Engine"
includedirs includedirs
{ {
-- Engine -- Engine
"%{prj.location}/src/",
"%{prj.location}/src/Engine/", "%{prj.location}/src/Engine/",
"%{prj.location}/src/Platform/GraphicsAPI",
"%{prj.location}/src/Platform/OS",
-- 3rd party -- 3rd party
(dependenciesdir .. "spdlog/include/"), (dependenciesdir .. "spdlog/include/"),

View file

@ -17,9 +17,6 @@ namespace Light {
m_Window = std::unique_ptr<Window>(Window::Create({ "Title", 800u, 600u, false }, std::bind(&Application::OnEvent, this , std::placeholders::_1))); m_Window = std::unique_ptr<Window>(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"); LT_ENGINE_INFO("Initialized Logger");
} }
@ -31,7 +28,10 @@ namespace Light {
{ {
while (m_Window->IsOpen()) while (m_Window->IsOpen())
{ {
m_Window->OnUpdate(); m_Window->PollEvents();
m_Window->GetGfxContext()->GetRenderCommand()->SwapBuffers();
m_Window->GetGfxContext()->GetRenderCommand()->ClearBackBuffer();
} }
} }

View file

@ -4,6 +4,8 @@
#include "Events/Event.h" #include "Events/Event.h"
#include "Graphics/GraphicsContext.h"
#include <string> #include <string>
namespace Light { namespace Light {
@ -18,13 +20,16 @@ namespace Light {
class Window class Window
{ {
protected: protected:
std::unique_ptr<GraphicsContext> m_GraphicsContext;
bool b_Open; bool b_Open;
public: public:
virtual ~Window() = default; virtual ~Window() = default;
inline GraphicsContext* GetGfxContext() { return m_GraphicsContext.get(); }
inline bool IsOpen() const { return b_Open; } inline bool IsOpen() const { return b_Open; }
virtual void OnUpdate() = 0; virtual void PollEvents() = 0;
virtual void OnEvent(const Event& event) = 0; virtual void OnEvent(const Event& event) = 0;
virtual unsigned int GetHeight() = 0; virtual unsigned int GetHeight() = 0;

View file

@ -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>(RenderCommand::Create(windowHandle));
// ...Renderer
// ...UserInterface...
return s_Context;
default:
return nullptr; // TODO: Add ASSERT
}
}
}

View file

@ -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<RenderCommand> 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(); }
};
}

View file

@ -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
}
}
}

View file

@ -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;
};
}

View file

@ -0,0 +1,11 @@
#include "ltpch.h"
namespace Light {
class UserInterface
{
private:
public:
};
}

View file

@ -1,11 +1,11 @@
#pragma once #pragma once
#include "Engine/Events/Event.h" #include "Events/Event.h"
#include "Engine/Events/KeyboardEvents.h" #include "Events/KeyboardEvents.h"
#include "Engine/Events/MouseEvents.h" #include "Events/MouseEvents.h"
#include "Engine/Core/Application.h" #include "Core/Application.h"
#include "Engine/Core/Logger.h" #include "Core/Logger.h"
#include "Engine/Base.h" #include "Base.h"
#include "Engine/EntryPoint.h" #include "EntryPoint.h"

View file

@ -0,0 +1,22 @@
#include "ltpch.h"
#include "glGraphicsContext.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
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));
}
}

View file

@ -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);
};
}

View file

@ -0,0 +1,35 @@
#include "ltpch.h"
#include "glRenderCommand.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
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);
}
}

View file

@ -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;
};
}

View file

@ -5,7 +5,7 @@
#include "Events/MouseEvents.h" #include "Events/MouseEvents.h"
#include "Events/WindowEvents.h" #include "Events/WindowEvents.h"
#include <glad/glad.h> #include "Graphics/GraphicsContext.h"
namespace Light { namespace Light {
@ -21,11 +21,10 @@ namespace Light {
m_Handle = glfwCreateWindow(properties.width, properties.height, properties.title.c_str(), nullptr, nullptr); 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); glfwSetWindowUserPointer(m_Handle, &m_EventCallback);
BindGlfwEvents(); BindGlfwEvents();
m_GraphicsContext = std::unique_ptr<GraphicsContext>(GraphicsContext::Create(GraphicsAPI::OpenGL, m_Handle));
} }
wWindow::~wWindow() wWindow::~wWindow()
@ -33,12 +32,9 @@ namespace Light {
glfwDestroyWindow(m_Handle); glfwDestroyWindow(m_Handle);
} }
void wWindow::OnUpdate() void wWindow::PollEvents()
{ {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.23f, 0.47f, 0.84f, 1.0f);
glfwPollEvents(); glfwPollEvents();
glfwSwapBuffers(m_Handle);
} }
void wWindow::OnEvent(const Event& event) void wWindow::OnEvent(const Event& event)

View file

@ -25,7 +25,7 @@ namespace Light {
~wWindow(); ~wWindow();
virtual void OnUpdate() override; virtual void PollEvents() override;
virtual void OnEvent(const Event& event) override; virtual void OnEvent(const Event& event) override;
virtual unsigned int GetWidth() override; virtual unsigned int GetWidth() override;

View file

@ -25,6 +25,8 @@ project "Sandbox"
-- Engine -- Engine
"%{wks.location}/Engine/src", "%{wks.location}/Engine/src",
"%{wks.location}/Engine/src/Engine", "%{wks.location}/Engine/src/Engine",
"%{wks.location}/Engine/src/Platform/GraphicsAPI",
"%{wks.location}/Engine/src/Platform/OS",
-- 3rd party -- 3rd party
(dependenciesdir .. "spdlog/include/"), (dependenciesdir .. "spdlog/include/"),

View file

@ -0,0 +1,28 @@
#include <LightEngine.h>
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; }
};