Implemented -scaleup and -nosound options. Some fixes for first release.
[crow/jumpnbump.git] / dos / input.c
1 /*
2  * input.h
3  * Copyright (C) 1998 Brainchild Design - http://brainchilddesign.com/
4  * 
5  * Copyright (C) 2001 tarzeau@space.ch
6  *
7  * Copyright (C) 2002 Florian Schulze - crow@icculus.org
8  *
9  * Portions of this code are from the MPEG software simulation group
10  * idct implementation. This code will be replaced with a new
11  * implementation soon.
12  *
13  * This file is part of Jump'n'Bump.
14  *
15  * Jump'n'Bump is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * Jump'n'Bump is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  */
29
30 #include "globals.h"
31
32 static int num_mouse_buttons;
33
34 int init_joy(void)
35 {
36         int c1;
37
38         outportb(0x201, 0);
39         while (c1 < 0x7fff) {
40                 if ((inportb(0x201) & 1) == 0)
41                         break;
42                 c1++;
43         }
44         if (c1 != 0x7fff)
45                 return 1;
46         else
47                 return 0;
48 }
49
50
51 void read_joy(void)
52 {
53         int c1;
54         int x, y;
55         int s1 = 0;
56         char flag;
57
58         c1 = x = y = flag = 0;
59         outportb(0x201, 0);
60
61         while (1) {
62
63                 s1 = inportb(0x201);
64
65                 if (x == 0) {
66                         if ((s1 & 1) == 0)
67                                 x = c1;
68                 }
69                 if (y == 0) {
70                         if ((s1 & 2) == 0)
71                                 y = c1;
72                 }
73                 if (x != 0 && y != 0)
74                         break;
75
76                 c1++;
77                 if (c1 == 0x7fff) {
78                         flag = 1;
79                         break;
80                 }
81
82         }
83
84         if (flag == 0) {
85                 joy.raw_x = x;
86                 joy.raw_y = y;
87
88                 if (joy.raw_x < joy.calib_data.x2)
89                         joy.x = ((long) (joy.raw_x - joy.calib_data.x2) << 10) / (joy.calib_data.x2 - joy.calib_data.x1);
90                 else
91                         joy.x = ((long) (joy.raw_x - joy.calib_data.x2) << 10) / (joy.calib_data.x3 - joy.calib_data.x2);
92                 if (joy.raw_y < joy.calib_data.y2)
93                         joy.y = ((long) (joy.raw_y - joy.calib_data.y2) << 10) / (joy.calib_data.y2 - joy.calib_data.y1);
94                 else
95                         joy.y = ((long) (joy.raw_y - joy.calib_data.y2) << 10) / (joy.calib_data.y3 - joy.calib_data.y2);
96
97                 if (joy.x < -1024)
98                         joy.x = -1024;
99                 if (joy.x > 1024)
100                         joy.x = 1024;
101                 if (joy.y < -1024)
102                         joy.y = -1024;
103                 if (joy.y > 1024)
104                         joy.y = 1024;
105
106                 s1 = inportb(0x201);
107                 joy.but1 = (((s1 >> 4) & 1) ^ 1);
108                 joy.but2 = (((s1 >> 5) & 1) ^ 1);
109         } else {
110                 joy.raw_x = joy.calib_data.x2;
111                 joy.raw_y = joy.calib_data.y2;
112
113                 joy.x = joy.y = 0;
114
115                 joy.but1 = joy.but2 = 0;
116         }
117
118 }
119
120
121 int calib_joy(int type)
122 {
123         int c1;
124         int x, y;
125         int s1 = 0;
126         int num_times;
127         char flag = 0;
128
129         while (joy.but1 == 1) {
130                 s1 = inportb(0x201);
131                 joy.but1 = (((s1 >> 4) & 1) ^ 1);
132                 if (key_pressed(1) == 1) {
133                         while (key_pressed(1) == 1);
134                         return 1;
135                 }
136         }
137
138         num_times = 0;
139
140         while (joy.but1 == 0) {
141
142                 c1 = x = y = flag = 0;
143                 outportb(0x201, 0);
144
145                 while (1) {
146
147                         s1 = inportb(0x201);
148
149                         if (x == 0) {
150                                 if ((s1 & 1) == 0)
151                                         x = c1;
152                         }
153                         if (y == 0) {
154                                 if ((s1 & 2) == 0)
155                                         y = c1;
156                         }
157                         if (x != 0 && y != 0)
158                                 break;
159
160                         c1++;
161                         if (c1 == 0x7fff) {
162                                 flag = 1;
163                                 break;
164                         }
165
166                 }
167
168                 joy.raw_x = x;
169                 joy.raw_y = y;
170
171                 s1 = inportb(0x201);
172                 joy.but1 = (((s1 >> 4) & 1) ^ 1);
173
174                 if (num_times < 0x7fffffff)
175                         num_times++;
176
177                 if (flag == 1)
178                         break;
179
180                 if (key_pressed(1) == 1) {
181                         while (key_pressed(1) == 1);
182                         return 1;
183                 }
184
185         }
186
187         if (num_times < 16)
188                 return 1;
189
190         if (flag == 0) {
191
192                 switch (type) {
193                 case 0:
194                         joy.calib_data.x1 = joy.raw_x;
195                         joy.calib_data.y1 = joy.raw_y;
196                         break;
197                 case 1:
198                         joy.calib_data.x3 = joy.raw_x;
199                         joy.calib_data.y3 = joy.raw_y;
200                         break;
201                 case 2:
202                         joy.calib_data.x2 = joy.raw_x;
203                         joy.calib_data.y2 = joy.raw_y;
204                         break;
205                 }
206
207                 while (joy.but1 == 1) {
208                         s1 = inportb(0x201);
209                         joy.but1 = (((s1 >> 4) & 1) ^ 1);
210                 }
211
212         }
213
214         return 0;
215
216 }
217
218
219 int init_mouse(int *_num_buttons)
220 {
221         __dpmi_regs regs;
222         int mouse_enabled, num_mouse_buttons;
223
224         regs.x.ax = 0;
225         __dpmi_int(0x33, &regs);
226         if (regs.x.ax == 0xffff) {
227                 mouse_enabled = 1;
228                 num_mouse_buttons = regs.x.bx;
229                 if (force2 == 1)
230                         num_mouse_buttons = 2;
231                 if (force3 == 1)
232                         num_mouse_buttons = 3;
233         } else {
234                 mouse_enabled = 0;
235         }
236         if (_num_buttons)
237                 _num_buttons = num_mouse_buttons;
238
239         return mouse_enabled;
240 }
241
242
243 void read_mouse(void)
244 {
245
246         regs.x.ax = 3;
247         __dpmi_int(0x33, &regs);
248         mouse.but1 = regs.x.bx & 1;
249         mouse.but2 = (regs.x.bx & 2) >> 1;
250         mouse.but3 = (regs.x.bx & 4) >> 2;
251
252 }
253
254
255 void update_player_actions(void)
256 {
257         if (main_info.mouse_enabled == 1)
258                 read_mouse();
259         if (main_info.joy_enabled == 1)
260                 read_joy();
261         player[0].action_left   = key_pressed(KEY_PL1_LEFT) == 1;
262         player[0].action_right  = key_pressed(KEY_PL1_RIGHT) == 1;
263         player[0].action_up     = key_pressed(KEY_PL1_JUMP) == 1;
264         player[1].action_left   = key_pressed(KEY_PL2_LEFT) == 1;
265         player[1].action_right  = key_pressed(KEY_PL2_RIGHT) == 1;
266         player[1].action_up     = key_pressed(KEY_PL2_JUMP) == 1;
267         player[2].action_left   = key_pressed(KEY_PL3_LEFT) == 1;
268         player[2].action_right  = key_pressed(KEY_PL3_RIGHT) == 1;
269         player[2].action_up     = key_pressed(KEY_PL3_JUMP) == 1;
270         player[3].action_left   = key_pressed(KEY_PL4_LEFT) == 1;
271         player[3].action_right  = key_pressed(KEY_PL4_RIGHT) == 1;
272         player[3].action_up     = key_pressed(KEY_PL4_JUMP) == 1;
273 }
274
275
276 void init_inputs(void)
277 {
278         main_info.mouse_enabled = init_mouse(&num_mouse_buttons);
279         main_info.joy_enabled = init_joy(&num_mouse_buttons);
280 }
281