refactor framework lib

This commit is contained in:
2018-12-10 23:31:36 -05:00
parent 8de34bc077
commit c3c1c80c6d
8 changed files with 298 additions and 119 deletions

View File

@@ -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)

View File

@@ -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)
export(TARGETS framework glfw glad
FILE FrameworkConfig.cmake)

View File

@@ -0,0 +1,74 @@
#ifndef GL_TEMPLATE_FRAMEWORK_H
#define GL_TEMPLATE_FRAMEWORK_H
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <string>
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

View File

@@ -1,10 +0,0 @@
#ifndef GL_TEMPLATE_HELLO_H
#define GL_TEMPLATE_HELLO_H
#include <string>
void hello();
int showWindow(const std::string &title);
#endif //GL_TEMPLATE_HELLO_H

165
framework/src/framework.cpp Normal file
View File

@@ -0,0 +1,165 @@
#include "framework.h"
#include <unordered_map>
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<GLFWwindow *, App *> 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);
}

View File

@@ -1,24 +0,0 @@
#include "hello.h"
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <string>
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;
}

View File

@@ -6,4 +6,5 @@ add_executable(sample
target_link_libraries(sample
glad
glm
glfw
framework)

View File

@@ -1,7 +1,4 @@
#include <hello.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <framework.h>
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
@@ -12,13 +9,13 @@
#include <cstdlib>
#include <cstdio>
#include <string>
#include <vector>
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<Vertex> 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();
}