summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/base/main.c8
-rw-r--r--src/base/main.h1
-rw-r--r--src/client/config.c188
-rw-r--r--src/client/config.h39
-rw-r--r--src/client/game.c3
-rw-r--r--src/screen/setting.c139
6 files changed, 283 insertions, 95 deletions
diff --git a/src/base/main.c b/src/base/main.c
index 8766b55..0356b1f 100644
--- a/src/base/main.c
+++ b/src/base/main.c
@@ -78,6 +78,14 @@ char *getString(int n)
return strdup(str);
}
+char* get_string_static(int n)
+{
+ static char str[STR_SIZE];
+ sprintf(str, "%d", n);
+
+ return str; // !!!!
+}
+
int *newInt(int x)
{
int *new;
diff --git a/src/base/main.h b/src/base/main.h
index 17c9838..5748c39 100644
--- a/src/base/main.h
+++ b/src/base/main.h
@@ -27,6 +27,7 @@ extern char *getParam(char *s);
extern char *getParamElse(char *s1, char *s2);
extern bool_t isParamFlag(char *s);
extern char *getString(int n);
+extern char* get_string_static(int n);
extern int *newInt(int x);
extern int isFillPath(const char *path);
extern void accessExistFile(const char *s);
diff --git a/src/client/config.c b/src/client/config.c
new file mode 100644
index 0000000..1be1f1a
--- /dev/null
+++ b/src/client/config.c
@@ -0,0 +1,188 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#include "main.h"
+#include "list.h"
+#include "homeDirector.h"
+#include "textFile.h"
+#include "configFile.h"
+#include "config.h"
+
+typedef struct setting_int_struct
+{
+ int key; /* identifikacny kluc */
+ char *param; /* parameter programu */
+ char *var; /* premenna v config subore */
+ int type; /* typ udaju - retazec/cislo */
+ int value_int; /* hodnota cele cislo */
+ char *value_str; /* hodnota retazec */
+} config_t;
+
+#define CONFIG_TYPE_INT 1
+#define CONFIG_TYPE_STR 2
+
+static config_t config_list[] =
+{
+ { .key=CFG_NAME_RIGHT, .var="NAME_PLAYER_RIGHT", .param="--name-right", .type=CONFIG_TYPE_STR, .value_str="name1" },
+ { .key=CFG_NAME_LEFT, .var="NAME_PLAYER_LEFT", .param="--name-left", .type=CONFIG_TYPE_STR, .value_str="name2" },
+ { .key=CFG_COUNT_ROUND, .var="COUNT_ROUND", .param="--count", .type=CONFIG_TYPE_INT, .value_int=15 },
+ { .key=CFG_AI, .var="AI", .param="--ai", .type=CONFIG_TYPE_INT, .value_int=0 },
+ { .key=CFG_MUSIC, .var="MUSIC", .param="--music", .type=CONFIG_TYPE_INT, .value_int=1 },
+ { .key=CFG_SOUND, .var="SOUND", .param="--sound", .type=CONFIG_TYPE_INT, .value_int=1 },
+ { .key=CFG_ARENA, .var="ARENA", .param="--arena", .type=CONFIG_TYPE_STR, .value_str="FAGN" },
+
+ { .key=CFG_GUN_DUAL_SIMPLE, .var="GUN_DUAL_SIMPLE", .param="--gun-dual-simple", .type=CONFIG_TYPE_INT, .value_int=1 },
+ { .key=CFG_GUN_SCATTER, .var="GUN_SCATTER", .param="--gun-scatter", .type=CONFIG_TYPE_INT, .value_int=1 },
+ { .key=CFG_GUN_TOMMY, .var="GUN_TOMMY", .param="--gun-tommy", .type=CONFIG_TYPE_INT, .value_int=1 },
+ { .key=CFG_GUN_LASSER, .var="GUN_LASSER", .param="--gun-lasser", .type=CONFIG_TYPE_INT, .value_int=1 },
+ { .key=CFG_GUN_MINE, .var="GUN_MINE", .param="--gun-mine", .type=CONFIG_TYPE_INT, .value_int=1 },
+ { .key=CFG_GUN_BOMBBALL, .var="GUN_BOMBBALL", .param="--gun-bombball", .type=CONFIG_TYPE_INT, .value_int=1 },
+
+ { .key=CFG_BONUS_SPEED, .var="BONUS_SPEED", .param="--bonus-speed", .type=CONFIG_TYPE_INT, .value_int=1 },
+ { .key=CFG_BONUS_SHOT, .var="BONUS_SHOT", .param="--bonus-shot", .type=CONFIG_TYPE_INT, .value_int=1 },
+ { .key=CFG_BONUS_TELEPORT, .var="BONUS_TELEPORT", .param="--bonus-teleport", .type=CONFIG_TYPE_INT, .value_int=1 },
+ { .key=CFG_BONUS_GHOST, .var="BONUS_GHOST", .param="--bonus-ghost", .type=CONFIG_TYPE_INT, .value_int=1 },
+ { .key=CFG_BONUS_4X, .var="BONUS_4X", .param="--bonus-4x", .type=CONFIG_TYPE_INT, .value_int=1 },
+ { .key=CFG_BONUS_HIDDEN, .var="BONUS_HIDDEN", .param="--bonus-hidden", .type=CONFIG_TYPE_INT, .value_int=1 }
+};
+
+static config_t* find_config(int key)
+{
+ int i;
+
+ for (i = 0; i < CONFIG_LIST_COUNT; i++) {
+ if (config_list[i].key == key) {
+ return &config_list[i];
+ }
+ }
+
+ return NULL;
+}
+
+static textFile_t *config_file;
+
+int config_init()
+{
+ config_load();
+ return 0;
+}
+
+void config_load()
+{
+ char path[STR_PATH_SIZE];
+ char val[STR_SIZE];
+ int i;
+
+ sprintf(path, "%s/tuxanci.conf", home_director_get());
+
+ config_file = text_file_load(path);
+
+ if (config_file == NULL) {
+ fprintf(stderr, _("[Error] Unable to load config file [%s]\n"), path);
+ fprintf(stderr, _("[Debug] Creating config file [%s]\n"), path);
+
+ config_file = text_file_new(path);
+ }
+
+ if (config_file == NULL) {
+ fprintf(stderr, _("[Error] Unable to create config file [%s]\n"), path);
+ return;
+ }
+
+ for (i = 0; i < CONFIG_LIST_COUNT; i++) {
+ if (config_list[i].type == CONFIG_TYPE_INT) {
+ char str[STR_SIZE];
+
+ sprintf(str, "%d", config_list[i].value_int);
+ loadValueFromConfigFile(config_file, config_list[i].var, val, STR_SIZE, str);
+ config_list[i].value_int = atoi(val);
+ } else {
+ loadValueFromConfigFile(config_file, config_list[i].var, val, STR_SIZE, config_list[i].value_str);
+ config_list[i].value_str = strdup(val);
+ }
+ }
+}
+
+void config_save()
+{
+ int i;
+
+ if (config_file == NULL) {
+ return;
+ }
+
+ for (i = 0; i < CONFIG_LIST_COUNT; i++) {
+ if (config_list[i].type == CONFIG_TYPE_INT) {
+ char str[STR_SIZE];
+
+ sprintf(str, "%d", config_list[i].value_int);
+ setValueInConfigFile(config_file, config_list[i].var, str);
+ } else {
+ setValueInConfigFile(config_file, config_list[i].var, config_list[i].value_str);
+ }
+ }
+
+ text_file_save(config_file);
+ text_file_destroy(config_file);
+}
+
+int config_get_int_value(int key)
+{
+ config_t *tmp;
+
+ tmp = find_config(key);
+
+ assert(tmp != NULL);
+ assert(tmp->type == CONFIG_TYPE_INT);
+
+ return tmp->value_int;
+}
+
+char* config_get_str_value(int key)
+{
+ config_t *tmp;
+
+ tmp = find_config(key);
+
+ assert(tmp != NULL);
+ assert(tmp->type == CONFIG_TYPE_STR);
+
+ return tmp->value_str;
+}
+
+void config_set_int_value(int key, int n)
+{
+ config_t *tmp;
+
+ tmp = find_config(key);
+
+ assert(tmp != NULL);
+ assert(tmp->type == CONFIG_TYPE_INT);
+
+ tmp->value_int = n;
+}
+
+void config_set_str_value(int key, char *str)
+{
+ config_t *tmp;
+
+ tmp = find_config(key);
+
+ assert(tmp != NULL);
+ assert(tmp->type == CONFIG_TYPE_STR);
+
+ if (tmp->value_str != NULL) {
+ free(tmp->value_str); // musi to byt alokovane
+ }
+
+ tmp->value_str = strdup(str);
+}
+
+int config_quit()
+{
+ config_save();
+ return 0;
+}
diff --git a/src/client/config.h b/src/client/config.h
new file mode 100644
index 0000000..c56c537
--- /dev/null
+++ b/src/client/config.h
@@ -0,0 +1,39 @@
+#ifndef CONFIG_H
+#define CONFIG_H
+
+#define CFG_NAME_RIGHT 1
+#define CFG_NAME_LEFT 2
+#define CFG_COUNT_ROUND 3
+#define CFG_AI 4
+#define CFG_MUSIC 5
+#define CFG_SOUND 6
+#define CFG_ARENA 7
+
+#define CFG_GUN_DUAL_SIMPLE 8
+#define CFG_GUN_SCATTER 9
+#define CFG_GUN_TOMMY 10
+#define CFG_GUN_LASSER 11
+#define CFG_GUN_MINE 12
+#define CFG_GUN_BOMBBALL 13
+
+
+#define CFG_BONUS_SPEED 14
+#define CFG_BONUS_SHOT 15
+#define CFG_BONUS_TELEPORT 16
+#define CFG_BONUS_GHOST 17
+#define CFG_BONUS_4X 18
+#define CFG_BONUS_HIDDEN 19
+
+#define CONFIG_LIST_COUNT 19
+
+extern int config_init();
+extern void config_load();
+extern void config_save();
+extern int config_get_int_value(int key);
+extern char* config_get_str_value(int key);
+extern void config_set_int_value(int key, int n);
+extern void config_set_str_value(int key, char *str);
+extern int config_quit();
+
+#endif /* CONFIG_H */
+
diff --git a/src/client/game.c b/src/client/game.c
index 12dff3a..07079b6 100644
--- a/src/client/game.c
+++ b/src/client/game.c
@@ -13,6 +13,7 @@
#include "item.h"
#include "shot.h"
#include "arenaFile.h"
+#include "config.h"
#include "image.h"
#include "layer.h"
@@ -63,6 +64,7 @@ static void initGame()
shot_init();
panel_init();
world_init();
+ config_init();
main_menu_init();
analyze_init();
@@ -91,6 +93,7 @@ void game_quit()
scredits_quit();
table_quit();
browser_quit();
+ config_quit();
layer_quit();
font_quit();
diff --git a/src/screen/setting.c b/src/screen/setting.c
index 2dadec7..ffe8e92 100644
--- a/src/screen/setting.c
+++ b/src/screen/setting.c
@@ -9,6 +9,7 @@
#include "director.h"
#include "homeDirector.h"
#include "arenaFile.h"
+#include "config.h"
#include "configFile.h"
#include "interface.h"
@@ -79,8 +80,6 @@ static widget_t *check_ai;
static widget_t *statusbar;
-static textFile_t *configFile;
-
static void hotkey_escape()
{
screen_set("mainMenu");
@@ -219,113 +218,63 @@ static void eventWidget(void *p)
static void initSettingFile()
{
- char path[STR_PATH_SIZE];
- char val[STR_SIZE];
-
- sprintf(path, "%s/tuxanci.conf", home_director_get());
-
- configFile = text_file_load(path);
-
- if (configFile == NULL) {
- fprintf(stderr, _("[Error] Unable to load config file [%s]\n"), path);
- fprintf(stderr, _("[Debug] Creating config file [%s]\n"), path);
-
- configFile = text_file_new(path);
- }
-
- if (configFile == NULL) {
- fprintf(stderr, _("[Error] Unable to create config file [%s]\n"), path);
- return;
- }
-
- loadValueFromConfigFile(configFile, "COUNT_ROUND", val, STR_SIZE, "15");
- text_field_set_text(textfield_count_cound, val);
-
- loadValueFromConfigFile(configFile, "NAME_PLAYER_RIGHT", val, STR_SIZE, NAME_PLAYER_RIGHT);
- text_field_set_text(textfield_name_player1, val);
-
- loadValueFromConfigFile(configFile, "NAME_PLAYER_LEFT", val, STR_SIZE, NAME_PLAYER_LEFT);
- text_field_set_text(textfield_name_player2, val);
-
- loadValueFromConfigFile(configFile, "GUN_DUAL_SIMPLE", val, STR_SIZE, "YES");
- check_set_status(check[GUN_DUAL_SIMPLE], isYesOrNO(val));
- loadValueFromConfigFile(configFile, "GUN_SCATTER", val, STR_SIZE, "YES");
- check_set_status(check[GUN_SCATTER], isYesOrNO(val));
- loadValueFromConfigFile(configFile, "GUN_TOMMY", val, STR_SIZE, "YES");
- check_set_status(check[GUN_TOMMY], isYesOrNO(val));
- loadValueFromConfigFile(configFile, "GUN_LASSER", val, STR_SIZE, "YES");
- check_set_status(check[GUN_LASSER], isYesOrNO(val));
- loadValueFromConfigFile(configFile, "GUN_MINE", val, STR_SIZE, "YES");
- check_set_status(check[GUN_MINE], isYesOrNO(val));
- loadValueFromConfigFile(configFile, "GUN_BOMBBALL", val, STR_SIZE, "YES");
- check_set_status(check[GUN_BOMBBALL], isYesOrNO(val));
-
- loadValueFromConfigFile(configFile, "BONUS_SPEED", val, STR_SIZE, "YES");
- check_set_status(check[BONUS_SPEED], isYesOrNO(val));
- loadValueFromConfigFile(configFile, "BONUS_SHOT", val, STR_SIZE, "YES");
- check_set_status(check[BONUS_SHOT], isYesOrNO(val));
- loadValueFromConfigFile(configFile, "BONUS_TELEPORT", val, STR_SIZE, "YES");
- check_set_status(check[BONUS_TELEPORT], isYesOrNO(val));
- loadValueFromConfigFile(configFile, "BONUS_GHOST", val, STR_SIZE, "YES");
- check_set_status(check[BONUS_GHOST], isYesOrNO(val));
- loadValueFromConfigFile(configFile, "BONUS_4X", val, STR_SIZE, "YES");
- check_set_status(check[BONUS_4X], isYesOrNO(val));
- loadValueFromConfigFile(configFile, "BONUS_HIDDEN", val, STR_SIZE, "YES");
- check_set_status(check[BONUS_HIDDEN], isYesOrNO(val));
-
- loadValueFromConfigFile(configFile, "AI", val, STR_SIZE, "NO");
- check_set_status(check_ai, isYesOrNO(val));
+ text_field_set_text(textfield_count_cound, get_string_static(config_get_int_value(CFG_COUNT_ROUND)));
+ text_field_set_text(textfield_name_player1, config_get_str_value(CFG_NAME_RIGHT));
+ text_field_set_text(textfield_name_player2, config_get_str_value(CFG_NAME_LEFT));
+
+ check_set_status(check[GUN_DUAL_SIMPLE], config_get_int_value(CFG_GUN_DUAL_SIMPLE));
+ check_set_status(check[GUN_SCATTER], config_get_int_value(CFG_GUN_SCATTER));
+ check_set_status(check[GUN_TOMMY], config_get_int_value(CFG_GUN_TOMMY));
+ check_set_status(check[GUN_LASSER], config_get_int_value(CFG_GUN_LASSER));
+ check_set_status(check[GUN_MINE], config_get_int_value(CFG_GUN_MINE));
+ check_set_status(check[GUN_BOMBBALL], config_get_int_value(CFG_GUN_BOMBBALL));
+
+ check_set_status(check[BONUS_SPEED], config_get_int_value(CFG_BONUS_SPEED));
+ check_set_status(check[BONUS_SHOT], config_get_int_value(CFG_BONUS_SHOT));
+ check_set_status(check[BONUS_TELEPORT], config_get_int_value(CFG_BONUS_TELEPORT));
+ check_set_status(check[BONUS_GHOST], config_get_int_value(CFG_BONUS_GHOST));
+ check_set_status(check[BONUS_4X], config_get_int_value(CFG_BONUS_4X));
+ check_set_status(check[BONUS_HIDDEN], config_get_int_value(CFG_BONUS_HIDDEN));
+
+ check_set_status(check_ai, config_get_int_value(CFG_AI));
#ifndef NO_SOUND
- loadValueFromConfigFile(configFile, "MUSIC", val, STR_SIZE, "YES");
- check_set_status(check_music, isYesOrNO(val));
-
- loadValueFromConfigFile(configFile, "SOUND", val, STR_SIZE, "YES");
- check_set_status(check_sound, isYesOrNO(val));
+ check_set_status(check_music, config_get_int_value(CFG_MUSIC));
+ check_set_status(check_sound, config_get_int_value(CFG_SOUND));
#endif
- loadValueFromConfigFile(configFile, "ARENA", val, STR_SIZE, "FAGN");
- choice_arena_set(arena_file_get_file_format_net_name(val));
-
- text_file_save(configFile);
+ choice_arena_set(arena_file_get_file_format_net_name(config_get_str_value(CFG_ARENA)));
}
static void saveAndDestroyConfigFile()
{
- if (configFile == NULL) {
- fprintf(stderr, _("[Error] Unable to save config file - not initialized\n"));
- return;
- }
-
- setValueInConfigFile(configFile, "COUNT_ROUND", text_field_get_text(textfield_count_cound));
- setValueInConfigFile(configFile, "NAME_PLAYER_RIGHT", text_field_get_text(textfield_name_player1));
- setValueInConfigFile(configFile, "NAME_PLAYER_LEFT", text_field_get_text(textfield_name_player2));
- setValueInConfigFile(configFile, "GUN_DUAL_SIMPLE", getYesOrNo(check_get_status(check[GUN_DUAL_SIMPLE])));
- setValueInConfigFile(configFile, "GUN_SCATTER", getYesOrNo(check_get_status(check[GUN_SCATTER])));
- setValueInConfigFile(configFile, "GUN_TOMMY", getYesOrNo(check_get_status(check[GUN_TOMMY])));
- setValueInConfigFile(configFile, "GUN_LASSER", getYesOrNo(check_get_status(check[GUN_LASSER])));
- setValueInConfigFile(configFile, "GUN_MINE", getYesOrNo(check_get_status(check[GUN_MINE])));
- setValueInConfigFile(configFile, "GUN_BOMBBALL", getYesOrNo(check_get_status(check[GUN_BOMBBALL])));
- setValueInConfigFile(configFile, "BONUS_SPEED", getYesOrNo(check_get_status(check[BONUS_SPEED])));
- setValueInConfigFile(configFile, "BONUS_SHOT", getYesOrNo(check_get_status(check[BONUS_SHOT])));
- setValueInConfigFile(configFile, "BONUS_TELEPORT", getYesOrNo(check_get_status (check[BONUS_TELEPORT])));
- setValueInConfigFile(configFile, "BONUS_GHOST", getYesOrNo(check_get_status(check[BONUS_GHOST])));
- setValueInConfigFile(configFile, "BONUS_4X", getYesOrNo(check_get_status(check[BONUS_4X])));
- setValueInConfigFile(configFile, "BONUS_HIDDEN", getYesOrNo(check_get_status(check[BONUS_HIDDEN])));
- setValueInConfigFile(configFile, "AI", getYesOrNo(check_get_status(check_ai)));
+ config_set_int_value(CFG_COUNT_ROUND, atoi(text_field_get_text(textfield_count_cound)));
+ config_set_str_value(CFG_NAME_RIGHT, text_field_get_text(textfield_name_player1));
+ config_set_str_value(CFG_NAME_LEFT, text_field_get_text(textfield_name_player2));
+
+ config_set_int_value(CFG_GUN_DUAL_SIMPLE, check_get_status(check[GUN_DUAL_SIMPLE]));
+ config_set_int_value(CFG_GUN_SCATTER, check_get_status(check[GUN_SCATTER]));
+ config_set_int_value(CFG_GUN_TOMMY, check_get_status(check[GUN_TOMMY]));
+ config_set_int_value(CFG_GUN_LASSER, check_get_status(check[GUN_LASSER]));
+ config_set_int_value(CFG_GUN_MINE, check_get_status(check[GUN_MINE]));
+ config_set_int_value(CFG_GUN_BOMBBALL, check_get_status(check[GUN_BOMBBALL]));
+ config_set_int_value(CFG_BONUS_SPEED, check_get_status(check[BONUS_SPEED]));
+ config_set_int_value(CFG_BONUS_SHOT, check_get_status(check[BONUS_SHOT]));
+ config_set_int_value(CFG_BONUS_TELEPORT, check_get_status(check[BONUS_TELEPORT]));
+ config_set_int_value(CFG_BONUS_GHOST, check_get_status(check[BONUS_GHOST]));
+ config_set_int_value(CFG_BONUS_4X, check_get_status(check[BONUS_4X]));
+ config_set_int_value(CFG_BONUS_HIDDEN, check_get_status(check[BONUS_HIDDEN]));
+
+ config_set_int_value(CFG_AI, check_get_status(check_ai));
#ifndef NO_SOUND
- setValueInConfigFile(configFile, "MUSIC", getYesOrNo(check_get_status(check_music)));
- setValueInConfigFile(configFile, "SOUND", getYesOrNo(check_get_status(check_sound)));
+ config_set_int_value(CFG_MUSIC, check_get_status(check_music));
+ config_set_int_value(CFG_SOUND, check_get_status(check_sound));
#endif
if (choice_arena_get() != NULL) {
- setValueInConfigFile(configFile, "ARENA", arena_file_get_net_name(choice_arena_get()));
+ config_set_str_value(CFG_ARENA, arena_file_get_net_name(choice_arena_get()));
}
- /* TODO */
-
- text_file_save(configFile);
- text_file_destroy(configFile);
}
void setting_init()