begin adding options

added the basic framework of how options are loaded.
This commit is contained in:
2025-04-18 21:38:09 +02:00
parent 45d4f91bd9
commit 45e5ec9764
4 changed files with 32 additions and 2 deletions

21
src/game/opts.c Normal file
View File

@@ -0,0 +1,21 @@
#include "../error.h"
#include "../util/compat.h"
#include "paths.h"
/* attempts to load the options,
returns 1 upon failure */
int load_opts(void) {
if (!path_opts) return 1;
if (!faccess(path_opts, FA_W | FA_R)) {
error("attempted to load opts file, but the given path wasn't writable");
return 1;
}
// TODO: load opts from the file
// the options shall be loaded as key-value-pairs
// lines starting with # are seen as comments
// the keys are stored on the left size, and the values on the right.
// if a field couldn't be parsed the program is not allowed to fail. I'd say we fix the field for the user.
return 0;
}

5
src/game/opts.h Normal file
View File

@@ -0,0 +1,5 @@
#pragma once
#include <stdint.h>
int load_opts(void);

View File

@@ -6,6 +6,7 @@
#include "../util/compat.h" #include "../util/compat.h"
char const* restrict path_dat = NULL; char const* restrict path_dat = NULL;
char const* restrict path_opts = NULL;
char const* restrict path_font = NULL; char const* restrict path_font = NULL;
char const* restrict path_music = NULL; char const* restrict path_music = NULL;
char const* restrict path_place_sfx = NULL; char const* restrict path_place_sfx = NULL;
@@ -52,14 +53,16 @@ int paths_init(void) {
if (!len) return 1; if (!len) return 1;
// these are explicitly static, as string literals just work like that // these are explicitly static, as string literals just work like that
path_font = init_path("pixeldroid_botic-regular.ttf", len + 28); path_opts = init_path("opts", len + 4); // TODO: shouldn't opts be stored at .config/?
path_font = init_path("pixeldroid_botic-regular.ttf", len + 28); // TODO: these three paths should not be stored like opts
path_music = init_path("korobeiniki.wav", len + 15); path_music = init_path("korobeiniki.wav", len + 15);
path_place_sfx = init_path("place.wav", len + 9); path_place_sfx = init_path("place.wav", len + 9);
return -(!path_font || !path_music || !path_place_sfx); return -(!path_opts || !path_font || !path_music || !path_place_sfx);
} }
void paths_free(void) { void paths_free(void) {
free((void*)path_dat), path_dat = NULL; free((void*)path_dat), path_dat = NULL;
free((void*)path_opts), path_opts = NULL;
free((void*)path_music), path_music = NULL; free((void*)path_music), path_music = NULL;
free((void*)path_place_sfx), path_place_sfx = NULL; free((void*)path_place_sfx), path_place_sfx = NULL;
} }

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
extern char const* restrict path_dat; extern char const* restrict path_dat;
extern char const* restrict path_opts;
extern char const* restrict path_font; extern char const* restrict path_font;
extern char const* restrict path_music; extern char const* restrict path_music;
extern char const* restrict path_place_sfx; extern char const* restrict path_place_sfx;