From 7127f6c844ee24e3629f292a8301395111266a7e Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Thu, 31 May 2007 23:16:23 +0000 Subject: Initial joystick support. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 887 --- setup/Makefile.am | 2 + setup/configfile.c | 11 ++-- setup/joystick.c | 111 +++++++++++++++++++++++++++++++++ setup/joystick.h | 40 ++++++++++++ setup/keyboard.c | 2 +- setup/mainmenu.c | 3 + setup/txt_joybinput.c | 156 +++++++++++++++++++++++++++++++++++++++++++++++ setup/txt_joybinput.h | 44 +++++++++++++ src/Makefile.am | 1 + src/i_joystick.c | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/i_joystick.h | 41 +++++++++++++ src/i_main.c | 2 +- src/i_system.c | 2 + src/m_misc.c | 7 +++ 14 files changed, 581 insertions(+), 7 deletions(-) create mode 100644 setup/joystick.c create mode 100644 setup/joystick.h create mode 100644 setup/txt_joybinput.c create mode 100644 setup/txt_joybinput.h create mode 100644 src/i_joystick.c create mode 100644 src/i_joystick.h diff --git a/setup/Makefile.am b/setup/Makefile.am index d5eebfbf..10085218 100644 --- a/setup/Makefile.am +++ b/setup/Makefile.am @@ -10,6 +10,7 @@ SOURCE_FILES = \ compatibility.c compatibility.h \ configfile.c configfile.h \ display.c display.h \ + joystick.c joystick.h \ keyboard.c keyboard.h \ m_argv.c m_argv.h \ mainmenu.c \ @@ -17,6 +18,7 @@ SOURCE_FILES = \ multiplayer.c multiplayer.h \ sound.c sound.h \ execute.c execute.h \ + txt_joybinput.c txt_joybinput.h \ txt_keyinput.c txt_keyinput.h \ txt_mouseinput.c txt_mouseinput.h diff --git a/setup/configfile.c b/setup/configfile.c index 3b2326d5..96f8bdd3 100644 --- a/setup/configfile.c +++ b/setup/configfile.c @@ -50,6 +50,7 @@ #include "compatibility.h" #include "display.h" +#include "joystick.h" #include "keyboard.h" #include "mouse.h" #include "multiplayer.h" @@ -136,11 +137,6 @@ static int screenblocks = 9; static int detailLevel = 0; static int usegamma = 0; -static int usejoystick = 0; -static int joybfire = 0; -static int joybstrafe = 1; -static int joybuse = 2; - // dos specific options: these are unused but should be maintained // so that the config file can be shared between chocolate // doom and doom.exe @@ -270,6 +266,11 @@ static default_t extra_defaults_list[] = {"player_name", &net_player_name, DEFAULT_STRING, 0, 0}, #endif {"video_driver", &video_driver, DEFAULT_STRING, 0, 0}, + {"joystick_index", &joystick_index, DEFAULT_INT, 0, 0}, + {"joystick_x_axis", &joystick_x_axis, DEFAULT_INT, 0, 0}, + {"joystick_x_invert", &joystick_x_invert, DEFAULT_INT, 0, 0}, + {"joystick_y_axis", &joystick_y_axis, DEFAULT_INT, 0, 0}, + {"joystick_y_invert", &joystick_y_invert, DEFAULT_INT, 0, 0}, }; static default_collection_t extra_defaults = diff --git a/setup/joystick.c b/setup/joystick.c new file mode 100644 index 00000000..435a864c --- /dev/null +++ b/setup/joystick.c @@ -0,0 +1,111 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 2007 Simon Howard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// + +#include + +#include "textscreen.h" +#include "txt_joybinput.h" + +#include "joystick.h" + +// Joystick enable/disable + +int usejoystick = 0; + +// Button mappings + +int joybfire = 0; +int joybstrafe = 1; +int joybuse = 2; +int joybspeed = 3; + +// Joystick to use, as an SDL joystick index: + +int joystick_index = -1; + +// Which joystick axis to use for horizontal movement, and whether to +// invert the direction: + +int joystick_x_axis = 0; +int joystick_x_invert = 0; + +// Which joystick axis to use for vertical movement, and whether to +// invert the direction: + +int joystick_y_axis = 1; +int joystick_y_invert = 0; + +static txt_button_t *joystick_button; + +static void SetJoystickButtonLabel(void) +{ + char *name; + + name = "None set"; + + if (joystick_index >= 0 && joystick_index < SDL_NumJoysticks()) + { + name = (char *) SDL_JoystickName(joystick_index); + } + + TXT_SetButtonLabel(joystick_button, name); +} + +void ConfigJoystick(void) +{ + txt_window_t *window; + txt_table_t *button_table; + txt_table_t *joystick_table; + + SDL_Init(SDL_INIT_JOYSTICK); + + window = TXT_NewWindow("Joystick configuration"); + + TXT_AddWidgets(window, + TXT_NewCheckBox("Enable joystick", &usejoystick), + joystick_table = TXT_NewTable(2), + TXT_NewSeparator("Joystick buttons"), + button_table = TXT_NewTable(2), + NULL); + + TXT_SetColumnWidths(joystick_table, 20, 15); + + TXT_AddWidgets(joystick_table, + TXT_NewLabel("Current joystick"), + joystick_button = TXT_NewButton("zzzz"), + NULL); + + TXT_SetColumnWidths(button_table, 20, 15); + + TXT_AddWidgets(button_table, + TXT_NewLabel("Fire"), + TXT_NewJoystickInput(&joybfire), + TXT_NewLabel("Speed"), + TXT_NewJoystickInput(&joybspeed), + TXT_NewLabel("Use"), + TXT_NewJoystickInput(&joybuse), + TXT_NewLabel("Strafe"), + TXT_NewJoystickInput(&joybstrafe), + NULL); + + SetJoystickButtonLabel(); +} + diff --git a/setup/joystick.h b/setup/joystick.h new file mode 100644 index 00000000..30b568ad --- /dev/null +++ b/setup/joystick.h @@ -0,0 +1,40 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 2006 Simon Howard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// + +#ifndef SETUP_JOYSTICK_H +#define SETUP_JOYSTICK_H + +extern int usejoystick; +extern int joybfire; +extern int joybstrafe; +extern int joybuse; +extern int joybspeed; + +extern int joystick_index; +extern int joystick_x_axis; +extern int joystick_x_invert; +extern int joystick_y_axis; +extern int joystick_y_invert; + +void ConfigJoystick(void); + +#endif /* #ifndef SETUP_JOYSTICK_H */ + diff --git a/setup/keyboard.c b/setup/keyboard.c index c10b9ce4..bb749d4d 100644 --- a/setup/keyboard.c +++ b/setup/keyboard.c @@ -23,6 +23,7 @@ #include "execute.h" #include "txt_keyinput.h" +#include "joystick.h" #include "keyboard.h" int key_left = KEY_LEFTARROW; @@ -35,7 +36,6 @@ int key_fire = KEY_RCTRL; int key_use = ' '; int key_strafe = KEY_RALT; int key_speed = KEY_RSHIFT; -int joybspeed = 3; int vanilla_keyboard_mapping = 1; diff --git a/setup/mainmenu.c b/setup/mainmenu.c index cb645463..48472bed 100644 --- a/setup/mainmenu.c +++ b/setup/mainmenu.c @@ -32,6 +32,7 @@ #include "compatibility.h" #include "display.h" +#include "joystick.h" #include "keyboard.h" #include "mouse.h" #include "multiplayer.h" @@ -106,6 +107,8 @@ void MainMenu(void) TXT_AddWidgets(window, TXT_NewButton2("Configure Display", (TxtWidgetSignalFunc) ConfigDisplay, NULL), + TXT_NewButton2("Configure Joystick", + (TxtWidgetSignalFunc) ConfigJoystick, NULL), TXT_NewButton2("Configure Keyboard", (TxtWidgetSignalFunc) ConfigKeyboard, NULL), TXT_NewButton2("Configure Mouse", diff --git a/setup/txt_joybinput.c b/setup/txt_joybinput.c new file mode 100644 index 00000000..ff82a88c --- /dev/null +++ b/setup/txt_joybinput.c @@ -0,0 +1,156 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 2006 Simon Howard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// + +#include +#include +#include + +#include "doomkeys.h" +#include "joystick.h" + +#include "txt_joybinput.h" +#include "txt_gui.h" +#include "txt_io.h" +#include "txt_label.h" +#include "txt_window.h" + +#define JOYSTICK_INPUT_WIDTH 10 + +static void OpenPromptWindow(txt_joystick_input_t *joystick_input) +{ + txt_window_t *window; + txt_label_t *label; + + window = TXT_NewWindow(NULL); + TXT_SetWindowAction(window, TXT_HORIZ_LEFT, NULL); + TXT_SetWindowAction(window, TXT_HORIZ_CENTER, + TXT_NewWindowAbortAction(window)); + TXT_SetWindowAction(window, TXT_HORIZ_RIGHT, NULL); + + label = TXT_NewLabel("Press the new joystick button..."); + + TXT_AddWidget(window, label); + TXT_SetWidgetAlign(label, TXT_HORIZ_CENTER); +} + +static void TXT_JoystickInputSizeCalc(TXT_UNCAST_ARG(joystick_input)) +{ + TXT_CAST_ARG(txt_joystick_input_t, joystick_input); + + // All joystickinputs are the same size. + + joystick_input->widget.w = JOYSTICK_INPUT_WIDTH; + joystick_input->widget.h = 1; +} + +static void GetJoystickButtonDescription(int button, char *buf) +{ + sprintf(buf, "BUTTON #%i", button + 1); +} + +static void TXT_JoystickInputDrawer(TXT_UNCAST_ARG(joystick_input), int selected) +{ + TXT_CAST_ARG(txt_joystick_input_t, joystick_input); + char buf[20]; + int i; + + if (*joystick_input->variable == -1) + { + strcpy(buf, ""); + } + else + { + GetJoystickButtonDescription(*joystick_input->variable, buf); + } + + if (selected) + { + TXT_BGColor(TXT_COLOR_GREY, 0); + } + else + { + TXT_BGColor(TXT_COLOR_BLUE, 0); + } + + TXT_FGColor(TXT_COLOR_BRIGHT_WHITE); + + TXT_DrawString(buf); + + for (i=strlen(buf); ivariable = variable; + + return joystick_input; +} + diff --git a/setup/txt_joybinput.h b/setup/txt_joybinput.h new file mode 100644 index 00000000..9a3b9c46 --- /dev/null +++ b/setup/txt_joybinput.h @@ -0,0 +1,44 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 2007 Simon Howard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// + +#ifndef TXT_JOYB_INPUT_H +#define TXT_JOYB_INPUT_H + +typedef struct txt_joystick_input_s txt_joystick_input_t; + +#include "txt_widget.h" + +// +// A joystick input is like an input box. When selected, a box pops up +// allowing a joystick button to be pressed to select it. +// + +struct txt_joystick_input_s +{ + txt_widget_t widget; + int *variable; +}; + +txt_joystick_input_t *TXT_NewJoystickInput(int *variable); + +#endif /* #ifndef TXT_JOYB_INPUT_H */ + + diff --git a/src/Makefile.am b/src/Makefile.am index db446819..da9a35f8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -48,6 +48,7 @@ hu_lib.c hu_lib.h \ hu_stuff.c hu_stuff.h \ i_main.c \ info.c info.h \ +i_joystick.c i_joystick.h \ i_scale.c i_scale.h \ i_swap.h \ i_system.c i_system.h \ diff --git a/src/i_joystick.c b/src/i_joystick.c new file mode 100644 index 00000000..4d73a3af --- /dev/null +++ b/src/i_joystick.c @@ -0,0 +1,166 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 2007 Simon Howard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// +// DESCRIPTION: +// SDL Joystick code. +// +//----------------------------------------------------------------------------- + + +#include "SDL.h" +#include "SDL_joystick.h" + +#include +#include +#include + +#include "doomdef.h" +#include "doomtype.h" +#include "d_event.h" +#include "d_main.h" +#include "i_joystick.h" + +static SDL_Joystick *joystick = NULL; + +// Configuration variables: + +// Standard default.cfg Joystick enable/disable + +extern int usejoystick; + +// Joystick to use, as an SDL joystick index: + +int joystick_index = -1; + +// Which joystick axis to use for horizontal movement, and whether to +// invert the direction: + +int joystick_x_axis = 0; +int joystick_x_invert = 0; + +// Which joystick axis to use for vertical movement, and whether to +// invert the direction: + +int joystick_y_axis = 1; +int joystick_y_invert = 0; + +void I_InitJoystick(void) +{ + int num_axes; + + if (!usejoystick) + { + return; + } + + if (SDL_Init(SDL_INIT_JOYSTICK) < 0) + { + return; + } + + // Open the joystick + + joystick = SDL_JoystickOpen(joystick_index); + + if (joystick == NULL) + { + printf("I_InitJoystick: Failed to open joystick #%i\n", + joystick_index); + SDL_QuitSubSystem(SDL_INIT_JOYSTICK); + return; + } + + num_axes = SDL_JoystickNumAxes(joystick); + + if (joystick_x_axis < 0 || joystick_x_axis >= num_axes + || joystick_y_axis < 0 || joystick_y_axis >= num_axes) + { + printf("I_InitJoystick: Invalid joystick axis for joystick #%i " + "(run joystick setup again)\n", + joystick_index); + + SDL_JoystickClose(joystick); + joystick = NULL; + SDL_QuitSubSystem(SDL_INIT_JOYSTICK); + } + + // Initialised okay! + + printf("I_InitJoystick: %s\n", SDL_JoystickName(joystick_index)); +} + +void I_ShutdownJoystick(void) +{ + if (joystick != NULL) + { + SDL_JoystickClose(joystick); + joystick = NULL; + SDL_QuitSubSystem(SDL_INIT_JOYSTICK); + } +} + +// Get a bitmask of all currently-pressed buttons + +static int GetButtonState(void) +{ + int i; + int result; + + result = 0; + + for (i=0; i