Add function to initialize shaders

This commit is contained in:
Dimitri Lozeve 2021-03-05 19:06:38 +01:00
parent a1307a0b2f
commit 371ecd7f9b
3 changed files with 90 additions and 67 deletions

View file

@ -116,36 +116,7 @@ int main(int argc, char *argv[]) {
state.inotify_fd = -1; state.inotify_fd = -1;
} }
state.screen_shader.filename = arguments.shader_file; initialize_shaders(&state, arguments.shader_file, arguments.buffer_file);
log_info("Screen shader file: %s", state.screen_shader.filename);
if (state.inotify_fd != -1) {
state.screen_shader.wd = inotify_add_watch(
state.inotify_fd, state.screen_shader.filename, IN_MODIFY);
if (state.screen_shader.wd == -1) {
log_warn("[inotify] Cannot watch file %s", state.screen_shader.filename);
perror("inotify_add_watch");
} else {
log_debug("[inotify] Watching file %s", state.screen_shader.filename);
}
}
if (arguments.buffer_file) {
state.buffer_shader.filename = arguments.buffer_file;
log_info("Buffer shader file: %s", state.buffer_shader.filename);
if (state.inotify_fd != -1) {
state.buffer_shader.wd = inotify_add_watch(
state.inotify_fd, state.buffer_shader.filename, IN_MODIFY);
if (state.buffer_shader.wd == -1) {
log_warn("[inotify] Cannot watch file %s",
state.buffer_shader.filename);
perror("inotify_add_watch");
} else {
log_debug("[inotify] Watching file %s", state.buffer_shader.filename);
}
}
}
state.window = initialize_window(WINDOW_WIDTH, WINDOW_HEIGHT); state.window = initialize_window(WINDOW_WIDTH, WINDOW_HEIGHT);
if (state.window == NULL) { if (state.window == NULL) {

View file

@ -1,52 +1,55 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/inotify.h>
#include "log.h" #include "log.h"
#include "renderer.h"
#include "shaders.h"
/** /**
* @brief Reads a file in a heap-allocated buffer. * @brief Initialize shaders and setup inotify if required.
* *
* This function computes the length of the file, allocate a buffer of * @param state The target renderer state.
* the correct size, and reads the file in the buffer. Returns `NULL` * @param shader_file The file name of the screen shader.
* on error. * @param buffer_file The file name of the buffer shader, or NULL if no buffer
* * shader.
* @param filename The file to read. * @return 0 on success, 1 on error.
* @return A buffer containing the contents of the file, or `NULL` if
* there was an error.
*/ */
char *read_file(const char *const filename) { int initialize_shaders(struct renderer_state *state, const char *shader_file,
FILE *fd = fopen(filename, "r"); const char *buffer_file) {
if (fd == NULL) { state->screen_shader.filename = shader_file;
log_error("Could not open file %s", filename); log_info("Screen shader file: %s", state->screen_shader.filename);
return NULL;
if (state->inotify_fd != -1) {
state->screen_shader.wd = inotify_add_watch(
state->inotify_fd, state->screen_shader.filename, IN_MODIFY);
if (state->screen_shader.wd == -1) {
log_warn("[inotify] Cannot watch file %s", state->screen_shader.filename);
perror("inotify_add_watch");
} else {
log_debug("[inotify] Watching file %s", state->screen_shader.filename);
}
} }
if (fseek(fd, 0, SEEK_END) == -1) { if (buffer_file) {
perror("fseek"); state->buffer_shader.filename = buffer_file;
return NULL; log_info("Buffer shader file: %s", state->buffer_shader.filename);
}
long size = ftell(fd); if (state->inotify_fd != -1) {
if (size == -1) { state->buffer_shader.wd = inotify_add_watch(
perror("ftell"); state->inotify_fd, state->buffer_shader.filename, IN_MODIFY);
return NULL; if (state->buffer_shader.wd == -1) {
} log_warn("[inotify] Cannot watch file %s",
rewind(fd); state->buffer_shader.filename);
char *buf = calloc(size + 1, 1); perror("inotify_add_watch");
if (buf == NULL) { } else {
log_error("Failed to allocate memory to read file %s", filename); log_debug("[inotify] Watching file %s", state->buffer_shader.filename);
return NULL; }
}
} }
long bytes_read = fread(buf, 1, size, fd); return 0;
if (bytes_read != size) {
log_error("Failed to read file %s (%ld bytes read out of %ld total)",
filename, bytes_read, size);
return NULL;
}
fclose(fd);
return buf;
} }
/** /**
@ -127,3 +130,48 @@ int compile_shaders(unsigned int *shader_program,
return 0; return 0;
} }
/**
* @brief Reads a file in a heap-allocated buffer.
*
* This function computes the length of the file, allocate a buffer of
* the correct size, and reads the file in the buffer. Returns `NULL`
* on error.
*
* @param filename The file to read.
* @return A buffer containing the contents of the file, or `NULL` if
* there was an error.
*/
char *read_file(const char *const filename) {
FILE *fd = fopen(filename, "r");
if (fd == NULL) {
log_error("Could not open file %s", filename);
return NULL;
}
if (fseek(fd, 0, SEEK_END) == -1) {
perror("fseek");
return NULL;
}
long size = ftell(fd);
if (size == -1) {
perror("ftell");
return NULL;
}
rewind(fd);
char *buf = calloc(size + 1, 1);
if (buf == NULL) {
log_error("Failed to allocate memory to read file %s", filename);
return NULL;
}
long bytes_read = fread(buf, 1, size, fd);
if (bytes_read != size) {
log_error("Failed to read file %s (%ld bytes read out of %ld total)",
filename, bytes_read, size);
return NULL;
}
fclose(fd);
return buf;
}

View file

@ -1,8 +1,12 @@
#ifndef SHADERS_H #ifndef SHADERS_H
#define SHADERS_H #define SHADERS_H
char *read_file(const char *const filename); #include "renderer.h"
int initialize_shaders(struct renderer_state *state, const char *shader_file,
const char *buffer_file);
int compile_shaders(unsigned int *shader_program, int compile_shaders(unsigned int *shader_program,
const char *const fragment_shader_file); const char *const fragment_shader_file);
char *read_file(const char *const filename);
#endif /* SHADERS_H */ #endif /* SHADERS_H */