-
Notifications
You must be signed in to change notification settings - Fork 0
/
CONTROL.C
177 lines (145 loc) · 5.17 KB
/
CONTROL.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// CONTROL.C
// Handles keyboard/joystick interfaces into a unified system.
// Copyright (C)1997 BJ Eirich
#include "keyboard.h"
/* -- ric: 03/May/98 -- */
struct keyb_map {
char pressed; // keyboard flags
short int boundscript; }; // bound script
struct keyb_map key_map[128]; // for recording bound keys
char j; // use joystick or not
char b1,b2,b3,b4; // four button flags for GamePad
char up,down,left,right; // stick position flags
int jx,jy; // joystick x / y values
char foundx,foundy; // found-flags for joystick read.
int cenx, ceny; // stick-center values
int upb,downb,leftb,rightb; // barriers for axis determination
// --- Control masks
char kb1, kb2, kb3, kb4; // keyboard definable controls.
char jb1, jb2, jb3, jb4; // joystick definable controls.
initcontrols(char joystk)
{ int i;
j=joystk;
if (j)
if (!calibrate()) j=0;
for (i=0;i<128;i++) key_map[i].boundscript=0; // no keys are bound yet
}
readb()
{
if (j) readbuttons();
else { b1=0; b2=0; b3=0; b4=0; }
if (keyboard_map[kb1]) b1=1;
if (keyboard_map[kb2]) b2=1;
if (keyboard_map[kb3]) b3=1;
if (keyboard_map[kb4]) b4=1;
if ((keyboard_map[SCAN_ALT]) &&
(keyboard_map[SCAN_X]))
err("Exiting: ALT-X pressed.");
if (keyboard_map[SCAN_F10])
{ keyboard_map[SCAN_F10]=0;
ScreenShot(); }
}
readcontrols()
{ int i;
if (j) readjoystick();
else { b1=0; b2=0; b3=0; b4=0;
up=0; down=0; left=0; right=0; }
if (keyboard_map[SCAN_UP]) up=1;
if (keyboard_map[SCAN_DOWN]) down=1;
if (keyboard_map[SCAN_LEFT]) left=1;
if (keyboard_map[SCAN_RIGHT]) right=1;
if (keyboard_map[kb1]) b1=1;
if (keyboard_map[kb2]) b2=1;
if (keyboard_map[kb3]) b3=1;
if (keyboard_map[kb4]) b4=1;
for (i=0;i<128;i++) { /* -- ric: 03/May/98 -- */
key_map[i].pressed=0;
if (keyboard_map[i]) key_map[i].pressed=1; // no keys are bound yet
}
if ((keyboard_map[SCAN_ALT]) &&
(keyboard_map[SCAN_X]))
err("Exiting: ALT-X pressed.");
if (keyboard_map[SCAN_F10])
{ keyboard_map[SCAN_F10]=0;
ScreenShot(); }
}
readbuttons()
{ unsigned char b;
char btbl[4];
b=inportb(0x201); // poll joystick port
b=b >> 4; // lose high nibble
b=b ^ 15; // flip mask bits
btbl[0]=b & 1; // mask button status
btbl[1]=b & 2;
btbl[2]=b & 4;
btbl[3]=b & 8;
if (btbl[jb1-1]) b1=1; else b1=0;
if (btbl[jb2-1]) b2=1; else b2=0;
if (btbl[jb3-1]) b3=1; else b3=0;
if (btbl[jb4-1]) b4=1; else b4=0;
}
getcoordinates()
// Gets raw, machine dependant coordinates from the joystick.
{
foundx=0;
foundy=0;
asm("cli \n\t" // disable interrupts
"movw $513, %%dx \n\t" // start joystick timer
"outb %%al, %%dx \n\t"
"xorl %%ecx, %%ecx \n\t" // clear out counter
"movl $2, %%ebx \n\t" // number of axii left to report
"joyloop: \n\t"
"incl %%ecx \n\t" // increment counter
"cmpl $65500, %%ecx \n\t" // time out?
"je j_end \n\t"
"inb %%dx, %%al \n\t" // poll joystick status
"cmpb $1, _foundx \n\t"
"je search_y \n\t"
"test $1, %%al \n\t" // is this axis in?
"jnz j0 \n\t"
"movl %%ecx, _jx \n\t" // if so, store coordinate
"movl $1, _foundx \n\t" // say we already got it
"decl %%ebx \n\t" // one less axis to go.
"jz j_end \n\t"
"j0: \n\t"
"cmp $1, _foundy \n\t"
"je joyloop \n\t"
"search_y: \n\t"
"test $2, %%al \n\t" // is the Y axis in?
"jnz joyloop \n\t"
"movl %%ecx, _jy \n\t" // if so, handle it.
"movl $1, _foundy \n\t"
"decl %%ebx \n\t"
"jz j_end \n\t" // are we done?
"jmp joyloop \n\t"
"j_end: \n\t"
"sti \n\t" // turn interrupts back on
:
:
: "eax","ebx","ecx","edx","cc" );
}
int calibrate()
{ // assumes the stick is centered when called.
getcoordinates(); // read stick position
if ((!foundx) || (!foundy))
{ printf("Could not detect joystick. Disabling.\n");
return 0; }
cenx=jx;
ceny=jy;
upb=(ceny*75)/100; // 25% dead zone
leftb=(cenx*75)/100;
rightb=(cenx*125)/100; // 25% dead zone
downb=(ceny*125)/100;
return 1;
}
readjoystick()
{
readbuttons();
getcoordinates();
up=0; down=0;
left=0; right=0;
if (jx<leftb) left=1;
if (jx>rightb) right=1;
if (jy<upb) up=1;
if (jy>downb) down=1;
}