common code for shaders
This commit is contained in:
+11
-1
@@ -5,6 +5,7 @@
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "logs.h"
|
||||
#include "strings.h"
|
||||
#include "types.h"
|
||||
|
||||
time_t get_file_time(File file) {
|
||||
@@ -64,4 +65,13 @@ File read_file(char *path) {
|
||||
return file;
|
||||
}
|
||||
|
||||
void free_file(File *file) { free(file->content); }
|
||||
void prepend_file(File *src, File extra) {
|
||||
char *old_src_content = src->content;
|
||||
src->content = concat(extra.content, src->content);
|
||||
free(old_src_content);
|
||||
}
|
||||
|
||||
void free_file(File *file) {
|
||||
free(file->content);
|
||||
free(file->path);
|
||||
}
|
||||
@@ -11,4 +11,6 @@ void update_file(File *file);
|
||||
|
||||
void free_file(File *file);
|
||||
|
||||
void prepend_file(File *src, File extra);
|
||||
|
||||
#endif
|
||||
+66
-26
@@ -36,21 +36,33 @@ void compute_fps(Window *window, Timer *timer) {
|
||||
}
|
||||
}
|
||||
|
||||
void loop(Window *window, ShaderProgram program, bool hot_reload,
|
||||
File *fragment_shaders, Timer *timer) {
|
||||
Context context;
|
||||
void hot_reload(ShaderProgram program, File *common_shader_code,
|
||||
File *fragment_shaders) {
|
||||
int i;
|
||||
bool force_update = false;
|
||||
|
||||
if (should_update_file(*common_shader_code)) {
|
||||
update_file(common_shader_code);
|
||||
force_update = true;
|
||||
}
|
||||
|
||||
for (i = 0; i < FRAG_COUNT; i++) {
|
||||
if (force_update || should_update_file(fragment_shaders[i])) {
|
||||
update_file(&fragment_shaders[i]);
|
||||
prepend_file(&fragment_shaders[i], *common_shader_code);
|
||||
update_program(program, fragment_shaders, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void loop(Window *window, ShaderProgram program, bool hr,
|
||||
File *common_shader_code, File *fragment_shaders, Timer *timer) {
|
||||
Context context;
|
||||
|
||||
compute_fps(window, timer);
|
||||
|
||||
if (hot_reload) {
|
||||
// TODO extract to function
|
||||
for (i = 0; i < FRAG_COUNT; i++) {
|
||||
if (should_update_file(fragment_shaders[i])) {
|
||||
update_file(&fragment_shaders[i]);
|
||||
update_program(program, fragment_shaders, i);
|
||||
}
|
||||
}
|
||||
if (hr) {
|
||||
hot_reload(program, common_shader_code, fragment_shaders);
|
||||
}
|
||||
|
||||
context = get_window_context(window);
|
||||
@@ -60,23 +72,53 @@ void loop(Window *window, ShaderProgram program, bool hot_reload,
|
||||
refresh_window(window);
|
||||
}
|
||||
|
||||
File read_fragment_shader_file(char *frag_path, int i) {
|
||||
File fragment_shader;
|
||||
char *file_path = malloc(sizeof(char) * 1024);
|
||||
|
||||
sprintf(file_path, "%s/frag%d.glsl", frag_path, i);
|
||||
fragment_shader = read_file(file_path);
|
||||
if (fragment_shader.error) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return fragment_shader;
|
||||
}
|
||||
|
||||
void init_files(char *frag_path, File *common_shader_code,
|
||||
File *fragment_shaders) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < FRAG_COUNT + 1; i++) {
|
||||
if (i == 0) {
|
||||
(*common_shader_code) = read_fragment_shader_file(frag_path, i);
|
||||
} else {
|
||||
fragment_shaders[i - 1] = read_fragment_shader_file(frag_path, i);
|
||||
|
||||
prepend_file(&fragment_shaders[i - 1], *common_shader_code);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void free_files(File *common_shader_code, File *fragment_shaders) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < FRAG_COUNT; i++) {
|
||||
free_file(&fragment_shaders[i]);
|
||||
}
|
||||
|
||||
free_file(common_shader_code);
|
||||
}
|
||||
|
||||
void forge_run(Parameters params) {
|
||||
File fragment_shaders[FRAG_COUNT];
|
||||
File common_shader_code;
|
||||
ShaderProgram program;
|
||||
Window *window;
|
||||
Timer timer;
|
||||
Context context;
|
||||
int i;
|
||||
char file_path[FRAG_COUNT][1024];
|
||||
|
||||
// TODO extract to function
|
||||
for (i = 0; i < FRAG_COUNT; i++) {
|
||||
sprintf(file_path[i], "%s/frag%d.glsl", params.frag_path, i + 1);
|
||||
fragment_shaders[i] = read_file(file_path[i]);
|
||||
if (fragment_shaders[i].error) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
init_files(params.frag_path, &common_shader_code, fragment_shaders);
|
||||
|
||||
window = init_window(PACKAGE " " VERSION, params.screen, error_callback,
|
||||
key_callback);
|
||||
@@ -93,13 +135,11 @@ void forge_run(Parameters params) {
|
||||
timer = create_timer(60);
|
||||
|
||||
while (!window_should_close(window)) {
|
||||
loop(window, program, params.hot_reload, fragment_shaders, &timer);
|
||||
loop(window, program, params.hot_reload, &common_shader_code,
|
||||
fragment_shaders, &timer);
|
||||
}
|
||||
|
||||
close_window(window, true);
|
||||
|
||||
// TODO extract to function
|
||||
for (i = 0; i < FRAG_COUNT; i++) {
|
||||
free_file(&fragment_shaders[i]);
|
||||
}
|
||||
free_files(&common_shader_code, fragment_shaders);
|
||||
}
|
||||
+1
-3
@@ -256,6 +256,4 @@ void apply_program(ShaderProgram program, Context context) {
|
||||
// draw output
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO clean buffers from opengl memories
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *concat(const char *s1, const char *s2) {
|
||||
char *result =
|
||||
malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
|
||||
// in real code you would check for errors in malloc here
|
||||
strcpy(result, s1);
|
||||
strcat(result, s2);
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef STRINGS_H
|
||||
#define STRINGS_H
|
||||
|
||||
char *concat(const char *s1, const char *s2);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user