FileManager

This commit is contained in:
Light3039 2021-05-31 19:09:27 +04:30
parent f511f6d771
commit 752258366d
8 changed files with 73 additions and 42 deletions

View file

@ -1,8 +0,0 @@
#version 450 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

View file

@ -1,8 +0,0 @@
#version 450 core
layout(location = 0) in vec2 a_Position;
void main()
{
gl_Position = vec4(a_Position, 0.0, 1.0);
}

View file

@ -14,26 +14,28 @@ namespace Light {
{
m_Context = this;
m_Shader = std::unique_ptr<Shader>(Shader::Create(
R"(
#version 450 core
m_Shader = std::unique_ptr<Shader>(Shader::Create("res/vertex.vertex", "res/fragment.fragment"));
layout(location = 0) in vec2 a_Position;
void main()
{
gl_Position = vec4(a_Position, 0.0, 1.0);
})",
R"(
#version 450 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
)"));
// m_Shader = std::unique_ptr<Shader>(Shader::Create(
// R"(
// #version 450 core
//
// layout(location = 0) in vec2 a_Position;
//
// void main()
// {
// gl_Position = vec4(a_Position, 0.0, 1.0);
// })",
// R"(
// #version 450 core
//
// out vec4 FragColor;
//
// void main()
// {
// FragColor = vec4(1.0, 0.0, 0.0, 1.0);
// }
// )"));
float vertices[] =
{

View file

@ -4,14 +4,20 @@
#include "GraphicsContext.h"
#include "Utility/FileManager.h"
namespace Light {
Shader* Shader::Create(const std::string& vertexPath, const std::string& pixelPath)
{
// load shader source
const std::string vertexSource = FileManager::ReadTXTFile(vertexPath);
const std::string pixelSource = FileManager::ReadTXTFile(pixelPath);
switch (GraphicsContext::GetGraphicsAPI())
{
case GraphicsAPI::OpenGL:
return new glShader(vertexPath, pixelPath);
return new glShader(vertexSource, pixelSource);
default:
LT_ENGINE_ASSERT(false, "Shader::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI());

View file

@ -0,0 +1,24 @@
#include "ltpch.h"
#include "FileManager.h"
namespace Light {
std::string FileManager::ReadTXTFile(const std::string& path)
{
// initialize
std::ifstream stream(path);
std::stringstream ss;
std::string line;
// check
LT_ENGINE_ASSERT(!path.empty(), "FileManager::ReadTXTFile: path is empty");
LT_ENGINE_ASSERT(stream.is_open(), "FileManager::ReadTXTFile: invalid path: {}", path);
// read
while (std::getline(stream, line))
ss << line << '\n';
return ss.str();
}
}

View file

@ -0,0 +1,14 @@
#pragma once
#include "Base.h"
namespace Light {
// TODO: optimize
class FileManager
{
public:
static std::string ReadTXTFile(const std::string& path);
};
}

View file

@ -5,18 +5,19 @@
namespace Light {
glShader::glShader(const std::string& vertexPath, const std::string& pixelPath)
glShader::glShader(const std::string& vertexSource, const std::string& fragmentSource)
{
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();
// & (address of) needs an lvalue
const char* lVertexSource = vertexSource.c_str();
const char* lFragmentSource = fragmentSource.c_str();
glShaderSource(vertexShader, 1, &vertexPath_cstr, NULL);
glShaderSource(pixelShader, 1, &pixelPath_cstr, NULL);
glShaderSource(vertexShader, 1, &lVertexSource, NULL);
glShaderSource(pixelShader, 1, &lFragmentSource, NULL);
glCompileShader(vertexShader);
glCompileShader(pixelShader);

View file

@ -11,7 +11,7 @@ namespace Light {
unsigned int m_ShaderID;
public:
glShader(const std::string& vertexPath, const std::string& pixelPath);
glShader(const std::string& vetexSource, const std::string& fragmentSource);
~glShader();
void Bind() override;