1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * (c) 1995-98 Artsoft Entertainment *
8 * phone: ++49 +521 290471 *
9 * email: aeglos@valinor.owl.de *
10 *----------------------------------------------------------*
12 ***********************************************************/
15 #include <machine/joystick.h>
21 void CheckJoystickData()
26 for(i=0; i<MAX_PLAYERS; i++)
28 if (setup.input[i].joy.xmiddle <= distance)
29 setup.input[i].joy.xmiddle = distance;
30 if (setup.input[i].joy.ymiddle <= distance)
31 setup.input[i].joy.ymiddle = distance;
33 if (setup.input[i].joy.xleft >= setup.input[i].joy.xmiddle)
34 setup.input[i].joy.xleft = setup.input[i].joy.xmiddle - distance;
35 if (setup.input[i].joy.xright <= setup.input[i].joy.xmiddle)
36 setup.input[i].joy.xright = setup.input[i].joy.xmiddle + distance;
38 if (setup.input[i].joy.yupper >= setup.input[i].joy.ymiddle)
39 setup.input[i].joy.yupper = setup.input[i].joy.ymiddle - distance;
40 if (setup.input[i].joy.ylower <= setup.input[i].joy.ymiddle)
41 setup.input[i].joy.ylower = setup.input[i].joy.ymiddle + distance;
46 static int JoystickPosition(int middle, int margin, int actual)
51 if (margin < middle && actual > middle)
53 if (margin > middle && actual < middle)
56 range = ABS(margin - middle);
57 pos = ABS(actual - middle);
58 percentage = (int)(pos * 100 / range);
68 int Joystick(int player_nr)
71 struct joystick joy_ctrl;
73 struct joystick_control
81 int joystick_fd = stored_player[player_nr].joystick_fd;
82 int js_x,js_y, js_b1,js_b2;
83 int left, right, up, down;
86 if (joystick_status == JOYSTICK_OFF)
89 if (game_status == SETUPINPUT)
92 if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
95 if (read(joystick_fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
97 Error(ERR_WARN, "cannot read joystick device '%s'",
98 setup.input[player_nr].joy.device_name);
99 joystick_status = JOYSTICK_OFF;
110 js_b1 = joy_ctrl.buttons & 1;
111 js_b2 = joy_ctrl.buttons & 2;
114 left = JoystickPosition(setup.input[player_nr].joy.xmiddle,
115 setup.input[player_nr].joy.xleft, js_x);
116 right = JoystickPosition(setup.input[player_nr].joy.xmiddle,
117 setup.input[player_nr].joy.xright, js_x);
118 up = JoystickPosition(setup.input[player_nr].joy.ymiddle,
119 setup.input[player_nr].joy.yupper, js_y);
120 down = JoystickPosition(setup.input[player_nr].joy.ymiddle,
121 setup.input[player_nr].joy.ylower, js_y);
123 if (left > JOYSTICK_PERCENT)
125 else if (right > JOYSTICK_PERCENT)
127 if (up > JOYSTICK_PERCENT)
129 else if (down > JOYSTICK_PERCENT)
133 result |= JOY_BUTTON_1;
135 result |= JOY_BUTTON_2;
142 /* allegro global variables for joystick control */
143 extern int num_joysticks;
144 extern JOYSTICK_INFO joy[];
146 int Joystick(int player_nr)
148 int joystick_nr = stored_player[player_nr].joystick_fd;
151 if (joystick_status == JOYSTICK_OFF)
154 if (game_status == SETUPINPUT)
160 /* the allegro global variable 'num_joysticks' contains the number
161 of joysticks found at initialization under MSDOS / Windows */
164 if (joystick_nr >= num_joysticks || !setup.input[player_nr].use_joystick)
169 if (joystick_nr >= num_joysticks ||
170 (game_status == PLAYING && !setup.input[player_nr].use_joystick))
173 if (joystick_nr >= num_joysticks)
181 if (joy[joystick_nr].stick[0].axis[0].d1)
183 else if (joy[joystick_nr].stick[0].axis[0].d2)
185 if (joy[joystick_nr].stick[0].axis[1].d1)
187 else if (joy[joystick_nr].stick[0].axis[1].d2)
190 if (joy[joystick_nr].button[0].b)
191 result |= JOY_BUTTON_1;
192 if (joy[joystick_nr].button[1].b)
193 result |= JOY_BUTTON_2;
199 int JoystickButton(int player_nr)
201 static int last_joy_button[MAX_PLAYERS] = { 0, 0, 0, 0 };
202 int joy_button = (Joystick(player_nr) & JOY_BUTTON);
207 if (last_joy_button[player_nr])
208 result = JOY_BUTTON_PRESSED;
210 result = JOY_BUTTON_NEW_PRESSED;
214 if (last_joy_button[player_nr])
215 result = JOY_BUTTON_NEW_RELEASED;
217 result = JOY_BUTTON_NOT_PRESSED;
220 last_joy_button[player_nr] = joy_button;
229 for (i=0; i<MAX_PLAYERS; i++)
233 if (!setup.input[i].use_joystick)
238 result |= Joystick(i);
244 int AnyJoystickButton()
249 for (i=0; i<MAX_PLAYERS; i++)
253 if (!setup.input[i].use_joystick)
258 result |= JoystickButton(i);
261 result = JoystickButton(i);
262 if (result != JOY_BUTTON_NOT_PRESSED)