From abc5ecfa342a278faa4b35147b3900cb647dce45 Mon Sep 17 00:00:00 2001 From: klemek Date: Thu, 18 Sep 2025 13:23:17 +0200 Subject: [PATCH] clarify code --- README.md | 3 +-- src/args.c | 18 ++++++------- src/args.h | 4 +-- src/config.h | 12 ++++----- src/constants.h | 2 +- src/file.c | 21 ++++++++------- src/file.h | 12 ++++----- src/forge.c | 69 +++++++++++++++++++++++++------------------------ src/forge.h | 2 +- src/logs.h | 2 +- src/main.c | 2 +- src/main.h | 2 +- src/shaders.c | 18 ++++++------- src/shaders.h | 8 +++--- src/strings.c | 2 +- src/strings.h | 4 +-- src/timer.c | 6 ++--- src/timer.h | 8 +++--- src/types.h | 2 +- src/window.c | 25 +++++++++--------- src/window.h | 14 +++++----- 21 files changed, 119 insertions(+), 117 deletions(-) diff --git a/README.md b/README.md index 7f4510a..32f4f52 100644 --- a/README.md +++ b/README.md @@ -103,9 +103,8 @@ make -f Makefile.dev release-arch - [x] Test 2 stages with render to texture - [x] 2 in 2 fx 1 mix 1 fx layout - [x] Include common code - - [ ] 16 input + 16 fx definition and selection (with param) + - [ ] 16 input + 16 fx definition and selection (with const param) - [x] Feedback texture - - [ ] Free opengl memory - [ ] Clean code - [ ] Midi - [ ] Read Midi events diff --git a/src/args.c b/src/args.c index d20cc56..83e30c4 100644 --- a/src/args.c +++ b/src/args.c @@ -7,7 +7,7 @@ #include "config.h" #include "logs.h" -void print_help(int status_code) { +static void print_help(int status_code) { puts(PACKAGE " " VERSION "\n\n" "usage: " PACKAGE " " @@ -27,26 +27,26 @@ void print_help(int status_code) { exit(status_code); } -void invalid_arg(char *arg) { +static void invalid_arg(char *arg) { log_error("invalid argument: '%s'", arg); print_help(EXIT_FAILURE); } -void invalid_value(char *arg, char *value) { +static void invalid_value(char *arg, char *value) { log_error("invalid value for argument '%s': '%s'", arg, value); print_help(EXIT_FAILURE); } -bool is_arg(char *arg, char *ref) { return strcoll(arg, ref) == 0; } +static bool is_arg(char *arg, char *ref) { return strcoll(arg, ref) == 0; } -char *split_arg_value(char *arg) { +static char *split_arg_value(char *arg) { strtok(arg, "="); return strtok(NULL, "="); } -bool is_digit(char c) { return c >= '0' && c <= '9'; } +static bool is_digit(char c) { return c >= '0' && c <= '9'; } -bool is_number(char *value) { +static bool is_number(char *value) { if (value == NULL) { return false; } @@ -60,7 +60,7 @@ bool is_number(char *value) { return true; } -unsigned char parse_uchar(char *arg, char *value) { +static unsigned char parse_uchar(char *arg, char *value) { if (!is_number(value)) { invalid_value(arg, value); } @@ -71,7 +71,7 @@ unsigned char parse_uchar(char *arg, char *value) { return (unsigned char)tmp_value; } -Parameters parse_args(int argc, char **argv) { +Parameters args_parse(int argc, char **argv) { Parameters params = {0, 0, false}; int i; diff --git a/src/args.h b/src/args.h index 85774b7..2d160ba 100644 --- a/src/args.h +++ b/src/args.h @@ -3,6 +3,6 @@ #ifndef ARGS_H #define ARGS_H -Parameters parse_args(int argc, char **argv); +Parameters args_parse(int argc, char **argv); -#endif \ No newline at end of file +#endif /* ARGS_H */ \ No newline at end of file diff --git a/src/config.h b/src/config.h index b5c8a3f..fa35645 100644 --- a/src/config.h +++ b/src/config.h @@ -3,22 +3,22 @@ #ifndef PACKAGE #define PACKAGE "forge" -#endif +#endif /* PACKAGE */ #ifndef VERSION #define VERSION "(dev)" -#endif +#endif /* VERSION */ #ifndef FRAG_COUNT #define FRAG_COUNT 6 -#endif +#endif /* FRAG_COUNT */ #ifndef TEX_COUNT #define TEX_COUNT 8 -#endif +#endif /* TEXT_COUNT */ #ifndef SUB_COUNT #define SUB_COUNT 16 -#endif +#endif /* SUB_COUNT */ -#endif \ No newline at end of file +#endif /* CONFIG_H */ \ No newline at end of file diff --git a/src/constants.h b/src/constants.h index 843d6ba..f1524b6 100644 --- a/src/constants.h +++ b/src/constants.h @@ -29,4 +29,4 @@ static const Vertex vertices[6] = {{{0.0f, 0.0f}}, {{0.0f, 1.0f}}, {{1.0f, 1.0f}}, {{0.0f, 0.0f}}, {{1.0f, 1.0f}}, {{1.0f, 0.0f}}}; -#endif \ No newline at end of file +#endif /* CONSTANTS_H */ \ No newline at end of file diff --git a/src/file.c b/src/file.c index 9cf0542..d8b2d0e 100644 --- a/src/file.c +++ b/src/file.c @@ -3,12 +3,13 @@ #include #include #include +#include #include "logs.h" #include "strings.h" #include "types.h" -time_t get_file_time(File file) { +static time_t get_file_time(File file) { struct stat attr; if (stat(file.path, &attr) == 0) { return attr.st_mtim.tv_sec; @@ -16,11 +17,11 @@ time_t get_file_time(File file) { return 0; } -bool should_update_file(File file) { +bool file_should_update(File file) { return file.last_write != get_file_time(file); } -void update_file(File *file) { +void file_update(File *file) { // free remaining data if (file->content != 0) { free(file->content); @@ -32,7 +33,7 @@ void update_file(File *file) { long length; // open file FILE *file_pointer = fopen(file->path, "rb"); - if (!file_pointer) { + if (file_pointer == NULL) { file->error = true; log_error("Cannot open file '%s'", file->path); return; @@ -43,7 +44,7 @@ void update_file(File *file) { // init buffer fseek(file_pointer, 0, SEEK_SET); file->content = (char *)malloc((length + 1) * sizeof(char)); - if (!file->content) { + if (file->content == NULL) { file->error = true; fclose(file_pointer); log_error("Cannot read file '%s'", file->path); @@ -59,19 +60,19 @@ void update_file(File *file) { file->last_write = get_file_time(*file); } -File read_file(char *path) { +File file_read(char *path) { File file = {path, 0, false, 0}; - update_file(&file); + file_update(&file); return file; } -void prepend_file(File *src, File extra) { +void file_prepend(File *src, File extra) { char *old_src_content = src->content; - src->content = concat(extra.content, src->content); + src->content = strings_concat(extra.content, src->content); free(old_src_content); } -void free_file(File *file) { +void file_free(File *file) { free(file->content); free(file->path); } \ No newline at end of file diff --git a/src/file.h b/src/file.h index be5170b..82891e1 100644 --- a/src/file.h +++ b/src/file.h @@ -3,14 +3,14 @@ #ifndef FILE_H #define FILE_H -File read_file(char *path); +File file_read(char *path); -bool should_update_file(File file); +bool file_should_update(File file); -void update_file(File *file); +void file_update(File *file); -void free_file(File *file); +void file_prepend(File *src, File extra); -void prepend_file(File *src, File extra); +void file_free(File *file); -#endif \ No newline at end of file +#endif /* FILE_H */ \ No newline at end of file diff --git a/src/forge.c b/src/forge.c index 2752b98..85c2abc 100644 --- a/src/forge.c +++ b/src/forge.c @@ -11,9 +11,9 @@ #include "types.h" #include "window.h" -void error_callback(int error, const char *description) { +static void error_callback(int error, const char *description) { log_error("[GLFW] %d: %s", error, description); - close_window(0, true); + window_close(0, true); exit(EXIT_FAILURE); } @@ -21,58 +21,59 @@ static void key_callback(Window *window, int key, __attribute__((unused)) int scancode, int action, __attribute__((unused)) int mods) { // close window on escape key - if (escape_key(key, action)) { - close_window(window, false); + if (window_escape_key(key, action)) { + window_close(window, false); } } -int compute_fps(Window *window, Timer *timer) { +static int compute_fps(Window *window, Timer *timer) { static double fps; char title[100]; - if (inc_timer(timer)) { - fps = reset_and_count(timer); + if (timer_inc(timer)) { + fps = timer_reset(timer); sprintf(title, PACKAGE " " VERSION " - %.0ffps", fps); - update_window_title(window, title); + window_update_title(window, title); } return (int)round(fps); } -void hot_reload(ShaderProgram program, File *common_shader_code, - File *fragment_shaders) { +static 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); + if (file_should_update(*common_shader_code)) { + file_update(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); + if (force_update || file_should_update(fragment_shaders[i])) { + file_update(&fragment_shaders[i]); + file_prepend(&fragment_shaders[i], *common_shader_code); + shaders_update(program, fragment_shaders, i); } } } -void loop(Window *window, ShaderProgram program, bool hr, - File *common_shader_code, File *fragment_shaders, Timer *timer) { +static void loop(Window *window, ShaderProgram program, bool hr, + File *common_shader_code, File *fragment_shaders, + Timer *timer) { Context context; if (hr) { hot_reload(program, common_shader_code, fragment_shaders); } - context = get_window_context(window); + context = window_get_context(window); context.fps = compute_fps(window, timer); - apply_program(program, context); + shaders_apply(program, context); - refresh_window(window); + window_refresh(window); } File read_fragment_shader_file(char *frag_path, int i) { @@ -80,7 +81,7 @@ File read_fragment_shader_file(char *frag_path, int i) { char *file_path = malloc(sizeof(char) * 1024); sprintf(file_path, "%s/frag%d.glsl", frag_path, i); - fragment_shader = read_file(file_path); + fragment_shader = file_read(file_path); if (fragment_shader.error) { exit(EXIT_FAILURE); } @@ -88,8 +89,8 @@ File read_fragment_shader_file(char *frag_path, int i) { return fragment_shader; } -void init_files(char *frag_path, File *common_shader_code, - File *fragment_shaders) { +static void init_files(char *frag_path, File *common_shader_code, + File *fragment_shaders) { int i; for (i = 0; i < FRAG_COUNT + 1; i++) { @@ -98,19 +99,19 @@ void init_files(char *frag_path, File *common_shader_code, } else { fragment_shaders[i - 1] = read_fragment_shader_file(frag_path, i); - prepend_file(&fragment_shaders[i - 1], *common_shader_code); + file_prepend(&fragment_shaders[i - 1], *common_shader_code); } } } -void free_files(File *common_shader_code, File *fragment_shaders) { +static void free_files(File *common_shader_code, File *fragment_shaders) { int i; for (i = 0; i < FRAG_COUNT; i++) { - free_file(&fragment_shaders[i]); + file_free(&fragment_shaders[i]); } - free_file(common_shader_code); + file_free(common_shader_code); } void forge_run(Parameters params) { @@ -123,26 +124,26 @@ void forge_run(Parameters params) { init_files(params.frag_path, &common_shader_code, fragment_shaders); - window = init_window(PACKAGE " " VERSION, params.screen, error_callback, + window = window_init(PACKAGE " " VERSION, params.screen, error_callback, key_callback); - context = get_window_context(window); + context = window_get_context(window); - program = init_program(fragment_shaders, context); + program = shaders_init(fragment_shaders, context); if (program.error) { - close_window(window, true); + window_close(window, true); exit(EXIT_FAILURE); } - timer = create_timer(30); + timer = timer_init(30); while (!window_should_close(window)) { loop(window, program, params.hot_reload, &common_shader_code, fragment_shaders, &timer); } - close_window(window, true); + window_close(window, true); free_files(&common_shader_code, fragment_shaders); } \ No newline at end of file diff --git a/src/forge.h b/src/forge.h index fe46c86..7d07457 100644 --- a/src/forge.h +++ b/src/forge.h @@ -5,4 +5,4 @@ void forge_run(Parameters params); -#endif \ No newline at end of file +#endif /* FORGE_H */ \ No newline at end of file diff --git a/src/logs.h b/src/logs.h index 1b97368..fc7c592 100644 --- a/src/logs.h +++ b/src/logs.h @@ -26,4 +26,4 @@ fprintf(stderr, ANSI_COLOR_RED "[FAIL] " format ANSI_COLOR_RESET \ "\n" __VA_OPT__(, ) __VA_ARGS__) -#endif \ No newline at end of file +#endif /* LOG_H */ \ No newline at end of file diff --git a/src/main.c b/src/main.c index 992bf9b..56d2f3d 100644 --- a/src/main.c +++ b/src/main.c @@ -8,7 +8,7 @@ int main(int argc, char **argv) { Parameters params; - params = parse_args(argc, argv); + params = args_parse(argc, argv); puts(PACKAGE " " VERSION); forge_run(params); return EXIT_SUCCESS; diff --git a/src/main.h b/src/main.h index b0790ce..1a48588 100644 --- a/src/main.h +++ b/src/main.h @@ -1,4 +1,4 @@ #ifndef MAIN_H #define MAIN_H -#endif \ No newline at end of file +#endif /* MAIN_H */ \ No newline at end of file diff --git a/src/shaders.c b/src/shaders.c index 3d53509..c531dba 100644 --- a/src/shaders.c +++ b/src/shaders.c @@ -7,7 +7,7 @@ #include "logs.h" #include "types.h" -bool compile_shader(GLuint shader_id, char *name, char *source_code) { +static bool compile_shader(GLuint shader_id, char *name, char *source_code) { GLint status_params; char log[1024]; @@ -32,7 +32,7 @@ bool compile_shader(GLuint shader_id, char *name, char *source_code) { return status_params == GL_TRUE; } -void init_textures(ShaderProgram *program, Context context) { +static void init_textures(ShaderProgram *program, Context context) { int i; glGenTextures(TEX_COUNT, program->textures); @@ -55,7 +55,7 @@ void init_textures(ShaderProgram *program, Context context) { } } -void init_framebuffers(ShaderProgram *program) { +static void init_framebuffers(ShaderProgram *program) { int i, j; glGenFramebuffers(FRAG_COUNT, program->frame_buffers); @@ -86,7 +86,7 @@ void init_framebuffers(ShaderProgram *program) { return; } -void init_vertices(ShaderProgram *program) { +static void init_vertices(ShaderProgram *program) { // create vertex buffer and setup vertices glGenBuffers(1, &program->vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, program->vertex_buffer); @@ -97,7 +97,7 @@ void init_vertices(ShaderProgram *program) { glBindVertexArray(program->vertex_array); } -void init_shaders(ShaderProgram *program, File *fragment_shaders) { +static void init_shaders(ShaderProgram *program, File *fragment_shaders) { int i; // compile vertex shader @@ -124,7 +124,7 @@ void init_shaders(ShaderProgram *program, File *fragment_shaders) { } } -void init_single_program(ShaderProgram *program, int i, bool output) { +static void init_single_program(ShaderProgram *program, int i, bool output) { int j; char name[32]; @@ -186,7 +186,7 @@ void init_single_program(ShaderProgram *program, int i, bool output) { log_success("Program %d initialized", i + 1); } -ShaderProgram init_program(File *fragment_shaders, Context context) { +ShaderProgram shaders_init(File *fragment_shaders, Context context) { int i; ShaderProgram program = {.error = false, .last_width = context.width, @@ -217,7 +217,7 @@ ShaderProgram init_program(File *fragment_shaders, Context context) { return program; } -void update_program(ShaderProgram program, File *fragment_shaders, int i) { +void shaders_update(ShaderProgram program, File *fragment_shaders, int i) { bool result; result = compile_shader(program.fragment_shaders[i], fragment_shaders[i].path, @@ -230,7 +230,7 @@ void update_program(ShaderProgram program, File *fragment_shaders, int i) { } } -void apply_program(ShaderProgram program, Context context) { +void shaders_apply(ShaderProgram program, Context context) { int i, j; GLuint subroutines[3]; diff --git a/src/shaders.h b/src/shaders.h index 71fd996..ead6118 100644 --- a/src/shaders.h +++ b/src/shaders.h @@ -3,10 +3,10 @@ #ifndef SHADERS_H #define SHADERS_H -ShaderProgram init_program(File *fragment_shader, Context context); +ShaderProgram shaders_init(File *fragment_shader, Context context); -void update_program(ShaderProgram program, File *fragment_shaders, int i); +void shaders_update(ShaderProgram program, File *fragment_shaders, int i); -void apply_program(ShaderProgram program, Context context); +void shaders_apply(ShaderProgram program, Context context); -#endif \ No newline at end of file +#endif /* SHADERS_H */ \ No newline at end of file diff --git a/src/strings.c b/src/strings.c index 6f7dd49..011bc91 100644 --- a/src/strings.c +++ b/src/strings.c @@ -1,7 +1,7 @@ #include #include -char *concat(const char *s1, const char *s2) { +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 diff --git a/src/strings.h b/src/strings.h index c7e907a..e9f9432 100644 --- a/src/strings.h +++ b/src/strings.h @@ -1,6 +1,6 @@ #ifndef STRINGS_H #define STRINGS_H -char *concat(const char *s1, const char *s2); +char *strings_concat(const char *s1, const char *s2); -#endif \ No newline at end of file +#endif /* STRINGS_H */ \ No newline at end of file diff --git a/src/timer.c b/src/timer.c index e615964..67f3aa2 100644 --- a/src/timer.c +++ b/src/timer.c @@ -2,7 +2,7 @@ #include "types.h" -Timer create_timer(const unsigned int target) { +Timer timer_init(const unsigned int target) { Timer output = { .counter = 0, .target = target, @@ -13,12 +13,12 @@ Timer create_timer(const unsigned int target) { return output; } -bool inc_timer(Timer *timer) { +bool timer_inc(Timer *timer) { timer->counter += 1; return timer->counter >= timer->target; } -double reset_and_count(Timer *timer) { +double timer_reset(Timer *timer) { struct timeval stop; double secs, per_secs; gettimeofday(&stop, NULL); diff --git a/src/timer.h b/src/timer.h index c24a79f..b9d0309 100644 --- a/src/timer.h +++ b/src/timer.h @@ -3,10 +3,10 @@ #ifndef TIMER_H #define TIMER_H -Timer create_timer(const unsigned int target); +Timer timer_init(const unsigned int target); -bool inc_timer(Timer *timer); +bool timer_inc(Timer *timer); -double reset_and_count(Timer *timer); +double timer_reset(Timer *timer); -#endif \ No newline at end of file +#endif /* TIMER_H */ \ No newline at end of file diff --git a/src/types.h b/src/types.h index a8b21e6..f648e5c 100644 --- a/src/types.h +++ b/src/types.h @@ -77,4 +77,4 @@ typedef struct Timer { unsigned int target; } Timer; -#endif \ No newline at end of file +#endif /* TYPES_H */ \ No newline at end of file diff --git a/src/window.c b/src/window.c index 5c6e7f6..fcc32ca 100644 --- a/src/window.c +++ b/src/window.c @@ -9,7 +9,7 @@ #define GLAD_GL_IMPLEMENTATION #include -void init_glfw(void (*error_callback)(int, const char *)) { +static void init_glfw(void (*error_callback)(int, const char *)) { log_info("[GLFW] Initializing..."); // set errors handler @@ -32,7 +32,7 @@ void init_glfw(void (*error_callback)(int, const char *)) { log_success("[GLFS] Initialized..."); } -GLFWmonitor *get_monitor(unsigned char monitor_index) { +static GLFWmonitor *get_monitor(unsigned char monitor_index) { // detect monitors int count; GLFWmonitor **monitors = glfwGetMonitors(&count); @@ -48,8 +48,9 @@ GLFWmonitor *get_monitor(unsigned char monitor_index) { return monitors[monitor_index]; } -GLFWwindow *create_window(GLFWmonitor *monitor, char *title, - void (*key_callback)(Window *, int, int, int, int)) { +static GLFWwindow *create_window(GLFWmonitor *monitor, char *title, + void (*key_callback)(Window *, int, int, int, + int)) { log_info("[GLFW] Creating window..."); @@ -63,7 +64,7 @@ GLFWwindow *create_window(GLFWmonitor *monitor, char *title, GLFWwindow *window = glfwCreateWindow(1, 1, title, monitor, NULL); // handle window creation fail - if (!window) { + if (window == NULL) { log_error("[GLFW] Window or context creation failed"); glfwTerminate(); exit(EXIT_FAILURE); @@ -79,7 +80,7 @@ GLFWwindow *create_window(GLFWmonitor *monitor, char *title, return window; } -void use_window(GLFWwindow *window) { +static void use_window(GLFWwindow *window) { // use current window glfwMakeContextCurrent(window); // link GLAD and GLFW window @@ -88,7 +89,7 @@ void use_window(GLFWwindow *window) { glfwSwapInterval(1); } -Window *init_window(char *title, unsigned char monitor_index, +Window *window_init(char *title, unsigned char monitor_index, void (*error_callback)(int, const char *), void (*key_callback)(Window *, int, int, int, int)) { GLFWwindow *window; @@ -105,18 +106,18 @@ Window *init_window(char *title, unsigned char monitor_index, return window; } -void update_window_title(Window *window, char *title) { +void window_update_title(Window *window, char *title) { glfwSetWindowTitle(window, title); } -void refresh_window(Window *window) { +void window_refresh(Window *window) { // swap front and back buffers glfwSwapBuffers(window); // listen to mouse and keyboard events glfwPollEvents(); } -Context get_window_context(Window *window) { +Context window_get_context(Window *window) { Context context; glfwGetFramebufferSize(window, &context.width, &context.height); @@ -126,7 +127,7 @@ Context get_window_context(Window *window) { return context; } -void close_window(Window *window, bool hard) { +void window_close(Window *window, bool hard) { if (hard) { log_info("[GLFW] Terminating library..."); glfwTerminate(); @@ -140,6 +141,6 @@ bool window_should_close(Window *window) { return glfwWindowShouldClose(window) == GLFW_TRUE; } -bool escape_key(int key, int action) { +bool window_escape_key(int key, int action) { return key == GLFW_KEY_ESCAPE && action == GLFW_PRESS; } \ No newline at end of file diff --git a/src/window.h b/src/window.h index 3a41a27..fa850e3 100644 --- a/src/window.h +++ b/src/window.h @@ -3,20 +3,20 @@ #ifndef WINDOW_H #define WINDOW_H -Window *init_window(char *title, unsigned char monitor_index, +Window *window_init(char *title, unsigned char monitor_index, void (*error_callback)(int, const char *), void (*key_callback)(Window *, int, int, int, int)); -void update_window_title(Window *window, char *title); +void window_update_title(Window *window, char *title); -void refresh_window(Window *window); +void window_refresh(Window *window); -Context get_window_context(Window *window); +Context window_get_context(Window *window); -void close_window(Window *window, bool hard); +void window_close(Window *window, bool hard); bool window_should_close(Window *window); -bool escape_key(int key, int action); +bool window_escape_key(int key, int action); -#endif \ No newline at end of file +#endif /* WINDOW_H */ \ No newline at end of file