X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=share%2Fconfig.c;h=0dddce3c6a5e62587f583e00808cd42095d32ee4;hb=9a0d95f9e851a54f69f05552ca9bf4041c8c88e0;hp=dd81b78715ebedd99442848724ec84006eaa2c71;hpb=3d583759f772b14e821c64ab290791e83347a65a;p=neverball diff --git a/share/config.c b/share/config.c index dd81b78..0dddce3 100644 --- a/share/config.c +++ b/share/config.c @@ -13,44 +13,216 @@ */ #include -#include #include #include #include -#include -#include +#include #include "config.h" -#include "glext.h" -#include "vec3.h" +#include "common.h" +#include "fs.h" /*---------------------------------------------------------------------------*/ -/* Define the mkdir symbol. */ - -#ifdef _WIN32 -#include -#else -#include -#endif +/* Integer options. */ + +int CONFIG_FULLSCREEN; +int CONFIG_WIDTH; +int CONFIG_HEIGHT; +int CONFIG_STEREO; +int CONFIG_CAMERA; +int CONFIG_TEXTURES; +int CONFIG_GEOMETRY; +int CONFIG_REFLECTION; +int CONFIG_MULTISAMPLE; +int CONFIG_MIPMAP; +int CONFIG_ANISO; +int CONFIG_BACKGROUND; +int CONFIG_SHADOW; +int CONFIG_AUDIO_BUFF; +int CONFIG_MOUSE_SENSE; +int CONFIG_MOUSE_INVERT; +int CONFIG_VSYNC; +int CONFIG_MOUSE_CAMERA_1; +int CONFIG_MOUSE_CAMERA_2; +int CONFIG_MOUSE_CAMERA_3; +int CONFIG_MOUSE_CAMERA_TOGGLE; +int CONFIG_MOUSE_CAMERA_L; +int CONFIG_MOUSE_CAMERA_R; +int CONFIG_NICE; +int CONFIG_FPS; +int CONFIG_SOUND_VOLUME; +int CONFIG_MUSIC_VOLUME; +int CONFIG_JOYSTICK; +int CONFIG_JOYSTICK_DEVICE; +int CONFIG_JOYSTICK_AXIS_X; +int CONFIG_JOYSTICK_AXIS_Y; +int CONFIG_JOYSTICK_AXIS_U; +int CONFIG_JOYSTICK_AXIS_X_INVERT; +int CONFIG_JOYSTICK_AXIS_Y_INVERT; +int CONFIG_JOYSTICK_AXIS_U_INVERT; +int CONFIG_JOYSTICK_BUTTON_A; +int CONFIG_JOYSTICK_BUTTON_B; +int CONFIG_JOYSTICK_BUTTON_R; +int CONFIG_JOYSTICK_BUTTON_L; +int CONFIG_JOYSTICK_BUTTON_EXIT; +int CONFIG_JOYSTICK_CAMERA_1; +int CONFIG_JOYSTICK_CAMERA_2; +int CONFIG_JOYSTICK_CAMERA_3; +int CONFIG_JOYSTICK_DPAD_L; +int CONFIG_JOYSTICK_DPAD_R; +int CONFIG_JOYSTICK_DPAD_U; +int CONFIG_JOYSTICK_DPAD_D; +int CONFIG_JOYSTICK_CAMERA_TOGGLE; +int CONFIG_JOYSTICK_ROTATE_FAST; +int CONFIG_KEY_CAMERA_1; +int CONFIG_KEY_CAMERA_2; +int CONFIG_KEY_CAMERA_3; +int CONFIG_KEY_CAMERA_TOGGLE; +int CONFIG_KEY_CAMERA_R; +int CONFIG_KEY_CAMERA_L; +int CONFIG_KEY_FORWARD; +int CONFIG_KEY_BACKWARD; +int CONFIG_KEY_LEFT; +int CONFIG_KEY_RIGHT; +int CONFIG_KEY_PAUSE; +int CONFIG_KEY_RESTART; +int CONFIG_KEY_SCORE_NEXT; +int CONFIG_KEY_ROTATE_FAST; +int CONFIG_VIEW_FOV; +int CONFIG_VIEW_DP; +int CONFIG_VIEW_DC; +int CONFIG_VIEW_DZ; +int CONFIG_ROTATE_FAST; +int CONFIG_ROTATE_SLOW; +int CONFIG_CHEAT; +int CONFIG_STATS; +int CONFIG_UNIFORM; +int CONFIG_SCREENSHOT; +int CONFIG_LOCK_GOALS; + +/* String options. */ + +int CONFIG_PLAYER; +int CONFIG_BALL_FILE; +int CONFIG_WIIMOTE_ADDR; +int CONFIG_REPLAY_NAME; /*---------------------------------------------------------------------------*/ -static int option_d[CONFIG_OPTION_D_COUNT]; -static char *option_s[CONFIG_OPTION_S_COUNT]; +static struct +{ + int *sym; + const char *name; + const int def; + int cur; +} option_d[] = { + { &CONFIG_FULLSCREEN, "fullscreen", 0 }, + { &CONFIG_WIDTH, "width", 800 }, + { &CONFIG_HEIGHT, "height", 600 }, + { &CONFIG_STEREO, "stereo", 0 }, + { &CONFIG_CAMERA, "camera", 0 }, + { &CONFIG_TEXTURES, "textures", 1 }, + { &CONFIG_GEOMETRY, "geometry", 1 }, + { &CONFIG_REFLECTION, "reflection", 1 }, + { &CONFIG_MULTISAMPLE, "multisample", 0 }, + { &CONFIG_MIPMAP, "mipmap", 0 }, + { &CONFIG_ANISO, "aniso", 0 }, + { &CONFIG_BACKGROUND, "background", 1 }, + { &CONFIG_SHADOW, "shadow", 1 }, + { &CONFIG_AUDIO_BUFF, "audio_buff", AUDIO_BUFF_HI }, + { &CONFIG_MOUSE_SENSE, "mouse_sense", 300 }, + { &CONFIG_MOUSE_INVERT, "mouse_invert", 0 }, + { &CONFIG_VSYNC, "vsync", 1 }, + + { &CONFIG_MOUSE_CAMERA_1, "mouse_camera_1", 0 }, + { &CONFIG_MOUSE_CAMERA_2, "mouse_camera_2", 0 }, + { &CONFIG_MOUSE_CAMERA_3, "mouse_camera_3", 0 }, + { &CONFIG_MOUSE_CAMERA_TOGGLE, "mouse_camera_toggle", SDL_BUTTON_MIDDLE }, + { &CONFIG_MOUSE_CAMERA_L, "mouse_camera_l", SDL_BUTTON_LEFT }, + { &CONFIG_MOUSE_CAMERA_R, "mouse_camera_r", SDL_BUTTON_RIGHT }, + + { &CONFIG_NICE, "nice", 0 }, + { &CONFIG_FPS, "fps", 0 }, + { &CONFIG_SOUND_VOLUME, "sound_volume", 10 }, + { &CONFIG_MUSIC_VOLUME, "music_volume", 6 }, + + { &CONFIG_JOYSTICK, "joystick", 1 }, + { &CONFIG_JOYSTICK_DEVICE, "joystick_device", 0 }, + { &CONFIG_JOYSTICK_AXIS_X, "joystick_axis_x", 0 }, + { &CONFIG_JOYSTICK_AXIS_Y, "joystick_axis_y", 1 }, + { &CONFIG_JOYSTICK_AXIS_U, "joystick_axis_u", 2 }, + { &CONFIG_JOYSTICK_AXIS_X_INVERT, "joystick_axis_x_invert", 0 }, + { &CONFIG_JOYSTICK_AXIS_Y_INVERT, "joystick_axis_y_invert", 0 }, + { &CONFIG_JOYSTICK_AXIS_U_INVERT, "joystick_axis_u_invert", 0 }, + { &CONFIG_JOYSTICK_BUTTON_A, "joystick_button_a", 0 }, + { &CONFIG_JOYSTICK_BUTTON_B, "joystick_button_b", 1 }, + { &CONFIG_JOYSTICK_BUTTON_R, "joystick_button_r", 2 }, + { &CONFIG_JOYSTICK_BUTTON_L, "joystick_button_l", 3 }, + { &CONFIG_JOYSTICK_BUTTON_EXIT, "joystick_button_exit", 4 }, + { &CONFIG_JOYSTICK_CAMERA_1, "joystick_camera_1", 5 }, + { &CONFIG_JOYSTICK_CAMERA_2, "joystick_camera_2", 6 }, + { &CONFIG_JOYSTICK_CAMERA_3, "joystick_camera_3", 7 }, + { &CONFIG_JOYSTICK_DPAD_L, "joystick_dpad_l", 8 }, + { &CONFIG_JOYSTICK_DPAD_R, "joystick_dpad_r", 9 }, + { &CONFIG_JOYSTICK_DPAD_U, "joystick_dpad_u", 10 }, + { &CONFIG_JOYSTICK_DPAD_D, "joystick_dpad_d", 11 }, + { &CONFIG_JOYSTICK_CAMERA_TOGGLE, "joystick_camera_toggle", 12 }, + { &CONFIG_JOYSTICK_ROTATE_FAST, "joystick_rotate_fast", 13 }, + + { &CONFIG_KEY_CAMERA_1, "key_camera_1", SDLK_F1 }, + { &CONFIG_KEY_CAMERA_2, "key_camera_2", SDLK_F2 }, + { &CONFIG_KEY_CAMERA_3, "key_camera_3", SDLK_F3 }, + { &CONFIG_KEY_CAMERA_TOGGLE, "key_camera_toggle", SDLK_e }, + { &CONFIG_KEY_CAMERA_R, "key_camera_r", SDLK_d }, + { &CONFIG_KEY_CAMERA_L, "key_camera_l", SDLK_s }, + { &CONFIG_KEY_FORWARD, "key_forward", SDLK_UP }, + { &CONFIG_KEY_BACKWARD, "key_backward", SDLK_DOWN }, + { &CONFIG_KEY_LEFT, "key_left", SDLK_LEFT }, + { &CONFIG_KEY_RIGHT, "key_right", SDLK_RIGHT }, + { &CONFIG_KEY_PAUSE, "key_pause", SDLK_ESCAPE }, + { &CONFIG_KEY_RESTART, "key_restart", SDLK_r }, + { &CONFIG_KEY_SCORE_NEXT, "key_score_next", SDLK_TAB }, + { &CONFIG_KEY_ROTATE_FAST, "key_rotate_fast", SDLK_LSHIFT }, + + { &CONFIG_VIEW_FOV, "view_fov", 50 }, + { &CONFIG_VIEW_DP, "view_dp", 75 }, + { &CONFIG_VIEW_DC, "view_dc", 25 }, + { &CONFIG_VIEW_DZ, "view_dz", 200 }, + { &CONFIG_ROTATE_FAST, "rotate_fast", 300 }, + { &CONFIG_ROTATE_SLOW, "rotate_slow", 150 }, + { &CONFIG_CHEAT, "cheat", 0 }, + { &CONFIG_STATS, "stats", 0 }, + { &CONFIG_UNIFORM, "uniform", 0 }, + { &CONFIG_SCREENSHOT, "screenshot", 0 }, + { &CONFIG_LOCK_GOALS, "lock_goals", 0 } +}; + +static struct +{ + int *sym; + const char *name; + const char *def; + char *cur; +} option_s[] = { + { &CONFIG_PLAYER, "player", "" }, + { &CONFIG_BALL_FILE, "ball_file", "ball/basic-ball/basic-ball" }, + { &CONFIG_WIIMOTE_ADDR, "wiimote_addr", "" }, + { &CONFIG_REPLAY_NAME, "replay_name", "%s-%l" } +}; static int dirty = 0; /*---------------------------------------------------------------------------*/ -static void config_key(const char *s, int i, int d) +static void config_key(const char *s, int i) { int c; - config_set_d(i, d); + config_set_d(i, option_d[i].def); for (c = 0; c < SDLK_LAST; c++) - if (strcmp(s, SDL_GetKeyName(c)) == 0) + if (strcmp(s, SDL_GetKeyName((SDLKey) c)) == 0) { config_set_d(i, c); break; @@ -59,522 +231,330 @@ static void config_key(const char *s, int i, int d) /*---------------------------------------------------------------------------*/ -void config_init(void) +static void config_mouse(const char *s, int i) { - memset(option_d, 0, CONFIG_OPTION_D_COUNT * sizeof (int)); - memset(option_s, 0, CONFIG_OPTION_S_COUNT * sizeof (char *)); - - config_set_d(CONFIG_FULLSCREEN, DEFAULT_FULLSCREEN); - config_set_d(CONFIG_WIDTH, DEFAULT_WIDTH); - config_set_d(CONFIG_HEIGHT, DEFAULT_HEIGHT); - config_set_d(CONFIG_STEREO, DEFAULT_STEREO); - config_set_d(CONFIG_CAMERA, DEFAULT_CAMERA); - config_set_d(CONFIG_TEXTURES, DEFAULT_TEXTURES); - config_set_d(CONFIG_GEOMETRY, DEFAULT_GEOMETRY); - config_set_d(CONFIG_REFLECTION, DEFAULT_REFLECTION); - config_set_d(CONFIG_BACKGROUND, DEFAULT_BACKGROUND); - config_set_d(CONFIG_SHADOW, DEFAULT_SHADOW); - config_set_d(CONFIG_AUDIO_RATE, DEFAULT_AUDIO_RATE); - config_set_d(CONFIG_AUDIO_BUFF, DEFAULT_AUDIO_BUFF); - config_set_d(CONFIG_MOUSE_SENSE, DEFAULT_MOUSE_SENSE); - config_set_d(CONFIG_MOUSE_INVERT, DEFAULT_MOUSE_INVERT); - config_set_d(CONFIG_NICE, DEFAULT_NICE); - config_set_d(CONFIG_FPS, DEFAULT_FPS); - config_set_d(CONFIG_SOUND_VOLUME, DEFAULT_SOUND_VOLUME); - config_set_d(CONFIG_MUSIC_VOLUME, DEFAULT_MUSIC_VOLUME); - config_set_d(CONFIG_JOYSTICK, DEFAULT_JOYSTICK); - config_set_d(CONFIG_JOYSTICK_DEVICE, DEFAULT_JOYSTICK_DEVICE); - config_set_d(CONFIG_JOYSTICK_AXIS_X, DEFAULT_JOYSTICK_AXIS_X); - config_set_d(CONFIG_JOYSTICK_AXIS_Y, DEFAULT_JOYSTICK_AXIS_Y); - config_set_d(CONFIG_JOYSTICK_BUTTON_A, DEFAULT_JOYSTICK_BUTTON_A); - config_set_d(CONFIG_JOYSTICK_BUTTON_B, DEFAULT_JOYSTICK_BUTTON_B); - config_set_d(CONFIG_JOYSTICK_BUTTON_L, DEFAULT_JOYSTICK_BUTTON_L); - config_set_d(CONFIG_JOYSTICK_BUTTON_R, DEFAULT_JOYSTICK_BUTTON_R); - config_set_d(CONFIG_JOYSTICK_BUTTON_EXIT, DEFAULT_JOYSTICK_BUTTON_EXIT); - config_set_d(CONFIG_JOYSTICK_CAMERA_1, DEFAULT_JOYSTICK_CAMERA_1); - config_set_d(CONFIG_JOYSTICK_CAMERA_2, DEFAULT_JOYSTICK_CAMERA_2); - config_set_d(CONFIG_JOYSTICK_CAMERA_3, DEFAULT_JOYSTICK_CAMERA_3); - config_set_d(CONFIG_KEY_CAMERA_1, DEFAULT_KEY_CAMERA_1); - config_set_d(CONFIG_KEY_CAMERA_2, DEFAULT_KEY_CAMERA_2); - config_set_d(CONFIG_KEY_CAMERA_3, DEFAULT_KEY_CAMERA_3); - config_set_d(CONFIG_KEY_CAMERA_R, DEFAULT_KEY_CAMERA_R); - config_set_d(CONFIG_KEY_CAMERA_L, DEFAULT_KEY_CAMERA_L); - config_set_d(CONFIG_VIEW_FOV, DEFAULT_VIEW_FOV); - config_set_d(CONFIG_VIEW_DP, DEFAULT_VIEW_DP); - config_set_d(CONFIG_VIEW_DC, DEFAULT_VIEW_DC); - config_set_d(CONFIG_VIEW_DZ, DEFAULT_VIEW_DZ); - config_set_d(CONFIG_ROTATE_FAST, DEFAULT_ROTATE_FAST); - config_set_d(CONFIG_ROTATE_SLOW, DEFAULT_ROTATE_SLOW); - config_set_d(CONFIG_LAST_SET, DEFAULT_LAST_SET); - config_set_d(CONFIG_MODE, DEFAULT_MODE); - config_set_d(CONFIG_CHEAT, DEFAULT_CHEAT); - config_set_s(CONFIG_PLAYER, DEFAULT_PLAYER); - config_set_s(CONFIG_BALL, DEFAULT_BALL); - config_set_s(CONFIG_BALL_BONUS, DEFAULT_BALL_BONUS); - config_set_s(CONFIG_LANG, DEFAULT_LANG); + if (strcmp(s, "none") == 0) + config_set_d(i, 0); + else if (strcmp(s, "left") == 0) + config_set_d(i, SDL_BUTTON_LEFT); + else if (strcmp(s, "right") == 0) + config_set_d(i, SDL_BUTTON_RIGHT); + else if (strcmp(s, "wheelup") == 0) + config_set_d(i, SDL_BUTTON_WHEELUP); + else if (strcmp(s, "middle") == 0) + config_set_d(i, SDL_BUTTON_MIDDLE); + else if (strcmp(s, "wheeldown") == 0) + config_set_d(i, SDL_BUTTON_WHEELDOWN); + else + config_set_d(i, atoi(s)); } -void config_load(void) +static const char *config_mouse_name(int b) { - FILE *fp; + static char buff[sizeof ("256")]; - if ((fp = fopen(config_user(USER_CONFIG_FILE), "r"))) - { - char buf[MAXSTR]; - char key[MAXSTR]; - char val[MAXSTR]; + sprintf(buff, "%d", b); - while (fgets(buf, MAXSTR, fp)) - if (sscanf(buf, "%s %s", key, val) == 2) - { - if (strcmp(key, "fullscreen") == 0) - config_set_d(CONFIG_FULLSCREEN, atoi(val)); - else if (strcmp(key, "width") == 0) - config_set_d(CONFIG_WIDTH, atoi(val)); - else if (strcmp(key, "height") == 0) - config_set_d(CONFIG_HEIGHT, atoi(val)); - else if (strcmp(key, "stereo") == 0) - config_set_d(CONFIG_STEREO, atoi(val)); - else if (strcmp(key, "camera") == 0) - config_set_d(CONFIG_CAMERA, atoi(val)); - else if (strcmp(key, "textures") == 0) - config_set_d(CONFIG_TEXTURES, atoi(val)); - else if (strcmp(key, "geometry") == 0) - config_set_d(CONFIG_GEOMETRY, atoi(val)); - else if (strcmp(key, "reflection") == 0) - config_set_d(CONFIG_REFLECTION, atoi(val)); - else if (strcmp(key, "background") == 0) - config_set_d(CONFIG_BACKGROUND, atoi(val)); - else if (strcmp(key, "shadow") == 0) - config_set_d(CONFIG_SHADOW, atoi(val)); - else if (strcmp(key, "audio_rate") == 0) - config_set_d(CONFIG_AUDIO_RATE, atoi(val)); - else if (strcmp(key, "audio_buff") == 0) - config_set_d(CONFIG_AUDIO_BUFF, atoi(val)); - else if (strcmp(key, "mouse_sense") == 0) - config_set_d(CONFIG_MOUSE_SENSE, atoi(val)); - else if (strcmp(key, "mouse_invert") == 0) - config_set_d(CONFIG_MOUSE_INVERT, atoi(val)); - else if (strcmp(key, "nice") == 0) - config_set_d(CONFIG_NICE, atoi(val)); - else if (strcmp(key, "fps") == 0) - config_set_d(CONFIG_FPS, atoi(val)); - else if (strcmp(key, "sound_volume") == 0) - config_set_d(CONFIG_SOUND_VOLUME, atoi(val)); - else if (strcmp(key, "music_volume") == 0) - config_set_d(CONFIG_MUSIC_VOLUME, atoi(val)); - else if (strcmp(key, "joystick") == 0) - config_set_d(CONFIG_JOYSTICK, atoi(val)); - else if (strcmp(key, "joystick_device") == 0) - config_set_d(CONFIG_JOYSTICK_DEVICE, atoi(val)); - else if (strcmp(key, "joystick_axis_x") == 0) - config_set_d(CONFIG_JOYSTICK_AXIS_X, atoi(val)); - else if (strcmp(key, "joystick_axis_y") == 0) - config_set_d(CONFIG_JOYSTICK_AXIS_Y, atoi(val)); - else if (strcmp(key, "joystick_button_a") == 0) - config_set_d(CONFIG_JOYSTICK_BUTTON_A, atoi(val)); - else if (strcmp(key, "joystick_button_b") == 0) - config_set_d(CONFIG_JOYSTICK_BUTTON_B, atoi(val)); - else if (strcmp(key, "joystick_button_r") == 0) - config_set_d(CONFIG_JOYSTICK_BUTTON_R, atoi(val)); - else if (strcmp(key, "joystick_button_l") == 0) - config_set_d(CONFIG_JOYSTICK_BUTTON_L, atoi(val)); - else if (strcmp(key, "joystick_button_exit") == 0) - config_set_d(CONFIG_JOYSTICK_BUTTON_EXIT, atoi(val)); - else if (strcmp(key, "joystick_camera_1") == 0) - config_set_d(CONFIG_JOYSTICK_CAMERA_1, atoi(val)); - else if (strcmp(key, "joystick_camera_2") == 0) - config_set_d(CONFIG_JOYSTICK_CAMERA_2, atoi(val)); - else if (strcmp(key, "joystick_camera_3") == 0) - config_set_d(CONFIG_JOYSTICK_CAMERA_3, atoi(val)); - else if (strcmp(key, "view_fov") == 0) - config_set_d(CONFIG_VIEW_FOV, atoi(val)); - else if (strcmp(key, "view_dp") == 0) - config_set_d(CONFIG_VIEW_DP, atoi(val)); - else if (strcmp(key, "view_dc") == 0) - config_set_d(CONFIG_VIEW_DC, atoi(val)); - else if (strcmp(key, "view_dz") == 0) - config_set_d(CONFIG_VIEW_DZ, atoi(val)); - else if (strcmp(key, "rotate_fast") == 0) - config_set_d(CONFIG_ROTATE_FAST, atoi(val)); - else if (strcmp(key, "rotate_slow") == 0) - config_set_d(CONFIG_ROTATE_SLOW, atoi(val)); - else if (strcmp(key, "last_set") == 0) - config_set_d(CONFIG_LAST_SET, atoi(val)); - else if (strcmp(key, "mode") == 0) - config_set_d(CONFIG_MODE, atoi(val)); - else if (strcmp(key, "cheat") == 0 && ALLOW_CHEAT) - config_set_d(CONFIG_CHEAT, atoi(val)); - - else if (strcmp(key, "key_camera_1") == 0) - config_key(val, CONFIG_KEY_CAMERA_1, DEFAULT_KEY_CAMERA_1); - else if (strcmp(key, "key_camera_2") == 0) - config_key(val, CONFIG_KEY_CAMERA_2, DEFAULT_KEY_CAMERA_2); - else if (strcmp(key, "key_camera_3") == 0) - config_key(val, CONFIG_KEY_CAMERA_3, DEFAULT_KEY_CAMERA_3); - else if (strcmp(key, "key_camera_r") == 0) - config_key(val, CONFIG_KEY_CAMERA_R, DEFAULT_KEY_CAMERA_R); - else if (strcmp(key, "key_camera_l") == 0) - config_key(val, CONFIG_KEY_CAMERA_L, DEFAULT_KEY_CAMERA_L); - - else if (strcmp(key, "player") == 0) - config_set_s(CONFIG_PLAYER, val); - else if (strcmp(key, "ball") == 0) - config_set_s(CONFIG_BALL, val); - else if (strcmp(key, "ball_bonus") == 0) - config_set_s(CONFIG_BALL_BONUS, val); - else if (strcmp(key, "lang") == 0) - config_set_s(CONFIG_LANG, val); - } - - fclose(fp); - - dirty = 0; + switch (b) + { + case 0: return "none"; break; + case SDL_BUTTON_LEFT: return "left"; break; + case SDL_BUTTON_RIGHT: return "right"; break; + case SDL_BUTTON_WHEELUP: return "wheelup"; break; + case SDL_BUTTON_MIDDLE: return "middle"; break; + case SDL_BUTTON_WHEELDOWN: return "wheeldown"; break; + default: return buff; break; } } -void config_save(void) +/*---------------------------------------------------------------------------*/ + +void config_init(void) { - FILE *fp; + int i; - if (dirty && (fp = fopen(config_user(USER_CONFIG_FILE), "w"))) + /* + * Store index of each option in its associated config symbol and + * initialise current values with defaults. + */ + + for (i = 0; i < ARRAYSIZE(option_d); i++) { - fprintf(fp, "fullscreen %d\n", - option_d[CONFIG_FULLSCREEN]); - fprintf(fp, "width %d\n", - option_d[CONFIG_WIDTH]); - fprintf(fp, "height %d\n", - option_d[CONFIG_HEIGHT]); - fprintf(fp, "stereo %d\n", - option_d[CONFIG_STEREO]); - fprintf(fp, "camera %d\n", - option_d[CONFIG_CAMERA]); - fprintf(fp, "textures %d\n", - option_d[CONFIG_TEXTURES]); - fprintf(fp, "geometry %d\n", - option_d[CONFIG_GEOMETRY]); - fprintf(fp, "reflection %d\n", - option_d[CONFIG_REFLECTION]); - fprintf(fp, "background %d\n", - option_d[CONFIG_BACKGROUND]); - fprintf(fp, "shadow %d\n", - option_d[CONFIG_SHADOW]); - fprintf(fp, "audio_rate %d\n", - option_d[CONFIG_AUDIO_RATE]); - fprintf(fp, "audio_buff %d\n", - option_d[CONFIG_AUDIO_BUFF]); - fprintf(fp, "mouse_sense %d\n", - option_d[CONFIG_MOUSE_SENSE]); - fprintf(fp, "mouse_invert %d\n", - option_d[CONFIG_MOUSE_INVERT]); - fprintf(fp, "nice %d\n", - option_d[CONFIG_NICE]); - fprintf(fp, "fps %d\n", - option_d[CONFIG_FPS]); - fprintf(fp, "sound_volume %d\n", - option_d[CONFIG_SOUND_VOLUME]); - fprintf(fp, "music_volume %d\n", - option_d[CONFIG_MUSIC_VOLUME]); - fprintf(fp, "joystick %d\n", - option_d[CONFIG_JOYSTICK]); - fprintf(fp, "joystick_device %d\n", - option_d[CONFIG_JOYSTICK_DEVICE]); - fprintf(fp, "joystick_axis_x %d\n", - option_d[CONFIG_JOYSTICK_AXIS_X]); - fprintf(fp, "joystick_axis_y %d\n", - option_d[CONFIG_JOYSTICK_AXIS_Y]); - fprintf(fp, "joystick_button_a %d\n", - option_d[CONFIG_JOYSTICK_BUTTON_A]); - fprintf(fp, "joystick_button_b %d\n", - option_d[CONFIG_JOYSTICK_BUTTON_B]); - fprintf(fp, "joystick_button_r %d\n", - option_d[CONFIG_JOYSTICK_BUTTON_R]); - fprintf(fp, "joystick_button_l %d\n", - option_d[CONFIG_JOYSTICK_BUTTON_L]); - fprintf(fp, "joystick_button_exit %d\n", - option_d[CONFIG_JOYSTICK_BUTTON_EXIT]); - fprintf(fp, "joystick_camera_1 %d\n", - option_d[CONFIG_JOYSTICK_CAMERA_1]); - fprintf(fp, "joystick_camera_2 %d\n", - option_d[CONFIG_JOYSTICK_CAMERA_2]); - fprintf(fp, "joystick_camera_3 %d\n", - option_d[CONFIG_JOYSTICK_CAMERA_3]); - fprintf(fp, "view_fov %d\n", - option_d[CONFIG_VIEW_FOV]); - fprintf(fp, "view_dp %d\n", - option_d[CONFIG_VIEW_DP]); - fprintf(fp, "view_dc %d\n", - option_d[CONFIG_VIEW_DC]); - fprintf(fp, "view_dz %d\n", - option_d[CONFIG_VIEW_DZ]); - fprintf(fp, "rotate_fast %d\n", - option_d[CONFIG_ROTATE_FAST]); - fprintf(fp, "rotate_slow %d\n", - option_d[CONFIG_ROTATE_SLOW]); - fprintf(fp, "last_set %d\n", - option_d[CONFIG_LAST_SET]); - fprintf(fp, "mode %d\n", - option_d[CONFIG_MODE]); - if (option_d[CONFIG_CHEAT]) - fprintf(fp, "cheat %d\n", - option_d[CONFIG_CHEAT]); - - fprintf(fp, "key_camera_1 %s\n", - SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_1])); - fprintf(fp, "key_camera_2 %s\n", - SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_2])); - fprintf(fp, "key_camera_3 %s\n", - SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_3])); - fprintf(fp, "key_camera_r %s\n", - SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_R])); - fprintf(fp, "key_camera_l %s\n", - SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_L])); - - fprintf(fp, "player %s\n", option_s[CONFIG_PLAYER]); - fprintf(fp, "ball %s\n", option_s[CONFIG_BALL]); - fprintf(fp, "ball_bonus %s\n", option_s[CONFIG_BALL_BONUS]); - fprintf(fp, "lang %s\n", option_s[CONFIG_LANG]); - - fclose(fp); + *option_d[i].sym = i; + config_set_d(i, option_d[i].def); } - dirty = 0; + for (i = 0; i < ARRAYSIZE(option_s); i++) + { + *option_s[i].sym = i; + config_set_s(i, option_s[i].def); + } } -/*---------------------------------------------------------------------------*/ +/* + * Scan a NUL-terminated string LINE according to the format + * '^?$' and store pointers to the start of key and + * value at DST_KEY and DST_VAL, respectively. No memory is allocated to store + * the strings; instead, the memory pointed to by LINE modified in-place as + * needed. + * + * Return 1 if LINE matches the format, return 0 otherwise. + */ -int config_mode(int f, int w, int h) +static int scan_key_and_value(char **dst_key, char **dst_val, char *line) { - int stereo = config_get_d(CONFIG_STEREO) ? 1 : 0; - int stencil = config_get_d(CONFIG_REFLECTION) ? 1 : 0; + if (line) + { + char *key, *val, *space; - SDL_GL_SetAttribute(SDL_GL_STEREO, stereo); - SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, stencil); + for (key = line; *key && isspace(*key); key++); - /* Try to set the currently specified mode. */ + if (*key) + { + if (dst_key) + *dst_key = key; + } + else + return 0; - if (SDL_SetVideoMode(w, h, 0, SDL_OPENGL | (f ? SDL_FULLSCREEN : 0))) - { - config_set_d(CONFIG_FULLSCREEN, f); - config_set_d(CONFIG_WIDTH, w); - config_set_d(CONFIG_HEIGHT, h); + for (space = key; *space && !isspace(*space); space++); - glViewport(0, 0, w, h); - glClearColor(0.0f, 0.0f, 0.1f, 0.0f); + if (*space) + { + /* NUL-terminate the key, if necessary. */ + + if (dst_key) + { + *space = '\0'; + space++; + } + } + else + return 0; + + for (val = space; *val && isspace(*val); val++); - glEnable(GL_NORMALIZE); - glEnable(GL_CULL_FACE); - glEnable(GL_DEPTH_TEST); - glEnable(GL_TEXTURE_2D); - glEnable(GL_LIGHTING); + if (*val) + { + if (dst_val) + *dst_val = val; + } + else + return 0; return 1; } - /* If the mode failed, try it without stereo. */ + return 0; +} - else if (stereo) +void config_load(void) +{ + fs_file fh; + + if ((fh = fs_open(USER_CONFIG_FILE, "r"))) { - config_set_d(CONFIG_STEREO, 0); - return config_mode(f, w, h); + char *line, *key, *val; + + while (read_line(&line, fh)) + { + if (scan_key_and_value(&key, &val, line)) + { + int i; + + /* Look up an integer option by that name. */ + + for (i = 0; i < ARRAYSIZE(option_d); i++) + { + if (strcmp(key, option_d[i].name) == 0) + { + /* Translate some strings to integers. */ + + if (i == CONFIG_MOUSE_CAMERA_1 || + i == CONFIG_MOUSE_CAMERA_2 || + i == CONFIG_MOUSE_CAMERA_3 || + i == CONFIG_MOUSE_CAMERA_TOGGLE || + i == CONFIG_MOUSE_CAMERA_L || + i == CONFIG_MOUSE_CAMERA_R) + { + config_mouse(val, i); + } + else if (i == CONFIG_KEY_FORWARD || + i == CONFIG_KEY_BACKWARD || + i == CONFIG_KEY_LEFT || + i == CONFIG_KEY_RIGHT || + i == CONFIG_KEY_CAMERA_1 || + i == CONFIG_KEY_CAMERA_2 || + i == CONFIG_KEY_CAMERA_3 || + i == CONFIG_KEY_CAMERA_TOGGLE || + i == CONFIG_KEY_CAMERA_R || + i == CONFIG_KEY_CAMERA_L || + i == CONFIG_KEY_PAUSE || + i == CONFIG_KEY_RESTART || + i == CONFIG_KEY_SCORE_NEXT || + i == CONFIG_KEY_ROTATE_FAST) + { + config_key(val, i); + } + else + config_set_d(i, atoi(val)); + + /* Stop looking. */ + + break; + } + } + + /* Look up a string option by that name.*/ + + for (i = 0; i < ARRAYSIZE(option_s); i++) + { + if (strcmp(key, option_s[i].name) == 0) + { + config_set_s(i, val); + break; + } + } + } + free(line); + } + fs_close(fh); + + dirty = 0; } +} - /* If that mode failed, try it without reflections. */ +void config_save(void) +{ + fs_file fh; - else if (stencil) + if (dirty && (fh = fs_open(USER_CONFIG_FILE, "w"))) { - config_set_d(CONFIG_REFLECTION, 0); - return config_mode(f, w, h); - } + int i; - /* If THAT mode failed, punt. */ + /* Write out integer options. */ - return 0; + for (i = 0; i < ARRAYSIZE(option_d); i++) + { + const char *s = NULL; + + /* Translate some integers to strings. */ + + if (i == CONFIG_MOUSE_CAMERA_1 || + i == CONFIG_MOUSE_CAMERA_2 || + i == CONFIG_MOUSE_CAMERA_3 || + i == CONFIG_MOUSE_CAMERA_TOGGLE || + i == CONFIG_MOUSE_CAMERA_L || + i == CONFIG_MOUSE_CAMERA_R) + { + s = config_mouse_name(option_d[i].cur); + } + else if (i == CONFIG_KEY_FORWARD || + i == CONFIG_KEY_BACKWARD || + i == CONFIG_KEY_LEFT || + i == CONFIG_KEY_RIGHT || + i == CONFIG_KEY_CAMERA_1 || + i == CONFIG_KEY_CAMERA_2 || + i == CONFIG_KEY_CAMERA_3 || + i == CONFIG_KEY_CAMERA_TOGGLE || + i == CONFIG_KEY_CAMERA_R || + i == CONFIG_KEY_CAMERA_L || + i == CONFIG_KEY_PAUSE || + i == CONFIG_KEY_RESTART || + i == CONFIG_KEY_SCORE_NEXT || + i == CONFIG_KEY_ROTATE_FAST) + { + s = SDL_GetKeyName((SDLKey) option_d[i].cur); + } + else if (i == CONFIG_CHEAT) + { + if (!config_cheat()) + continue; + } + + if (s) + fs_printf(fh, "%-25s %s\n", option_d[i].name, s); + else + fs_printf(fh, "%-25s %d\n", option_d[i].name, option_d[i].cur); + } + + /* Write out string options. */ + + for (i = 0; i < ARRAYSIZE(option_s); i++) + { + if (option_s[i].cur && *option_s[i].cur) + fs_printf(fh, "%-25s %s\n", option_s[i].name, option_s[i].cur); + } + + fs_close(fh); + } + + dirty = 0; } /*---------------------------------------------------------------------------*/ void config_set_d(int i, int d) { - option_d[i] = d; + option_d[i].cur = d; dirty = 1; } void config_tgl_d(int i) { - option_d[i] = (option_d[i] ? 0 : 1); + option_d[i].cur = (option_d[i].cur ? 0 : 1); dirty = 1; } int config_tst_d(int i, int d) { - return (option_d[i] == d) ? 1 : 0; + return (option_d[i].cur == d) ? 1 : 0; } int config_get_d(int i) { - return option_d[i]; + return option_d[i].cur; } /*---------------------------------------------------------------------------*/ void config_set_s(int i, const char *src) { - int len = (int) strlen(src); + if (option_s[i].cur) + free(option_s[i].cur); - if (option_s[i]) - free(option_s[i]); - - if ((option_s[i] = (char *) malloc(len + 1))) - strncpy(option_s[i], src, len + 1); + option_s[i].cur = strdup(src); dirty = 1; } -void config_get_s(int i, char *dst, int len) -{ - strncpy(dst, option_s[i], len); -} - -const char * config_simple_get_s(int i) +const char *config_get_s(int i) { - return option_s[i]; + return option_s[i].cur; } /*---------------------------------------------------------------------------*/ -static int grabbed = 0; -static int paused = 0; - -void config_set_grab(void) -{ - SDL_WarpMouse(config_get_d(CONFIG_WIDTH) / 2, - config_get_d(CONFIG_HEIGHT) / 2); - SDL_WM_GrabInput(SDL_GRAB_ON); - SDL_ShowCursor(SDL_DISABLE); - grabbed = 1; -} - -void config_clr_grab(void) -{ - SDL_WM_GrabInput(SDL_GRAB_OFF); - SDL_ShowCursor(SDL_ENABLE); - grabbed = 0; -} - -int config_get_grab(void) -{ - return grabbed; -} - -int config_get_pause(void) +int config_cheat(void) { - return paused; + return config_get_d(CONFIG_CHEAT); } -void config_set_pause(void) +void config_set_cheat(void) { - Mix_PauseMusic(); - paused = 1; - - if (grabbed) - { - SDL_ShowCursor(SDL_ENABLE); - SDL_WM_GrabInput(SDL_GRAB_OFF); - } + config_set_d(CONFIG_CHEAT, 1); } -void config_clr_pause(void) +void config_clr_cheat(void) { - Mix_ResumeMusic(); - paused = 0; - - if (grabbed) - { - SDL_WM_GrabInput(SDL_GRAB_ON); - SDL_ShowCursor(SDL_DISABLE); - } + config_set_d(CONFIG_CHEAT, 0); } -void config_tgl_pause(void) -{ - if (paused) - config_clr_pause(); - else - config_set_pause(); -} /*---------------------------------------------------------------------------*/ -void config_push_persp(float fov, float n, float f) +int config_screenshot(void) { - GLdouble m[4][4]; - - GLdouble r = fov / 2 * V_PI / 180; - GLdouble s = sin(r); - GLdouble c = cos(r) / s; - - GLdouble a = ((GLdouble) option_d[CONFIG_WIDTH] / - (GLdouble) option_d[CONFIG_HEIGHT]); - - glMatrixMode(GL_PROJECTION); - { - glPushMatrix(); - glLoadIdentity(); - - m[0][0] = c/a; - m[0][1] = 0.0; - m[0][2] = 0.0; - m[0][3] = 0.0; - m[1][0] = 0.0; - m[1][1] = c; - m[1][2] = 0.0; - m[1][3] = 0.0; - m[2][0] = 0.0; - m[2][1] = 0.0; - m[2][2] = -(f + n) / (f - n); - m[2][3] = -1.0; - m[3][0] = 0.0; - m[3][1] = 0.0; - m[3][2] = -2.0 * n * f / (f - n); - m[3][3] = 0.0; - - glMultMatrixd(&m[0][0]); - } - glMatrixMode(GL_MODELVIEW); -} - -void config_push_ortho(void) -{ - GLdouble w = (GLdouble) option_d[CONFIG_WIDTH]; - GLdouble h = (GLdouble) option_d[CONFIG_HEIGHT]; - - glMatrixMode(GL_PROJECTION); - { - glPushMatrix(); - glLoadIdentity(); - glOrtho(0.0, w, 0.0, h, -1.0, +1.0); - } - glMatrixMode(GL_MODELVIEW); -} - -void config_pop_matrix(void) -{ - glMatrixMode(GL_PROJECTION); - { - glPopMatrix(); - } - glMatrixMode(GL_MODELVIEW); -} - -void config_clear(void) -{ - if (option_d[CONFIG_REFLECTION]) - glClear(GL_COLOR_BUFFER_BIT | - GL_DEPTH_BUFFER_BIT | - GL_STENCIL_BUFFER_BIT); - else - glClear(GL_COLOR_BUFFER_BIT | - GL_DEPTH_BUFFER_BIT); + return ++option_d[CONFIG_SCREENSHOT].cur; } /*---------------------------------------------------------------------------*/