src/shader.c

changeset 78
9bc544f4ce8f
parent 77
2187a732f4d7
equal deleted inserted replaced
77:2187a732f4d7 78:9bc544f4ce8f
28 #include "ascension/shader.h" 28 #include "ascension/shader.h"
29 #include "ascension/error.h" 29 #include "ascension/error.h"
30 30
31 #include <GL/glew.h> 31 #include <GL/glew.h>
32 #include <string.h> 32 #include <string.h>
33 #include <cx/buffer.h>
34 #include <cx/streams.h>
33 35
34 /** 36 /**
35 * Compiles a shader from the given source code. 37 * Compiles a shader from the given source code.
36 * 38 *
37 * The ID of the returned shader will be zero when something went wrong. 39 * The ID of the returned shader will be zero when something went wrong.
129 for (unsigned i = 0; i < n; i++) { 131 for (unsigned i = 0; i < n; i++) {
130 glDeleteShader(shader[i]); 132 glDeleteShader(shader[i]);
131 } 133 }
132 return prog; 134 return prog;
133 } 135 }
136
137 static int asc_shader_load_code_file(const char *filename, char **code) {
138 if (filename == NULL) {
139 *code = NULL;
140 return 0;
141 }
142 FILE *f = fopen(filename, "r");
143 if (f == NULL) return -1;
144 CxBuffer buffer;
145 cxBufferInit(&buffer, NULL, 1024, NULL, 0);
146 cx_stream_copy(f, &buffer, (cx_read_func) fread, cxBufferWriteFunc);
147 cxBufferPut(&buffer, '\0');
148 *code = realloc(buffer.space, buffer.size);
149 return *code == NULL ? -1 : 0;
150 }
151
152 int asc_shader_load_code_files(AscShaderCodeFiles files, AscShaderCodes *codes) {
153 int ret = 0;
154 ret |= asc_shader_load_code_file(files.vtx, &codes->vtx);
155 ret |= asc_shader_load_code_file(files.frag, &codes->frag);
156 return ret;
157 }
158
159 void asc_shader_free_codes(AscShaderCodes codes) {
160 free(codes.vtx);
161 free(codes.frag);
162 }

mercurial