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>
22 static int JoystickPosition(int middle, int margin, int actual)
27 if (margin < middle && actual > middle)
29 if (margin > middle && actual < middle)
32 range = ABS(margin - middle);
33 pos = ABS(actual - middle);
34 percentage = (int)(pos * 100 / range);
43 #ifdef USE_SDL_JOYSTICK
45 static SDL_Joystick *sdl_joystick[MAX_PLAYERS] = { NULL, NULL, NULL, NULL };
46 static int sdl_js_axis[MAX_PLAYERS][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} };
47 static int sdl_js_button[MAX_PLAYERS][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} };
49 SDL_Joystick *Get_SDL_Joystick(int nr)
51 return sdl_joystick[nr];
54 boolean Open_SDL_Joystick(int nr)
56 if (nr < 0 || nr > MAX_PLAYERS)
59 return ((sdl_joystick[nr] = SDL_JoystickOpen(nr)) == NULL ? FALSE : TRUE);
62 void Close_SDL_Joystick(int nr)
64 if (nr < 0 || nr > MAX_PLAYERS)
67 SDL_JoystickClose(sdl_joystick[nr]);
70 boolean Check_SDL_JoystickOpened(int nr)
72 if (nr < 0 || nr > MAX_PLAYERS)
75 return (SDL_JoystickOpened(nr) ? TRUE : FALSE);
78 void HandleJoystickEvent(Event *event)
82 case SDL_JOYAXISMOTION:
83 if (event->jaxis.axis < 2)
85 sdl_js_axis[event->jaxis.which][event->jaxis.axis]= event->jaxis.value;
88 printf("js_%d %s-axis: %d\n",
90 (event->jaxis.axis == 0 ? "x" : "y"),
96 case SDL_JOYBUTTONDOWN:
97 if (event->jbutton.button < 2)
99 sdl_js_button[event->jbutton.which][event->jbutton.button] = TRUE;
102 printf("js_%d button %d: pressed\n",
103 event->jbutton.which,
104 event->jbutton.button);
109 case SDL_JOYBUTTONUP:
110 if (event->jbutton.button < 2)
112 sdl_js_button[event->jbutton.which][event->jbutton.button] = FALSE;
115 printf("js_%d button %d: released\n",
116 event->jbutton.which,
117 event->jbutton.button);
127 int Get_SDL_Joystick_Axis(int nr, int axis)
129 if (nr < 0 || nr > MAX_PLAYERS)
132 if (axis < 0 || axis > 1)
135 return sdl_js_axis[nr][axis];
138 void CheckJoystickData()
142 int Joystick(int player_nr)
144 int joystick_nr = stored_player[player_nr].joystick_fd;
145 int js_x,js_y, js_b1,js_b2;
146 int left, right, up, down;
149 if (joystick_status == JOYSTICK_OFF)
152 if (game_status == SETUPINPUT)
155 if (!setup.input[player_nr].use_joystick ||
156 !Check_SDL_JoystickOpened(joystick_nr))
159 js_x = sdl_js_axis[joystick_nr][0];
160 js_y = sdl_js_axis[joystick_nr][1];
162 js_b1 = sdl_js_button[joystick_nr][0];
163 js_b2 = sdl_js_button[joystick_nr][1];
168 printf("JOYSTICK %d: js_x == %d, js_y == %d, js_b1 == %d, js_b2 == %d\n",
169 joystick_nr, js_x, js_y, js_b1, js_b2);
174 left = JoystickPosition(setup.input[player_nr].joy.xmiddle,
175 setup.input[player_nr].joy.xleft, js_x);
176 right = JoystickPosition(setup.input[player_nr].joy.xmiddle,
177 setup.input[player_nr].joy.xright, js_x);
178 up = JoystickPosition(setup.input[player_nr].joy.ymiddle,
179 setup.input[player_nr].joy.yupper, js_y);
180 down = JoystickPosition(setup.input[player_nr].joy.ymiddle,
181 setup.input[player_nr].joy.ylower, js_y);
183 if (left > JOYSTICK_PERCENT)
185 else if (right > JOYSTICK_PERCENT)
187 if (up > JOYSTICK_PERCENT)
189 else if (down > JOYSTICK_PERCENT)
193 result |= JOY_BUTTON_1;
195 result |= JOY_BUTTON_2;
200 printf("result == 0x%08x\n", result);
208 #else /* !USE_SDL_JOYSTICK */
210 void CheckJoystickData()
215 for(i=0; i<MAX_PLAYERS; i++)
217 if (setup.input[i].joy.xmiddle <= distance)
218 setup.input[i].joy.xmiddle = distance;
219 if (setup.input[i].joy.ymiddle <= distance)
220 setup.input[i].joy.ymiddle = distance;
222 if (setup.input[i].joy.xleft >= setup.input[i].joy.xmiddle)
223 setup.input[i].joy.xleft = setup.input[i].joy.xmiddle - distance;
224 if (setup.input[i].joy.xright <= setup.input[i].joy.xmiddle)
225 setup.input[i].joy.xright = setup.input[i].joy.xmiddle + distance;
227 if (setup.input[i].joy.yupper >= setup.input[i].joy.ymiddle)
228 setup.input[i].joy.yupper = setup.input[i].joy.ymiddle - distance;
229 if (setup.input[i].joy.ylower <= setup.input[i].joy.ymiddle)
230 setup.input[i].joy.ylower = setup.input[i].joy.ymiddle + distance;
235 int Joystick(int player_nr)
238 struct joystick joy_ctrl;
240 struct joystick_control
248 int joystick_fd = stored_player[player_nr].joystick_fd;
249 int js_x,js_y, js_b1,js_b2;
250 int left, right, up, down;
253 if (joystick_status == JOYSTICK_OFF)
256 if (game_status == SETUPINPUT)
259 if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
262 if (read(joystick_fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
264 Error(ERR_WARN, "cannot read joystick device '%s'",
265 setup.input[player_nr].joy.device_name);
266 joystick_status = JOYSTICK_OFF;
277 js_b1 = joy_ctrl.buttons & 1;
278 js_b2 = joy_ctrl.buttons & 2;
281 left = JoystickPosition(setup.input[player_nr].joy.xmiddle,
282 setup.input[player_nr].joy.xleft, js_x);
283 right = JoystickPosition(setup.input[player_nr].joy.xmiddle,
284 setup.input[player_nr].joy.xright, js_x);
285 up = JoystickPosition(setup.input[player_nr].joy.ymiddle,
286 setup.input[player_nr].joy.yupper, js_y);
287 down = JoystickPosition(setup.input[player_nr].joy.ymiddle,
288 setup.input[player_nr].joy.ylower, js_y);
290 if (left > JOYSTICK_PERCENT)
292 else if (right > JOYSTICK_PERCENT)
294 if (up > JOYSTICK_PERCENT)
296 else if (down > JOYSTICK_PERCENT)
300 result |= JOY_BUTTON_1;
302 result |= JOY_BUTTON_2;
309 /* allegro global variables for joystick control */
310 extern int num_joysticks;
311 extern JOYSTICK_INFO joy[];
313 int Joystick(int player_nr)
315 int joystick_nr = stored_player[player_nr].joystick_fd;
318 if (joystick_status == JOYSTICK_OFF)
321 if (game_status == SETUPINPUT)
327 /* the allegro global variable 'num_joysticks' contains the number
328 of joysticks found at initialization under MSDOS / Windows */
331 if (joystick_nr >= num_joysticks || !setup.input[player_nr].use_joystick)
336 if (joystick_nr >= num_joysticks ||
337 (game_status == PLAYING && !setup.input[player_nr].use_joystick))
340 if (joystick_nr >= num_joysticks)
348 if (joy[joystick_nr].stick[0].axis[0].d1)
350 else if (joy[joystick_nr].stick[0].axis[0].d2)
352 if (joy[joystick_nr].stick[0].axis[1].d1)
354 else if (joy[joystick_nr].stick[0].axis[1].d2)
357 if (joy[joystick_nr].button[0].b)
358 result |= JOY_BUTTON_1;
359 if (joy[joystick_nr].button[1].b)
360 result |= JOY_BUTTON_2;
366 #endif /* !USE_SDL_JOYSTICK */
368 int JoystickButton(int player_nr)
370 static int last_joy_button[MAX_PLAYERS] = { 0, 0, 0, 0 };
371 int joy_button = (Joystick(player_nr) & JOY_BUTTON);
376 if (last_joy_button[player_nr])
377 result = JOY_BUTTON_PRESSED;
379 result = JOY_BUTTON_NEW_PRESSED;
383 if (last_joy_button[player_nr])
384 result = JOY_BUTTON_NEW_RELEASED;
386 result = JOY_BUTTON_NOT_PRESSED;
389 last_joy_button[player_nr] = joy_button;
398 for (i=0; i<MAX_PLAYERS; i++)
402 if (!setup.input[i].use_joystick)
407 result |= Joystick(i);
413 int AnyJoystickButton()
418 for (i=0; i<MAX_PLAYERS; i++)
422 if (!setup.input[i].use_joystick)
427 result |= JoystickButton(i);
430 result = JoystickButton(i);
431 if (result != JOY_BUTTON_NOT_PRESSED)