From a88099b9d5154c37628caeb7296cc08510c15feb Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Sun, 26 Feb 2012 00:47:41 +0100 Subject: [PATCH] more work on config --- src/about.h | 2 +- src/config.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++---- src/config.h | 4 +++- src/intro.h | 2 +- src/level.h | 2 +- src/loading.h | 2 +- src/main.c | 2 +- src/main.h | 4 ++-- src/map.h | 2 +- src/menu.h | 2 +- 10 files changed, 60 insertions(+), 14 deletions(-) diff --git a/src/about.h b/src/about.h index a668d64..3d236e6 100644 --- a/src/about.h +++ b/src/about.h @@ -4,4 +4,4 @@ void About_Draw(struct Game *game); void About_Preload(struct Game *game); void About_Unload(struct Game *game); void About_Load(struct Game *game); -int About_Keydown(struct Game *game, ALLEGRO_EVENT *ev); \ No newline at end of file +int About_Keydown(struct Game *game, ALLEGRO_EVENT *ev); diff --git a/src/config.c b/src/config.c index 65db045..b3ce398 100644 --- a/src/config.c +++ b/src/config.c @@ -21,6 +21,8 @@ Keys CANNOT be repeated in the same section. Rewriting config file removes all comments from it. + + If config file is not following this guides, it may be handled incorrectly. */ struct ConfigOption { @@ -32,6 +34,26 @@ struct ConfigOption { struct ConfigOption *config; +void AppendToConfig(char* section, char* name, char* value) { + struct ConfigOption *new = malloc(sizeof(struct ConfigOption)); + new->next = NULL; + strcpy(new->section, section); + strcpy(new->name, name); + strcpy(new->value, value); + if (config==NULL) config = new; + struct ConfigOption *old = config; + while (old->next != NULL) { + if (!strcmp(old->section, section)) break; + old=old->next; + } + if (old->next) { + new->next = old->next; + old->next = new; + } else { + old->next = new; + } +} + void InitConfig() { FILE *file = fopen("SuperDerpy.ini","r+"); if (! file) { return; } @@ -67,7 +89,19 @@ void InitConfig() { fclose(file); } -char* ReadConfigOption(char* section, char* name) { +void SetConfigOption(char* section, char* name, char* value) { + struct ConfigOption *old = config; + while (old!=NULL) { + if (!strcmp(section, old->section) && !strcmp(name, old->name)) { + strcpy(old->value, value); + return; + } + old=old->next; + } + AppendToConfig(section, name, value); +} + +char* GetConfigOption(char* section, char* name) { struct ConfigOption *old = config; char *ret = malloc(sizeof(char)*255); while (old!=NULL) { @@ -81,11 +115,21 @@ char* ReadConfigOption(char* section, char* name) { return NULL; } +char* GetConfigOptionDefault(char* section, char* name, char* def) { + char* ret = GetConfigOption(section, name); + if (!ret) return def; else return ret; +} + void DeinitConfig() { - FILE *file = fopen("SuperDerpy.ini","w+"); - //char* section[255] = {}; + FILE *file = fopen("SuperDerpy.ini","w"); + char section[255] = {}; struct ConfigOption *old = config; while (old!=NULL) { + if (strcmp(section, old->section)) { + strcpy(section, old->section); + fprintf(file, "%s\n", section); + } + fprintf(file, "%s=%s\n", old->name, old->value); struct ConfigOption *prev = old; old=old->next; free(prev->name); @@ -94,4 +138,4 @@ void DeinitConfig() { free(prev); } fclose(file); -} \ No newline at end of file +} diff --git a/src/config.h b/src/config.h index c43d3bf..aa378ab 100644 --- a/src/config.h +++ b/src/config.h @@ -1,3 +1,5 @@ void InitConfig(); -char* ReadConfigOption(char* section, char* name); +char* GetConfigOption(char* section, char* name); +char* GetConfigOptionDefault(char* section, char* name, char* def); +void SetConfigOption(char* section, char* name, char* value); void DeinitConfig(); diff --git a/src/intro.h b/src/intro.h index e29603b..4be4ce5 100644 --- a/src/intro.h +++ b/src/intro.h @@ -4,4 +4,4 @@ void Intro_Draw(struct Game *game); void Intro_Preload(struct Game *game); void Intro_Unload(struct Game *game); void Intro_Load(struct Game *game); -int Intro_Keydown(struct Game *game, ALLEGRO_EVENT *ev); \ No newline at end of file +int Intro_Keydown(struct Game *game, ALLEGRO_EVENT *ev); diff --git a/src/level.h b/src/level.h index 7634ad9..61f8f1b 100644 --- a/src/level.h +++ b/src/level.h @@ -4,4 +4,4 @@ void Level_Draw(struct Game *game); void Level_Preload(struct Game *game); void Level_Unload(struct Game *game); void Level_Load(struct Game *game); -int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev); \ No newline at end of file +int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev); diff --git a/src/loading.h b/src/loading.h index d05cca1..bedd15b 100644 --- a/src/loading.h +++ b/src/loading.h @@ -4,4 +4,4 @@ void Loading_Draw(struct Game *game); void Loading_Preload(struct Game *game); void Loading_Unload(struct Game *game); void Loading_Load(struct Game *game); -int Loading_Keydown(struct Game *game, ALLEGRO_EVENT *ev); \ No newline at end of file +int Loading_Keydown(struct Game *game, ALLEGRO_EVENT *ev); diff --git a/src/main.c b/src/main.c index 1803a29..116fdef 100644 --- a/src/main.c +++ b/src/main.c @@ -100,7 +100,7 @@ void LoadGameState(struct Game *game) { int main(int argc, char **argv){ srand(time(NULL)); - + InitConfig(); bool redraw = true; diff --git a/src/main.h b/src/main.h index 398c547..d3a7cee 100644 --- a/src/main.h +++ b/src/main.h @@ -72,7 +72,7 @@ struct Game { ALLEGRO_EVENT_QUEUE *event_queue; ALLEGRO_TIMER *timer; ALLEGRO_BITMAP *console; - bool showconsole; + bool showconsole, fx, music, fullscreen; struct Menu menu; struct Loading loading; struct Intro intro; @@ -88,4 +88,4 @@ void LoadGameState(struct Game *game); void PrintConsole(struct Game *game, char* format, ...); void DrawConsole(struct Game *game); -#endif \ No newline at end of file +#endif diff --git a/src/map.h b/src/map.h index 5fa8caa..b02a8c3 100644 --- a/src/map.h +++ b/src/map.h @@ -4,4 +4,4 @@ void Map_Draw(struct Game *game); void Map_Preload(struct Game *game); void Map_Unload(struct Game *game); void Map_Load(struct Game *game); -int Map_Keydown(struct Game *game, ALLEGRO_EVENT *ev); \ No newline at end of file +int Map_Keydown(struct Game *game, ALLEGRO_EVENT *ev); diff --git a/src/menu.h b/src/menu.h index f16e611..1d90598 100644 --- a/src/menu.h +++ b/src/menu.h @@ -4,4 +4,4 @@ void Menu_Draw(struct Game *game); void Menu_Preload(struct Game *game); void Menu_Unload(struct Game *game); void Menu_Load(struct Game *game); -int Menu_Keydown(struct Game *game, ALLEGRO_EVENT *ev); \ No newline at end of file +int Menu_Keydown(struct Game *game, ALLEGRO_EVENT *ev);