7bf648ed75cd766059ff843b7a966659ac884c4c
[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 #ifndef MSDOS
94   if (read(joystick_device, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
95   {
96     fprintf(stderr,"%s: cannot read joystick settings - no joystick support\n",
97             progname);
98     joystick_status = JOYSTICK_OFF;
99     return(0);
100   }
101
102   js_x  = joy_ctrl.x;
103   js_y  = joy_ctrl.y;
104 #ifdef __FreeBSD__
105   js_b1 = joy_ctrl.b1;
106   js_b2 = joy_ctrl.b2;
107 #else
108   js_b1 = joy_ctrl.buttons & 1;
109   js_b2 = joy_ctrl.buttons & 2;
110 #endif
111
112   left = JoystickPosition(joystick[joystick_nr].xmiddle,
113                           joystick[joystick_nr].xleft,  js_x);
114   right = JoystickPosition(joystick[joystick_nr].xmiddle,
115                            joystick[joystick_nr].xright, js_x);
116   up =    JoystickPosition(joystick[joystick_nr].ymiddle,
117                            joystick[joystick_nr].yupper, js_y);
118   down =  JoystickPosition(joystick[joystick_nr].ymiddle,
119                            joystick[joystick_nr].ylower, js_y);
120
121   if (left>JOYSTICK_PERCENT)
122     result |= JOY_LEFT;
123   else if (right>JOYSTICK_PERCENT)
124     result |= JOY_RIGHT;
125   if (up>JOYSTICK_PERCENT)
126     result |= JOY_UP;
127   else if (down>JOYSTICK_PERCENT)
128     result |= JOY_DOWN;
129   if (js_b1)
130     result |= JOY_BUTTON_1;
131   if (js_b2)
132     result |= JOY_BUTTON_2;
133
134   return(result);
135 #else
136   return(0);
137 #endif
138 }
139
140 int JoystickButton()
141 {
142   static int last_joy_button = 0;
143   int joy_button = (Joystick() & JOY_BUTTON);
144   int result;
145
146   if (joy_button)
147   {
148     if (last_joy_button)
149       result = JOY_BUTTON_PRESSED;
150     else
151       result = JOY_BUTTON_NEW_PRESSED;
152   }
153   else
154   {
155     if (last_joy_button)
156       result = JOY_BUTTON_NEW_RELEASED;
157     else
158       result = JOY_BUTTON_NOT_PRESSED;
159   }
160
161   last_joy_button = joy_button;
162   return(result);
163 }