empty base

This commit is contained in:
Christoph J. Scherr 2023-10-20 11:20:01 +02:00
parent 0820aa31ac
commit fe00f536bd
3 changed files with 57 additions and 19 deletions

View File

@ -1,3 +1,4 @@
#include <boost/log/trivial.hpp>
#include <exception>
#include <glad/glad.c>
// glad must be loaded before GLFW
@ -10,18 +11,26 @@ using namespace std;
/* class ShaderProgram Implementations */
string ShaderProgram::baseName() {
/*
BOOST_LOG_TRIVIAL(debug) << "this->base: " << this->base
<< " Base::Empty: " << Base::Empty
<< " Base::Triangle: " << Base::Triangle;
*/
switch (this->base) {
case Empty:
case ShaderProgram::Base::Empty:
return "Empty";
case Triangle:
case ShaderProgram::Base::Triangle:
return "Triangle";
default:
throw exception();
// this should never occur, as the switch case should cover all variants.
throw std::runtime_error("Undefined base Program");
}
}
ShaderProgram::ShaderProgram(Base base, char** vertexSource,
char** fragSource) {
ShaderProgram::ShaderProgram(Base base, char **vertexSource,
char **fragSource) {
this->base = base;
BOOST_LOG_TRIVIAL(info) << "With base program \"" << this->baseName() << "\"";
// NOTE: char arrays and char pointers are not actually the same.
// glShaderSource requires a char[] as it appears. We pass the
// variable by reference to avoid duplication in memory.
@ -29,8 +38,7 @@ ShaderProgram::ShaderProgram(Base base, char** vertexSource,
this->vertex = glCreateShader(GL_VERTEX_SHADER);
this->frag = glCreateShader(GL_FRAGMENT_SHADER);
BOOST_LOG_TRIVIAL(trace) << "loading vertex shader";
glShaderSource(this->vertex, 1, vertexSource,
NULL);
glShaderSource(this->vertex, 1, vertexSource, NULL);
BOOST_LOG_TRIVIAL(trace) << "compiling vertex shader";
glCompileShader(this->vertex);
BOOST_LOG_TRIVIAL(trace) << "loading fragments shader";
@ -39,13 +47,28 @@ ShaderProgram::ShaderProgram(Base base, char** vertexSource,
glCompileShader(this->frag);
}
int ShaderProgram::run() { return 255; }
/** runs for every frame
*
* will be called in `mainWindow`.
*
* FIXME: dont run the switch case every frame.
*/
int ShaderProgram::run() {
switch (this->base) {
case Empty: // same as default
default:
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
break;
}
return 0;
}
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height);
}
int initGl() {
GLFWwindow *initGl() {
/* graphics stuff */
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@ -57,23 +80,37 @@ int initGl() {
if (window == NULL) {
printf("Failed to create GLFW window\n");
glfwTerminate();
return EXIT_GL;
exit(EXIT_GL);
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
printf("Failed to initialize GLAD\n");
return EXIT_GL;
exit(EXIT_GL);
}
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
return 0;
return window;
}
int mainWindow(ShaderProgram *shaderProgram) {
int mainWindow(ShaderProgram *shaderProgram, GLFWwindow *window) {
int result = shaderProgram->run();
int result = 0;
BOOST_LOG_TRIVIAL(trace) << "Entering window loop";
while (!glfwWindowShouldClose(window)) {
processInput(window);
shaderProgram->run();
glfwSwapBuffers(window);
glfwPollEvents();
}
BOOST_LOG_TRIVIAL(trace) << "Left window loop";
glfwTerminate();
return result;
}
void processInput(GLFWwindow *window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}

View File

@ -17,6 +17,7 @@ protected:
unsigned int frag;
Base base;
};
int mainWindow(ShaderProgram* shaderProgramm);
int initGl();
int mainWindow(ShaderProgram* shaderProgramm, GLFWwindow* window);
GLFWwindow* initGl();
void processInput(GLFWwindow *window);
#endif

View File

@ -54,7 +54,7 @@ int main(int argc, char **argv) {
}
/* init graphics stuff */
result = initGl();
GLFWwindow* window = initGl();
if (result != 0) {
return EXIT_GL;
}
@ -93,8 +93,8 @@ int main(int argc, char **argv) {
/* Run our main program */
BOOST_LOG_TRIVIAL(trace) << "Starting mainWindow";
result = mainWindow(&shaderProgram);
/* if (result != 0) { return EXIT_GL;} */
result = mainWindow(&shaderProgram, window);
BOOST_LOG_TRIVIAL(trace) << "mainWindow stopped, goodbye";
return result;
}