break apart into multiple files
This commit is contained in:
@@ -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;
|
||||
|
||||
51
cosets/src/util/shader.hpp
Normal file
51
cosets/src/util/shader.hpp
Normal 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;
|
||||
}
|
||||
60
cosets/src/util/window.hpp
Normal file
60
cosets/src/util/window.hpp
Normal 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);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user