From c3c1c80c6d810ff07425c64028f7d9133eb9ade2 Mon Sep 17 00:00:00 2001 From: allem Date: Mon, 10 Dec 2018 23:31:36 -0500 Subject: [PATCH] refactor framework lib --- CMakeLists.txt | 6 +- framework/CMakeLists.txt | 6 +- framework/include/framework.h | 74 +++++++++++++++ framework/include/hello.h | 10 --- framework/src/framework.cpp | 165 ++++++++++++++++++++++++++++++++++ framework/src/hello.cpp | 24 ----- glapp/CMakeLists.txt | 1 + glapp/src/main.cpp | 131 +++++++++++---------------- 8 files changed, 298 insertions(+), 119 deletions(-) create mode 100644 framework/include/framework.h delete mode 100644 framework/include/hello.h create mode 100644 framework/src/framework.cpp delete mode 100644 framework/src/hello.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 11e5d8a..31af640 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,9 @@ project(gl_template) set(CMAKE_CXX_STANDARD 17) +add_library(glad vendor/glad/src/glad.c) +target_include_directories(glad PUBLIC vendor/glad/include) + option(GLFW_BUILD_DOCS OFF) option(GLFW_BUILD_EXAMPLES OFF) option(GLFW_BUILD_TESTS OFF) @@ -11,8 +14,5 @@ add_subdirectory(vendor/glfw) option(GLM_TEST_ENABLE OFF) add_subdirectory(vendor/glm) -add_library(glad vendor/glad/src/glad.c) -target_include_directories(glad PUBLIC vendor/glad/include) - add_subdirectory(framework) add_subdirectory(glapp) diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index 93a07fd..311c489 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -1,9 +1,10 @@ project(framework) add_library(framework - src/hello.cpp) + src/framework.cpp) target_link_libraries(framework + glad glfw) target_include_directories(framework @@ -13,4 +14,5 @@ target_include_directories(framework PRIVATE src) -export(TARGETS framework glfw FILE FrameworkConfig.cmake) \ No newline at end of file +export(TARGETS framework glfw glad + FILE FrameworkConfig.cmake) \ No newline at end of file diff --git a/framework/include/framework.h b/framework/include/framework.h new file mode 100644 index 0000000..ca2a265 --- /dev/null +++ b/framework/include/framework.h @@ -0,0 +1,74 @@ +#ifndef GL_TEMPLATE_FRAMEWORK_H +#define GL_TEMPLATE_FRAMEWORK_H + +#include +#include + +#include + +class App { +private: + GLFWwindow *_window = nullptr; + int _gl_major, _gl_minor; + std::string _title; + + static void onKey(GLFWwindow *window, int key, int scan_code, int action, int mods); + + static void onSize(GLFWwindow *window, int width, int height); + +protected: + App(int gl_major, int gl_minor); + + void setTitle(std::string title); + + void setX(int x); + + void setY(int y); + + void setPos(int x, int y); + + void setWidth(int width); + + void setHeight(int height); + + void setSize(int width, int height); + + virtual void onKey(int key, int scan_code, int action, int mods) {} + + virtual void onSize(int width, int height) {} + + virtual void init() {} + + virtual void display() {} + + void swapBuffers(); + +public: + GLFWwindow *getWindow(); + + std::string getTitle(); + + int getX(); + + int getY(); + + void getPos(int *x, int *y); + + int getWidth(); + + int getHeight(); + + void getSize(int *width, int *height); + + int run(); + + void launch(); + + void center(); + + void center(GLFWmonitor *monitor); + + void close(); +}; + +#endif //GL_TEMPLATE_FRAMEWORK_H diff --git a/framework/include/hello.h b/framework/include/hello.h deleted file mode 100644 index f928c68..0000000 --- a/framework/include/hello.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef GL_TEMPLATE_HELLO_H -#define GL_TEMPLATE_HELLO_H - -#include - -void hello(); - -int showWindow(const std::string &title); - -#endif //GL_TEMPLATE_HELLO_H diff --git a/framework/src/framework.cpp b/framework/src/framework.cpp new file mode 100644 index 0000000..f0991b9 --- /dev/null +++ b/framework/src/framework.cpp @@ -0,0 +1,165 @@ +#include "framework.h" + +#include + +App::App(int gl_major, int gl_minor) { + _gl_major = gl_major; + _gl_minor = gl_minor; +} + +void App::setTitle(std::string title) { + _title = title; + glfwSetWindowTitle(getWindow(), title.c_str()); +} + +void App::setX(int x) { + setPos(x, getY()); +} + +void App::setY(int y) { + setPos(getX(), y); +} + +void App::setPos(int x, int y) { + glfwSetWindowPos(getWindow(), x, y); +} + +void App::setWidth(int width) { + setSize(width, getHeight()); +} + +void App::setHeight(int height) { + setSize(getWidth(), height); +} + +void App::setSize(int width, int height) { + glfwSetWindowSize(getWindow(), width, height); +} + +void App::swapBuffers() { + glfwSwapBuffers(getWindow()); +} + +GLFWwindow *App::getWindow() { + return _window; +} + +std::string App::getTitle() { + return _title; +} + +int App::getX() { + int x; + getPos(&x, nullptr); + return x; +} + +int App::getY() { + int y; + getPos(nullptr, &y); + return y; +} + +void App::getPos(int *x, int *y) { + glfwGetWindowPos(getWindow(), x, y); +} + +int App::getWidth() { + int w; + getSize(&w, nullptr); + return w; +} + +int App::getHeight() { + int h; + getSize(nullptr, &h); + return h; +} + +void App::getSize(int *width, int *height) { + glfwGetWindowSize(getWindow(), width, height); +} + +void App::center() { + center(glfwGetPrimaryMonitor()); +} + +void App::center(GLFWmonitor *monitor) { + int x, y; + int w, h; + const GLFWvidmode *mode = glfwGetVideoMode(monitor); + + glfwGetMonitorPos(monitor, &x, &y); + getSize(&w, &h); + + setPos(x + (mode->width - w) / 2, + y + (mode->height - h) / 2); +} + +void App::close() { + glfwSetWindowShouldClose(getWindow(), GLFW_TRUE); +} + +std::unordered_map manager; + +void App::onKey(GLFWwindow *window, int key, int scan_code, int action, int mods) { + auto m = manager.find(window); + + if (m != manager.end()) + m->second->onKey(key, scan_code, action, mods); +} + +void App::onSize(GLFWwindow *window, int width, int height) { + auto m = manager.find(window); + + if (m != manager.end()) + m->second->onSize(width, height); +} + +int App::run() { + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, _gl_major); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, _gl_minor); + _title = "GLFW App"; + + if (!(_window = glfwCreateWindow(1280, 720, _title.c_str(), nullptr, nullptr))) + return EXIT_FAILURE; + + center(); + + glfwSetWindowSizeCallback(getWindow(), App::onSize); + glfwSetKeyCallback(getWindow(), App::onKey); + + glfwMakeContextCurrent(_window); + gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); + glfwSwapInterval(0); + + init(); + + manager[getWindow()] = this; + while (!glfwWindowShouldClose(_window)) { + display(); + + glfwPollEvents(); + }; + manager.erase(getWindow()); + + glfwDestroyWindow(_window); + + return EXIT_SUCCESS; +} + +void error_callback(int error, const char *description) { + fprintf(stderr, "Error (%d): %s\n", error, description); +} + +void App::launch() { + if (!glfwInit()) exit(EXIT_FAILURE); + + glfwSetErrorCallback(error_callback); + + int code = run(); + + glfwTerminate(); + + exit(code); +} diff --git a/framework/src/hello.cpp b/framework/src/hello.cpp deleted file mode 100644 index c9c20d4..0000000 --- a/framework/src/hello.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "hello.h" - -#include - -#include -#include - -void hello() { - printf("Hello, World!\n"); -} - -int showWindow(const std::string &title) { - if (!glfwInit()) return EXIT_FAILURE; - - GLFWwindow *window = glfwCreateWindow(1280, 720, title.c_str(), nullptr, nullptr); - - while (!glfwWindowShouldClose(window)) { - glfwPollEvents(); - } - - glfwDestroyWindow(window); - glfwTerminate(); - return EXIT_SUCCESS; -} \ No newline at end of file diff --git a/glapp/CMakeLists.txt b/glapp/CMakeLists.txt index da2ecf3..b2d5945 100644 --- a/glapp/CMakeLists.txt +++ b/glapp/CMakeLists.txt @@ -6,4 +6,5 @@ add_executable(sample target_link_libraries(sample glad glm + glfw framework) diff --git a/glapp/src/main.cpp b/glapp/src/main.cpp index 4cdb5c5..81e8aa4 100644 --- a/glapp/src/main.cpp +++ b/glapp/src/main.cpp @@ -1,7 +1,4 @@ -#include - -#include -#include +#include #include #include @@ -12,13 +9,13 @@ #include #include -#include #include struct Vertex { glm::vec2 pos; glm::vec3 col; }; + static const char *vertex_shader_text = "uniform mat4 pvm;\n" "attribute vec3 vCol;\n" @@ -39,20 +36,8 @@ static const char *fragment_shader_text = "}\n"; -static void error_callback(int error, const char *description) { - fprintf(stderr, "Error: %s\n", description); -} - - -static void key_callback(GLFWwindow *window, int key, int scan_code, int action, int mods) { - if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) - glfwSetWindowShouldClose(window, GLFW_TRUE); -} - - -int main() { - hello(); - +class GLApp : public App { +private: std::vector vertices = { {glm::vec2(+0.5f, +0.5f), glm::vec3(1, 0, 0)}, {glm::vec2(+0.5f, -0.5f), glm::vec3(0, 1, 0)}, @@ -60,70 +45,52 @@ int main() { {glm::vec2(-0.5f, +0.5f), glm::vec3(1, 1, 1)}, }; - const int i_width = 1280, i_height = 720; + GLuint vertex_buffer = 0; + GLuint vertex_shader = 0, fragment_shader = 0, program = 0; + GLuint pvm_location = 0, vpos_location = 0, vcol_location = 0; - GLFWwindow *window; - GLFWmonitor *monitor; - const GLFWvidmode *mode; - GLuint vertex_buffer, vertex_shader, fragment_shader, program; - GLuint pvm_location, vpos_location, vcol_location; - - glfwSetErrorCallback(error_callback); - - if (!glfwInit()) { - exit(EXIT_FAILURE); +protected: + void onKey(int key, int scan_code, int action, int mods) override { + if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) + close(); } - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); + void init() override { + setTitle("Hello Square!"); - window = glfwCreateWindow(i_width, i_height, "Hello Triangle", nullptr, nullptr); - if (!window) { - glfwTerminate(); - exit(EXIT_FAILURE); + glGenBuffers(1, &vertex_buffer); + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); + glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices.front(), GL_STATIC_DRAW); + + vertex_shader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertex_shader, 1, &vertex_shader_text, nullptr); + glCompileShader(vertex_shader); + + fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragment_shader, 1, &fragment_shader_text, nullptr); + glCompileShader(fragment_shader); + + program = glCreateProgram(); + glAttachShader(program, vertex_shader); + glAttachShader(program, fragment_shader); + glLinkProgram(program); + + pvm_location = (GLuint) glGetUniformLocation(program, "pvm"); + vpos_location = (GLuint) glGetAttribLocation(program, "vPos"); + vcol_location = (GLuint) glGetAttribLocation(program, "vCol"); + + glEnableVertexAttribArray(vpos_location); + glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void *) nullptr); + + glEnableVertexAttribArray(vcol_location); + glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void *) (sizeof(float) * 2)); } - monitor = glfwGetPrimaryMonitor(); - mode = glfwGetVideoMode(monitor); - glfwSetWindowPos(window, (mode->width - i_width) / 2, (mode->height - i_height) / 2); - glfwSetKeyCallback(window, key_callback); - glfwMakeContextCurrent(window); - gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); - glfwSwapInterval(1); - - // NOTE: OpenGL error checks have been omitted for brevity - glGenBuffers(1, &vertex_buffer); - glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); - glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices.front(), GL_STATIC_DRAW); - - vertex_shader = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertex_shader, 1, &vertex_shader_text, nullptr); - glCompileShader(vertex_shader); - - fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragment_shader, 1, &fragment_shader_text, nullptr); - glCompileShader(fragment_shader); - - program = glCreateProgram(); - glAttachShader(program, vertex_shader); - glAttachShader(program, fragment_shader); - glLinkProgram(program); - - pvm_location = (GLuint) glGetUniformLocation(program, "pvm"); - vpos_location = (GLuint) glGetAttribLocation(program, "vPos"); - vcol_location = (GLuint) glGetAttribLocation(program, "vCol"); - - glEnableVertexAttribArray(vpos_location); - glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void *) nullptr); - - glEnableVertexAttribArray(vcol_location); - glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void *) (sizeof(float) * 2)); - - while (!glfwWindowShouldClose(window)) { + void display() override { int width, height; float ratio; - glfwGetFramebufferSize(window, &width, &height); + glfwGetFramebufferSize(getWindow(), &width, &height); ratio = (float) width / height; glViewport(0, 0, width, height); @@ -139,11 +106,15 @@ int main() { glDrawArrays(GL_QUADS, 0, (GLsizei) vertices.size()); - glfwSwapBuffers(window); - glfwPollEvents(); + swapBuffers(); } - glfwDestroyWindow(window); - glfwTerminate(); - exit(EXIT_SUCCESS); -} +public: + GLApp() : App(3, 0) {} +}; + + +int main() { + GLApp app = GLApp(); + app.launch(); +} \ No newline at end of file