summaryrefslogtreecommitdiff
path: root/src/shader.c
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/shader.c
parent2b6ddb0810a60b159501d60dbeaea5fd1b24daa7 (diff)
refactor: unify error handling
Diffstat (limited to 'src/shader.c')
-rw-r--r--src/shader.c82
1 files changed, 48 insertions, 34 deletions
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;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */