clarify code

This commit is contained in:
2025-09-18 13:23:17 +02:00
parent ee8953fea0
commit abc5ecfa34
21 changed files with 119 additions and 117 deletions
+1 -2
View File
@@ -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
+9 -9
View File
@@ -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;
+2 -2
View File
@@ -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
#endif /* ARGS_H */
+6 -6
View File
@@ -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
#endif /* CONFIG_H */
+1 -1
View File
@@ -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
#endif /* CONSTANTS_H */
+11 -10
View File
@@ -3,12 +3,13 @@
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#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);
}
+6 -6
View File
@@ -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
#endif /* FILE_H */
+35 -34
View File
@@ -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);
}
+1 -1
View File
@@ -5,4 +5,4 @@
void forge_run(Parameters params);
#endif
#endif /* FORGE_H */
+1 -1
View File
@@ -26,4 +26,4 @@
fprintf(stderr, ANSI_COLOR_RED "[FAIL] " format ANSI_COLOR_RESET \
"\n" __VA_OPT__(, ) __VA_ARGS__)
#endif
#endif /* LOG_H */
+1 -1
View File
@@ -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;
+1 -1
View File
@@ -1,4 +1,4 @@
#ifndef MAIN_H
#define MAIN_H
#endif
#endif /* MAIN_H */
+9 -9
View File
@@ -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];
+4 -4
View File
@@ -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
#endif /* SHADERS_H */
+1 -1
View File
@@ -1,7 +1,7 @@
#include <stdlib.h>
#include <string.h>
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
+2 -2
View File
@@ -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
#endif /* STRINGS_H */
+3 -3
View File
@@ -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);
+4 -4
View File
@@ -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
#endif /* TIMER_H */
+1 -1
View File
@@ -77,4 +77,4 @@ typedef struct Timer {
unsigned int target;
} Timer;
#endif
#endif /* TYPES_H */
+13 -12
View File
@@ -9,7 +9,7 @@
#define GLAD_GL_IMPLEMENTATION
#include <glad/gl.h>
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;
}
+7 -7
View File
@@ -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
#endif /* WINDOW_H */