diff --git a/src/args.c b/src/args.c index 83e30c4..00c3a1d 100644 --- a/src/args.c +++ b/src/args.c @@ -47,11 +47,12 @@ static char *split_arg_value(char *arg) { static bool is_digit(char c) { return c >= '0' && c <= '9'; } static bool is_number(char *value) { + unsigned long value_len; + unsigned int i; if (value == NULL) { return false; } - unsigned long value_len = strlen(value); - unsigned int i; + value_len = strlen(value); for (i = 0; i < value_len; i++) { if (!is_digit(value[i])) { return false; @@ -72,11 +73,15 @@ static unsigned char parse_uchar(char *arg, char *value) { } Parameters args_parse(int argc, char **argv) { - Parameters params = {0, 0, false}; - + Parameters params; int i; char *arg; char *value; + + params.screen = 0; + params.frag_path = 0; + params.hot_reload = false; + for (i = 1; i < argc; i++) { arg = argv[i]; value = split_arg_value(arg); diff --git a/src/file.c b/src/file.c index d8b2d0e..f769210 100644 --- a/src/file.c +++ b/src/file.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -22,6 +23,9 @@ bool file_should_update(File file) { } void file_update(File *file) { + long length; + FILE *file_pointer; + // free remaining data if (file->content != 0) { free(file->content); @@ -30,9 +34,9 @@ void file_update(File *file) { // init empty file file->content = 0; file->error = false; - long length; + // open file - FILE *file_pointer = fopen(file->path, "rb"); + file_pointer = fopen(file->path, "rb"); if (file_pointer == NULL) { file->error = true; log_error("Cannot open file '%s'", file->path); @@ -61,18 +65,28 @@ void file_update(File *file) { } File file_read(char *path) { - File file = {path, 0, false, 0}; + File file; + + file.path = path; + file.content = NULL; + file.error = false; + file.last_write = 0; + file_update(&file); return file; } void file_prepend(File *src, File extra) { - char *old_src_content = src->content; + char *old_src_content; + + old_src_content = src->content; src->content = strings_concat(extra.content, src->content); free(old_src_content); } -void file_free(File *file) { +void file_free(File *file, bool free_path) { free(file->content); - free(file->path); + if (free_path) { + free(file->path); + } } \ No newline at end of file diff --git a/src/file.h b/src/file.h index 82891e1..48a33b5 100644 --- a/src/file.h +++ b/src/file.h @@ -11,6 +11,6 @@ void file_update(File *file); void file_prepend(File *src, File extra); -void file_free(File *file); +void file_free(File *file, bool free_path); #endif /* FILE_H */ \ No newline at end of file diff --git a/src/forge.c b/src/forge.c index 85c2abc..86e8950 100644 --- a/src/forge.c +++ b/src/forge.c @@ -42,7 +42,9 @@ static int compute_fps(Window *window, Timer *timer) { static void hot_reload(ShaderProgram program, File *common_shader_code, File *fragment_shaders) { int i; - bool force_update = false; + bool force_update; + + force_update = false; if (file_should_update(*common_shader_code)) { file_update(common_shader_code); @@ -78,7 +80,9 @@ static void loop(Window *window, ShaderProgram program, bool hr, File read_fragment_shader_file(char *frag_path, int i) { File fragment_shader; - char *file_path = malloc(sizeof(char) * 1024); + char *file_path; + + file_path = malloc(sizeof(char) * 1024); sprintf(file_path, "%s/frag%d.glsl", frag_path, i); fragment_shader = file_read(file_path); @@ -108,10 +112,10 @@ static void free_files(File *common_shader_code, File *fragment_shaders) { int i; for (i = 0; i < FRAG_COUNT; i++) { - file_free(&fragment_shaders[i]); + file_free(&fragment_shaders[i], true); } - file_free(common_shader_code); + file_free(common_shader_code, true); } void forge_run(Parameters params) { diff --git a/src/main.c b/src/main.c index 56d2f3d..3c45b5f 100644 --- a/src/main.c +++ b/src/main.c @@ -8,8 +8,12 @@ int main(int argc, char **argv) { Parameters params; + params = args_parse(argc, argv); + puts(PACKAGE " " VERSION); + forge_run(params); + return EXIT_SUCCESS; } diff --git a/src/shaders.c b/src/shaders.c index c531dba..93438e7 100644 --- a/src/shaders.c +++ b/src/shaders.c @@ -188,9 +188,11 @@ static void init_single_program(ShaderProgram *program, int i, bool output) { ShaderProgram shaders_init(File *fragment_shaders, Context context) { int i; - ShaderProgram program = {.error = false, - .last_width = context.width, - .last_height = context.height}; + ShaderProgram program; + + program.error = false; + program.last_width = context.width; + program.last_height = context.height; init_textures(&program, context); @@ -233,6 +235,7 @@ void shaders_update(ShaderProgram program, File *fragment_shaders, int i) { void shaders_apply(ShaderProgram program, Context context) { int i, j; GLuint subroutines[3]; + vec2 resolution; // viewport changed if (context.width != program.last_width || @@ -248,7 +251,8 @@ void shaders_apply(ShaderProgram program, Context context) { } } - vec2 resolution = {(float)context.width, (float)context.height}; + resolution[0] = (float)context.width; + resolution[1] = (float)context.height; for (i = 0; i < FRAG_COUNT + 1; i++) { // use specific shader program diff --git a/src/strings.c b/src/strings.c index 011bc91..ddf11e9 100644 --- a/src/strings.c +++ b/src/strings.c @@ -2,10 +2,12 @@ #include char *strings_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 + char *result; + + result = malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator + strcpy(result, s1); strcat(result, s2); + return result; } \ No newline at end of file diff --git a/src/timer.c b/src/timer.c index 67f3aa2..78980a0 100644 --- a/src/timer.c +++ b/src/timer.c @@ -3,10 +3,10 @@ #include "types.h" Timer timer_init(const unsigned int target) { - Timer output = { - .counter = 0, - .target = target, - }; + Timer output; + + output.counter = 0; + output.target = target; gettimeofday(&output.start, NULL); @@ -21,11 +21,15 @@ bool timer_inc(Timer *timer) { double timer_reset(Timer *timer) { struct timeval stop; double secs, per_secs; + gettimeofday(&stop, NULL); + secs = (double)(stop.tv_usec - timer->start.tv_usec) / 1000000 + (double)(stop.tv_sec - timer->start.tv_sec); per_secs = ((float)timer->counter) / secs; + timer->start = stop; timer->counter = 0; + return per_secs; } \ No newline at end of file diff --git a/src/window.c b/src/window.c index fcc32ca..048d930 100644 --- a/src/window.c +++ b/src/window.c @@ -33,9 +33,11 @@ static void init_glfw(void (*error_callback)(int, const char *)) { } static GLFWmonitor *get_monitor(unsigned char monitor_index) { - // detect monitors int count; - GLFWmonitor **monitors = glfwGetMonitors(&count); + GLFWmonitor **monitors; + + // detect monitors + monitors = glfwGetMonitors(&count); // check selected monitor availability if (monitor_index >= count) { @@ -51,6 +53,7 @@ static GLFWmonitor *get_monitor(unsigned char monitor_index) { static GLFWwindow *create_window(GLFWmonitor *monitor, char *title, void (*key_callback)(Window *, int, int, int, int)) { + GLFWwindow *window; log_info("[GLFW] Creating window..."); @@ -61,7 +64,7 @@ static GLFWwindow *create_window(GLFWmonitor *monitor, char *title, glfwWindowHint(GLFW_FOCUS_ON_SHOW, GLFW_FALSE); // create fullscreen window in selected monitor - GLFWwindow *window = glfwCreateWindow(1, 1, title, monitor, NULL); + window = glfwCreateWindow(1, 1, title, monitor, NULL); // handle window creation fail if (window == NULL) {