summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2020-06-01 13:22:08 -0500
committersanine-a <sanine.not@pm.me>2020-06-01 13:22:08 -0500
commitea197a4459f3dc8ad885e5134e6358173650901f (patch)
treeca2749291fc3e60697413764615588b68834d0c4 /src
parent2b6ddb0810a60b159501d60dbeaea5fd1b24daa7 (diff)
refactor: unify error handling
Diffstat (limited to 'src')
-rw-r--r--src/error.c116
-rw-r--r--src/shader.c82
2 files changed, 164 insertions, 34 deletions
diff --git a/src/error.c b/src/error.c
new file mode 100644
index 0000000..6fc45ad
--- /dev/null
+++ b/src/error.c
@@ -0,0 +1,116 @@
+#include "../include/common.h"
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+
+void honey_error_clear_strings() {
+ memset(honey_error_data.string1, 0, HONEY_ERROR_DATA_STRING_LENGTH);
+ memset(honey_error_data.string2, 0, HONEY_ERROR_DATA_STRING_LENGTH);
+}
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+
+void honey_error_set_string1(char* string) {
+ size_t n_bytes = strlen(string) + 1;
+ if (n_bytes > HONEY_ERROR_DATA_STRING_LENGTH)
+ n_bytes = HONEY_ERROR_DATA_STRING_LENGTH;
+ memcpy(honey_error_data.string1, string, n_bytes);
+ honey_error_data.string1[HONEY_ERROR_DATA_STRING_LENGTH-1] = 0;
+}
+
+void honey_error_set_string2(char* string) {
+ size_t n_bytes = strlen(string) + 1;
+ if (n_bytes > HONEY_ERROR_DATA_STRING_LENGTH)
+ n_bytes = HONEY_ERROR_DATA_STRING_LENGTH;
+ memcpy(honey_error_data.string2, string, n_bytes);
+ honey_error_data.string2[HONEY_ERROR_DATA_STRING_LENGTH-1] = 0;
+}
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+
+void honey_human_readable_error(char* error_string, honey_error error) {
+ size_t string_size = sizeof(char)*3*HONEY_ERROR_DATA_STRING_LENGTH;
+
+ switch(error) {
+ case HONEY_OK:
+ snprintf(error_string, string_size, "[honey] OK");
+ break;
+
+ case HONEY_MEMORY_ALLOCATION_ERROR:
+ snprintf(error_string, string_size, "[honey] ERROR: failed to allocate memory");
+ break;
+
+ case HONEY_FILE_READ_ERROR:
+ if (honey_error_data.string1 != NULL) {
+ snprintf(error_string,
+ string_size,
+ "[honey] ERROR: failed to read file '%s'",
+ honey_error_data.string1);
+ } else {
+ snprintf(error_string, string_size, "[honey] ERROR: failed to read file");
+ }
+ break;
+
+ case HONEY_VERTEX_SHADER_COMPILATION_ERROR:
+ if (honey_error_data.string1 != NULL) {
+ if (honey_error_data.string2 != NULL) {
+ snprintf(error_string,
+ string_size,
+ "[honey] ERROR: failed to compile vertex shader '%s'\n"
+ "[honey] GLSL compiler output:\n%s\n",
+ honey_error_data.string2,
+ honey_error_data.string1);
+ } else {
+ snprintf(error_string,
+ string_size,
+ "[honey] ERROR: failed to compile vertex shader\n"
+ "[honey] GLSL compiler output:\n%s\n",
+ honey_error_data.string1);
+ }
+ } else {
+ snprintf(error_string,
+ string_size,
+ "[honey] ERROR: failed to compile vertex shader.");
+ }
+ break;
+
+
+ case HONEY_FRAGMENT_SHADER_COMPILATION_ERROR:
+ if (honey_error_data.string1 != NULL) {
+ if (honey_error_data.string2 != NULL) {
+ snprintf(error_string,
+ string_size,
+ "[honey] ERROR: failed to compile fragment shader '%s'\n"
+ "[honey] GLSL compiler output:\n%s\n",
+ honey_error_data.string2,
+ honey_error_data.string1);
+ } else {
+ snprintf(error_string,
+ string_size,
+ "[honey] ERROR: failed to compile fragment shader\n"
+ "[honey] GLSL compiler output:\n%s\n",
+ honey_error_data.string1);
+ }
+ } else {
+ snprintf(error_string,
+ string_size,
+ "[honey] ERROR: failed to compile fragment shader.");
+ }
+ break;
+
+ case HONEY_SHADER_LINK_ERROR:
+
+ case HONEY_MESH_BAD_VERTEX_DATA:
+
+ case HONEY_MESH_BAD_INDEX_DATA:
+
+ default:
+ break;
+ }
+}
+
+
+
+
+
+
+
diff --git a/src/shader.c b/src/shader.c
index b7f2916..d250629 100644
--- a/src/shader.c
+++ b/src/shader.c
@@ -1,46 +1,59 @@
-#include "include/shader.h"
+#include "../include/shader.h"
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
-static char* read_file(char* file_path) {
+static honey_error read_file(char** destination, char* file_path) {
FILE* f = fopen(file_path, "r");
if (f == NULL) {
- return NULL;
+ honey_error_set_string1(file_path);
+ return HONEY_FILE_READ_ERROR;
}
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
- char* file_string = malloc(fsize + 1);
- if (file_string == NULL) {
- return NULL;
+ *destination = malloc(fsize + 1);
+ if (*destination == NULL) {
+ return HONEY_MEMORY_ALLOCATION_ERROR;
}
- fread(file_string, 1, fsize, f);
+ fread(*destination, 1, fsize, f);
fclose(f);
- file_string[fsize] = 0;
+ (*destination)[fsize] = 0;
- return file_string;
+ return HONEY_OK;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
-enum honey_shader_result honey_shader_load(honey_shader* shader,
- char* vertex_shader_path,
- char* fragment_shader_path) {
- enum honey_shader_result result;
-
+honey_error honey_shader_load(honey_shader* shader,
+ char* vertex_shader_path,
+ char* fragment_shader_path) {
/* load vertex shader code */
- char* vertex_shader_code = read_file(vertex_shader_path);
+ char* vertex_shader_code;
+ honey_error result = read_file(&vertex_shader_code,
+ vertex_shader_path);
+ if (result != HONEY_OK)
+ return result;
/* load fragment shader code */
- char* fragment_shader_code = read_file(fragment_shader_path);
+ char* fragment_shader_code;
+ result = read_file(&fragment_shader_code,
+ fragment_shader_path);
+ if (result != HONEY_OK)
+ return result;
result = honey_shader_new(shader,
vertex_shader_code,
fragment_shader_code);
+ if (result == HONEY_VERTEX_SHADER_COMPILATION_ERROR)
+ honey_error_set_string2(vertex_shader_path);
+
+ if (result == HONEY_FRAGMENT_SHADER_COMPILATION_ERROR)
+ honey_error_set_string2(fragment_shader_path);
+
free(vertex_shader_code);
free(fragment_shader_code);
@@ -49,11 +62,9 @@ enum honey_shader_result honey_shader_load(honey_shader* shader,
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
-enum honey_shader_result honey_shader_new(honey_shader* shader,
- char* vertex_shader_code,
- char* fragment_shader_code) {
- enum honey_shader_result result;
-
+honey_error honey_shader_new(honey_shader* shader,
+ char* vertex_shader_code,
+ char* fragment_shader_code) {
/* compile shaders */
int success;
char error[512];
@@ -63,10 +74,11 @@ enum honey_shader_result honey_shader_new(honey_shader* shader,
glCompileShader(vertex_shader);
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);
if (!success) {
- glGetShaderInfoLog(vertex_shader, 512, NULL, error);
- printf("Failed to compile vertex shader: %s\n", error);
- result = VERTEX_SHADER_FAILED;
- return result;
+ honey_error_clear_strings();
+ char compiler_error[HONEY_ERROR_DATA_STRING_LENGTH];
+ glGetShaderInfoLog(vertex_shader, HONEY_ERROR_DATA_STRING_LENGTH, NULL, compiler_error);
+ honey_error_set_string1(compiler_error);
+ return HONEY_VERTEX_SHADER_COMPILATION_ERROR;
}
int fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
@@ -74,10 +86,11 @@ enum honey_shader_result honey_shader_new(honey_shader* shader,
glCompileShader(fragment_shader);
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success);
if (!success) {
- glGetShaderInfoLog(fragment_shader, 512, NULL, error);
- printf("Failed to compile fragment shader: %s\n", error);
- result = FRAGMENT_SHADER_FAILED;
- return result;
+ honey_error_clear_strings();
+ char compiler_error[HONEY_ERROR_DATA_STRING_LENGTH];
+ glGetShaderInfoLog(fragment_shader, HONEY_ERROR_DATA_STRING_LENGTH, NULL, compiler_error);
+ honey_error_set_string1(compiler_error);
+ return HONEY_FRAGMENT_SHADER_COMPILATION_ERROR;
}
/* link shaders */
@@ -87,16 +100,17 @@ enum honey_shader_result honey_shader_new(honey_shader* shader,
glLinkProgram(*shader);
glGetShaderiv(*shader, GL_LINK_STATUS, &success);
if (!success) {
- glGetProgramInfoLog(*shader, 512, NULL, error);
- printf("Failed to link shaders: %s\n", error);
- result = SHADER_LINK_FAILED;
- return result;
+ honey_error_clear_strings();
+ char compiler_error[HONEY_ERROR_DATA_STRING_LENGTH];
+ glGetShaderInfoLog(vertex_shader, HONEY_ERROR_DATA_STRING_LENGTH, NULL, compiler_error);
+ honey_error_set_string1(compiler_error);
+ return HONEY_SHADER_LINK_ERROR;
}
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
- result = SHADER_OK;
+ return HONEY_OK;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */