Assertion
This commit is contained in:
		
							parent
							
								
									ab92d8abc2
								
							
						
					
					
						commit
						f3fe4c3713
					
				
					 13 changed files with 159 additions and 18 deletions
				
			
		| 
						 | 
				
			
			@ -11,4 +11,8 @@
 | 
			
		|||
#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(); } }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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>(RenderCommand::Create(windowHandle));
 | 
			
		||||
			s_Context->m_UserInterface = std::unique_ptr<UserInterface>(UserInterface::Create(windowHandle));
 | 
			
		||||
			// ...Renderer
 | 
			
		||||
			// ...UserInterface...
 | 
			
		||||
		}
 | 
			
		||||
		s_Context->m_RenderCommand = std::unique_ptr<RenderCommand>(RenderCommand::Create(windowHandle));
 | 
			
		||||
		s_Context->m_UserInterface = std::unique_ptr<UserInterface>(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;
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -29,6 +29,8 @@ namespace Light {
 | 
			
		|||
 | 
			
		||||
		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 end() { return m_Layers.end(); }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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));
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										76
									
								
								Engine/src/Platform/GraphicsAPI/OpenGL/glShader.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								Engine/src/Platform/GraphicsAPI/OpenGL/glShader.cpp
									
										
									
									
									
										Normal 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);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								Engine/src/Platform/GraphicsAPI/OpenGL/glShader.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								Engine/src/Platform/GraphicsAPI/OpenGL/glShader.h
									
										
									
									
									
										Normal 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;
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -20,14 +20,16 @@ namespace Light {
 | 
			
		|||
	wWindow::wWindow(const WindowProperties& properties, std::function<void(Event&)> 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>(GraphicsContext::Create(GraphicsAPI::OpenGL, m_Handle));
 | 
			
		||||
		LT_ENGINE_ASSERT(m_GraphicsContext, "wWindow::wWindow: graphics context creation failed");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	wWindow::~wWindow()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue