rewrite config handling using allegro routines

This commit is contained in:
Sebastian Krzyszkowiak 2012-03-05 10:35:25 +01:00
parent e5a9f12358
commit 5627ddecaf
7 changed files with 28 additions and 151 deletions

1
TODO
View file

@ -2,6 +2,5 @@
- rewrite music handling into mixers
- control settings
- video settings
- rewrite config handling to allegro
- playable levels :D

View file

@ -1,27 +1,5 @@
/*! \file config.c
* \brief Configuration manager code.
*
* Please keep in mind that config file has to be well-formed in order to function properly.
*
* Lines starting with '[' contain section name. No additional whitespace
* between braces and name is allowed.
*
* Section CANNOT be declared multiple times.
*
* Lines starting with '#' are ignored.
*
* All other lines have to look like this one:
*
* key=value
*
* All whitespace at beginning, end or around '=' char will belong to key or value.
* If multiple '=' are present, the first one is used to split line into key and value.
*
* 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.
*/
/*
* Copyright (c) Sebastian Krzyszkowiak <dos@dosowisko.net>
@ -41,129 +19,29 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <allegro5/allegro.h>
#include "config.h"
/*! \brief One config option in list of options. */
struct ConfigOption {
char* name; /*!< Name of the config entry. */
char* value; /*!< Value of the config entry. */
char* section; /*!< Section of the config entry (including braces). */
struct ConfigOption *next; /*!< Pointer to next option in list, or NULL if there is no more. */
};
struct ConfigOption *config; /*!< Pointer to first config entry in list. */
/*! \brief Adds new config entry. */
void AppendToConfig(char* section, char* name, char* value) {
struct ConfigOption *new = malloc(sizeof(struct ConfigOption));
new->next = NULL;
new->section = malloc(sizeof(char)*255);
new->name = malloc(sizeof(char)*255);
new->value = malloc(sizeof(char)*255);
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 {
if (old!=new) old->next = new;
}
}
ALLEGRO_CONFIG *config;
void InitConfig() {
FILE *file = fopen("SuperDerpy.ini","r+");
if (! file) { return; }
char string[255];
char section[255] = "[SuperDerpy]";
struct ConfigOption *old = NULL;
while ( fgets (string , 255 , file) != NULL ) {
if ((string[0]=='#') || (string[0]=='\n')) { 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 = 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';
new->next = NULL;
int i;
for (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(file);
config = al_load_config_file("SuperDerpy.ini");
}
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);
al_set_config_value(config, section, name, value);
}
char* GetConfigOption(char* section, char* name) {
struct ConfigOption *old = config;
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;
const char* GetConfigOption(char* section, char* name) {
return al_get_config_value(config, section, name);
}
char* GetConfigOptionDefault(char* section, char* name, char* def) {
char* ret = GetConfigOption(section, name);
const char* GetConfigOptionDefault(char* section, char* name, const char* def) {
const char* ret = GetConfigOption(section, name);
if (!ret) return def; else return ret;
}
void DeinitConfig() {
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);
free(prev->value);
free(prev->section);
free(prev);
}
fclose(file);
al_save_config_file("SuperDerpy.ini", config);
al_destroy_config(config);
}

View file

@ -21,9 +21,9 @@
/*! \brief Reads config from file into memory. */
void InitConfig();
/*! \brief Returns value of requested config entry. */
char* GetConfigOption(char* section, char* name);
const char* GetConfigOption(char* section, char* name);
/*! \brief Returns value of requested config entry, or def if no such entry exists. */
char* GetConfigOptionDefault(char* section, char* name, char* def);
const char* GetConfigOptionDefault(char* section, char* name, const char* def);
/*! \brief Sets new value of requested config entry, or created new if no such entry exists. */
void SetConfigOption(char* section, char* name, char* value);
/*! \brief Writes config from memory to file. */

View file

@ -249,15 +249,15 @@ int main(int argc, char **argv){
struct Game game;
game.fullscreen = atoi(GetConfigOptionDefault("[SuperDerpy]", "fullscreen", "1"));
game.music = atoi(GetConfigOptionDefault("[SuperDerpy]", "music", "1"));
game.fx = atoi(GetConfigOptionDefault("[SuperDerpy]", "fx", "1"));
game.fps = atoi(GetConfigOptionDefault("[SuperDerpy]", "fps", "60"));
game.fullscreen = atoi(GetConfigOptionDefault("SuperDerpy", "fullscreen", "1"));
game.music = atoi(GetConfigOptionDefault("SuperDerpy", "music", "1"));
game.fx = atoi(GetConfigOptionDefault("SuperDerpy", "fx", "1"));
game.fps = atoi(GetConfigOptionDefault("SuperDerpy", "fps", "60"));
if (game.fps<1) game.fps=60;
game.debug = atoi(GetConfigOptionDefault("[SuperDerpy]", "debug", "0"));
game.width = atoi(GetConfigOptionDefault("[SuperDerpy]", "width", "800"));
game.debug = atoi(GetConfigOptionDefault("SuperDerpy", "debug", "0"));
game.width = atoi(GetConfigOptionDefault("SuperDerpy", "width", "800"));
if (game.width<320) game.width=320;
game.height = atoi(GetConfigOptionDefault("[SuperDerpy]", "height", "500"));
game.height = atoi(GetConfigOptionDefault("SuperDerpy", "height", "500"));
if (game.height<200) game.height=200;
if(!al_init()) {

View file

@ -98,7 +98,7 @@ int Map_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
}
void Map_Preload(struct Game *game) {
game->map.available = atoi(GetConfigOptionDefault("[MuffinAttack]", "level", "1"));
game->map.available = atoi(GetConfigOptionDefault("MuffinAttack", "level", "1"));
if ((game->map.available<1) || (game->map.available>6)) game->map.available=1;
game->map.selected = game->map.available;
PrintConsole(game, "Last level available: %d", game->map.selected);

View file

@ -232,10 +232,10 @@ int Menu_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
PrintConsole(game, "options state changed %d", game->menu.options);
} else if ((game->menu.options) && (game->menu.selected==2)) {
al_stop_samples();
if ((game->music) && (game->fx)) { game->music=0; SetConfigOption("[SuperDerpy]", "music", "0"); }
else if (game->fx) { game->music=1; game->fx=0; SetConfigOption("[SuperDerpy]", "music", "1"); SetConfigOption("[SuperDerpy]", "fx", "0"); }
else if (game->music) { game->music=0; SetConfigOption("[SuperDerpy]", "music", "0"); }
else { game->music=1; game->fx=1; SetConfigOption("[SuperDerpy]", "music", "1"); SetConfigOption("[SuperDerpy]", "fx", "1"); }
if ((game->music) && (game->fx)) { game->music=0; SetConfigOption("SuperDerpy", "music", "0"); }
else if (game->fx) { game->music=1; game->fx=0; SetConfigOption("SuperDerpy", "music", "1"); SetConfigOption("SuperDerpy", "fx", "0"); }
else if (game->music) { game->music=0; SetConfigOption("SuperDerpy", "music", "0"); }
else { game->music=1; game->fx=1; SetConfigOption("SuperDerpy", "music", "1"); SetConfigOption("SuperDerpy", "fx", "1"); }
if (game->fx) al_play_sample(game->menu.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
play_samples(game);
}

View file

@ -51,10 +51,10 @@ int Pause_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
PrintConsole(game, "options state changed %d", game->pause.options);
} else if ((game->pause.options) && (game->pause.selected==2)) {
al_stop_samples();
if ((game->music) && (game->fx)) { game->music=0; SetConfigOption("[SuperDerpy]", "music", "0"); }
else if (game->fx) { game->music=1; game->fx=0; SetConfigOption("[SuperDerpy]", "music", "1"); SetConfigOption("[SuperDerpy]", "fx", "0"); }
else if (game->music) { game->music=0; SetConfigOption("[SuperDerpy]", "music", "0"); }
else { game->music=1; game->fx=1; SetConfigOption("[SuperDerpy]", "music", "1"); SetConfigOption("[SuperDerpy]", "fx", "1"); }
if ((game->music) && (game->fx)) { game->music=0; SetConfigOption("SuperDerpy", "music", "0"); }
else if (game->fx) { game->music=1; game->fx=0; SetConfigOption("SuperDerpy", "music", "1"); SetConfigOption("SuperDerpy", "fx", "0"); }
else if (game->music) { game->music=0; SetConfigOption("SuperDerpy", "music", "0"); }
else { game->music=1; game->fx=1; SetConfigOption("SuperDerpy", "music", "1"); SetConfigOption("SuperDerpy", "fx", "1"); }
if (game->fx) al_play_sample(game->menu.click_sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
//play_samples(game);
}