more work on config

This commit is contained in:
Sebastian Krzyszkowiak 2012-02-26 00:47:41 +01:00
parent 537dac3cfb
commit a88099b9d5
10 changed files with 60 additions and 14 deletions

View file

@ -4,4 +4,4 @@ void About_Draw(struct Game *game);
void About_Preload(struct Game *game); void About_Preload(struct Game *game);
void About_Unload(struct Game *game); void About_Unload(struct Game *game);
void About_Load(struct Game *game); void About_Load(struct Game *game);
int About_Keydown(struct Game *game, ALLEGRO_EVENT *ev); int About_Keydown(struct Game *game, ALLEGRO_EVENT *ev);

View file

@ -21,6 +21,8 @@
Keys CANNOT be repeated in the same section. Keys CANNOT be repeated in the same section.
Rewriting config file removes all comments from it. Rewriting config file removes all comments from it.
If config file is not following this guides, it may be handled incorrectly.
*/ */
struct ConfigOption { struct ConfigOption {
@ -32,6 +34,26 @@ struct ConfigOption {
struct ConfigOption *config; 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() { void InitConfig() {
FILE *file = fopen("SuperDerpy.ini","r+"); FILE *file = fopen("SuperDerpy.ini","r+");
if (! file) { return; } if (! file) { return; }
@ -67,7 +89,19 @@ void InitConfig() {
fclose(file); 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; struct ConfigOption *old = config;
char *ret = malloc(sizeof(char)*255); char *ret = malloc(sizeof(char)*255);
while (old!=NULL) { while (old!=NULL) {
@ -81,11 +115,21 @@ char* ReadConfigOption(char* section, char* name) {
return NULL; return NULL;
} }
char* GetConfigOptionDefault(char* section, char* name, char* def) {
char* ret = GetConfigOption(section, name);
if (!ret) return def; else return ret;
}
void DeinitConfig() { void DeinitConfig() {
FILE *file = fopen("SuperDerpy.ini","w+"); FILE *file = fopen("SuperDerpy.ini","w");
//char* section[255] = {}; char section[255] = {};
struct ConfigOption *old = config; struct ConfigOption *old = config;
while (old!=NULL) { 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; struct ConfigOption *prev = old;
old=old->next; old=old->next;
free(prev->name); free(prev->name);
@ -94,4 +138,4 @@ void DeinitConfig() {
free(prev); free(prev);
} }
fclose(file); fclose(file);
} }

View file

@ -1,3 +1,5 @@
void InitConfig(); 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(); void DeinitConfig();

View file

@ -4,4 +4,4 @@ void Intro_Draw(struct Game *game);
void Intro_Preload(struct Game *game); void Intro_Preload(struct Game *game);
void Intro_Unload(struct Game *game); void Intro_Unload(struct Game *game);
void Intro_Load(struct Game *game); void Intro_Load(struct Game *game);
int Intro_Keydown(struct Game *game, ALLEGRO_EVENT *ev); int Intro_Keydown(struct Game *game, ALLEGRO_EVENT *ev);

View file

@ -4,4 +4,4 @@ void Level_Draw(struct Game *game);
void Level_Preload(struct Game *game); void Level_Preload(struct Game *game);
void Level_Unload(struct Game *game); void Level_Unload(struct Game *game);
void Level_Load(struct Game *game); void Level_Load(struct Game *game);
int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev); int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev);

View file

@ -4,4 +4,4 @@ void Loading_Draw(struct Game *game);
void Loading_Preload(struct Game *game); void Loading_Preload(struct Game *game);
void Loading_Unload(struct Game *game); void Loading_Unload(struct Game *game);
void Loading_Load(struct Game *game); void Loading_Load(struct Game *game);
int Loading_Keydown(struct Game *game, ALLEGRO_EVENT *ev); int Loading_Keydown(struct Game *game, ALLEGRO_EVENT *ev);

View file

@ -100,7 +100,7 @@ void LoadGameState(struct Game *game) {
int main(int argc, char **argv){ int main(int argc, char **argv){
srand(time(NULL)); srand(time(NULL));
InitConfig(); InitConfig();
bool redraw = true; bool redraw = true;

View file

@ -72,7 +72,7 @@ struct Game {
ALLEGRO_EVENT_QUEUE *event_queue; ALLEGRO_EVENT_QUEUE *event_queue;
ALLEGRO_TIMER *timer; ALLEGRO_TIMER *timer;
ALLEGRO_BITMAP *console; ALLEGRO_BITMAP *console;
bool showconsole; bool showconsole, fx, music, fullscreen;
struct Menu menu; struct Menu menu;
struct Loading loading; struct Loading loading;
struct Intro intro; struct Intro intro;
@ -88,4 +88,4 @@ void LoadGameState(struct Game *game);
void PrintConsole(struct Game *game, char* format, ...); void PrintConsole(struct Game *game, char* format, ...);
void DrawConsole(struct Game *game); void DrawConsole(struct Game *game);
#endif #endif

View file

@ -4,4 +4,4 @@ void Map_Draw(struct Game *game);
void Map_Preload(struct Game *game); void Map_Preload(struct Game *game);
void Map_Unload(struct Game *game); void Map_Unload(struct Game *game);
void Map_Load(struct Game *game); void Map_Load(struct Game *game);
int Map_Keydown(struct Game *game, ALLEGRO_EVENT *ev); int Map_Keydown(struct Game *game, ALLEGRO_EVENT *ev);

View file

@ -4,4 +4,4 @@ void Menu_Draw(struct Game *game);
void Menu_Preload(struct Game *game); void Menu_Preload(struct Game *game);
void Menu_Unload(struct Game *game); void Menu_Unload(struct Game *game);
void Menu_Load(struct Game *game); void Menu_Load(struct Game *game);
int Menu_Keydown(struct Game *game, ALLEGRO_EVENT *ev); int Menu_Keydown(struct Game *game, ALLEGRO_EVENT *ev);