break apart into multiple files

This commit is contained in:
2019-09-08 23:26:44 -04:00
parent 39c9866389
commit 76e022317f
3 changed files with 113 additions and 105 deletions

View File

@@ -1,9 +1,5 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <string>
#include <iostream>
#include <cstdio>
#include "util/window.hpp"
#include "util/shader.hpp"
#include <glm/mat4x4.hpp>
#include <glm/gtc/type_ptr.hpp>
@@ -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;

View File

@@ -0,0 +1,51 @@
#pragma once
#include <glad/glad.h>
#include <string>
#include <cstdio>
#include <cstdlib>
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;
}

View File

@@ -0,0 +1,60 @@
#pragma once
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <cstdlib>
#include <cstdio>
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);
}
};