diff --git a/res/shaders/Basic.shader b/res/shaders/Basic.shader index 09823c3..95189d6 100644 --- a/res/shaders/Basic.shader +++ b/res/shaders/Basic.shader @@ -9,6 +9,9 @@ void main() { #shader fragment #version 330 core layout(location = 0) out vec4 color; + +uniform vec4 u_Color; + void main() { - color = vec4(0.2, 0.3, 0.8, 1.0); + color = u_Color; }; diff --git a/src/sdl.cpp b/src/sdl.cpp index 5d5593a..bf8d963 100644 --- a/src/sdl.cpp +++ b/src/sdl.cpp @@ -1,6 +1,7 @@ #include // Include GLEW before ? #include "sdl.hpp" #include "colormod.h" +#include #include #include #include @@ -23,7 +24,6 @@ #define DEBUG_BREAK() raise(SIGTRAP) #endif - // ASSERT macro that shows file, line, and the failed expression #define ASSERT(x) \ do { \ @@ -53,7 +53,10 @@ SdlWindow::SdlWindow(const char* title, int width, int height) m_height(height), m_glContext(nullptr), m_windowedWidth(width), - m_windowedHeight(height) + m_windowedHeight(height), + r(0.0f), + location(), + increment(0.05f) { // 1. Set attributes SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); @@ -91,7 +94,7 @@ SdlWindow::SdlWindow(const char* title, int width, int height) #endif // 5. Set vsync (optional) - SDL_GL_SetSwapInterval(1); + SDL_GL_SetSwapInterval(1); //Vsync // 6. Mark as running m_isRunning = true; @@ -125,6 +128,11 @@ SdlWindow::SdlWindow(const char* title, int width, int height) ShaderProgramSource source = parseShader("res/shaders/Basic.shader"); unsigned int shader = createShader(source.VertexSource, source.FragmentSource); GLCall(glUseProgram(shader)); + + GLCall(int location = glGetUniformLocation(shader, "u_Color")); + ASSERT(location != -1); // -1 is an error + GLCall(glUniform4f(location, 0.8f, 0.3f, 0.8f, 1.0f)); + } @@ -207,16 +215,24 @@ SDL_Event event; void SdlWindow::update() { // Update game/application logic here + if (r > 1.0f) { + increment = -0.05f; + } else if (r < 0.0f) { + increment = 0.05f; + } + r += increment; } void SdlWindow::render() { // Use GL calls instead of SDL’s renderer - GLCall(glClearColor(0.0f, 0.0f, 0.0f, 1.0f)); + GLCall(glClearColor(0.0f, 0.0f, 0.0f, 1.0f)); //background GLCall(glClear(GL_COLOR_BUFFER_BIT)); // TODO: Draw with OpenGL here (shaders, triangles, etc.) + GLCall(glUniform4f( location, r, 0.3f, 0.8f, 1.0f)); //glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr); GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr)); //macro assert for debugging + // Swap buffers SDL_GL_SwapWindow(m_window); } diff --git a/src/sdl.hpp b/src/sdl.hpp index 71ba56f..fc049f2 100644 --- a/src/sdl.hpp +++ b/src/sdl.hpp @@ -53,7 +53,10 @@ private: int m_height; int m_windowedWidth; // stored width before fullscreen int m_windowedHeight; // stored height before fullscreen - SDL_GLContext m_glContext; + float r; + int location; + SDL_GLContext m_glContext; + float increment; // temp shader stuff std::string vetexShader; std::string fragmentShader;