-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathinput.c
338 lines (317 loc) · 8.65 KB
/
input.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* Digger Remastered
Copyright (c) Andrew Jenner 1998-2004 */
#include "def.h"
#include "input.h"
#include "main.h"
#include "sound.h"
#include "hardware.h"
#include "record.h"
#include "digger.h"
#include "game.h"
#ifdef _SDL
#include "sdl_kbd.h"
#elif defined(_VGL)
#include "fbsd_kbd.h"
#endif
/* global variables first */
bool escape=false,firepflag=false,fire2pflag=false,pausef=false,mode_change=false;
bool krdf[NKEYS]={false,false,false,false,false,false,false,false,false,false,
false,false,false,false,false,false,false,false};
static bool aleftpressed=false,arightpressed=false,
auppressed=false,adownpressed=false,start=false,af1pressed=false;
static bool aleft2pressed=false,aright2pressed=false,
aup2pressed=false,adown2pressed=false,af12pressed=false;
int16_t akeypressed;
static int16_t dynamicdir=-1,dynamicdir2=-1,staticdir=-1,staticdir2=-1,joyx=0,joyy=0;
static bool joybut1=false;
static int16_t keydir=0,keydir2=0,jleftthresh=0,jupthresh=0,jrightthresh=0,
jdownthresh=0,joyanax=0,joyanay=0;
static bool joyflag=false;
void readjoy(void);
/* The standard ASCII keyboard is also checked so that very short keypresses
are not overlooked. The functions kbhit() (returns bool denoting whether or
not there is a key in the buffer) and getkey() (wait until a key is in the
buffer, then return it) are used. These functions are emulated on platforms
which only provide an inkey() function (return the key in the buffer, unless
there is none, in which case return -1. It is done this way around for
historical reasons, there is no fundamental reason why it shouldn't be the
other way around. */
void checkkeyb(void)
{
int i,j,k=0;
bool *aflagp[10]={&arightpressed,&auppressed,&aleftpressed,&adownpressed,
&af1pressed,&aright2pressed,&aup2pressed,&aleft2pressed,
&adown2pressed,&af12pressed};
if (leftpressed)
aleftpressed=true;
if (rightpressed)
arightpressed=true;
if (uppressed)
auppressed=true;
if (downpressed)
adownpressed=true;
if (f1pressed)
af1pressed=true;
if (left2pressed)
aleft2pressed=true;
if (right2pressed)
aright2pressed=true;
if (up2pressed)
aup2pressed=true;
if (down2pressed)
adown2pressed=true;
if (f12pressed)
af12pressed=true;
while (kbhit()) {
akeypressed=getkey(true);
for (i=0;i<10;i++)
for (j=2;j<5;j++)
if (akeypressed==keycodes[i][j])
*aflagp[i]=true;
for (i=10;i<NKEYS;i++)
for (j=0;j<5;j++)
if (akeypressed==keycodes[i][j])
k=i;
switch (k) {
case DKEY_CHT: /* Cheat! */
if (!dgstate.gauntlet) {
playing=false;
drfvalid=false;
}
break;
case DKEY_SUP: /* Increase speed */
if (dgstate.ftime>10000l)
dgstate.ftime-=10000l;
break;
case DKEY_SDN: /* Decrease speed */
dgstate.ftime+=10000l;
break;
case DKEY_MTG: /* Toggle music */
musicflag=!musicflag;
break;
case DKEY_STG: /* Toggle sound */
soundflag=!soundflag;
break;
case DKEY_EXT: /* Exit */
escape=true;
break;
case DKEY_PUS: /* Pause */
pausef=true;
break;
case DKEY_MCH: /* Mode change */
mode_change=true;
break;
case DKEY_SDR: /* Save DRF */
savedrf=true;
break;
}
if (!mode_change)
start=true; /* Change number of players */
}
}
/* Joystick not yet implemented. It will be, though, using gethrt on platform
DOSPC. */
void readjoy(void)
{
}
void detectjoy(void)
{
joyflag=false;
staticdir=dynamicdir=DIR_NONE;
}
/* Contrary to some beliefs, you don't need a separate OS call to flush the
keyboard buffer. */
void flushkeybuf(void)
{
while (kbhit())
getkey(true);
aleftpressed=arightpressed=auppressed=adownpressed=af1pressed=false;
aleft2pressed=aright2pressed=aup2pressed=adown2pressed=af12pressed=false;
}
void clearfire(int n)
{
if (n==0)
af1pressed=false;
else
af12pressed=false;
}
bool oupressed=false,odpressed=false,olpressed=false,orpressed=false;
bool ou2pressed=false,od2pressed=false,ol2pressed=false,or2pressed=false;
void readdirect(int n)
{
int16_t j;
bool u=false,d=false,l=false,r=false;
bool u2=false,d2=false,l2=false,r2=false;
if (n==0) {
if (auppressed || uppressed) { u=true; auppressed=false; }
if (adownpressed || downpressed) { d=true; adownpressed=false; }
if (aleftpressed || leftpressed) { l=true; aleftpressed=false; }
if (arightpressed || rightpressed) { r=true; arightpressed=false; }
if (f1pressed || af1pressed) {
firepflag=true;
af1pressed=false;
}
else
firepflag=false;
if (u && !oupressed)
staticdir=dynamicdir=DIR_UP;
if (d && !odpressed)
staticdir=dynamicdir=DIR_DOWN;
if (l && !olpressed)
staticdir=dynamicdir=DIR_LEFT;
if (r && !orpressed)
staticdir=dynamicdir=DIR_RIGHT;
if ((oupressed && !u && dynamicdir==DIR_UP) ||
(odpressed && !d && dynamicdir==DIR_DOWN) ||
(olpressed && !l && dynamicdir==DIR_LEFT) ||
(orpressed && !r && dynamicdir==DIR_RIGHT)) {
dynamicdir=DIR_NONE;
if (u) dynamicdir=staticdir=2;
if (d) dynamicdir=staticdir=6;
if (l) dynamicdir=staticdir=4;
if (r) dynamicdir=staticdir=0;
}
oupressed=u;
odpressed=d;
olpressed=l;
orpressed=r;
keydir=staticdir;
if (dynamicdir!=DIR_NONE)
keydir=dynamicdir;
staticdir=DIR_NONE;
}
else {
if (aup2pressed || up2pressed) { u2=true; aup2pressed=false; }
if (adown2pressed || down2pressed) { d2=true; adown2pressed=false; }
if (aleft2pressed || left2pressed) { l2=true; aleft2pressed=false; }
if (aright2pressed || right2pressed) { r2=true; aright2pressed=false; }
if (f12pressed || af12pressed) {
fire2pflag=true;
af12pressed=false;
}
else
fire2pflag=false;
if (u2 && !ou2pressed)
staticdir2=dynamicdir2=DIR_UP;
if (d2 && !od2pressed)
staticdir2=dynamicdir2=DIR_DOWN;
if (l2 && !ol2pressed)
staticdir2=dynamicdir2=DIR_LEFT;
if (r2 && !or2pressed)
staticdir2=dynamicdir2=DIR_RIGHT;
if ((ou2pressed && !u2 && dynamicdir2==DIR_UP) ||
(od2pressed && !d2 && dynamicdir2==DIR_DOWN) ||
(ol2pressed && !l2 && dynamicdir2==DIR_LEFT) ||
(or2pressed && !r2 && dynamicdir2==DIR_RIGHT)) {
dynamicdir2=DIR_NONE;
if (u2) dynamicdir2=staticdir2=2;
if (d2) dynamicdir2=staticdir2=6;
if (l2) dynamicdir2=staticdir2=4;
if (r2) dynamicdir2=staticdir2=0;
}
ou2pressed=u2;
od2pressed=d2;
ol2pressed=l2;
or2pressed=r2;
keydir2=staticdir2;
if (dynamicdir2!=DIR_NONE)
keydir2=dynamicdir2;
staticdir2=DIR_NONE;
}
if (joyflag) {
incpenalty();
incpenalty();
joyanay=0;
joyanax=0;
for (j=0;j<4;j++) {
readjoy();
joyanax+=joyx;
joyanay+=joyy;
}
joyx=joyanax>>2;
joyy=joyanay>>2;
if (joybut1)
firepflag=true;
else
firepflag=false;
}
}
/* Calibrate joystick while waiting at title screen. This works more
effectively if the user waggles the joystick in the title screen. */
bool teststart(void)
{
int16_t j;
bool startf=false;
if (joyflag) {
readjoy();
if (joybut1)
startf=true;
}
if (start) {
start=false;
startf=true;
joyflag=false;
}
if (!startf)
return false;
if (joyflag) {
joyanay=0;
joyanax=0;
for (j=0;j<50;j++) {
readjoy();
joyanax+=joyx;
joyanay+=joyy;
}
joyx=joyanax/50;
joyy=joyanay/50;
jleftthresh=joyx-35;
if (jleftthresh<0)
jleftthresh=0;
jleftthresh+=10;
jupthresh=joyy-35;
if (jupthresh<0)
jupthresh=0;
jupthresh+=10;
jrightthresh=joyx+35;
if (jrightthresh>255)
jrightthresh=255;
jrightthresh-=10;
jdownthresh=joyy+35;
if (jdownthresh>255)
jdownthresh=255;
jdownthresh-=10;
joyanax=joyx;
joyanay=joyy;
}
return true;
}
/* Why the joystick reading is split between readdirect and getdir like this is a
mystery to me. */
int16_t getdirect(int n)
{
int16_t dir=((n==0) ? keydir : keydir2);
if (joyflag) {
dir=DIR_NONE;
if (joyx<jleftthresh)
dir=DIR_LEFT;
if (joyx>jrightthresh)
dir=DIR_RIGHT;
if (joyx>=jleftthresh && joyx<=jrightthresh) {
if (joyy<jupthresh)
dir=DIR_UP;
if (joyy>jdownthresh)
dir=DIR_DOWN;
}
}
if (n==0) {
if (playing)
playgetdir(&dir,&firepflag);
recputdir(dir,firepflag);
}
else {
if (playing)
playgetdir(&dir,&fire2pflag);
recputdir(dir,fire2pflag);
}
return dir;
}