summaryrefslogtreecommitdiff
path: root/src/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/config.c b/src/config.c
new file mode 100644
index 0000000..9a673ee
--- /dev/null
+++ b/src/config.c
@@ -0,0 +1,95 @@
+#include "ini.h"
+
+Config cfg = {0};
+
+#define expect(X) if (!(X)) { \
+ perr("expect failed: "STR(X)); \
+ if (ini != NULL) ini_free(ini); \
+ config_deinit(); \
+}
+
+void
+config_init(const char *path)
+{
+ ini_t *ini = NULL;
+
+ ini = ini_load(path);
+ expect(ini != NULL);
+
+ const char *tileset = ini_get(ini, "tileset", "path");
+ if (tileset == NULL) {
+ pwrn("[tileset] path unspecified, using default");
+ tileset = "tileset.png";
+ }
+ cfg.tileset_path = alloc(strlen(tileset) + 1);
+ expect(cfg.tileset_path != NULL);
+ strcpy(cfg.tileset_path, tileset);
+
+ const char *world_path = ini_get(ini, "world", "path");
+ if (world_path == NULL) {
+ pwrn("[world] path unspecified, using default");
+ world_path = "world.csv";
+ }
+ cfg.world_path = alloc(strlen(world_path) + 1);
+ expect(cfg.world_path != NULL);
+ strcpy(cfg.world_path, world_path);
+
+ with (tile_width, ini_get(ini, "tile", "width")) {
+ cfg.tile_width = atoi(tile_width);
+ } else {
+ pwrn("[tile] width unspecified, using default");
+ cfg.tile_width = 16;
+ }
+ expect(cfg.tile_width > 0);
+
+ with (tile_height, ini_get(ini, "tile", "height")) {
+ cfg.tile_height = atoi(tile_height);
+ } else {
+ pwrn("[tile] height unspecified, using default");
+ cfg.tile_height = 16;
+ }
+ expect(cfg.tile_height > 0);
+
+ with (cell_width, ini_get(ini, "cell", "width")) {
+ cfg.cell_width = atoi(cell_width);
+ } else {
+ pwrn("[cell] width unspecified, using default");
+ cfg.cell_width = 25;
+ }
+ expect(cfg.cell_width > 0);
+
+ with (cell_height, ini_get(ini, "cell", "height")) {
+ cfg.cell_height = atoi(cell_height);
+ } else {
+ pwrn("[cell] height unspecified, using default");
+ cfg.cell_height = 14;
+ }
+ expect(cfg.cell_height > 0);
+
+ with (world_width, ini_get(ini, "world", "width")) {
+ cfg.world_width = atoi(world_width);
+ } else {
+ pwrn("[world] width unspecified, using default");
+ cfg.world_width = 16;
+ }
+ expect(cfg.world_width > 0);
+
+ with (world_height, ini_get(ini, "world", "height")) {
+ cfg.world_height = atoi(world_height);
+ } else {
+ pwrn("[world] height unspecified, using default");
+ cfg.world_height = 16;
+ }
+ expect(cfg.world_height > 0);
+
+ ini_free(ini);
+}
+
+void
+config_deinit(void)
+{
+ if (cfg.world_path != NULL)
+ free(cfg.world_path);
+ if (cfg.tileset_path != NULL)
+ free(cfg.tileset_path);
+}