From 0b7da3fad7b21bfacfd38c0cc59fc9a370c6acaf Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 15 Sep 2024 23:20:10 +0100 Subject: [PATCH] Cleanup, extra comments and error logging --- src/main.cc | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/src/main.cc b/src/main.cc index 96f7ff2..2354d92 100644 --- a/src/main.cc +++ b/src/main.cc @@ -38,6 +38,7 @@ struct App { GLuint vbo; GLuint shader_program; + bool running; }; int init (App &app); @@ -55,8 +56,10 @@ int main() { return result; } + // Setup the geometry create_vertex_spec(app); + // Setup graphics pipeline. At the very minimum creating vertex and fragment shaders create_graphics_pipeline(app); run_main_loop(app); @@ -92,7 +95,7 @@ int init(App &app) { return EXIT_CODE_OPENGL_CONTEXT_FAILED; } - // Init glad + // Init GLAD if (!gladLoadGLLoader(SDL_GL_GetProcAddress)) { fprintf(stderr, "Failed to initialise glad"); return EXIT_CODE_GLAD_LOADER_FAILED; @@ -110,11 +113,12 @@ int init(App &app) { void create_vertex_spec(App &app) { const std::vector vertices = { - -0.8f, -0.8f, 0.0f, - 0.8f, -0.8f, 0.0f, - 0.0f, 0.8f, 0.0f, + -0.8f, -0.8f, 0.0f, // position + 0.8f, -0.8f, 0.0f, // position + 0.0f, 0.8f, 0.0f, // position }; + // Create and activate the VAO glGenVertexArrays(1, &app.vao); glBindVertexArray(app.vao); @@ -134,7 +138,8 @@ void create_vertex_spec(App &app) { // for each vertex and the offset of the first instance of the component in the buffer glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0); - // Cleanup. The order matters. Unbind VAO first then disable the attributes. If you do it the + // Cleanup. + // The order matters. Unbind VAO first then disable the attributes. If you do it the // other way around, the VAO will store that the attributes are disabled and nothing will be // drawn during the rendering stage glBindVertexArray(0); @@ -160,13 +165,13 @@ void create_graphics_pipeline(App &app) { } void run_main_loop(App &app) { - bool running = true; + app.running = true; - while (running) { + while (app.running) { while (SDL_PollEvent(&app.event)) { switch (app.event.type) { case SDL_QUIT: - running = false; + app.running = false; break; } } @@ -188,6 +193,9 @@ void run_main_loop(App &app) { glDrawArrays(GL_TRIANGLES, 0, 3); // End draw call + // Not necessary if we only have one shader program + glUseProgram(0); + SDL_GL_SwapWindow(app.window); } } @@ -207,8 +215,25 @@ GLuint create_shader_program(const std::string &vertex_shader_source, const std: glAttachShader(shader_program, fragment_shader); glLinkProgram (shader_program); + GLint link_status; + glGetProgramiv(shader_program, GL_LINK_STATUS, &link_status); + if (link_status != GL_TRUE) { + GLsizei log_length = 0; + GLchar message[1024]; + glGetProgramInfoLog(shader_program, 1024, &log_length, message); + fprintf(stderr, "Failed to link program: %s\n", message); + } + // Validate the shader_program glValidateProgram(shader_program); + GLint validation_status; + glGetProgramiv(shader_program, GL_VALIDATE_STATUS, &validation_status); + if (validation_status != GL_TRUE) { + GLsizei log_length = 0; + GLchar message[1024]; + glGetProgramInfoLog(shader_program, 1024, &log_length, message); + fprintf(stderr, "Failed to validate program: %s\n", message); + } return shader_program; } @@ -226,7 +251,7 @@ GLuint compile_shader(GLuint type, const std::string &source) { GLsizei log_length = 0; GLchar message[1024]; glGetShaderInfoLog(shader, 1024, &log_length, message); - fprintf(stderr, "%s\n", message); + fprintf(stderr, "Failed to compile shader: %s\n", message); } return shader;