More code cleanups. Separated some stuff into new functions.
[crow/jumpnbump.git] / dos / input.c
1 #include "globals.h"
2
3 static int num_mouse_buttons;
4
5 int init_joy(void)
6 {
7         int c1;
8
9         outportb(0x201, 0);
10         while (c1 < 0x7fff) {
11                 if ((inportb(0x201) & 1) == 0)
12                         break;
13                 c1++;
14         }
15         if (c1 != 0x7fff)
16                 return 1;
17         else
18                 return 0;
19 }
20
21
22 void read_joy(void)
23 {
24         int c1;
25         int x, y;
26         int s1 = 0;
27         char flag;
28
29         c1 = x = y = flag = 0;
30         outportb(0x201, 0);
31
32         while (1) {
33
34                 s1 = inportb(0x201);
35
36                 if (x == 0) {
37                         if ((s1 & 1) == 0)
38                                 x = c1;
39                 }
40                 if (y == 0) {
41                         if ((s1 & 2) == 0)
42                                 y = c1;
43                 }
44                 if (x != 0 && y != 0)
45                         break;
46
47                 c1++;
48                 if (c1 == 0x7fff) {
49                         flag = 1;
50                         break;
51                 }
52
53         }
54
55         if (flag == 0) {
56                 joy.raw_x = x;
57                 joy.raw_y = y;
58
59                 if (joy.raw_x < joy.calib_data.x2)
60                         joy.x = ((long) (joy.raw_x - joy.calib_data.x2) << 10) / (joy.calib_data.x2 - joy.calib_data.x1);
61                 else
62                         joy.x = ((long) (joy.raw_x - joy.calib_data.x2) << 10) / (joy.calib_data.x3 - joy.calib_data.x2);
63                 if (joy.raw_y < joy.calib_data.y2)
64                         joy.y = ((long) (joy.raw_y - joy.calib_data.y2) << 10) / (joy.calib_data.y2 - joy.calib_data.y1);
65                 else
66                         joy.y = ((long) (joy.raw_y - joy.calib_data.y2) << 10) / (joy.calib_data.y3 - joy.calib_data.y2);
67
68                 if (joy.x < -1024)
69                         joy.x = -1024;
70                 if (joy.x > 1024)
71                         joy.x = 1024;
72                 if (joy.y < -1024)
73                         joy.y = -1024;
74                 if (joy.y > 1024)
75                         joy.y = 1024;
76
77                 s1 = inportb(0x201);
78                 joy.but1 = (((s1 >> 4) & 1) ^ 1);
79                 joy.but2 = (((s1 >> 5) & 1) ^ 1);
80         } else {
81                 joy.raw_x = joy.calib_data.x2;
82                 joy.raw_y = joy.calib_data.y2;
83
84                 joy.x = joy.y = 0;
85
86                 joy.but1 = joy.but2 = 0;
87         }
88
89 }
90
91
92 int calib_joy(char type)
93 {
94         int c1;
95         int x, y;
96         int s1 = 0;
97         int num_times;
98         char flag = 0;
99
100         while (joy.but1 == 1) {
101                 s1 = inportb(0x201);
102                 joy.but1 = (((s1 >> 4) & 1) ^ 1);
103                 if (key_pressed(1) == 1) {
104                         while (key_pressed(1) == 1);
105                         return 1;
106                 }
107         }
108
109         num_times = 0;
110
111         while (joy.but1 == 0) {
112
113                 c1 = x = y = flag = 0;
114                 outportb(0x201, 0);
115
116                 while (1) {
117
118                         s1 = inportb(0x201);
119
120                         if (x == 0) {
121                                 if ((s1 & 1) == 0)
122                                         x = c1;
123                         }
124                         if (y == 0) {
125                                 if ((s1 & 2) == 0)
126                                         y = c1;
127                         }
128                         if (x != 0 && y != 0)
129                                 break;
130
131                         c1++;
132                         if (c1 == 0x7fff) {
133                                 flag = 1;
134                                 break;
135                         }
136
137                 }
138
139                 joy.raw_x = x;
140                 joy.raw_y = y;
141
142                 s1 = inportb(0x201);
143                 joy.but1 = (((s1 >> 4) & 1) ^ 1);
144
145                 if (num_times < 0x7fffffff)
146                         num_times++;
147
148                 if (flag == 1)
149                         break;
150
151                 if (key_pressed(1) == 1) {
152                         while (key_pressed(1) == 1);
153                         return 1;
154                 }
155
156         }
157
158         if (num_times < 16)
159                 return 1;
160
161         if (flag == 0) {
162
163                 switch (type) {
164                 case 0:
165                         joy.calib_data.x1 = joy.raw_x;
166                         joy.calib_data.y1 = joy.raw_y;
167                         break;
168                 case 1:
169                         joy.calib_data.x3 = joy.raw_x;
170                         joy.calib_data.y3 = joy.raw_y;
171                         break;
172                 case 2:
173                         joy.calib_data.x2 = joy.raw_x;
174                         joy.calib_data.y2 = joy.raw_y;
175                         break;
176                 }
177
178                 while (joy.but1 == 1) {
179                         s1 = inportb(0x201);
180                         joy.but1 = (((s1 >> 4) & 1) ^ 1);
181                 }
182
183         }
184
185         return 0;
186
187 }
188
189
190 int init_mouse(int *_num_buttons)
191 {
192         __dpmi_regs regs;
193         int mouse_enabled, num_mouse_buttons;
194
195         regs.x.ax = 0;
196         __dpmi_int(0x33, &regs);
197         if (regs.x.ax == 0xffff) {
198                 mouse_enabled = 1;
199                 num_mouse_buttons = regs.x.bx;
200                 if (force2 == 1)
201                         num_mouse_buttons = 2;
202                 if (force3 == 1)
203                         num_mouse_buttons = 3;
204         } else {
205                 mouse_enabled = 0;
206         }
207         if (_num_buttons)
208                 _num_buttons = num_mouse_buttons;
209
210         return mouse_enabled;
211 }
212
213
214 void read_mouse(void)
215 {
216
217         regs.x.ax = 3;
218         __dpmi_int(0x33, &regs);
219         mouse.but1 = regs.x.bx & 1;
220         mouse.but2 = (regs.x.bx & 2) >> 1;
221         mouse.but3 = (regs.x.bx & 4) >> 2;
222
223 }
224
225
226 void update_player_actions(void)
227 {
228         if (main_info.mouse_enabled == 1)
229                 read_mouse();
230         if (main_info.joy_enabled == 1)
231                 read_joy();
232         player[0].action_left   = key_pressed(KEY_PL1_LEFT) == 1;
233         player[0].action_right  = key_pressed(KEY_PL1_RIGHT) == 1;
234         player[0].action_up     = key_pressed(KEY_PL1_JUMP) == 1;
235         player[1].action_left   = key_pressed(KEY_PL2_LEFT) == 1;
236         player[1].action_right  = key_pressed(KEY_PL2_RIGHT) == 1;
237         player[1].action_up     = key_pressed(KEY_PL2_JUMP) == 1;
238         player[2].action_left   = key_pressed(KEY_PL3_LEFT) == 1;
239         player[2].action_right  = key_pressed(KEY_PL3_RIGHT) == 1;
240         player[2].action_up     = key_pressed(KEY_PL3_JUMP) == 1;
241         player[3].action_left   = key_pressed(KEY_PL4_LEFT) == 1;
242         player[3].action_right  = key_pressed(KEY_PL4_RIGHT) == 1;
243         player[3].action_up     = key_pressed(KEY_PL4_JUMP) == 1;
244 }
245
246
247 void init_inputs(void)
248 {
249         main_info.mouse_enabled = init_mouse(&num_mouse_buttons);
250         main_info.joy_enabled = init_joy(&num_mouse_buttons);
251 }
252