From e0e2697df0d0da483a91b1248c120aef6b3caf9b Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Sun, 24 Mar 2002 01:37:22 +0100 Subject: [PATCH] rnd-20020324-1-src --- src/files.c | 262 +++++++++++++++++++++++++ src/files.h | 3 + src/init.c | 118 +----------- src/init.h | 1 - src/libgame/joystick.c | 429 ++++++++++------------------------------- src/libgame/joystick.h | 32 +-- src/libgame/misc.h | 6 - src/libgame/msdos.c | 54 ++++++ src/libgame/msdos.h | 16 +- src/libgame/sdl.c | 120 ++++++++++++ src/libgame/sdl.h | 4 + src/libgame/setup.c | 366 +++++------------------------------ src/libgame/setup.h | 38 +++- src/libgame/system.c | 52 ++++- src/libgame/system.h | 22 ++- src/libgame/types.h | 1 + src/main.c | 1 - src/main.h | 5 +- src/screens.c | 216 ++++++--------------- src/tools.c | 8 - 20 files changed, 785 insertions(+), 969 deletions(-) diff --git a/src/files.c b/src/files.c index 00e47161..1bcbec75 100644 --- a/src/files.c +++ b/src/files.c @@ -38,6 +38,10 @@ #define SCORE_COOKIE "ROCKSNDIAMONDS_SCORE_FILE_VERSION_1.2" +/* ========================================================================= */ +/* level file functions */ +/* ========================================================================= */ + static void setLevelInfoToDefaults() { int i, x, y; @@ -668,6 +672,11 @@ void SaveLevel(int level_nr) SetFilePermissions(filename, PERMS_PRIVATE); } + +/* ========================================================================= */ +/* tape file functions */ +/* ========================================================================= */ + static void setTapeInfoToDefaults() { int i; @@ -1064,6 +1073,11 @@ void DumpTape(struct TapeInfo *tape) printf("-------------------------------------------------------------------------------\n"); } + +/* ========================================================================= */ +/* score file functions */ +/* ========================================================================= */ + void LoadScore(int level_nr) { int i; @@ -1140,3 +1154,251 @@ void SaveScore(int level_nr) SetFilePermissions(filename, PERMS_PUBLIC); } + + +/* ========================================================================= */ +/* setup file functions */ +/* ========================================================================= */ + +#define TOKEN_STR_PLAYER_PREFIX "player_" + +/* global setup */ +#define SETUP_TOKEN_PLAYER_NAME 0 +#define SETUP_TOKEN_SOUND 1 +#define SETUP_TOKEN_SOUND_LOOPS 2 +#define SETUP_TOKEN_SOUND_MUSIC 3 +#define SETUP_TOKEN_SOUND_SIMPLE 4 +#define SETUP_TOKEN_SCROLL_DELAY 5 +#define SETUP_TOKEN_SOFT_SCROLLING 6 +#define SETUP_TOKEN_FADING 7 +#define SETUP_TOKEN_AUTORECORD 8 +#define SETUP_TOKEN_QUICK_DOORS 9 +#define SETUP_TOKEN_TEAM_MODE 10 +#define SETUP_TOKEN_HANDICAP 11 +#define SETUP_TOKEN_TIME_LIMIT 12 +#define SETUP_TOKEN_FULLSCREEN 13 + +#define NUM_GLOBAL_SETUP_TOKENS 14 + +/* player setup */ +#define SETUP_TOKEN_USE_JOYSTICK 0 +#define SETUP_TOKEN_JOY_DEVICE_NAME 1 +#define SETUP_TOKEN_JOY_XLEFT 2 +#define SETUP_TOKEN_JOY_XMIDDLE 3 +#define SETUP_TOKEN_JOY_XRIGHT 4 +#define SETUP_TOKEN_JOY_YUPPER 5 +#define SETUP_TOKEN_JOY_YMIDDLE 6 +#define SETUP_TOKEN_JOY_YLOWER 7 +#define SETUP_TOKEN_JOY_SNAP 8 +#define SETUP_TOKEN_JOY_BOMB 9 +#define SETUP_TOKEN_KEY_LEFT 10 +#define SETUP_TOKEN_KEY_RIGHT 11 +#define SETUP_TOKEN_KEY_UP 12 +#define SETUP_TOKEN_KEY_DOWN 13 +#define SETUP_TOKEN_KEY_SNAP 14 +#define SETUP_TOKEN_KEY_BOMB 15 + +#define NUM_PLAYER_SETUP_TOKENS 16 + +static struct SetupInfo si; +static struct SetupInputInfo sii; + +static struct TokenInfo global_setup_tokens[] = +{ + /* global setup */ + { TYPE_STRING, &si.player_name, "player_name" }, + { TYPE_SWITCH, &si.sound, "sound" }, + { TYPE_SWITCH, &si.sound_loops, "repeating_sound_loops" }, + { TYPE_SWITCH, &si.sound_music, "background_music" }, + { TYPE_SWITCH, &si.sound_simple, "simple_sound_effects" }, + { TYPE_SWITCH, &si.scroll_delay, "scroll_delay" }, + { TYPE_SWITCH, &si.soft_scrolling, "soft_scrolling" }, + { TYPE_SWITCH, &si.fading, "screen_fading" }, + { TYPE_SWITCH, &si.autorecord, "automatic_tape_recording" }, + { TYPE_SWITCH, &si.quick_doors, "quick_doors" }, + { TYPE_SWITCH, &si.team_mode, "team_mode" }, + { TYPE_SWITCH, &si.handicap, "handicap" }, + { TYPE_SWITCH, &si.time_limit, "time_limit" }, + { TYPE_SWITCH, &si.fullscreen, "fullscreen" } +}; + +static struct TokenInfo player_setup_tokens[] = +{ + /* player setup */ + { TYPE_BOOLEAN, &sii.use_joystick, ".use_joystick" }, + { TYPE_STRING, &sii.joy.device_name, ".joy.device_name" }, + { TYPE_INTEGER, &sii.joy.xleft, ".joy.xleft" }, + { TYPE_INTEGER, &sii.joy.xmiddle, ".joy.xmiddle" }, + { TYPE_INTEGER, &sii.joy.xright, ".joy.xright" }, + { TYPE_INTEGER, &sii.joy.yupper, ".joy.yupper" }, + { TYPE_INTEGER, &sii.joy.ymiddle, ".joy.ymiddle" }, + { TYPE_INTEGER, &sii.joy.ylower, ".joy.ylower" }, + { TYPE_INTEGER, &sii.joy.snap, ".joy.snap_field" }, + { TYPE_INTEGER, &sii.joy.bomb, ".joy.place_bomb" }, + { TYPE_KEY, &sii.key.left, ".key.move_left" }, + { TYPE_KEY, &sii.key.right, ".key.move_right" }, + { TYPE_KEY, &sii.key.up, ".key.move_up" }, + { TYPE_KEY, &sii.key.down, ".key.move_down" }, + { TYPE_KEY, &sii.key.snap, ".key.snap_field" }, + { TYPE_KEY, &sii.key.bomb, ".key.place_bomb" } +}; + +static void setSetupInfoToDefaults(struct SetupInfo *si) +{ + int i; + + si->player_name = getStringCopy(getLoginName()); + + si->sound = TRUE; + si->sound_loops = TRUE; + si->sound_music = TRUE; + si->sound_simple = TRUE; + si->toons = TRUE; + si->double_buffering = TRUE; + si->direct_draw = !si->double_buffering; + si->scroll_delay = TRUE; + si->soft_scrolling = TRUE; + si->fading = FALSE; + si->autorecord = TRUE; + si->quick_doors = FALSE; + si->team_mode = FALSE; + si->handicap = TRUE; + si->time_limit = TRUE; + si->fullscreen = FALSE; + + for (i=0; iinput[i].use_joystick = FALSE; + si->input[i].joy.device_name=getStringCopy(getDeviceNameFromJoystickNr(i)); + si->input[i].joy.xleft = JOYSTICK_XLEFT; + si->input[i].joy.xmiddle = JOYSTICK_XMIDDLE; + si->input[i].joy.xright = JOYSTICK_XRIGHT; + si->input[i].joy.yupper = JOYSTICK_YUPPER; + si->input[i].joy.ymiddle = JOYSTICK_YMIDDLE; + si->input[i].joy.ylower = JOYSTICK_YLOWER; + si->input[i].joy.snap = (i == 0 ? JOY_BUTTON_1 : 0); + si->input[i].joy.bomb = (i == 0 ? JOY_BUTTON_2 : 0); + si->input[i].key.left = (i == 0 ? DEFAULT_KEY_LEFT : KSYM_UNDEFINED); + si->input[i].key.right = (i == 0 ? DEFAULT_KEY_RIGHT : KSYM_UNDEFINED); + si->input[i].key.up = (i == 0 ? DEFAULT_KEY_UP : KSYM_UNDEFINED); + si->input[i].key.down = (i == 0 ? DEFAULT_KEY_DOWN : KSYM_UNDEFINED); + si->input[i].key.snap = (i == 0 ? DEFAULT_KEY_SNAP : KSYM_UNDEFINED); + si->input[i].key.bomb = (i == 0 ? DEFAULT_KEY_BOMB : KSYM_UNDEFINED); + } +} + +static void decodeSetupFileList(struct SetupFileList *setup_file_list) +{ + int i, pnr; + + if (!setup_file_list) + return; + + /* handle global setup values */ + si = setup; + for (i=0; i MAX_PLAYER_NAME_LEN) + setup.player_name[MAX_PLAYER_NAME_LEN] = '\0'; + else if (strlen(setup.player_name) < MAX_PLAYER_NAME_LEN) + { + char *new_name = checked_malloc(MAX_PLAYER_NAME_LEN + 1); + + strcpy(new_name, setup.player_name); + free(setup.player_name); + setup.player_name = new_name; + } + } + else + Error(ERR_WARN, "using default setup values"); +} + +void SaveSetup() +{ + char *filename = getSetupFilename(); + FILE *file; + int i, pnr; + + InitUserDataDirectory(); + + if (!(file = fopen(filename, MODE_WRITE))) + { + Error(ERR_WARN, "cannot write setup file '%s'", filename); + return; + } + + fprintf(file, "%s\n", getFormattedSetupEntry(TOKEN_STR_FILE_IDENTIFIER, + getCookie("SETUP"))); + fprintf(file, "\n"); + + /* handle global setup values */ + si = setup; + for (i=0; i= SDL_NumJoysticks()) - joystick_nr = -1; - - /* misuse joystick file descriptor variable to store joystick number */ - joystick.fd[i] = joystick_nr; - - /* this allows subsequent calls to 'InitJoysticks' for re-initialization */ - if (Check_SDL_JoystickOpened(joystick_nr)) - Close_SDL_Joystick(joystick_nr); - - if (!setup.input[i].use_joystick) - continue; - - if (!Open_SDL_Joystick(joystick_nr)) - { - Error(ERR_WARN, "cannot open joystick %d", joystick_nr); - continue; - } - - joystick.status = JOYSTICK_ACTIVATED; - } - -#else /* !TARGET_SDL */ - -#if defined(PLATFORM_UNIX) - for (i=0; i= num_joysticks) - joystick_nr = -1; - - /* misuse joystick file descriptor variable to store joystick number */ - joystick.fd[i] = joystick_nr; - } -#endif - -#endif /* !TARGET_SDL */ -} - void InitGfx() { int i; diff --git a/src/init.h b/src/init.h index 1088db26..f7f447c1 100644 --- a/src/init.h +++ b/src/init.h @@ -18,6 +18,5 @@ void OpenAll(void); void CloseAllAndExit(int); -void InitJoysticks(void); #endif diff --git a/src/libgame/joystick.c b/src/libgame/joystick.c index 4bca8893..04731ec8 100644 --- a/src/libgame/joystick.c +++ b/src/libgame/joystick.c @@ -19,20 +19,89 @@ #include "misc.h" -#define TRANSLATE_JOYSYMBOL_TO_JOYNAME 0 -#define TRANSLATE_JOYNAME_TO_JOYSYMBOL 1 +/* ========================================================================= */ +/* platform dependant joystick functions */ +/* ========================================================================= */ + +#if defined(PLATFORM_UNIX) && !defined(TARGET_SDL) +void UnixInitJoysticks() +{ + int i; + + for (i=0; i middle) - return 0; - if (margin > middle && actual < middle) - return 0; - - range = ABS(margin - middle); - pos = ABS(actual - middle); - percentage = (int)(pos * 100 / range); - - if (percentage > 100) - percentage = 100; - - return percentage; -} -#endif - -#if defined(TARGET_SDL) - -static SDL_Joystick *sdl_joystick[MAX_PLAYERS] = { NULL, NULL, NULL, NULL }; -static int sdl_js_axis[MAX_PLAYERS][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} }; -static int sdl_js_button[MAX_PLAYERS][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} }; - -SDL_Joystick *Get_SDL_Joystick(int nr) -{ - return sdl_joystick[nr]; -} - -boolean Open_SDL_Joystick(int nr) -{ - if (nr < 0 || nr > MAX_PLAYERS) - return FALSE; - - return ((sdl_joystick[nr] = SDL_JoystickOpen(nr)) == NULL ? FALSE : TRUE); -} - -void Close_SDL_Joystick(int nr) -{ - if (nr < 0 || nr > MAX_PLAYERS) - return; - - SDL_JoystickClose(sdl_joystick[nr]); -} - -boolean Check_SDL_JoystickOpened(int nr) -{ - if (nr < 0 || nr > MAX_PLAYERS) - return FALSE; - - return (SDL_JoystickOpened(nr) ? TRUE : FALSE); -} - -void HandleJoystickEvent(Event *event) -{ - switch(event->type) - { - case SDL_JOYAXISMOTION: - if (event->jaxis.axis < 2) - { - sdl_js_axis[event->jaxis.which][event->jaxis.axis]= event->jaxis.value; - -#if 0 - printf("js_%d %s-axis: %d\n", - event->jaxis.which, - (event->jaxis.axis == 0 ? "x" : "y"), - event->jaxis.value); -#endif - } - break; - - case SDL_JOYBUTTONDOWN: - if (event->jbutton.button < 2) - { - sdl_js_button[event->jbutton.which][event->jbutton.button] = TRUE; - -#if 0 - printf("js_%d button %d: pressed\n", - event->jbutton.which, - event->jbutton.button); -#endif - } - break; - - case SDL_JOYBUTTONUP: - if (event->jbutton.button < 2) - { - sdl_js_button[event->jbutton.which][event->jbutton.button] = FALSE; - -#if 0 - printf("js_%d button %d: released\n", - event->jbutton.which, - event->jbutton.button); -#endif - } - break; - - default: - break; - } -} - -int Get_SDL_Joystick_Axis(int nr, int axis) -{ - if (nr < 0 || nr > MAX_PLAYERS) - return 0; - - if (axis < 0 || axis > 1) - return 0; - - return sdl_js_axis[nr][axis]; -} - -void CheckJoystickData() -{ -} - -int Joystick(int player_nr) +static int JoystickPositionPercent(int center, int border, int actual) { -#if 0 - int joystick_nr = stored_player[player_nr].joystick_fd; -#else - int joystick_nr = joystick.fd[player_nr]; -#endif - int js_x,js_y, js_b1,js_b2; - int left, right, up, down; - int result = 0; + long range, position; + int percent; - if (joystick.status != JOYSTICK_ACTIVATED) + if (border < center && actual > center) return 0; - - if (!setup.input[player_nr].use_joystick || - !Check_SDL_JoystickOpened(joystick_nr)) + if (border > center && actual < center) return 0; - js_x = sdl_js_axis[joystick_nr][0]; - js_y = sdl_js_axis[joystick_nr][1]; - - js_b1 = sdl_js_button[joystick_nr][0]; - js_b2 = sdl_js_button[joystick_nr][1]; - - - -#if 0 - printf("JOYSTICK %d: js_x == %d, js_y == %d, js_b1 == %d, js_b2 == %d\n", - joystick_nr, js_x, js_y, js_b1, js_b2); -#endif - - - - left = JoystickPosition(setup.input[player_nr].joy.xmiddle, - setup.input[player_nr].joy.xleft, js_x); - right = JoystickPosition(setup.input[player_nr].joy.xmiddle, - setup.input[player_nr].joy.xright, js_x); - up = JoystickPosition(setup.input[player_nr].joy.ymiddle, - setup.input[player_nr].joy.yupper, js_y); - down = JoystickPosition(setup.input[player_nr].joy.ymiddle, - setup.input[player_nr].joy.ylower, js_y); - - if (left > JOYSTICK_PERCENT) - result |= JOY_LEFT; - else if (right > JOYSTICK_PERCENT) - result |= JOY_RIGHT; - if (up > JOYSTICK_PERCENT) - result |= JOY_UP; - else if (down > JOYSTICK_PERCENT) - result |= JOY_DOWN; - - if (js_b1) - result |= JOY_BUTTON_1; - if (js_b2) - result |= JOY_BUTTON_2; - + range = ABS(border - center); + position = ABS(actual - center); + percent = (int)(position * 100 / range); -#if 0 - printf("result == 0x%08x\n", result); -#endif - + if (percent > 100) + percent = 100; - - return result; + return percent; } -#else /* !TARGET_SDL */ - void CheckJoystickData() { int i; @@ -326,11 +226,6 @@ void CheckJoystickData() for(i=0; i= setup.input[i].joy.xmiddle) setup.input[i].joy.xleft = setup.input[i].joy.xmiddle - distance; if (setup.input[i].joy.xright <= setup.input[i].joy.xmiddle) @@ -343,62 +238,37 @@ void CheckJoystickData() } } -#if defined(PLATFORM_UNIX) int Joystick(int player_nr) { -#if defined(PLATFORM_FREEBSD) - struct joystick joy_ctrl; -#else - struct joystick_control - { - int buttons; - int x; - int y; - } joy_ctrl; -#endif - -#if 0 - int joystick_fd = stored_player[player_nr].joystick_fd; -#else int joystick_fd = joystick.fd[player_nr]; -#endif - int js_x,js_y, js_b1,js_b2; + int js_x, js_y; + boolean js_b1, js_b2; int left, right, up, down; - int result = 0; + int result = JOY_NO_ACTION; if (joystick.status != JOYSTICK_ACTIVATED) - return 0; + return JOY_NO_ACTION; if (joystick_fd < 0 || !setup.input[player_nr].use_joystick) - return 0; + return JOY_NO_ACTION; - if (read(joystick_fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl)) + if (!ReadJoystick(joystick_fd, &js_x, &js_y, &js_b1, &js_b2)) { Error(ERR_WARN, "cannot read joystick device '%s'", setup.input[player_nr].joy.device_name); + joystick.status = JOYSTICK_NOT_AVAILABLE; - return 0; + return JOY_NO_ACTION; } - js_x = joy_ctrl.x; - js_y = joy_ctrl.y; - -#if defined(PLATFORM_FREEBSD) - js_b1 = joy_ctrl.b1; - js_b2 = joy_ctrl.b2; -#else - js_b1 = joy_ctrl.buttons & 1; - js_b2 = joy_ctrl.buttons & 2; -#endif - - left = JoystickPosition(setup.input[player_nr].joy.xmiddle, - setup.input[player_nr].joy.xleft, js_x); - right = JoystickPosition(setup.input[player_nr].joy.xmiddle, - setup.input[player_nr].joy.xright, js_x); - up = JoystickPosition(setup.input[player_nr].joy.ymiddle, - setup.input[player_nr].joy.yupper, js_y); - down = JoystickPosition(setup.input[player_nr].joy.ymiddle, - setup.input[player_nr].joy.ylower, js_y); + left = JoystickPositionPercent(setup.input[player_nr].joy.xmiddle, + setup.input[player_nr].joy.xleft, js_x); + right = JoystickPositionPercent(setup.input[player_nr].joy.xmiddle, + setup.input[player_nr].joy.xright, js_x); + up = JoystickPositionPercent(setup.input[player_nr].joy.ymiddle, + setup.input[player_nr].joy.yupper, js_y); + down = JoystickPositionPercent(setup.input[player_nr].joy.ymiddle, + setup.input[player_nr].joy.ylower, js_y); if (left > JOYSTICK_PERCENT) result |= JOY_LEFT; @@ -417,74 +287,6 @@ int Joystick(int player_nr) return result; } -#else /* PLATFORM_MSDOS */ - -/* allegro global variables for joystick control */ -extern int num_joysticks; -extern JOYSTICK_INFO joy[]; - -int Joystick(int player_nr) -{ -#if 0 - int joystick_nr = stored_player[player_nr].joystick_fd; -#else - int joystick_nr = joystick.fd[player_nr]; -#endif - int result = 0; - - if (joystick.status != JOYSTICK_ACTIVATED) - return 0; - - if (joystick_nr < 0) - return 0; - - /* the allegro global variable 'num_joysticks' contains the number - of joysticks found at initialization under MS-DOS / Windows */ - -#if 0 - if (joystick_nr >= num_joysticks || !setup.input[player_nr].use_joystick) - return 0; -#else - -#if 1 - /* - if (joystick_nr >= num_joysticks || - (game_status == PLAYING && !setup.input[player_nr].use_joystick)) - return 0; - */ - - if (joystick_nr >= num_joysticks || !setup.input[player_nr].use_joystick) - return 0; - -#else - if (joystick_nr >= num_joysticks) - return 0; -#endif - -#endif - - poll_joystick(); - - if (joy[joystick_nr].stick[0].axis[0].d1) - result |= JOY_LEFT; - else if (joy[joystick_nr].stick[0].axis[0].d2) - result |= JOY_RIGHT; - if (joy[joystick_nr].stick[0].axis[1].d1) - result |= JOY_UP; - else if (joy[joystick_nr].stick[0].axis[1].d2) - result |= JOY_DOWN; - - if (joy[joystick_nr].button[0].b) - result |= JOY_BUTTON_1; - if (joy[joystick_nr].button[1].b) - result |= JOY_BUTTON_2; - - return result; -} -#endif /* PLATFORM_MSDOS */ - -#endif /* !TARGET_SDL */ - int JoystickButton(int player_nr) { static int last_joy_button[MAX_PLAYERS] = { 0, 0, 0, 0 }; @@ -516,16 +318,7 @@ int AnyJoystick() int result = 0; for (i=0; i= num_joysticks) + joystick_nr = -1; + + /* misuse joystick file descriptor variable to store joystick number */ + joystick.fd[i] = joystick_nr; + } +} + +boolean MSDOSReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2) +{ + /* the allegro global variable 'num_joysticks' contains the number + of joysticks found at initialization under MS-DOS / Windows */ + + if (nr < 0 || nr >= num_joysticks) + return FALSE; + + poll_joystick(); + + if (x != NULL) + *x = joy[nr].stick[0].axis[0].pos; + if (y != NULL) + *y = joy[nr].stick[0].axis[1].pos; + + if (b1 != NULL) + *b1 = joy[nr].button[0].b; + if (b2 != NULL) + *b2 = joy[nr].button[1].b; + + return TRUE; +} + #endif /* PLATFORM_MSDOS */ diff --git a/src/libgame/msdos.h b/src/libgame/msdos.h index 6e16b214..ab0ea083 100644 --- a/src/libgame/msdos.h +++ b/src/libgame/msdos.h @@ -40,6 +40,18 @@ #define XRES 800 #define YRES 600 +/* allegro defines some macros that bother the rest of the program */ +#ifdef joy_x +#undef joy_x +#undef joy_y +#undef joy_left +#undef joy_right +#undef joy_up +#undef joy_down +#undef joy_b1 +#undef joy_b2 +#endif + /* additional Allegro keyboard mapping */ /* The following are all undefined in Allegro */ @@ -450,7 +462,6 @@ /* end of X11 keyboard mapping */ -#define JOYSTICK_FILENAME "joystick.cnf" #define screen myscreen @@ -741,4 +752,7 @@ void MSDOSCloseAudio(void); void NetworkServer(int, int); +void MSDOSInitJoysticks(); +boolean MSDOSReadJoystick(int, int *, int *, boolean *, boolean *); + #endif /* MSDOS_H */ diff --git a/src/libgame/sdl.c b/src/libgame/sdl.c index a2d82fab..7cd64118 100644 --- a/src/libgame/sdl.c +++ b/src/libgame/sdl.c @@ -13,6 +13,7 @@ #include "system.h" #include "sound.h" +#include "joystick.h" #include "misc.h" @@ -932,4 +933,123 @@ inline void SDLNextEvent(Event *event) #endif } + +/* ========================================================================= */ +/* joystick functions */ +/* ========================================================================= */ + +static SDL_Joystick *sdl_joystick[MAX_PLAYERS] = { NULL, NULL, NULL, NULL }; +static int sdl_js_axis[MAX_PLAYERS][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} }; +static int sdl_js_button[MAX_PLAYERS][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} }; + +static boolean SDLOpenJoystick(int nr) +{ + if (nr < 0 || nr > MAX_PLAYERS) + return FALSE; + + return ((sdl_joystick[nr] = SDL_JoystickOpen(nr)) == NULL ? FALSE : TRUE); +} + +static void SDLCloseJoystick(int nr) +{ + if (nr < 0 || nr > MAX_PLAYERS) + return; + + SDL_JoystickClose(sdl_joystick[nr]); +} + +static boolean SDLCheckJoystickOpened(int nr) +{ + if (nr < 0 || nr > MAX_PLAYERS) + return FALSE; + + return (SDL_JoystickOpened(nr) ? TRUE : FALSE); +} + +void HandleJoystickEvent(Event *event) +{ + switch(event->type) + { + case SDL_JOYAXISMOTION: + if (event->jaxis.axis < 2) + sdl_js_axis[event->jaxis.which][event->jaxis.axis]= event->jaxis.value; + break; + + case SDL_JOYBUTTONDOWN: + if (event->jbutton.button < 2) + sdl_js_button[event->jbutton.which][event->jbutton.button] = TRUE; + break; + + case SDL_JOYBUTTONUP: + if (event->jbutton.button < 2) + sdl_js_button[event->jbutton.which][event->jbutton.button] = FALSE; + break; + + default: + break; + } +} + +void SDLInitJoysticks() +{ + static boolean sdl_joystick_subsystem_initialized = FALSE; + int i; + + if (!sdl_joystick_subsystem_initialized) + { + sdl_joystick_subsystem_initialized = TRUE; + + if (SDL_Init(SDL_INIT_JOYSTICK) < 0) + { + Error(ERR_EXIT, "SDL_Init() failed: %s", SDL_GetError()); + return; + } + } + + for (i=0; i= SDL_NumJoysticks()) + joystick_nr = -1; + + /* misuse joystick file descriptor variable to store joystick number */ + joystick.fd[i] = joystick_nr; + + /* this allows subsequent calls to 'InitJoysticks' for re-initialization */ + if (SDLCheckJoystickOpened(joystick_nr)) + SDLCloseJoystick(joystick_nr); + + if (!setup.input[i].use_joystick) + continue; + + if (!SDLOpenJoystick(joystick_nr)) + { + Error(ERR_WARN, "cannot open joystick %d", joystick_nr); + continue; + } + + joystick.status = JOYSTICK_ACTIVATED; + } +} + +boolean SDLReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2) +{ + if (nr < 0 || nr >= MAX_PLAYERS) + return FALSE; + + if (x != NULL) + *x = sdl_js_axis[nr][0]; + if (y != NULL) + *y = sdl_js_axis[nr][1]; + + if (b1 != NULL) + *b1 = sdl_js_button[nr][0]; + if (b2 != NULL) + *b2 = sdl_js_button[nr][1]; + + return TRUE; +} + #endif /* TARGET_SDL */ diff --git a/src/libgame/sdl.h b/src/libgame/sdl.h index fcc566ed..b29e1950 100644 --- a/src/libgame/sdl.h +++ b/src/libgame/sdl.h @@ -330,4 +330,8 @@ inline void SDLCloseAudio(void); inline void SDLNextEvent(Event *); +void HandleJoystickEvent(Event *); +void SDLInitJoysticks(void); +boolean SDLReadJoystick(int, int *, int *, boolean *, boolean *); + #endif /* SDL_H */ diff --git a/src/libgame/setup.c b/src/libgame/setup.c index d0f2bc06..fcc105bd 100644 --- a/src/libgame/setup.c +++ b/src/libgame/setup.c @@ -21,12 +21,6 @@ #include "text.h" #include "misc.h" - -/* file identifier strings */ -#define SETUP_COOKIE "ROCKSNDIAMONDS_SETUP_FILE_VERSION_1.2" -#define LEVELSETUP_COOKIE "ROCKSNDIAMONDS_LEVELSETUP_FILE_VERSION_1.2" -#define LEVELINFO_COOKIE "ROCKSNDIAMONDS_LEVELINFO_FILE_VERSION_1.2" - /* file names and filename extensions */ #if !defined(PLATFORM_MSDOS) #define LEVELSETUP_DIRECTORY "levelsetup" @@ -79,46 +73,15 @@ static char *levelclass_desc[NUM_LEVELCLASS_DESC] = IS_LEVELCLASS_USER(n) ? 7 : \ 9) -/* values for setup file stuff */ -#define TYPE_BOOLEAN 1 -#define TYPE_SWITCH 2 -#define TYPE_KEY 3 -#define TYPE_INTEGER 4 -#define TYPE_STRING 5 - -#define TOKEN_STR_FILE_IDENTIFIER "file_identifier" - #define TOKEN_VALUE_POSITION 30 -struct SetupFileList -{ - char *token; - char *value; - struct SetupFileList *next; -}; - -struct TokenInfo -{ - int type; - void *value; - char *text; -}; +#define MAX_COOKIE_LEN 256 /* ------------------------------------------------------------------------- */ /* file functions */ /* ------------------------------------------------------------------------- */ -/* -int get_string_integer_value(char *); -boolean get_string_boolean_value(char *); -char *getFormattedSetupEntry(char *, char *); -void freeSetupFileList(struct SetupFileList *); -char *getTokenValue(struct SetupFileList *, char *); -struct SetupFileList *loadSetupFileList(char *); -void checkSetupFileListIdentifier(struct SetupFileList *, char *); -*/ - char *getLevelClassDescription(struct LevelDirInfo *ldi) { int position = ldi->sort_priority / 100; @@ -129,12 +92,6 @@ char *getLevelClassDescription(struct LevelDirInfo *ldi) return "Unknown Level Class"; } -/* for 'InitUserLevelDir()' */ -static void SaveUserLevelInfo(); - -/* for 'SaveUserLevelInfo()' */ -static char *getSetupLine(struct TokenInfo *, char *, int); - static char *getUserLevelDir(char *level_subdir) { static char *userlevel_dir = NULL; @@ -249,6 +206,18 @@ char *getScoreFilename(int nr) return filename; } +char *getSetupFilename() +{ + static char *filename = NULL; + + if (filename != NULL) + free(filename); + + filename = getPath2(getSetupDir(), SETUP_FILENAME); + + return filename; +} + void InitTapeDirectory(char *level_subdir) { createDirectory(getUserDataDir(), "user data", PERMS_PRIVATE); @@ -262,6 +231,8 @@ void InitScoreDirectory(char *level_subdir) createDirectory(getScoreDir(level_subdir), "level score", PERMS_PUBLIC); } +static void SaveUserLevelInfo(); + void InitUserLevelDirectory(char *level_subdir) { if (access(getUserLevelDir(level_subdir), F_OK) != 0) @@ -645,6 +616,21 @@ void SetFilePermissions(char *filename, int permission_class) FILE_PERMS_PRIVATE : FILE_PERMS_PUBLIC)); } +char *getCookie(char *file_type) +{ + static char cookie[MAX_COOKIE_LEN + 1]; + + if (strlen(program.cookie_prefix) + 1 + + strlen(file_type) + strlen("_FILE_VERSION_x.x") > MAX_COOKIE_LEN) + return "[COOKIE ERROR]"; /* should never happen */ + + sprintf(cookie, "%s_%s_FILE_VERSION_%d.%d", + program.cookie_prefix, file_type, + program.version_major, program.version_minor); + + return cookie; +} + int getFileVersionFromCookieString(const char *cookie) { const char *ptr_cookie1, *ptr_cookie2; @@ -928,9 +914,9 @@ void checkSetupFileListIdentifier(struct SetupFileList *setup_file_list, if (strcmp(setup_file_list->token, TOKEN_STR_FILE_IDENTIFIER) == 0) { - if (strcmp(setup_file_list->value, identifier) != 0) + if (!checkCookieString(setup_file_list->value, identifier)) { - Error(ERR_WARN, "configuration file has wrong version"); + Error(ERR_WARN, "configuration file has wrong file identifier"); return; } else @@ -941,7 +927,7 @@ void checkSetupFileListIdentifier(struct SetupFileList *setup_file_list, checkSetupFileListIdentifier(setup_file_list->next, identifier); else { - Error(ERR_WARN, "configuration file has no version information"); + Error(ERR_WARN, "configuration file has no file identifier"); return; } } @@ -954,41 +940,6 @@ void checkSetupFileListIdentifier(struct SetupFileList *setup_file_list, #define TOKEN_STR_LAST_LEVEL_SERIES "last_level_series" #define TOKEN_STR_LAST_PLAYED_LEVEL "last_played_level" #define TOKEN_STR_HANDICAP_LEVEL "handicap_level" -#define TOKEN_STR_PLAYER_PREFIX "player_" - -/* global setup */ -#define SETUP_TOKEN_PLAYER_NAME 0 -#define SETUP_TOKEN_SOUND 1 -#define SETUP_TOKEN_SOUND_LOOPS 2 -#define SETUP_TOKEN_SOUND_MUSIC 3 -#define SETUP_TOKEN_SOUND_SIMPLE 4 -#define SETUP_TOKEN_SCROLL_DELAY 5 -#define SETUP_TOKEN_SOFT_SCROLLING 6 -#define SETUP_TOKEN_FADING 7 -#define SETUP_TOKEN_AUTORECORD 8 -#define SETUP_TOKEN_QUICK_DOORS 9 -#define SETUP_TOKEN_TEAM_MODE 10 -#define SETUP_TOKEN_HANDICAP 11 -#define SETUP_TOKEN_TIME_LIMIT 12 -#define SETUP_TOKEN_FULLSCREEN 13 - -/* player setup */ -#define SETUP_TOKEN_USE_JOYSTICK 0 -#define SETUP_TOKEN_JOY_DEVICE_NAME 1 -#define SETUP_TOKEN_JOY_XLEFT 2 -#define SETUP_TOKEN_JOY_XMIDDLE 3 -#define SETUP_TOKEN_JOY_XRIGHT 4 -#define SETUP_TOKEN_JOY_YUPPER 5 -#define SETUP_TOKEN_JOY_YMIDDLE 6 -#define SETUP_TOKEN_JOY_YLOWER 7 -#define SETUP_TOKEN_JOY_SNAP 8 -#define SETUP_TOKEN_JOY_BOMB 9 -#define SETUP_TOKEN_KEY_LEFT 10 -#define SETUP_TOKEN_KEY_RIGHT 11 -#define SETUP_TOKEN_KEY_UP 12 -#define SETUP_TOKEN_KEY_DOWN 13 -#define SETUP_TOKEN_KEY_SNAP 14 -#define SETUP_TOKEN_KEY_BOMB 15 /* level directory info */ #define LEVELINFO_TOKEN_NAME 0 @@ -1002,57 +953,9 @@ void checkSetupFileListIdentifier(struct SetupFileList *setup_file_list, #define LEVELINFO_TOKEN_LEVEL_GROUP 8 #define LEVELINFO_TOKEN_READONLY 9 -#define FIRST_GLOBAL_SETUP_TOKEN SETUP_TOKEN_PLAYER_NAME -#define LAST_GLOBAL_SETUP_TOKEN SETUP_TOKEN_FULLSCREEN - -#define FIRST_PLAYER_SETUP_TOKEN SETUP_TOKEN_USE_JOYSTICK -#define LAST_PLAYER_SETUP_TOKEN SETUP_TOKEN_KEY_BOMB +#define NUM_LEVELINFO_TOKENS 10 -#define FIRST_LEVELINFO_TOKEN LEVELINFO_TOKEN_NAME -#define LAST_LEVELINFO_TOKEN LEVELINFO_TOKEN_READONLY - -static struct SetupInfo si; -static struct SetupInputInfo sii; static struct LevelDirInfo ldi; -static struct TokenInfo global_setup_tokens[] = -{ - /* global setup */ - { TYPE_STRING, &si.player_name, "player_name" }, - { TYPE_SWITCH, &si.sound, "sound" }, - { TYPE_SWITCH, &si.sound_loops, "repeating_sound_loops" }, - { TYPE_SWITCH, &si.sound_music, "background_music" }, - { TYPE_SWITCH, &si.sound_simple, "simple_sound_effects" }, - { TYPE_SWITCH, &si.scroll_delay, "scroll_delay" }, - { TYPE_SWITCH, &si.soft_scrolling, "soft_scrolling" }, - { TYPE_SWITCH, &si.fading, "screen_fading" }, - { TYPE_SWITCH, &si.autorecord, "automatic_tape_recording" }, - { TYPE_SWITCH, &si.quick_doors, "quick_doors" }, - { TYPE_SWITCH, &si.team_mode, "team_mode" }, - { TYPE_SWITCH, &si.handicap, "handicap" }, - { TYPE_SWITCH, &si.time_limit, "time_limit" }, - { TYPE_SWITCH, &si.fullscreen, "fullscreen" } -}; - -static struct TokenInfo player_setup_tokens[] = -{ - /* player setup */ - { TYPE_BOOLEAN, &sii.use_joystick, ".use_joystick" }, - { TYPE_STRING, &sii.joy.device_name, ".joy.device_name" }, - { TYPE_INTEGER, &sii.joy.xleft, ".joy.xleft" }, - { TYPE_INTEGER, &sii.joy.xmiddle, ".joy.xmiddle" }, - { TYPE_INTEGER, &sii.joy.xright, ".joy.xright" }, - { TYPE_INTEGER, &sii.joy.yupper, ".joy.yupper" }, - { TYPE_INTEGER, &sii.joy.ymiddle, ".joy.ymiddle" }, - { TYPE_INTEGER, &sii.joy.ylower, ".joy.ylower" }, - { TYPE_INTEGER, &sii.joy.snap, ".joy.snap_field" }, - { TYPE_INTEGER, &sii.joy.bomb, ".joy.place_bomb" }, - { TYPE_KEY, &sii.key.left, ".key.move_left" }, - { TYPE_KEY, &sii.key.right, ".key.move_right" }, - { TYPE_KEY, &sii.key.up, ".key.move_up" }, - { TYPE_KEY, &sii.key.down, ".key.move_down" }, - { TYPE_KEY, &sii.key.snap, ".key.snap_field" }, - { TYPE_KEY, &sii.key.bomb, ".key.place_bomb" } -}; static struct TokenInfo levelinfo_tokens[] = { @@ -1134,52 +1037,8 @@ static void setLevelDirInfoToDefaultsFromParent(struct LevelDirInfo *ldi, ldi->next = NULL; } -static void setSetupInfoToDefaults(struct SetupInfo *si) -{ - int i; - - si->player_name = getStringCopy(getLoginName()); - - si->sound = TRUE; - si->sound_loops = TRUE; - si->sound_music = TRUE; - si->sound_simple = TRUE; - si->toons = TRUE; - si->double_buffering = TRUE; - si->direct_draw = !si->double_buffering; - si->scroll_delay = TRUE; - si->soft_scrolling = TRUE; - si->fading = FALSE; - si->autorecord = TRUE; - si->quick_doors = FALSE; - si->team_mode = FALSE; - si->handicap = TRUE; - si->time_limit = TRUE; - si->fullscreen = FALSE; - - for (i=0; iinput[i].use_joystick = FALSE; - si->input[i].joy.device_name=getStringCopy(getDeviceNameFromJoystickNr(i)); - si->input[i].joy.xleft = JOYSTICK_XLEFT; - si->input[i].joy.xmiddle = JOYSTICK_XMIDDLE; - si->input[i].joy.xright = JOYSTICK_XRIGHT; - si->input[i].joy.yupper = JOYSTICK_YUPPER; - si->input[i].joy.ymiddle = JOYSTICK_YMIDDLE; - si->input[i].joy.ylower = JOYSTICK_YLOWER; - si->input[i].joy.snap = (i == 0 ? JOY_BUTTON_1 : 0); - si->input[i].joy.bomb = (i == 0 ? JOY_BUTTON_2 : 0); - si->input[i].key.left = (i == 0 ? DEFAULT_KEY_LEFT : KSYM_UNDEFINED); - si->input[i].key.right = (i == 0 ? DEFAULT_KEY_RIGHT : KSYM_UNDEFINED); - si->input[i].key.up = (i == 0 ? DEFAULT_KEY_UP : KSYM_UNDEFINED); - si->input[i].key.down = (i == 0 ? DEFAULT_KEY_DOWN : KSYM_UNDEFINED); - si->input[i].key.snap = (i == 0 ? DEFAULT_KEY_SNAP : KSYM_UNDEFINED); - si->input[i].key.bomb = (i == 0 ? DEFAULT_KEY_BOMB : KSYM_UNDEFINED); - } -} - -static void setSetupInfo(struct TokenInfo *token_info, - int token_nr, char *token_value) +void setSetupInfo(struct TokenInfo *token_info, + int token_nr, char *token_value) { int token_type = token_info[token_nr].type; void *setup_value = token_info[token_nr].value; @@ -1214,40 +1073,6 @@ static void setSetupInfo(struct TokenInfo *token_info, } } -static void decodeSetupFileList(struct SetupFileList *setup_file_list) -{ - int i, pnr; - - if (!setup_file_list) - return; - - /* handle global setup values */ - si = setup; - for (i=FIRST_GLOBAL_SETUP_TOKEN; i<=LAST_GLOBAL_SETUP_TOKEN; i++) - setSetupInfo(global_setup_tokens, i, - getTokenValue(setup_file_list, global_setup_tokens[i].text)); - setup = si; - - /* handle player specific setup values */ - for (pnr=0; pnr MAX_PLAYER_NAME_LEN) - setup.player_name[MAX_PLAYER_NAME_LEN] = '\0'; - else if (strlen(setup.player_name) < MAX_PLAYER_NAME_LEN) - { - char *new_name = checked_malloc(MAX_PLAYER_NAME_LEN + 1); - - strcpy(new_name, setup.player_name); - free(setup.player_name); - setup.player_name = new_name; - } - } - else - Error(ERR_WARN, "using default setup values"); - - free(filename); -} - -static char *getSetupLine(struct TokenInfo *token_info, - char *prefix, int token_nr) +char *getSetupLine(struct TokenInfo *token_info, char *prefix, int token_nr) { int i; static char entry[MAX_LINE_LEN]; @@ -1584,57 +1369,6 @@ static char *getSetupLine(struct TokenInfo *token_info, return entry; } -void SaveSetup() -{ - int i, pnr; - char *filename; - FILE *file; - - InitUserDataDirectory(); - - filename = getPath2(getSetupDir(), SETUP_FILENAME); - - if (!(file = fopen(filename, MODE_WRITE))) - { - Error(ERR_WARN, "cannot write setup file '%s'", filename); - free(filename); - return; - } - - fprintf(file, "%s\n", - getFormattedSetupEntry(TOKEN_STR_FILE_IDENTIFIER, SETUP_COOKIE)); - fprintf(file, "\n"); - - /* handle global setup values */ - si = setup; - for (i=FIRST_GLOBAL_SETUP_TOKEN; i<=LAST_GLOBAL_SETUP_TOKEN; i++) - { - fprintf(file, "%s\n", getSetupLine(global_setup_tokens, "", i)); - - /* just to make things nicer :) */ - if (i == SETUP_TOKEN_PLAYER_NAME) - fprintf(file, "\n"); - } - - /* handle player specific setup values */ - for (pnr=0; pnr/levelsetup.conf */ /* ----------------------------------------------------------------------- */ filename = getPath2(getSetupDir(), LEVELSETUP_FILENAME); @@ -1658,7 +1392,7 @@ void LoadLevelSetup_LastSeries() if (leveldir_current == NULL) leveldir_current = leveldir_first; - checkSetupFileListIdentifier(level_setup_list, LEVELSETUP_COOKIE); + checkSetupFileListIdentifier(level_setup_list, getCookie("LEVELSETUP")); freeSetupFileList(level_setup_list); } @@ -1675,7 +1409,7 @@ void SaveLevelSetup_LastSeries() FILE *file; /* ----------------------------------------------------------------------- */ - /* ~/.rocksndiamonds/levelsetup.conf */ + /* ~/./levelsetup.conf */ /* ----------------------------------------------------------------------- */ InitUserDataDirectory(); @@ -1690,7 +1424,7 @@ void SaveLevelSetup_LastSeries() } fprintf(file, "%s\n\n", getFormattedSetupEntry(TOKEN_STR_FILE_IDENTIFIER, - LEVELSETUP_COOKIE)); + getCookie("LEVELSETUP"))); fprintf(file, "%s\n", getFormattedSetupEntry(TOKEN_STR_LAST_LEVEL_SERIES, level_subdir)); @@ -1761,7 +1495,7 @@ void LoadLevelSetup_SeriesInfo() checkSeriesInfo(leveldir_current); /* ----------------------------------------------------------------------- */ - /* ~/.rocksndiamonds/levelsetup//levelsetup.conf */ + /* ~/./levelsetup//levelsetup.conf */ /* ----------------------------------------------------------------------- */ level_subdir = leveldir_current->filename; @@ -1801,7 +1535,7 @@ void LoadLevelSetup_SeriesInfo() leveldir_current->handicap_level = level_nr; } - checkSetupFileListIdentifier(level_setup_list, LEVELSETUP_COOKIE); + checkSetupFileListIdentifier(level_setup_list, getCookie("LEVELSETUP")); freeSetupFileList(level_setup_list); } @@ -1820,7 +1554,7 @@ void SaveLevelSetup_SeriesInfo() FILE *file; /* ----------------------------------------------------------------------- */ - /* ~/.rocksndiamonds/levelsetup//levelsetup.conf */ + /* ~/./levelsetup//levelsetup.conf */ /* ----------------------------------------------------------------------- */ InitLevelSetupDirectory(level_subdir); @@ -1835,7 +1569,7 @@ void SaveLevelSetup_SeriesInfo() } fprintf(file, "%s\n\n", getFormattedSetupEntry(TOKEN_STR_FILE_IDENTIFIER, - LEVELSETUP_COOKIE)); + getCookie("LEVELSETUP"))); fprintf(file, "%s\n", getFormattedSetupEntry(TOKEN_STR_LAST_PLAYED_LEVEL, level_nr_str)); fprintf(file, "%s\n", getFormattedSetupEntry(TOKEN_STR_HANDICAP_LEVEL, diff --git a/src/libgame/setup.h b/src/libgame/setup.h index edec4d0a..a467079a 100644 --- a/src/libgame/setup.h +++ b/src/libgame/setup.h @@ -17,6 +17,30 @@ #include "system.h" +/* values for setup file handling */ +#define TYPE_BOOLEAN 1 +#define TYPE_SWITCH 2 +#define TYPE_KEY 3 +#define TYPE_INTEGER 4 +#define TYPE_STRING 5 + +#define TOKEN_STR_FILE_IDENTIFIER "file_identifier" + +/* structures for setup file handling */ +struct SetupFileList +{ + char *token; + char *value; + struct SetupFileList *next; +}; + +struct TokenInfo +{ + int type; + void *value; + char *text; +}; + /* sort priorities of level series (also used as level series classes) */ #define LEVELCLASS_TUTORIAL_START 10 #define LEVELCLASS_TUTORIAL_END 99 @@ -85,6 +109,8 @@ char *getLevelFilename(int); char *getTapeFilename(int); char *getScoreFilename(int); +char *getSetupFilename(void); + void InitTapeDirectory(char *); void InitScoreDirectory(char *); void InitUserLevelDirectory(char *); @@ -112,12 +138,20 @@ char *getSetupDir(void); void createDirectory(char *, char *, int); void InitUserDataDirectory(void); void SetFilePermissions(char *, int); + +char *getCookie(char *); int getFileVersionFromCookieString(const char *); boolean checkCookieString(const char *, const char *); +char *getFormattedSetupEntry(char *, char *); +void freeSetupFileList(struct SetupFileList *); +char *getTokenValue(struct SetupFileList *, char *); +struct SetupFileList *loadSetupFileList(char *); +void checkSetupFileListIdentifier(struct SetupFileList *, char *); +void setSetupInfo(struct TokenInfo *, int, char *); +char *getSetupLine(struct TokenInfo *, char *, int); + void LoadLevelInfo(void); -void LoadSetup(void); -void SaveSetup(void); void LoadLevelSetup_LastSeries(void); void SaveLevelSetup_LastSeries(void); void LoadLevelSetup_SeriesInfo(void); diff --git a/src/libgame/system.c b/src/libgame/system.c index 5604ad21..a91d2438 100644 --- a/src/libgame/system.c +++ b/src/libgame/system.c @@ -23,6 +23,7 @@ #include "system.h" #include "sound.h" #include "setup.h" +#include "joystick.h" #include "misc.h" @@ -111,7 +112,8 @@ void ClosePlatformDependantStuff(void) void InitProgramInfo(char *unix_userdata_directory, char *program_title, char *window_title, char *icon_title, char *x11_icon_basename, char *x11_iconmask_basename, - char *msdos_pointer_basename) + char *msdos_pointer_basename, + char *cookie_prefix, int program_version) { char *x11_icon_filename = getPath2(options.graphics_directory, x11_icon_basename); @@ -132,6 +134,10 @@ void InitProgramInfo(char *unix_userdata_directory, char *program_title, program.x11_icon_filename = x11_icon_filename; program.x11_iconmask_filename = x11_iconmask_filename; program.msdos_pointer_filename = msdos_pointer_filename; + program.cookie_prefix = cookie_prefix; + program.version_major = VERSION_MAJOR(program_version); + program.version_minor = VERSION_MINOR(program_version); + program.version_patch = VERSION_PATCH(program_version); } void InitGfxFieldInfo(int sx, int sy, int sxsize, int sysize, @@ -623,10 +629,10 @@ inline void OpenAudio(void) #if defined(TARGET_SDL) SDLOpenAudio(); -#elif defined(PLATFORM_MSDOS) - MSDOSOpenAudio(); #elif defined(PLATFORM_UNIX) UnixOpenAudio(); +#elif defined(PLATFORM_MSDOS) + MSDOSOpenAudio(); #endif } @@ -634,10 +640,10 @@ inline void CloseAudio(void) { #if defined(TARGET_SDL) SDLCloseAudio(); -#elif defined(PLATFORM_MSDOS) - MSDOSCloseAudio(); #elif defined(PLATFORM_UNIX) UnixCloseAudio(); +#elif defined(PLATFORM_MSDOS) + MSDOSCloseAudio(); #endif audio.sound_enabled = FALSE; @@ -727,9 +733,39 @@ inline boolean CheckCloseWindowEvent(ClientMessageEvent *event) } -inline void dummy(void) +/* ========================================================================= */ +/* joystick functions */ +/* ========================================================================= */ + +inline void InitJoysticks() { -#ifdef TARGET_SDL -#else + int i; + +#ifdef NO_JOYSTICK + return; /* joysticks generally deactivated by compile-time directive */ +#endif + + /* always start with reliable default values */ + joystick.status = JOYSTICK_NOT_AVAILABLE; + for (i=0; i #include +#include #include typedef unsigned char boolean; diff --git a/src/main.c b/src/main.c index 528741b9..8533d336 100644 --- a/src/main.c +++ b/src/main.c @@ -29,7 +29,6 @@ boolean level_editor_test_game = FALSE; boolean network_playing = FALSE; int key_joystick_mapping = 0; -int global_joystick_status = JOYSTICK_STATUS; boolean redraw[MAX_BUF_XSIZE][MAX_BUF_YSIZE]; int redraw_x1 = 0, redraw_y1 = 0; diff --git a/src/main.h b/src/main.h index 0768ed90..5c7c417c 100644 --- a/src/main.h +++ b/src/main.h @@ -341,15 +341,11 @@ extern Pixmap tile_clipmask[]; extern DrawBuffer *fieldbuffer; extern DrawBuffer *drawto_field; -extern int joystick_device; -extern char *joystick_device_name[]; - extern int game_status; extern boolean level_editor_test_game; extern boolean network_playing; extern int key_joystick_mapping; -extern int global_joystick_status; extern boolean redraw[MAX_BUF_XSIZE][MAX_BUF_YSIZE]; extern int redraw_x1, redraw_y1; @@ -1467,6 +1463,7 @@ extern int num_element_info; #define WINDOW_SUBTITLE_STRING PROGRAM_RIGHTS_STRING " " PROGRAM_AUTHOR_STRING #define ICON_TITLE_STRING PROGRAM_TITLE_STRING #define UNIX_USERDATA_DIRECTORY ".rocksndiamonds" +#define COOKIE_PREFIX "ROCKSNDIAMONDS" #define X11_ICON_FILENAME "rocks_icon.xbm" #define X11_ICONMASK_FILENAME "rocks_iconmask.xbm" diff --git a/src/screens.c b/src/screens.c index 5c317af0..99cbd2ea 100644 --- a/src/screens.c +++ b/src/screens.c @@ -1589,23 +1589,11 @@ void HandleSetupScreen(int mx, int my, int dx, int dy, int button) DrawSetupInputScreen(); redraw = TRUE; } - else if (y==pos_end-1 || y==pos_end) + else if (y == pos_end - 1 || y == pos_end) { - if (y==pos_end) - { + if (y == pos_end) SaveSetup(); - /* - SaveJoystickData(); - */ - -#if defined(PLATFORM_MSDOS) - save_joystick_data(JOYSTICK_FILENAME); -#endif - - - } - game_status = MAINMENU; DrawMainMenu(); redraw = TRUE; @@ -1845,30 +1833,6 @@ void HandleSetupInputScreen(int mx, int my, int dx, int dy, int button) setJoystickDeviceToNr(device_name, new_device_nr); } - - /* - InitJoysticks(); - */ - - -#if 0 - int one_joystick_nr = (dx >= 0 ? 0 : 1); - int the_other_joystick_nr = (dx >= 0 ? 1 : 0); - - if (setup.input[player_nr].use_joystick) - { - if (setup.input[player_nr].joystick_nr == one_joystick_nr) - setup.input[player_nr].joystick_nr = the_other_joystick_nr; - else - setup.input[player_nr].use_joystick = FALSE; - } - else - { - setup.input[player_nr].use_joystick = TRUE; - setup.input[player_nr].joystick_nr = one_joystick_nr; - } -#endif - drawPlayerSetupInputInfo(player_nr); } else if (y == 5) @@ -2033,56 +1997,29 @@ void CustomizeKeyboard(int player_nr) DrawSetupInputScreen(); } -void CalibrateJoystick(int player_nr) +static boolean CalibrateJoystickMain(int player_nr) { -#ifdef __FreeBSD__ - struct joystick joy_ctrl; -#else - struct joystick_control - { - int buttons; - int x; - int y; - } joy_ctrl; -#endif - -#if !defined(PLATFORM_MSDOS) - int new_joystick_xleft = 128, new_joystick_xright = 128; - int new_joystick_yupper = 128, new_joystick_ylower = 128; + int new_joystick_xleft = JOYSTICK_XMIDDLE; + int new_joystick_xright = JOYSTICK_XMIDDLE; + int new_joystick_yupper = JOYSTICK_YMIDDLE; + int new_joystick_ylower = JOYSTICK_YMIDDLE; int new_joystick_xmiddle, new_joystick_ymiddle; -#else - int calibration_step = 1; -#endif int joystick_fd = joystick.fd[player_nr]; int x, y, last_x, last_y, xpos = 8, ypos = 3; boolean check[3][3]; - int check_remaining; + int check_remaining = 3 * 3; + int joy_x, joy_y; int joy_value; int result = -1; - if (joystick.status == JOYSTICK_NOT_AVAILABLE || - joystick_fd < 0 || - !setup.input[player_nr].use_joystick) - goto error_out; + if (joystick.status == JOYSTICK_NOT_AVAILABLE) + return FALSE; - ClearWindow(); + if (joystick_fd < 0 || !setup.input[player_nr].use_joystick) + return FALSE; -#if !defined(PLATFORM_MSDOS) - DrawText(SX, SY + 6*32, " ROTATE JOYSTICK ", FS_BIG, FC_YELLOW); - DrawText(SX, SY + 7*32, "IN ALL DIRECTIONS", FS_BIG, FC_YELLOW); - DrawText(SX + 16, SY + 9*32, " IF ALL BALLS ", FS_BIG, FC_YELLOW); - DrawText(SX, SY + 10*32, " ARE YELLOW, ", FS_BIG, FC_YELLOW); - DrawText(SX, SY + 11*32, "PRESS ANY BUTTON!", FS_BIG, FC_YELLOW); - check_remaining = 3 * 3; -#else - DrawText(SX, SY + 7*32, " MOVE JOYSTICK ", FS_BIG, FC_YELLOW); - DrawText(SX + 16, SY + 8*32, " TO ", FS_BIG, FC_YELLOW); - DrawText(SX, SY + 9*32, " CENTER POSITION ", FS_BIG, FC_YELLOW); - DrawText(SX, SY + 10*32, " AND ", FS_BIG, FC_YELLOW); - DrawText(SX, SY + 11*32, "PRESS ANY BUTTON!", FS_BIG, FC_YELLOW); - check_remaining = 0; -#endif + ClearWindow(); for(y=0; y<3; y++) { @@ -2093,21 +2030,29 @@ void CalibrateJoystick(int player_nr) } } + DrawText(SX, SY + 6 * 32, " ROTATE JOYSTICK ", FS_BIG, FC_YELLOW); + DrawText(SX, SY + 7 * 32, "IN ALL DIRECTIONS", FS_BIG, FC_YELLOW); + DrawText(SX + 16, SY + 9 * 32, " IF ALL BALLS ", FS_BIG, FC_YELLOW); + DrawText(SX, SY + 10 * 32, " ARE YELLOW, ", FS_BIG, FC_YELLOW); + DrawText(SX, SY + 11 * 32, " CENTER JOYSTICK ", FS_BIG, FC_YELLOW); + DrawText(SX, SY + 12 * 32, " AND ", FS_BIG, FC_YELLOW); + DrawText(SX, SY + 13 * 32, "PRESS ANY BUTTON!", FS_BIG, FC_YELLOW); + joy_value = Joystick(player_nr); last_x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0); last_y = (joy_value & JOY_UP ? -1 : joy_value & JOY_DOWN ? +1 : 0); - DrawGraphic(xpos + last_x, ypos + last_y, GFX_KUGEL_ROT); - BackToFront(); + /* eventually uncalibrated center position (joystick could be uncentered) */ + if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL)) + return FALSE; -#ifdef __FreeBSD__ - joy_ctrl.b1 = joy_ctrl.b2 = 0; -#else - joy_ctrl.buttons = 0; -#endif + new_joystick_xmiddle = joy_x; + new_joystick_ymiddle = joy_y; - while(Joystick(player_nr) & JOY_BUTTON); + DrawGraphic(xpos + last_x, ypos + last_y, GFX_KUGEL_ROT); + BackToFront(); + while(Joystick(player_nr) & JOY_BUTTON); /* wait for released button */ InitAnimation(); while(result < 0) @@ -2147,28 +2092,13 @@ void CalibrateJoystick(int player_nr) } } -#if !defined(PLATFORM_MSDOS) - -#if defined(TARGET_SDL) - joy_ctrl.x = Get_SDL_Joystick_Axis(joystick_fd, 0); - joy_ctrl.y = Get_SDL_Joystick_Axis(joystick_fd, 1); -#else - if (read(joystick_fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl)) - { - joystick.status = JOYSTICK_NOT_AVAILABLE; - goto error_out; - } -#endif + if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL)) + return FALSE; - new_joystick_xleft = MIN(new_joystick_xleft, joy_ctrl.x); - new_joystick_xright = MAX(new_joystick_xright, joy_ctrl.x); - new_joystick_yupper = MIN(new_joystick_yupper, joy_ctrl.y); - new_joystick_ylower = MAX(new_joystick_ylower, joy_ctrl.y); - - new_joystick_xmiddle = - new_joystick_xleft + (new_joystick_xright - new_joystick_xleft) / 2; - new_joystick_ymiddle = - new_joystick_yupper + (new_joystick_ylower - new_joystick_yupper) / 2; + new_joystick_xleft = MIN(new_joystick_xleft, joy_x); + new_joystick_xright = MAX(new_joystick_xright, joy_x); + new_joystick_yupper = MIN(new_joystick_yupper, joy_y); + new_joystick_ylower = MAX(new_joystick_ylower, joy_y); setup.input[player_nr].joy.xleft = new_joystick_xleft; setup.input[player_nr].joy.yupper = new_joystick_yupper; @@ -2178,60 +2108,18 @@ void CalibrateJoystick(int player_nr) setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle; CheckJoystickData(); -#endif joy_value = Joystick(player_nr); if (joy_value & JOY_BUTTON && check_remaining == 0) - { result = 1; -#if defined(PLATFORM_MSDOS) - if (calibration_step == 1) - { - remove_joystick(); - InitJoysticks(); - } - else if (calibrate_joystick(joystick_fd) != 0) - { - joystick.status = JOYSTICK_NOT_AVAILABLE; - goto error_out; - } - - if (joy[joystick_fd].flags & JOYFLAG_CALIBRATE) - { - calibration_step++; - result = -1; - - DrawText(SX, SY + 7*32, " MOVE JOYSTICK ", FS_BIG, FC_YELLOW); - DrawText(SX + 16, SY + 8*32, " TO ", FS_BIG, FC_YELLOW); - - if (calibration_step == 2) - DrawText(SX + 16, SY + 9*32," THE UPPER LEFT ", FS_BIG, FC_YELLOW); - else - DrawText(SX, SY + 9*32," THE LOWER RIGHT ", FS_BIG, FC_YELLOW); - - DrawText(SX, SY + 10*32, " AND ", FS_BIG, FC_YELLOW); - DrawText(SX, SY + 11*32, "PRESS ANY BUTTON!", FS_BIG, FC_YELLOW); - - BackToFront(); - - while(Joystick(player_nr) & JOY_BUTTON) - DoAnimation(); - } -#endif - } - x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0); y = (joy_value & JOY_UP ? -1 : joy_value & JOY_DOWN ? +1 : 0); if (x != last_x || y != last_y) { -#if !defined(PLATFORM_MSDOS) DrawGraphic(xpos + last_x, ypos + last_y, GFX_KUGEL_GELB); -#else - DrawGraphic(xpos + last_x, ypos + last_y, GFX_KUGEL_BLAU); -#endif DrawGraphic(xpos + x, ypos + y, GFX_KUGEL_ROT); last_x = x; @@ -2244,6 +2132,7 @@ void CalibrateJoystick(int player_nr) } #if 0 +#ifdef DEBUG printf("LEFT / MIDDLE / RIGHT == %d / %d / %d\n", setup.input[player_nr].joy.xleft, setup.input[player_nr].joy.xmiddle, @@ -2252,6 +2141,7 @@ void CalibrateJoystick(int player_nr) setup.input[player_nr].joy.yupper, setup.input[player_nr].joy.ymiddle, setup.input[player_nr].joy.ylower); +#endif #endif } @@ -2263,12 +2153,19 @@ void CalibrateJoystick(int player_nr) Delay(10); } + /* calibrated center position (joystick should now be centered) */ + if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL)) + return FALSE; + + new_joystick_xmiddle = joy_x; + new_joystick_ymiddle = joy_y; + StopAnimation(); DrawSetupInputScreen(); /* wait until the last pressed button was released */ - while(Joystick(player_nr) & JOY_BUTTON) + while (Joystick(player_nr) & JOY_BUTTON) { if (PendingEvent()) /* got event */ { @@ -2280,16 +2177,21 @@ void CalibrateJoystick(int player_nr) Delay(10); } } - return; - error_out: + return TRUE; +} - ClearWindow(); - DrawText(SX + 16, SY + 6*32, " JOYSTICK NOT ", FS_BIG, FC_YELLOW); - DrawText(SX, SY + 7*32, " AVAILABLE ", FS_BIG, FC_YELLOW); - BackToFront(); - Delay(2000); - DrawSetupInputScreen(); +void CalibrateJoystick(int player_nr) +{ + if (!CalibrateJoystickMain(player_nr)) + { + ClearWindow(); + + DrawText(SX + 16, SY + 6*32, " JOYSTICK NOT ", FS_BIG, FC_YELLOW); + DrawText(SX, SY + 7*32, " AVAILABLE ", FS_BIG, FC_YELLOW); + BackToFront(); + Delay(2000); /* show error message for two seconds */ + } } void HandleGameActions() diff --git a/src/tools.c b/src/tools.c index 8a5f48e0..8946e84b 100644 --- a/src/tools.c +++ b/src/tools.c @@ -11,14 +11,6 @@ * tools.c * ***********************************************************/ -#if 0 -#include - -#if defined(PLATFORM_FREEBSD) -#include -#endif -#endif - #include "libgame/libgame.h" #include "tools.h" -- 2.34.1