From 001d795b7315887f299fcada958bedc4e57442c4 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Wed, 18 Oct 2023 19:48:48 +0200 Subject: [PATCH] modules are good now --- build.sh | 1 + run.sh | 1 + src/graphics.cpp | 23 ++++++++++------- src/graphics.hpp | 4 +-- src/main.cpp | 64 +++++++++++++++++++++++++++++++++--------------- src/main.hpp | 21 ++++++++++++++++ 6 files changed, 82 insertions(+), 32 deletions(-) create mode 100644 src/main.hpp diff --git a/build.sh b/build.sh index b06c961..7ab3144 100755 --- a/build.sh +++ b/build.sh @@ -5,4 +5,5 @@ cmake $flags . cmake --build . echo -e "running the program, passing the given args" echo -e "========================================" +# shellcheck disable=SC2046 # Intended splitting of OPTIONS ./run.sh $@ diff --git a/run.sh b/run.sh index a3aeb31..ad36daa 100755 --- a/run.sh +++ b/run.sh @@ -1,3 +1,4 @@ #!/bin/bash set -e +# shellcheck disable=SC2046 # Intended splitting of OPTIONS ./bin/loader/loader $@ diff --git a/src/graphics.cpp b/src/graphics.cpp index 2161ac7..fe50594 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -1,28 +1,33 @@ +#include +// glad must be loaded before GLFW #include "graphics.hpp" +#include "main.hpp" #include -#include using namespace std; +/* class ShaderProgram Implementations */ string ShaderProgram::baseName() { switch (this->base) { - case Empty: - return "Empty"; - case Triangle: - return "Triangle"; + case Empty: + return "Empty"; + case Triangle: + return "Triangle"; } } -ShaderProgram::ShaderProgram(Base base, string vertexFile, string fragFile) { +ShaderProgram::ShaderProgram(Base base, string vertexSource, + string fragSource) {} -} +int ShaderProgram::run() { return 255; } void framebuffer_size_callback(GLFWwindow *window, int width, int height) { glViewport(0, 0, width, height); } -int mainWindow(ShaderProgram shaderProgram) { +/* extended main function */ +int mainWindow(ShaderProgram* shaderProgram) { /* graphics stuff */ glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); @@ -45,7 +50,7 @@ int mainWindow(ShaderProgram shaderProgram) { glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); - int result = shaderProgram.run(); + int result = shaderProgram->run(); glfwTerminate(); return result; diff --git a/src/graphics.hpp b/src/graphics.hpp index f7d826f..253dee3 100644 --- a/src/graphics.hpp +++ b/src/graphics.hpp @@ -1,7 +1,5 @@ #ifndef GRAPHICS_HPP #define GRAPHICS_HPP -#include -// glad must be loaded before GLFW #include #include @@ -11,7 +9,7 @@ class ShaderProgram { public: enum Base { Empty, Triangle }; std::string baseName(); - ShaderProgram(Base base, std::string vertexFile, std::string fragFile); + ShaderProgram(Base base, std::string vertexSource, std::string fragSource); int run(); protected: diff --git a/src/main.cpp b/src/main.cpp index 7a25611..2ad608c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,18 +1,8 @@ -#include -#include -#include -#include +#include #include -#include "graphics.hpp" - -namespace po = boost::program_options; -using namespace std; - -int main(int argc, char **argv); - -void help(char *prog, po::options_description desc); +#include "main.hpp" int main(int argc, char **argv) { @@ -45,19 +35,45 @@ int main(int argc, char **argv) { } catch (po::error &e) { cout << e.what() << endl << endl; help(argv[0], desc); - return 1; - } - - if (vm.count("verbose")) { - printf("verbose!"); + return EXIT_USAGE; } /* setup the shader program */ - ShaderProgram* shaderProgram = new ShaderProgram( - ShaderProgram::Base::Empty, vm["vert"].as(), vm["frag"].as()); + if (vm.count("verbose")) { + // TODO: make verbose flag somehow global? + printf("Reading shader files\n"); + } + ifstream vertexFileStream(vm["vert"].as()); + ifstream fragFileStream(vm["frag"].as()); + std::stringstream vertexSource; + std::stringstream fragSource; + vertexSource << vertexFileStream.rdbuf(); + fragSource << fragFileStream.rdbuf(); + + if (vertexFileStream.fail()) { + cout << "Error while reading the vertex shader file." << endl; + return EXIT_IO; + } + if (fragFileStream.fail()) { + cout << "Error while reading the frag shader file." << endl; + return EXIT_IO; + } + vertexFileStream.close(); + fragFileStream.close(); + + if (vm.count("verbose")) { + printf("Creating ShaderProgram\n"); + } + + // TODO: determinde ShaderProgram::Base with a CLI arg + ShaderProgram shaderProgram(ShaderProgram::Base::Empty, vertexSource.str(), + fragSource.str()); /* Run our main program */ - int result = mainWindow(shaderProgram); + if (vm.count("verbose")) { + printf("Starting mainWindow\n"); + } + int result = mainWindow(&shaderProgram); return result; } @@ -68,5 +84,13 @@ void help(char *prog, po::options_description desc) { << " [-vh] [--vert] shaders/vertex.glsl [--frag] shaders/fragment.glsl" << endl << endl + << "return values:" << endl + << "0\tSuccess" + << "1\tGeneric Failure" + << "2\tBad arguments" + << "3\tI/O Error" << endl + << "3\tOpenGL Error" << endl + << endl << desc << endl; + // NOTE: constants for the error codes are defined in main.hpp } diff --git a/src/main.hpp b/src/main.hpp new file mode 100644 index 0000000..7fd719f --- /dev/null +++ b/src/main.hpp @@ -0,0 +1,21 @@ +#ifndef MAIN_HPP +#define MAIN_HPP +#include +#include +#include +#include + +#include "graphics.hpp" + +// EXIT_FAILURE and EXIT_SUCCESS are defined in cstdlib +const int EXIT_USAGE = 2; +const int EXIT_IO = 3; +const int EXIT_GL = 4; + +namespace po = boost::program_options; +using namespace std; + +int main(int argc, char **argv); + +void help(char *prog, po::options_description desc); +#endif