Assertion

This commit is contained in:
Light3039 2021-05-27 19:54:05 +04:30
parent ab92d8abc2
commit f3fe4c3713
13 changed files with 159 additions and 18 deletions

View file

@ -11,4 +11,8 @@
#endif #endif
#define BIT(x) 1 << x #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(); } }

View file

@ -26,6 +26,8 @@ namespace Light {
void Application::GameLoop() void Application::GameLoop()
{ {
LT_ENGINE_ASSERT(!m_LayerStack.IsEmpty(), "Application::GameLoop: Layerstack is empty");
while (m_Window->IsOpen()) while (m_Window->IsOpen())
{ {
// Events // Events

View file

@ -18,26 +18,36 @@ namespace Light {
s_Context->m_UserInterface.reset(); 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 // create gfx context
switch (api) switch (api)
{ {
case Light::GraphicsAPI::Default:
case Light::GraphicsAPI::OpenGL: case Light::GraphicsAPI::OpenGL:
s_Context = new glGraphicsContext(windowHandle); s_Context = new glGraphicsContext(windowHandle);
break; break;
default: default:
break; LT_ENGINE_ASSERT(false, "GraphicsContext::Create: invalid/unsupported GraphicsAPI {}", api);
// TODO: ASSERT return nullptr;
} }
// create gfx context dependent classes // create gfx context dependent classes
if (s_Context) s_Context->m_RenderCommand = std::unique_ptr<RenderCommand>(RenderCommand::Create(windowHandle));
{ s_Context->m_UserInterface = std::unique_ptr<UserInterface>(UserInterface::Create(windowHandle));
s_Context->m_RenderCommand = std::unique_ptr<RenderCommand>(RenderCommand::Create(windowHandle)); // ...Renderer
s_Context->m_UserInterface = std::unique_ptr<UserInterface>(UserInterface::Create(windowHandle)); // ...UserInterface...
// ...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; return s_Context;
} }

View file

@ -2,7 +2,6 @@
#include "Base.h" #include "Base.h"
struct GLFWwindow; struct GLFWwindow;
namespace Light { namespace Light {
@ -10,9 +9,13 @@ namespace Light {
class RenderCommand; class RenderCommand;
class UserInterface; class UserInterface;
enum class GraphicsAPI enum class GraphicsAPI
{ {
Default, OpenGL // TODO: Add DirectX, Vulkan, Metal. Default = 0,
OpenGL,
DirectX,
Vulkan,
Metal
}; };
class GraphicsContext class GraphicsContext

View file

@ -14,7 +14,8 @@ namespace Light {
return new glRenderCommand(windowHandle); return new glRenderCommand(windowHandle);
default: default:
return nullptr; // TODO: Add ASSERT LT_ENGINE_ASSERT(false, "RenderCommand::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI());
return nullptr;
} }
} }

View file

@ -1,7 +1,22 @@
#include "ltpch.h" #include "ltpch.h"
#include "Shader.h" #include "Shader.h"
#include "OpenGL/glShader.h"
#include "GraphicsContext.h"
namespace Light { 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;
}
}
} }

View file

@ -6,7 +6,7 @@ namespace Light {
{ {
private: private:
public: public:
static Shader* Create(); static Shader* Create(const std::string& vertexPath, const std::string& pixelPath);
virtual ~Shader() = default; virtual ~Shader() = default;

View file

@ -29,6 +29,8 @@ namespace Light {
void OnEvent(const Event& event); void OnEvent(const Event& event);
inline bool IsEmpty() { return m_Layers.empty(); }
std::vector<Layer*>::iterator begin() { return m_Layers.begin(); } std::vector<Layer*>::iterator begin() { return m_Layers.begin(); }
std::vector<Layer*>::iterator end() { return m_Layers.end(); } std::vector<Layer*>::iterator end() { return m_Layers.end(); }

View file

@ -18,6 +18,10 @@ namespace Light {
{ {
case GraphicsAPI::OpenGL: case GraphicsAPI::OpenGL:
return new glUserInterface(windowHandle); return new glUserInterface(windowHandle);
default:
LT_ENGINE_ASSERT(false, "UserInterface::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI());
return nullptr;
} }
} }

View file

@ -16,7 +16,8 @@ namespace Light {
m_GraphicsAPI = GraphicsAPI::OpenGL; m_GraphicsAPI = GraphicsAPI::OpenGL;
glfwMakeContextCurrent(windowHandle); 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("glGraphicsContext:");
LT_ENGINE_INFO(" Renderer: {}", glGetString(GL_RENDERER)); LT_ENGINE_INFO(" Renderer: {}", glGetString(GL_RENDERER));

View file

@ -0,0 +1,76 @@
#include "ltpch.h"
#include "glShader.h"
#include <GLAD/glad.h>
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<char> 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<char> 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);
}
}

View file

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

View file

@ -20,14 +20,16 @@ namespace Light {
wWindow::wWindow(const WindowProperties& properties, std::function<void(Event&)> callback) wWindow::wWindow(const WindowProperties& properties, std::function<void(Event&)> callback)
: m_Properties(properties), m_EventCallback(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); 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); glfwSetWindowUserPointer(m_Handle, &m_EventCallback);
BindGlfwEvents(); BindGlfwEvents();
m_GraphicsContext = std::unique_ptr<GraphicsContext>(GraphicsContext::Create(GraphicsAPI::OpenGL, m_Handle)); m_GraphicsContext = std::unique_ptr<GraphicsContext>(GraphicsContext::Create(GraphicsAPI::OpenGL, m_Handle));
LT_ENGINE_ASSERT(m_GraphicsContext, "wWindow::wWindow: graphics context creation failed");
} }
wWindow::~wWindow() wWindow::~wWindow()