rnd-19970921-src
[rocksndiamonds.git] / src / joystick.c
1 /***********************************************************
2 *  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
3 *----------------------------------------------------------*
4 *  ©1995 Artsoft Development                               *
5 *        Holger Schemel                                    *
6 *        33604 Bielefeld                                   *
7 *        Telefon: (0521) 290471                            *
8 *        eMail: aeglos@valinor.owl.de                      *
9 *               aeglos@uni-paderborn.de                    *
10 *----------------------------------------------------------*
11 *  joystick.c                                              *
12 ***********************************************************/
13
14 #ifdef __FreeBSD__
15 #include <machine/joystick.h>
16 #endif
17
18 #include "joystick.h"
19
20 /*
21 #include "tools.h"
22 #include "game.h"
23 #include "events.h"
24 #include "sound.h"
25 #include "misc.h"
26 #include "buttons.h"
27 #include <math.h>
28 */
29
30 void CheckJoystickData()
31 {
32   int i;
33   int distance = 100;
34
35   for(i=0;i<2;i++)
36   {
37     if (joystick[i].xmiddle <= distance)
38       joystick[i].xmiddle = distance;
39     if (joystick[i].ymiddle <= distance)
40       joystick[i].ymiddle = distance;
41
42     if (joystick[i].xleft >= joystick[i].xmiddle)
43       joystick[i].xleft = joystick[i].xmiddle-distance;
44     if (joystick[i].xright <= joystick[i].xmiddle)
45       joystick[i].xright = joystick[i].xmiddle+distance;
46
47     if (joystick[i].yupper >= joystick[i].ymiddle)
48       joystick[i].yupper = joystick[i].ymiddle-distance;
49     if (joystick[i].ylower <= joystick[i].ymiddle)
50       joystick[i].ylower = joystick[i].ymiddle+distance;
51   }
52 }
53
54 int JoystickPosition(int middle, int margin, int actual)
55 {
56   long range, pos;
57   int percentage;
58
59   if (margin<middle && actual>middle)
60     return(0);
61   if (margin>middle && actual<middle)
62     return(0);
63
64   range = ABS(margin-middle);
65   pos = ABS(actual-middle);
66   percentage = (int)(pos*100/range);
67   if (percentage>100)
68     percentage = 100;
69
70   return(percentage);
71 }
72
73 int Joystick()
74 {
75 #ifdef __FreeBSD__
76   struct joystick joy_ctrl;
77 #else
78   struct joystick_control
79   {
80     int buttons;
81     int x;
82     int y;
83   } joy_ctrl;
84 #endif
85
86   int js_x,js_y, js_b1,js_b2;
87   int left, right, up, down;
88   int result=0;
89
90   if (joystick_status==JOYSTICK_OFF)
91     return(0);
92
93   if (read(joystick_device, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
94   {
95     fprintf(stderr,"%s: cannot read joystick settings - no joystick support\n",
96             progname);
97     joystick_status = JOYSTICK_OFF;
98     return(0);
99   }
100
101   js_x  = joy_ctrl.x;
102   js_y  = joy_ctrl.y;
103 #ifdef __FreeBSD__
104   js_b1 = joy_ctrl.b1;
105   js_b2 = joy_ctrl.b2;
106 #else
107   js_b1 = joy_ctrl.buttons & 1;
108   js_b2 = joy_ctrl.buttons & 2;
109 #endif
110
111   left = JoystickPosition(joystick[joystick_nr].xmiddle,
112                           joystick[joystick_nr].xleft,  js_x);
113   right = JoystickPosition(joystick[joystick_nr].xmiddle,
114                            joystick[joystick_nr].xright, js_x);
115   up =    JoystickPosition(joystick[joystick_nr].ymiddle,
116                            joystick[joystick_nr].yupper, js_y);
117   down =  JoystickPosition(joystick[joystick_nr].ymiddle,
118                            joystick[joystick_nr].ylower, js_y);
119
120   if (left>JOYSTICK_PERCENT)
121     result |= JOY_LEFT;
122   else if (right>JOYSTICK_PERCENT)
123     result |= JOY_RIGHT;
124   if (up>JOYSTICK_PERCENT)
125     result |= JOY_UP;
126   else if (down>JOYSTICK_PERCENT)
127     result |= JOY_DOWN;
128   if (js_b1)
129     result |= JOY_BUTTON_1;
130   if (js_b2)
131     result |= JOY_BUTTON_2;
132
133   return(result);
134 }
135
136 int JoystickButton()
137 {
138   static int last_joy_button = 0;
139   int joy_button = (Joystick() & JOY_BUTTON);
140   int result;
141
142   if (joy_button)
143   {
144     if (last_joy_button)
145       result = JOY_BUTTON_PRESSED;
146     else
147       result = JOY_BUTTON_NEW_PRESSED;
148   }
149   else
150   {
151     if (last_joy_button)
152       result = JOY_BUTTON_NEW_RELEASED;
153     else
154       result = JOY_BUTTON_NOT_PRESSED;
155   }
156
157   last_joy_button = joy_button;
158   return(result);
159 }