# HG changeset patch # User Mike Becker # Date 1753223266 -7200 # Node ID 14eddd43b3f7ad8a02af89c11cff2112af18d612 # Parent 6b266e907f89d0f9bb03b9cb6f8e71ca53298ea7 slightly improve error handling diff -r 6b266e907f89 -r 14eddd43b3f7 src/2d.c --- a/src/2d.c Tue Jul 22 21:38:02 2025 +0200 +++ b/src/2d.c Wed Jul 23 00:27:46 2025 +0200 @@ -88,7 +88,9 @@ } asc_shader_free_codes(codes); - asc_error_catch_all_gl(); + if (asc_error_catch_gl("Creating rectangle shader")) { + // TODO: error handling + } return shader; } @@ -258,7 +260,9 @@ } asc_shader_free_codes(codes); - asc_error_catch_all_gl(); + if (asc_error_catch_gl("Creating ellipsis shader")) { + // TODO: error handling + } return shader; } diff -r 6b266e907f89 -r 14eddd43b3f7 src/ascension/error.h --- a/src/ascension/error.h Tue Jul 22 21:38:02 2025 +0200 +++ b/src/ascension/error.h Wed Jul 23 00:27:46 2025 +0200 @@ -34,7 +34,15 @@ void asc_error_gl(unsigned code, const char *message); -int asc_error_catch_all_gl(void); +/** + * Catches all OpenGL errors. + * + * Generates one error message per active error flag. + * + * @param message the text to include in the error messages + * @returns non-zero if any error was caught and zero when no error exists + */ +int asc_error_catch_gl(const char *message); bool asc_has_error(void); diff -r 6b266e907f89 -r 14eddd43b3f7 src/error.c --- a/src/error.c Tue Jul 22 21:38:02 2025 +0200 +++ b/src/error.c Wed Jul 23 00:27:46 2025 +0200 @@ -110,11 +110,12 @@ asc_error("%s - GL Error: %s", message, glerr); } -int asc_error_catch_all_gl(void) { +int asc_error_catch_gl(const char *message) { + // TODO: a printf-like signature does not hurt GLenum error; int ret = 0; while ((error = glGetError()) != GL_NO_ERROR) { - asc_error_gl(error, "Uncaught"); + asc_error_gl(error, message); ret = 1; } return ret; diff -r 6b266e907f89 -r 14eddd43b3f7 src/mesh.c --- a/src/mesh.c Tue Jul 22 21:38:02 2025 +0200 +++ b/src/mesh.c Wed Jul 23 00:27:46 2025 +0200 @@ -42,7 +42,7 @@ mesh[i].vbo = buffers[i]; mesh[i].vao = arrays[i]; } - return asc_error_catch_all_gl(); + return asc_error_catch_gl("Allocating mesh buffers"); } void asc_mesh_free_buffers(AscMesh *mesh, unsigned count) { @@ -58,15 +58,16 @@ mesh[i].vao = 0; } + asc_error_catch_gl("OpenGL has unchecked error flags before deleting mesh buffers"); glDeleteBuffers(count, buffers); glDeleteVertexArrays(count, arrays); - asc_error_catch_all_gl(); + asc_error_catch_gl("Deleting mesh buffers"); } void asc_mesh_draw_triangle_strip(const AscMesh *mesh) { glBindVertexArray(mesh->vao); glDrawArrays(GL_TRIANGLE_STRIP, 0, mesh->vtx_count); - asc_error_catch_all_gl(); + asc_error_catch_gl("Drawing mesh"); #ifndef NDEBUG // only unbind in debug mode to detect accidental re-use of the wrong VAO glBindVertexArray(0); @@ -92,6 +93,7 @@ // only bind the buffer for updating the data glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo); } + if (asc_error_catch_gl("Binding VBO or VAO")) return; unsigned required_memory = 4 * sizeof(asc_vertex2d); @@ -132,7 +134,5 @@ data[3].pos = ASC_VEC2F(args.size.x, args.size.y); data[3].uv = ASC_VEC2F(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y + args.uv_scale.y); glBufferData(GL_ARRAY_BUFFER, mesh->vtx_data_size, mesh->vtx_data, GL_STATIC_DRAW); - - // TODO: replace with specific error handling for setting the buffer - asc_error_catch_all_gl(); + asc_error_catch_gl("Writing VBO data"); } diff -r 6b266e907f89 -r 14eddd43b3f7 src/shader.c --- a/src/shader.c Tue Jul 22 21:38:02 2025 +0200 +++ b/src/shader.c Wed Jul 23 00:27:46 2025 +0200 @@ -185,7 +185,7 @@ glUseProgram(shader->gl_id); glUniformMatrix4fv(shader->projection, 1, GL_FALSE, camera->projection); glUniformMatrix4fv(shader->view, 1, GL_FALSE, camera->view); - return asc_error_catch_all_gl(); + return asc_error_catch_gl("Activating shader"); } static int asc_shader_load_code_file(const char *filename, char **code) { diff -r 6b266e907f89 -r 14eddd43b3f7 src/sprite.c --- a/src/sprite.c Tue Jul 22 21:38:02 2025 +0200 +++ b/src/sprite.c Wed Jul 23 00:27:46 2025 +0200 @@ -59,7 +59,9 @@ sprite_shader->tex = asc_shader_get_uniform_loc(shader, "tex"); asc_shader_free_codes(codes); - asc_error_catch_all_gl(); + if (asc_error_catch_gl("Creating sprite shader")) { + // TODO: error handling + } return shader; } diff -r 6b266e907f89 -r 14eddd43b3f7 src/text.c --- a/src/text.c Tue Jul 22 21:38:02 2025 +0200 +++ b/src/text.c Wed Jul 23 00:27:46 2025 +0200 @@ -58,7 +58,9 @@ text_shader->tex = asc_shader_get_uniform_loc(shader, "tex"); asc_shader_free_codes(codes); - asc_error_catch_all_gl(); + if (asc_error_catch_gl("Creating text shader")) { + // TODO: error handling + } return shader; } diff -r 6b266e907f89 -r 14eddd43b3f7 src/texture.c --- a/src/texture.c Tue Jul 22 21:38:02 2025 +0200 +++ b/src/texture.c Wed Jul 23 00:27:46 2025 +0200 @@ -44,7 +44,7 @@ } glBindTexture(tex->target, tex->tex_id); glUniform1i(uniform_location, unit); - asc_error_catch_all_gl(); + asc_error_catch_gl("Binding texture to uniform location"); } void asc_texture_from_surface(AscTexture *tex, const SDL_Surface *surface) { @@ -57,6 +57,7 @@ glBindTexture(tex->target,tex->tex_id); glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch / surface->format->BytesPerPixel); + if (asc_error_catch_gl("Binding texture object")) return; // Determine the format and internal format based on the SDL surface format GLint internal_format; @@ -88,8 +89,7 @@ surface->w, surface->h, 0, format, GL_UNSIGNED_BYTE, surface->pixels); - // TODO: replace catch all with proper error handling for this single call - asc_error_catch_all_gl(); + asc_error_catch_gl("Writing texture data"); } void asc_texture_from_file(AscTexture *tex, const char *name) { @@ -131,6 +131,7 @@ GLuint textures[count]; glGenTextures(count, textures); + if (asc_error_catch_gl("Creating texture objects")) return; for (unsigned i = 0; i < count; ++i) { memset(&tex[i], 0, sizeof(AscTexture)); @@ -143,9 +144,7 @@ texture_filters[mag_filter]); asc_dprintf("Initialized texture: %u", tex[i].tex_id); } - - // TODO: proper error handling for each gl call - asc_error_catch_all_gl(); + asc_error_catch_gl("Initializing texture objects"); } void asc_texture_destroy(AscTexture *tex, unsigned count) {