diff --git a/src/graphics.cpp b/src/graphics.cpp index 5d36263..baeecd4 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -1,3 +1,4 @@ +#include #include #include // glad must be loaded before GLFW @@ -10,18 +11,26 @@ using namespace std; /* class ShaderProgram Implementations */ string ShaderProgram::baseName() { + /* + BOOST_LOG_TRIVIAL(debug) << "this->base: " << this->base + << " Base::Empty: " << Base::Empty + << " Base::Triangle: " << Base::Triangle; + */ switch (this->base) { - case Empty: + case ShaderProgram::Base::Empty: return "Empty"; - case Triangle: + case ShaderProgram::Base::Triangle: return "Triangle"; default: - throw exception(); + // this should never occur, as the switch case should cover all variants. + throw std::runtime_error("Undefined base Program"); } } -ShaderProgram::ShaderProgram(Base base, char** vertexSource, - char** fragSource) { +ShaderProgram::ShaderProgram(Base base, char **vertexSource, + char **fragSource) { + this->base = base; + BOOST_LOG_TRIVIAL(info) << "With base program \"" << this->baseName() << "\""; // NOTE: char arrays and char pointers are not actually the same. // glShaderSource requires a char[] as it appears. We pass the // variable by reference to avoid duplication in memory. @@ -29,8 +38,7 @@ ShaderProgram::ShaderProgram(Base base, char** vertexSource, this->vertex = glCreateShader(GL_VERTEX_SHADER); this->frag = glCreateShader(GL_FRAGMENT_SHADER); BOOST_LOG_TRIVIAL(trace) << "loading vertex shader"; - glShaderSource(this->vertex, 1, vertexSource, - NULL); + glShaderSource(this->vertex, 1, vertexSource, NULL); BOOST_LOG_TRIVIAL(trace) << "compiling vertex shader"; glCompileShader(this->vertex); BOOST_LOG_TRIVIAL(trace) << "loading fragments shader"; @@ -39,13 +47,28 @@ ShaderProgram::ShaderProgram(Base base, char** vertexSource, glCompileShader(this->frag); } -int ShaderProgram::run() { return 255; } +/** runs for every frame + * + * will be called in `mainWindow`. + * + * FIXME: dont run the switch case every frame. + */ +int ShaderProgram::run() { + switch (this->base) { + case Empty: // same as default + default: + glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + break; + } + return 0; +} void framebuffer_size_callback(GLFWwindow *window, int width, int height) { glViewport(0, 0, width, height); } -int initGl() { +GLFWwindow *initGl() { /* graphics stuff */ glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); @@ -57,23 +80,37 @@ int initGl() { if (window == NULL) { printf("Failed to create GLFW window\n"); glfwTerminate(); - return EXIT_GL; + exit(EXIT_GL); } glfwMakeContextCurrent(window); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { printf("Failed to initialize GLAD\n"); - return EXIT_GL; + exit(EXIT_GL); } glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); - return 0; + return window; } -int mainWindow(ShaderProgram *shaderProgram) { +int mainWindow(ShaderProgram *shaderProgram, GLFWwindow *window) { - int result = shaderProgram->run(); + int result = 0; + + BOOST_LOG_TRIVIAL(trace) << "Entering window loop"; + while (!glfwWindowShouldClose(window)) { + processInput(window); + shaderProgram->run(); + glfwSwapBuffers(window); + glfwPollEvents(); + } + BOOST_LOG_TRIVIAL(trace) << "Left window loop"; glfwTerminate(); return result; } + +void processInput(GLFWwindow *window) { + if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) + glfwSetWindowShouldClose(window, true); +} diff --git a/src/graphics.hpp b/src/graphics.hpp index c6a888c..9dc1039 100644 --- a/src/graphics.hpp +++ b/src/graphics.hpp @@ -17,6 +17,7 @@ protected: unsigned int frag; Base base; }; -int mainWindow(ShaderProgram* shaderProgramm); -int initGl(); +int mainWindow(ShaderProgram* shaderProgramm, GLFWwindow* window); +GLFWwindow* initGl(); +void processInput(GLFWwindow *window); #endif diff --git a/src/main.cpp b/src/main.cpp index 5067c89..d2afa7e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,7 +54,7 @@ int main(int argc, char **argv) { } /* init graphics stuff */ - result = initGl(); + GLFWwindow* window = initGl(); if (result != 0) { return EXIT_GL; } @@ -93,8 +93,8 @@ int main(int argc, char **argv) { /* Run our main program */ BOOST_LOG_TRIVIAL(trace) << "Starting mainWindow"; - result = mainWindow(&shaderProgram); - /* if (result != 0) { return EXIT_GL;} */ + result = mainWindow(&shaderProgram, window); + BOOST_LOG_TRIVIAL(trace) << "mainWindow stopped, goodbye"; return result; }