rnd-19981002-1
[rocksndiamonds.git] / src / joystick.c
1 /***********************************************************
2 *  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
3 *----------------------------------------------------------*
4 *  (c) 1995-98 Artsoft Entertainment                       *
5 *              Holger Schemel                              *
6 *              Oststrasse 11a                              *
7 *              33604 Bielefeld                             *
8 *              phone: ++49 +521 290471                     *
9 *              email: aeglos@valinor.owl.de                *
10 *----------------------------------------------------------*
11 *  joystick.c                                              *
12 ***********************************************************/
13
14 #ifdef __FreeBSD__
15 #include <machine/joystick.h>
16 #endif
17
18 #include "joystick.h"
19 #include "misc.h"
20
21 /*
22 #include "tools.h"
23 #include "game.h"
24 #include "events.h"
25 #include "sound.h"
26 #include "misc.h"
27 #include "buttons.h"
28 #include <math.h>
29 */
30
31 void CheckJoystickData()
32 {
33   int i;
34   int distance = 100;
35
36   for(i=0;i<2;i++)
37   {
38     if (joystick[i].xmiddle <= distance)
39       joystick[i].xmiddle = distance;
40     if (joystick[i].ymiddle <= distance)
41       joystick[i].ymiddle = distance;
42
43     if (joystick[i].xleft >= joystick[i].xmiddle)
44       joystick[i].xleft = joystick[i].xmiddle-distance;
45     if (joystick[i].xright <= joystick[i].xmiddle)
46       joystick[i].xright = joystick[i].xmiddle+distance;
47
48     if (joystick[i].yupper >= joystick[i].ymiddle)
49       joystick[i].yupper = joystick[i].ymiddle-distance;
50     if (joystick[i].ylower <= joystick[i].ymiddle)
51       joystick[i].ylower = joystick[i].ymiddle+distance;
52   }
53 }
54
55 int JoystickPosition(int middle, int margin, int actual)
56 {
57   long range, pos;
58   int percentage;
59
60   if (margin<middle && actual>middle)
61     return(0);
62   if (margin>middle && actual<middle)
63     return(0);
64
65   range = ABS(margin-middle);
66   pos = ABS(actual-middle);
67   percentage = (int)(pos*100/range);
68   if (percentage>100)
69     percentage = 100;
70
71   return(percentage);
72 }
73
74 int Joystick()
75 {
76 #ifdef __FreeBSD__
77   struct joystick joy_ctrl;
78 #else
79   struct joystick_control
80   {
81     int buttons;
82     int x;
83     int y;
84   } joy_ctrl;
85 #endif
86
87   int js_x,js_y, js_b1,js_b2;
88   int left, right, up, down;
89   int result=0;
90
91   if (joystick_status==JOYSTICK_OFF)
92     return(0);
93
94 #ifndef MSDOS
95   if (read(joystick_device, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
96   {
97     Error(ERR_RETURN, "cannot read joystick settings - no joystick support");
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 }