From 76e022317f887c4e3cdb02bd7df71770377ac952 Mon Sep 17 00:00:00 2001 From: David Allemang Date: Sun, 8 Sep 2019 23:26:44 -0400 Subject: [PATCH] break apart into multiple files --- cosets/src/main.cpp | 107 +------------------------------------ cosets/src/util/shader.hpp | 51 ++++++++++++++++++ cosets/src/util/window.hpp | 60 +++++++++++++++++++++ 3 files changed, 113 insertions(+), 105 deletions(-) create mode 100644 cosets/src/util/shader.hpp create mode 100644 cosets/src/util/window.hpp diff --git a/cosets/src/main.cpp b/cosets/src/main.cpp index 12b3337..eedb1fa 100644 --- a/cosets/src/main.cpp +++ b/cosets/src/main.cpp @@ -1,9 +1,5 @@ -#include -#include - -#include -#include -#include +#include "util/window.hpp" +#include "util/shader.hpp" #include #include @@ -12,105 +8,6 @@ using namespace std; -GLint build_shader(GLenum type, const string &name, const string &src) { - const char *c_str = src.c_str(); - - auto s = glCreateShader(type); - glShaderSource(s, 1, &c_str, nullptr); - glCompileShader(s); - - GLint stat; - glGetShaderiv(s, GL_COMPILE_STATUS, &stat); - if (!stat) { - GLint log_len; - glGetShaderiv(s, GL_INFO_LOG_LENGTH, &log_len); - char log[log_len]; - glGetShaderInfoLog(s, log_len, nullptr, log); - fprintf(stderr, "SHADER LOG (%s):\n%s", name.c_str(), log); - - glDeleteShader(s); - return 0; - } - - return s; -} - -GLint build_program(const string &name, GLint vs, GLint fs) { - GLint p = glCreateProgram(); - glAttachShader(p, vs); - glAttachShader(p, fs); - glLinkProgram(p); - - GLint stat; - glGetProgramiv(p, GL_LINK_STATUS, &stat); - if (!stat) { - GLint log_len; - glGetShaderiv(p, GL_INFO_LOG_LENGTH, &log_len); - char log[log_len]; - glGetProgramInfoLog(p, log_len, nullptr, log); - fprintf(stderr, "SHADER LOG (%s):\n%s", name.c_str(), log); - - glDeleteProgram(p); - return 0; - } - - return p; -} - -class Window { -protected: - GLFWwindow *_window = nullptr; - -public: - virtual void init() {} - - virtual void update() {} - - virtual void render() {} - - virtual void deinit() {} - - void getBounds(int &width, int &height) { - glfwGetFramebufferSize(_window, &width, &height); - } - - void swapbuffers() { - glfwSwapBuffers(_window); - } - - void run() { - int _gl_major = 4, _gl_minor = 0; - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, _gl_major); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, _gl_minor); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - - - _window = glfwCreateWindow(640, 480, "GLFW App", nullptr, nullptr); - if (!_window) { - fprintf(stderr, "Failed to create window;"); - glfwTerminate(); - exit(EXIT_FAILURE); - } - - glfwMakeContextCurrent(_window); - gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); - glfwSwapInterval(0); - - init(); - - while (!glfwWindowShouldClose(_window)) { - update(); - render(); - - glfwPollEvents(); - } - - deinit(); - - glfwDestroyWindow(_window); - } -}; - class CosetsWindow : public Window { GLint program; GLuint tri; diff --git a/cosets/src/util/shader.hpp b/cosets/src/util/shader.hpp new file mode 100644 index 0000000..7db357c --- /dev/null +++ b/cosets/src/util/shader.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include +#include +#include +#include + +GLint build_shader(GLenum type, const std::string &name, const std::string &src) { + const char *c_str = src.c_str(); + + auto s = glCreateShader(type); + glShaderSource(s, 1, &c_str, nullptr); + glCompileShader(s); + + GLint stat; + glGetShaderiv(s, GL_COMPILE_STATUS, &stat); + if (!stat) { + GLint log_len; + glGetShaderiv(s, GL_INFO_LOG_LENGTH, &log_len); + char log[log_len]; + glGetShaderInfoLog(s, log_len, nullptr, log); + fprintf(stderr, "SHADER LOG (%s):\n%s", name.c_str(), log); + + glDeleteShader(s); + return 0; + } + + return s; +} + +GLint build_program(const std::string &name, GLint vs, GLint fs) { + GLint p = glCreateProgram(); + glAttachShader(p, vs); + glAttachShader(p, fs); + glLinkProgram(p); + + GLint stat; + glGetProgramiv(p, GL_LINK_STATUS, &stat); + if (!stat) { + GLint log_len; + glGetShaderiv(p, GL_INFO_LOG_LENGTH, &log_len); + char log[log_len]; + glGetProgramInfoLog(p, log_len, nullptr, log); + fprintf(stderr, "SHADER LOG (%s):\n%s", name.c_str(), log); + + glDeleteProgram(p); + return 0; + } + + return p; +} diff --git a/cosets/src/util/window.hpp b/cosets/src/util/window.hpp new file mode 100644 index 0000000..a90adb1 --- /dev/null +++ b/cosets/src/util/window.hpp @@ -0,0 +1,60 @@ +#pragma once + +#include +#include +#include +#include + +class Window { +protected: + GLFWwindow *_window = nullptr; + +public: + virtual void init() {} + + virtual void update() {} + + virtual void render() {} + + virtual void deinit() {} + + void getBounds(int &width, int &height) { + glfwGetFramebufferSize(_window, &width, &height); + } + + void swapbuffers() { + glfwSwapBuffers(_window); + } + + void run() { + int _gl_major = 4, _gl_minor = 0; + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, _gl_major); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, _gl_minor); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + + + _window = glfwCreateWindow(640, 480, "GLFW App", nullptr, nullptr); + if (!_window) { + fprintf(stderr, "Failed to create window;"); + glfwTerminate(); + exit(EXIT_FAILURE); + } + + glfwMakeContextCurrent(_window); + gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); + glfwSwapInterval(0); + + init(); + + while (!glfwWindowShouldClose(_window)) { + update(); + render(); + + glfwPollEvents(); + } + + deinit(); + + glfwDestroyWindow(_window); + } +};