add config manager

This commit is contained in:
Sebastian Krzyszkowiak 2012-02-24 13:03:30 +01:00
parent 626d5bb884
commit 52aa7c3f1e
5 changed files with 96 additions and 2 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
*~
allegro.log
others
SuperDerpy.ini

View file

@ -6,7 +6,7 @@ SRCDIR=src
ODIR=obj
LIBS=-lallegro -lallegro_audio-debug -lallegro_acodec-debug -lallegro_image-debug -lallegro_font-debug -lallegro_ttf-debug -lm
_OBJ = main.o about.o intro.o loading.o map.o menu.o level.o
_OBJ = config.o main.o about.o intro.o loading.o map.o menu.o level.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
OUTPUTDIR = bin

85
src/config.c Normal file
View file

@ -0,0 +1,85 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
struct ConfigOption {
char* name;
char* value;
char* section;
struct ConfigOption *next;
};
struct Config {
FILE *file;
struct ConfigOption *list;
};
struct Config config;
void InitConfig() {
config.file = fopen("SuperDerpy.ini","r+");
if (! config.file) { fopen("SuperDerpy.ini","w+"); return; }
char string[255];
char section[255] = "[MuffinAttack]";
struct ConfigOption *old = NULL;
while ( fgets (string , 255 , config.file) != NULL ) {
if (string[0]=='#') { continue; }
if (string[strlen(string)-1]=='\n') string[strlen(string)-1]='\0';
if (string[0]=='[') { strcpy(section, string); continue; }
bool before=true;
struct ConfigOption *new = malloc(sizeof(struct ConfigOption));
if (old==NULL) {
config.list = new;
old = new;
} else { old->next = new; old = new; }
new->section = malloc(sizeof(char)*255);
strcpy(new->section, section);
new->name = malloc(sizeof(char)*255);
new->name[0] = '\0';
new->value = malloc(sizeof(char)*255);
new->value[0] = '\0';
for (int i=0; i<strlen(string); i++) {
if (string[i]=='=') { before=false; continue; }
char temp[2];
temp[0]=string[i];
temp[1]='\0';
if (before) strcat(new->name, temp);
else strcat(new->value, temp);
//printf("%s", temp);
}
}
fclose(config.file);
/* old = config.list;
while (old!=NULL) {
printf("Section: %s, name: %s, value: %s\n", old->section, old->name, old->value);
old=old->next;
}*/
}
char* ReadConfigOption(char* section, char* name) {
struct ConfigOption *old = config.list;
char *ret = malloc(sizeof(char)*255);
while (old!=NULL) {
if (!strcmp(section, old->section) && !strcmp(name, old->name)) {
strcpy(ret, old->value);
return ret;
}
old=old->next;
}
free(ret);
return NULL;
}
void DeinitConfig() {
struct ConfigOption *old = config.list;
while (old!=NULL) {
struct ConfigOption *prev = old;
old=old->next;
free(prev->name);
free(prev->value);
free(prev->section);
free(prev);
}
}

3
src/config.h Normal file
View file

@ -0,0 +1,3 @@
void InitConfig();
char* ReadConfigOption(char* section, char* name);
void DeinitConfig();

View file

@ -6,6 +6,7 @@
#include "intro.h"
#include "map.h"
#include "level.h"
#include "config.h"
float FPS = 60;
int DISPLAY_WIDTH = 800;
@ -143,6 +144,9 @@ void LoadGameState(struct Game *game) {
int main(int argc, char **argv){
srand(time(NULL));
InitConfig();
//ALLEGRO_DISPLAY_MODE disp_data;
bool redraw = true;
@ -227,7 +231,7 @@ int main(int argc, char **argv){
al_flip_display();
al_start_timer(game.timer);
printf("%s\n", ReadConfigOption("[MuffinAttack]", "lol"));
game.loadstate = GAMESTATE_LOADING;
PreloadGameState(&game);
LoadGameState(&game);
@ -325,5 +329,6 @@ int main(int argc, char **argv){
al_destroy_event_queue(game.event_queue);
al_destroy_font(game.font);
al_destroy_font(game.font_console);
DeinitConfig();
return 0;
}