From f3fe4c3713730dfe819fbf2905eed8ddb4de969d Mon Sep 17 00:00:00 2001 From: Light3039 Date: Thu, 27 May 2021 19:54:05 +0430 Subject: [PATCH] Assertion --- Engine/src/Engine/Base.h | 6 +- Engine/src/Engine/Core/Application.cpp | 2 + .../src/Engine/Graphics/GraphicsContext.cpp | 30 +++++--- Engine/src/Engine/Graphics/GraphicsContext.h | 9 ++- Engine/src/Engine/Graphics/RenderCommand.cpp | 3 +- Engine/src/Engine/Graphics/Shader.cpp | 15 ++++ Engine/src/Engine/Graphics/Shader.h | 2 +- Engine/src/Engine/Layer/LayerStack.h | 2 + .../Engine/UserInterface/UserInterface.cpp | 4 + .../GraphicsAPI/OpenGL/glGraphicsContext.cpp | 3 +- .../Platform/GraphicsAPI/OpenGL/glShader.cpp | 76 +++++++++++++++++++ .../Platform/GraphicsAPI/OpenGL/glShader.h | 21 +++++ Engine/src/Platform/OS/Windows/wWindow.cpp | 4 +- 13 files changed, 159 insertions(+), 18 deletions(-) create mode 100644 Engine/src/Platform/GraphicsAPI/OpenGL/glShader.cpp create mode 100644 Engine/src/Platform/GraphicsAPI/OpenGL/glShader.h diff --git a/Engine/src/Engine/Base.h b/Engine/src/Engine/Base.h index e06aa7a..aafa530 100644 --- a/Engine/src/Engine/Base.h +++ b/Engine/src/Engine/Base.h @@ -11,4 +11,8 @@ #endif -#define BIT(x) 1 << x \ No newline at end of file +#define BIT(x) 1 << x + +#define LT_ENGINE_ASSERT(x, ...) { if(!(x)) { LT_ENGINE_CRITICAL(__VA_ARGS__); __debugbreak(); } } +#define LT_CLIENT_ASSERT(x, ...) { if(!(x)) { LT_CLIENT_CRITICAL(__VA_ARGS__); __debugbreak(); } } + diff --git a/Engine/src/Engine/Core/Application.cpp b/Engine/src/Engine/Core/Application.cpp index d669b87..d62e040 100644 --- a/Engine/src/Engine/Core/Application.cpp +++ b/Engine/src/Engine/Core/Application.cpp @@ -26,6 +26,8 @@ namespace Light { void Application::GameLoop() { + LT_ENGINE_ASSERT(!m_LayerStack.IsEmpty(), "Application::GameLoop: Layerstack is empty"); + while (m_Window->IsOpen()) { // Events diff --git a/Engine/src/Engine/Graphics/GraphicsContext.cpp b/Engine/src/Engine/Graphics/GraphicsContext.cpp index 70006bd..41344eb 100644 --- a/Engine/src/Engine/Graphics/GraphicsContext.cpp +++ b/Engine/src/Engine/Graphics/GraphicsContext.cpp @@ -18,26 +18,36 @@ namespace Light { s_Context->m_UserInterface.reset(); } + // determine the default api + if (api == GraphicsAPI::Default) + { +#ifdef LIGHT_PLATFORM_WINDOWS + api = GraphicsAPI::OpenGL; // TODO: Change to DirectX +#endif + } + // create gfx context switch (api) { - case Light::GraphicsAPI::Default: case Light::GraphicsAPI::OpenGL: s_Context = new glGraphicsContext(windowHandle); break; + default: - break; - // TODO: ASSERT + LT_ENGINE_ASSERT(false, "GraphicsContext::Create: invalid/unsupported GraphicsAPI {}", api); + return nullptr; } // create gfx context dependent classes - if (s_Context) - { - s_Context->m_RenderCommand = std::unique_ptr(RenderCommand::Create(windowHandle)); - s_Context->m_UserInterface = std::unique_ptr(UserInterface::Create(windowHandle)); - // ...Renderer - // ...UserInterface... - } + s_Context->m_RenderCommand = std::unique_ptr(RenderCommand::Create(windowHandle)); + s_Context->m_UserInterface = std::unique_ptr(UserInterface::Create(windowHandle)); + // ...Renderer + // ...UserInterface... + + // sanity check + LT_ENGINE_ASSERT(s_Context->m_RenderCommand, "GraphicsContext::Create: RenderCommand creation failed"); + LT_ENGINE_ASSERT(s_Context->m_UserInterface, "GraphicsContext::Create: UserInterface creation failed"); + return s_Context; } diff --git a/Engine/src/Engine/Graphics/GraphicsContext.h b/Engine/src/Engine/Graphics/GraphicsContext.h index d5ebeda..506ccfc 100644 --- a/Engine/src/Engine/Graphics/GraphicsContext.h +++ b/Engine/src/Engine/Graphics/GraphicsContext.h @@ -2,7 +2,6 @@ #include "Base.h" - struct GLFWwindow; namespace Light { @@ -10,9 +9,13 @@ namespace Light { class RenderCommand; class UserInterface; - enum class GraphicsAPI + enum class GraphicsAPI { - Default, OpenGL // TODO: Add DirectX, Vulkan, Metal. + Default = 0, + OpenGL, + DirectX, + Vulkan, + Metal }; class GraphicsContext diff --git a/Engine/src/Engine/Graphics/RenderCommand.cpp b/Engine/src/Engine/Graphics/RenderCommand.cpp index 1aa0877..e75ac36 100644 --- a/Engine/src/Engine/Graphics/RenderCommand.cpp +++ b/Engine/src/Engine/Graphics/RenderCommand.cpp @@ -14,7 +14,8 @@ namespace Light { return new glRenderCommand(windowHandle); default: - return nullptr; // TODO: Add ASSERT + LT_ENGINE_ASSERT(false, "RenderCommand::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI()); + return nullptr; } } diff --git a/Engine/src/Engine/Graphics/Shader.cpp b/Engine/src/Engine/Graphics/Shader.cpp index 80022d1..e73a6d8 100644 --- a/Engine/src/Engine/Graphics/Shader.cpp +++ b/Engine/src/Engine/Graphics/Shader.cpp @@ -1,7 +1,22 @@ #include "ltpch.h" #include "Shader.h" +#include "OpenGL/glShader.h" + +#include "GraphicsContext.h" namespace Light { + Shader* Shader::Create(const std::string& vertexPath, const std::string& pixelPath) + { + switch (GraphicsContext::GetGraphicsAPI()) + { + case GraphicsAPI::OpenGL: + return new glShader(vertexPath, pixelPath); + + default: + LT_ENGINE_ASSERT(false, "Shader::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI()); + return nullptr; + } + } } \ No newline at end of file diff --git a/Engine/src/Engine/Graphics/Shader.h b/Engine/src/Engine/Graphics/Shader.h index eb99c6b..2ce7e5a 100644 --- a/Engine/src/Engine/Graphics/Shader.h +++ b/Engine/src/Engine/Graphics/Shader.h @@ -6,7 +6,7 @@ namespace Light { { private: public: - static Shader* Create(); + static Shader* Create(const std::string& vertexPath, const std::string& pixelPath); virtual ~Shader() = default; diff --git a/Engine/src/Engine/Layer/LayerStack.h b/Engine/src/Engine/Layer/LayerStack.h index 7dd3814..686d320 100644 --- a/Engine/src/Engine/Layer/LayerStack.h +++ b/Engine/src/Engine/Layer/LayerStack.h @@ -29,6 +29,8 @@ namespace Light { void OnEvent(const Event& event); + inline bool IsEmpty() { return m_Layers.empty(); } + std::vector::iterator begin() { return m_Layers.begin(); } std::vector::iterator end() { return m_Layers.end(); } diff --git a/Engine/src/Engine/UserInterface/UserInterface.cpp b/Engine/src/Engine/UserInterface/UserInterface.cpp index 7a95071..235f24e 100644 --- a/Engine/src/Engine/UserInterface/UserInterface.cpp +++ b/Engine/src/Engine/UserInterface/UserInterface.cpp @@ -18,6 +18,10 @@ namespace Light { { case GraphicsAPI::OpenGL: return new glUserInterface(windowHandle); + + default: + LT_ENGINE_ASSERT(false, "UserInterface::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI()); + return nullptr; } } diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glGraphicsContext.cpp b/Engine/src/Platform/GraphicsAPI/OpenGL/glGraphicsContext.cpp index b3e588b..bba95a0 100644 --- a/Engine/src/Platform/GraphicsAPI/OpenGL/glGraphicsContext.cpp +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glGraphicsContext.cpp @@ -16,7 +16,8 @@ namespace Light { m_GraphicsAPI = GraphicsAPI::OpenGL; glfwMakeContextCurrent(windowHandle); - gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); + LT_ENGINE_ASSERT(gladLoadGLLoader((GLADloadproc)glfwGetProcAddress), + "glGraphicsContext::glGraphicsContext: gladLoadGLLoader: failed to initialize opengl context"); LT_ENGINE_INFO("glGraphicsContext:"); LT_ENGINE_INFO(" Renderer: {}", glGetString(GL_RENDERER)); diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glShader.cpp b/Engine/src/Platform/GraphicsAPI/OpenGL/glShader.cpp new file mode 100644 index 0000000..5a7c0c3 --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glShader.cpp @@ -0,0 +1,76 @@ +#include "ltpch.h" +#include "glShader.h" + +#include + +namespace Light { + + glShader::glShader(const std::string& vertexPath, const std::string& pixelPath) + { + m_ShaderID = glCreateProgram(); + + unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER); + unsigned int pixelShader = glCreateShader(GL_FRAGMENT_SHADER); + + const char* vertexPath_cstr = vertexPath.c_str(); + const char* pixelPath_cstr = pixelPath.c_str(); + + glShaderSource(vertexShader, 1, &vertexPath_cstr, NULL); + glShaderSource(pixelShader, 1, &pixelPath_cstr, NULL); + + glCompileShader(vertexShader); + glCompileShader(pixelShader); + + // TEMP + int isCompiled = 0; + glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &isCompiled); + if (isCompiled == GL_FALSE) + { + GLint maxLength = 0; + glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &maxLength); + + std::vector errorLog(maxLength); + glGetShaderInfoLog(vertexShader, maxLength, &maxLength, &errorLog[0]); + + glDeleteShader(vertexShader); + } + + glGetShaderiv(pixelShader, GL_COMPILE_STATUS, &isCompiled); + if (isCompiled == GL_FALSE) + { + GLint maxLength = 0; + glGetShaderiv(pixelShader, GL_INFO_LOG_LENGTH, &maxLength); + + std::vector errorLog(maxLength); + glGetShaderInfoLog(pixelShader, maxLength, &maxLength, &errorLog[0]); + + glDeleteShader(pixelShader); + } + // TEMP + + glAttachShader(m_ShaderID, vertexShader); + glAttachShader(m_ShaderID, pixelShader); + glLinkProgram(m_ShaderID); + + glDeleteShader(vertexShader); + glDeleteShader(pixelShader); + + // TODO: validate program + } + + glShader::~glShader() + { + glDeleteProgram(m_ShaderID); + } + + void glShader::Bind() + { + glUseProgram(m_ShaderID); + } + + void glShader::UnBind() + { + glUseProgram(NULL); + } + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glShader.h b/Engine/src/Platform/GraphicsAPI/OpenGL/glShader.h new file mode 100644 index 0000000..eb9ac04 --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glShader.h @@ -0,0 +1,21 @@ +#pragma once + +#include "Base.h" +#include "Graphics/Shader.h" + +namespace Light { + + class glShader : public Shader + { + private: + unsigned int m_ShaderID; + + public: + glShader(const std::string& vertexPath, const std::string& pixelPath); + ~glShader(); + + void Bind() override; + void UnBind() override; + }; + +} \ No newline at end of file diff --git a/Engine/src/Platform/OS/Windows/wWindow.cpp b/Engine/src/Platform/OS/Windows/wWindow.cpp index 3a0f193..6daa9e7 100644 --- a/Engine/src/Platform/OS/Windows/wWindow.cpp +++ b/Engine/src/Platform/OS/Windows/wWindow.cpp @@ -20,14 +20,16 @@ namespace Light { wWindow::wWindow(const WindowProperties& properties, std::function callback) : m_Properties(properties), m_EventCallback(callback) { - if (!glfwInit()) __debugbreak(); + LT_ENGINE_ASSERT(glfwInit(), "wWindow::wWindow: glfwInit: failed to initialize glfw"); m_Handle = glfwCreateWindow(properties.width, properties.height, properties.title.c_str(), nullptr, nullptr); + LT_ENGINE_ASSERT(m_Handle, "wWindow::wWindow: glfwCreateWindow: failed to create glfw window"); glfwSetWindowUserPointer(m_Handle, &m_EventCallback); BindGlfwEvents(); m_GraphicsContext = std::unique_ptr(GraphicsContext::Create(GraphicsAPI::OpenGL, m_Handle)); + LT_ENGINE_ASSERT(m_GraphicsContext, "wWindow::wWindow: graphics context creation failed"); } wWindow::~wWindow()