diff --git a/res/shaders/Basic.shader b/res/shaders/Basic.shader new file mode 100644 index 0000000..09823c3 --- /dev/null +++ b/res/shaders/Basic.shader @@ -0,0 +1,14 @@ +#shader vertex +#version 330 core +layout(location = 0) in vec4 position; +void main() { + gl_Position = position; + +}; + +#shader fragment +#version 330 core +layout(location = 0) out vec4 color; +void main() { + color = vec4(0.2, 0.3, 0.8, 1.0); +}; diff --git a/src/sdl.cpp b/src/sdl.cpp index decc3ac..4ead8be 100644 --- a/src/sdl.cpp +++ b/src/sdl.cpp @@ -1,10 +1,17 @@ #include // Include GLEW before ? #include "sdl.hpp" #include +#include #include +#include +#include //#include #include #include +#include +struct ShaderProgramSource { + std::string VertexSource, FragmentSource; +}; SdlWindow::SdlWindow(const char* title, int width, int height) : m_window(nullptr), @@ -73,26 +80,8 @@ SdlWindow::SdlWindow(const char* title, int width, int height) glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0); - std::string vertexShader = - "#version 330 core\n" - "\n" - "layout(location = 0) in vec4 position;\n" - "\n" - "void main()\n" - "{\n" - " gl_Position = position;\n" - "}\n"; - - std::string fragmentShader = - "#version 330 core\n" - "\n" - "layout(location = 0) out vec4 color;\n" - "\n" - "void main()\n" - "{\n" - " color = vec4(1.0, 0.0, 0.0, 1.0);\n" - "}\n"; - unsigned int shader = createShader(vertexShader, fragmentShader); + ShaderProgramSource source = parseShader("res/shaders/Basic.shader"); + unsigned int shader = createShader(source.VertexSource, source.FragmentSource); glUseProgram(shader); } @@ -254,3 +243,38 @@ unsigned int SdlWindow::createShader(const std::string& vertexShader, const std: return program; } +SdlWindow::ShaderProgramSource SdlWindow::parseShader(const std::string& filepath) { + //can be changed to c standard which is a bit faster + std::ifstream stream(filepath); + if (!stream.is_open()) { + std::cerr << "parseShader ERROR: Could not open file " << filepath << std::endl; + // Return empty (or handle however you prefer) + return {"", ""}; + } + + enum class ShaderType { + NONE = -1, VERTEX = 0, FRAGMENT = 1 + }; + std::string line; + std::stringstream ss[2]; //vertex - fragment + ShaderType type = ShaderType::NONE; + + while (getline(stream, line)) { + if (line.find("#shader") != std::string::npos) { + if (line.find("vertex") != std::string::npos) { + type = ShaderType::VERTEX; + + } + else if (line.find("fragment") != std::string::npos) { + type = ShaderType::FRAGMENT; + + } + } + else { + ss[(int)type] << line << '\n'; + } + } + return {ss[0].str(), ss[1].str() }; + +} + diff --git a/src/sdl.hpp b/src/sdl.hpp index 068305d..8e59087 100644 --- a/src/sdl.hpp +++ b/src/sdl.hpp @@ -10,6 +10,9 @@ #endif //#include +#include +#include +#include // Forward declaration of classes and structs if needed // class SomethingElse; @@ -26,6 +29,14 @@ public: void run(); unsigned int compileShader(unsigned int type, const std::string& source); unsigned int createShader(const std::string& vetexShader, const std::string& fragmentShader); + // for spliting our shaders + //struct ShaderProgramSource { + // std::string VetexSource, FragmentSource; + struct ShaderProgramSource { + std::string VertexSource; + std::string FragmentSource; + }; + ShaderProgramSource parseShader(const std::string& filepath); void setFullscreen(bool fullscreen);