--- a/src/shader.c Tue Mar 18 22:43:31 2025 +0100 +++ b/src/shader.c Wed Mar 19 22:43:37 2025 +0100 @@ -30,6 +30,8 @@ #include <GL/glew.h> #include <string.h> +#include <cx/buffer.h> +#include <cx/streams.h> /** * Compiles a shader from the given source code. @@ -131,3 +133,30 @@ } return prog; } + +static int asc_shader_load_code_file(const char *filename, char **code) { + if (filename == NULL) { + *code = NULL; + return 0; + } + FILE *f = fopen(filename, "r"); + if (f == NULL) return -1; + CxBuffer buffer; + cxBufferInit(&buffer, NULL, 1024, NULL, 0); + cx_stream_copy(f, &buffer, (cx_read_func) fread, cxBufferWriteFunc); + cxBufferPut(&buffer, '\0'); + *code = realloc(buffer.space, buffer.size); + return *code == NULL ? -1 : 0; +} + +int asc_shader_load_code_files(AscShaderCodeFiles files, AscShaderCodes *codes) { + int ret = 0; + ret |= asc_shader_load_code_file(files.vtx, &codes->vtx); + ret |= asc_shader_load_code_file(files.frag, &codes->frag); + return ret; +} + +void asc_shader_free_codes(AscShaderCodes codes) { + free(codes.vtx); + free(codes.frag); +}