55 lines
1.2 KiB
C++
55 lines
1.2 KiB
C++
//#include <GLFW/glfw3.h>
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <cassert>
|
|
#include <stdlib.h>
|
|
#include <sys/stat.h>
|
|
#include <glad/glad.h>
|
|
|
|
#define CONST_READSHADER_MAX_FN_LEN 256
|
|
#define CONST_READSHADER_MAX_FN_SIZE 32*1024
|
|
int readshader(const char *fn, GLchar** pshdr_source) {
|
|
char shader_fn[CONST_READSHADER_MAX_FN_LEN];
|
|
assert(strlen(fn) + 9 < CONST_READSHADER_MAX_FN_LEN);
|
|
struct stat status_buffer;
|
|
|
|
GLchar *shdr_source;
|
|
|
|
snprintf(shader_fn, CONST_READSHADER_MAX_FN_LEN, "res/shaders/%s", fn);
|
|
|
|
printf("%s\n", shader_fn);
|
|
|
|
if (stat(shader_fn, &status_buffer) == -1) {
|
|
printf("Failed to stat file '%s'\n", shader_fn);
|
|
return 0;
|
|
}
|
|
assert(status_buffer.st_size < CONST_READSHADER_MAX_FN_SIZE-1);
|
|
|
|
FILE *fh = fopen(shader_fn, "r");
|
|
assert(fh != NULL);
|
|
|
|
shdr_source = (GLchar *)malloc(CONST_READSHADER_MAX_FN_SIZE);
|
|
assert(shdr_source != NULL);
|
|
|
|
size_t read_bytes = fread((void *)shdr_source, sizeof(char),
|
|
CONST_READSHADER_MAX_FN_SIZE-1, fh);
|
|
assert(read_bytes < CONST_READSHADER_MAX_FN_SIZE-1);
|
|
shdr_source[read_bytes] = '\0';
|
|
|
|
puts(shdr_source);
|
|
|
|
fclose(fh);
|
|
assert(shdr_source != NULL);
|
|
|
|
|
|
*pshdr_source = shdr_source;
|
|
|
|
return 0;
|
|
}
|
|
|
|
void freeshader(void *pshdr_source) {
|
|
assert(pshdr_source != NULL);
|
|
free(pshdr_source);
|
|
}
|
|
|