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

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;
}