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