slightly improve error handling default tip

Wed, 23 Jul 2025 00:27:46 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 23 Jul 2025 00:27:46 +0200
changeset 221
14eddd43b3f7
parent 220
6b266e907f89

slightly improve error handling

src/2d.c file | annotate | diff | comparison | revisions
src/ascension/error.h file | annotate | diff | comparison | revisions
src/error.c file | annotate | diff | comparison | revisions
src/mesh.c file | annotate | diff | comparison | revisions
src/shader.c file | annotate | diff | comparison | revisions
src/sprite.c file | annotate | diff | comparison | revisions
src/text.c file | annotate | diff | comparison | revisions
src/texture.c file | annotate | diff | comparison | revisions
--- 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;
 }
--- 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);
 
--- 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;
--- 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");
 }
--- 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) {
--- 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;
 }
--- 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;
 }
--- 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) {

mercurial