diff options
| author | yuzu-eva <cafebabe@disroot.org> | 2025-06-08 19:35:33 +0200 |
|---|---|---|
| committer | yuzu-eva <cafebabe@disroot.org> | 2025-06-08 19:35:33 +0200 |
| commit | cf09cfbe0a4769823d30f337e7191aca3eaff651 (patch) | |
| tree | dfdf46b18c3fd44d13e6311eff3191d38191f6c7 /src/plug.c | |
| parent | 14d8943fb2eafc74259784cd8dfdc39b24e632d0 (diff) | |
abstracted loading of assets into func get_asset_path
Diffstat (limited to 'src/plug.c')
| -rw-r--r-- | src/plug.c | 74 |
1 files changed, 34 insertions, 40 deletions
@@ -45,6 +45,37 @@ float out_smear[SAMPLE_SIZE]; // char render_option = 'b'; +char *get_asset_path(const char *asset, const char *type) +{ + char *asset_path = malloc(sizeof(char) * PATH_MAX); + char exe_path[PATH_MAX]; + char exe_dir[PATH_MAX]; + ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path)); + if (len == -1) { + perror("readlink"); + exit(EXIT_FAILURE); + } + exe_path[len] = '\0'; + + strncpy(exe_dir, exe_path, sizeof(exe_dir)); + exe_dir[sizeof(exe_dir) - 1] = '\0'; + char *dir = dirname(exe_dir); + + char rel_asset_base[PATH_MAX]; + snprintf(rel_asset_base, sizeof(rel_asset_base), "%s/../share/%s", dir, type); + + char asset_base[PATH_MAX]; + if (!realpath(rel_asset_base, asset_base)) { + fprintf(stderr, "ERROR: realpath failed for asset %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + if ((size_t)snprintf(asset_path, PATH_MAX, "%s/%s", asset_base, asset) >= PATH_MAX) { + fprintf(stderr, "Asset path truncated -- path too long\n"); + exit(EXIT_FAILURE); + } + return asset_path; +} + void fft(float in[], size_t stride, float complex out[], size_t n) { assert(n > 0); @@ -313,50 +344,13 @@ void plug_post_reload(Plug *prev) void plug_init(void) { - char exe_path[PATH_MAX]; - char exe_dir[PATH_MAX]; - ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path)); - if (len == -1) { - perror("readlink"); - exit(EXIT_FAILURE); - } - exe_path[len] = '\0'; - - strncpy(exe_dir, exe_path, sizeof(exe_dir)); - exe_dir[sizeof(exe_dir) - 1] = '\0'; - char *dir = dirname(exe_dir); - - char rel_shader_base[PATH_MAX]; - char rel_font_base[PATH_MAX]; - snprintf(rel_shader_base, sizeof(rel_shader_base), "%s/../share/shaders", dir); - snprintf(rel_font_base, sizeof(rel_font_base), "%s/../share/fonts", dir); - - char shader_base[PATH_MAX]; - char font_base[PATH_MAX]; - - if (!realpath(rel_shader_base, shader_base)) { - fprintf(stderr, "ERROR: realpath failed for shaders %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - if (!realpath(rel_font_base, font_base)) { - fprintf(stderr, "ERROR: realpath failed for fonts %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - - if ((size_t)snprintf(circle_shader, sizeof(circle_shader), "%s/circles.fs", shader_base) >= sizeof(circle_shader) || - (size_t)snprintf(smear_shader, sizeof(smear_shader), "%s/smear.fs", shader_base) >= sizeof(smear_shader) || - (size_t)snprintf(font, sizeof(font), "%s/AlegreyaSans-Regular.ttf", font_base) >= sizeof(font)) { - fprintf(stderr, "Asset path truncated -- path too long\n"); - exit(EXIT_FAILURE); - } - plug = malloc(sizeof(*plug)); assert(plug != NULL && "Not enough RAM!"); memset(plug, 0, sizeof(*plug)); - plug->font = LoadFontEx(font, FONT_SIZE, NULL, 0); - plug->circle = LoadShader(NULL, circle_shader); - plug->smear = LoadShader(NULL, smear_shader); + plug->font = LoadFontEx(get_asset_path("AlegreyaSans-Regular.ttf", "fonts"), FONT_SIZE, NULL, 0); + plug->circle = LoadShader(NULL, get_asset_path("circles.fs", "shaders")); + plug->smear = LoadShader(NULL, get_asset_path("smear.fs", "shaders")); plug->audio_volume = 0.5f; plug->render_option = 'b'; } |
