summaryrefslogtreecommitdiff
path: root/src/config.c
blob: 9a673eea313165ea89bca74bd9ed35847989eff5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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);
}