This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
INPUT.CPP
executable file
·340 lines (280 loc) · 7.1 KB
/
INPUT.CPP
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
339
340
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include "types.h"
#ifdef DOS
extern "C" {
void __cdecl ReadAnalogJoyPos(int *,int);
int __cdecl ReadAnalogJoyButtons();
};
#endif
#ifdef WIN95
#include <windows.h>
#include <dinput.h>
#endif
#include "input.h"
#include "dd.h"
//#include "object.h"
//input device settings.
extern inputdevicesettings *ids;
//default keymaps
keymap defkeymap[2]=
{
{
0x47,0x48,0x49,
0x4B,0x4D,
0x4F,0x50,0x51,
0x18,0x19,0x1a,
0x26,0x27,0x28
},
{
0x13,0x14,0x15,
0x21,0x23,
0x2F,0x30,0x31,
0x10,0x11,0x12,
0x1E,0x1F,0x20
},
};
//refresh during keypress
int input::refreshkeyboard(char kbscan)
{
if (inputdevice[0]) if (inputdevice[0]->key(kbscan)) return 1;
if (inputdevice[1]) if (inputdevice[1]->key(kbscan)) return 1;
return 0;
}
//refresh during imer
void input::refreshtimer()
{
if (inputdevice[0]) inputdevice[0]->timer();
if (inputdevice[1]) inputdevice[1]->timer();
}
//refresh during main loop
void input::refreshmain()
{
if (inputdevice[0]) inputdevice[0]->main();
if (inputdevice[1]) inputdevice[1]->main();
}
//binds the input device to an object...
void input::bind(object *t)
{
if (!this) return;
//unbind the object from whatever it's attached to
unbind();
//disable();
//bind to object
// o=t; if (o) o->in=this;
// enable();
}
//unbinds input device from object it's attached to
void input::unbind()
{
if (!this) return;
// disable();
// if (o) o->in=NULL;
o=NULL;
// enable();
}
unsigned input::getstat()
{
if (!this) return 0;
// if (o && o->d && (stat&3)) return stat^3;
return stat;
}
//--------------------------
// specific input devices
//-------------------------
class inputnone:public input
{
public:
inputnone():input(ID_NONE) {};
virtual void read() {stat=0; but=0;}
};
#include "uutimer.h"
class joystick:public input
{
protected:
int installed;
joythreshold *joythresh; //pointer to original
joythreshold t; //copy of threshhold for the joystick
lpoint pos; //analog position of each axis
int buttons; //holding state of buttons
// int rfrtime;
uutimer refreshtimer;
public:
virtual void savesettings() {*joythresh=t;}
joystick(int type,joythreshold *jt):input(type),
refreshtimer(3) //refresh every 3 ticks
{
installed=1;
// rfrtime=0;
joythresh=jt;
#ifdef DOS
t=*jt;
int joypos[4];
ReadAnalogJoyPos(joypos,1024);
if (type!=ID_JOY2) {pos.x=joypos[0]; pos.y=joypos[1];}
else {pos.x=joypos[2]; pos.y=joypos[3];}
t.l=pos.x*3/6;
t.r=pos.x*9/6;
t.u=pos.y*3/6;
t.d=pos.y*9/6;
#endif
#ifdef WIN95
t.l=32768-5000; t.r=32768+5000;
t.u=32768-5000; t.d=32768+5000;
#endif
}
virtual void read()
{
stat&=~0xF; //clear stat dirs
if (pos.x>t.r) stat|=ID_RIGHT; else
if (pos.x<t.l) stat|=ID_LEFT;
if (pos.y>t.d) stat|=ID_DOWN; else
if (pos.y<t.u) stat|=ID_UP;
oldbut=but;
but=buttons<<4;
stat|=((oldbut^but)&but);
}
virtual joythreshold *getjoythreshold() {return &t;};
};
//analog joystick 1
class analogjoy1:public joystick
{
public:
analogjoy1():joystick(ID_JOY1,&ids->aj[0]) {}
virtual void main()
{
if (!installed) return;
if (!refreshtimer.check()) return;
refreshtimer.reset();
#ifdef WIN95
JOYINFO joypos;
if (joyGetPos(0,&joypos)!=JOYERR_NOERROR) {pos.x=pos.y=buttons=0; installed=0; return;}
pos.x=joypos.wXpos; pos.y=joypos.wYpos; buttons=joypos.wButtons;
#endif
#ifdef DOS
int joypos[4];
ReadAnalogJoyPos(joypos,1024);
pos.x=joypos[0]; pos.y=joypos[1]; buttons=ReadAnalogJoyButtons()&3;
#endif
};
};
//analog joystick 2
class analogjoy2:public joystick
{
public:
analogjoy2():joystick(ID_JOY2,&ids->aj[1]) {}
virtual void main()
{
if (!installed) return;
if (!refreshtimer.check()) return;
refreshtimer.reset();
#ifdef WIN95
JOYINFO joypos;
if (joyGetPos(1,&joypos)!=JOYERR_NOERROR) {pos.x=pos.y=buttons=0; installed=0; return;}
pos.x=joypos.wXpos; pos.y=joypos.wYpos; buttons=joypos.wButtons;
#endif
#ifdef DOS
int joypos[4];
ReadAnalogJoyPos(joypos,1024);
pos.x=joypos[2]; pos.y=joypos[3]; buttons=(ReadAnalogJoyButtons()>>2)&3;
#endif
};
};
extern int blah;
//gravis gamepad
class gravis:public joystick
{
static unsigned char gravtable[16];
public:
gravis():joystick(ID_GRAVIS,&ids->aj[0]) {}
virtual void main()
{
if (!installed) return;
if (!refreshtimer.check()) return;
refreshtimer.reset();
#ifdef WIN95
JOYINFO joypos;
if (joyGetPos(0,&joypos)!=JOYERR_NOERROR) {pos.x=pos.y=buttons=0; installed=0; return;}
pos.x=joypos.wXpos; pos.y=joypos.wYpos;
buttons=gravtable[(joypos.wButtons&15)];
#endif
#ifdef DOS
int joypos[4];
ReadAnalogJoyPos(joypos,1024);
pos.x=joypos[0]; pos.y=joypos[1];
buttons=gravtable[(ReadAnalogJoyButtons()&15)];
#endif
};
};
unsigned char gravis::gravtable[]={0,1,4,5,2,3,6,7,8,9,12,13,10,11,14,15,};
int keymap::read(char kbscan,unsigned &kstat)
{
if (!(kbscan&0x80)) //press
{
for (int i=0; i<sizeof(keymap); i++)
if (kbscan==((char *)this)[i]) //compare scan codes
{kstat|=1<<i; return 1;} //set bit
} else //release
{
kbscan&=0x7F;
for (int i=0; i<sizeof(keymap); i++)
if (kbscan==((char *)this)[i]) //compare scan codes
{kstat&=~(1<<i); return 1;} //release bit
}
return 0;
}
class keyboard:public input
{
protected:
keymap *kmptr; //pointer to original keymap
keymap km; //keymap for this keybaord
unsigned keystat; //status of keys pressed (each bit represents a key in keymap)
public:
virtual void savesettings() {*kmptr=km;}
keyboard(int type,keymap *tkm):input(type),km(*tkm),kmptr(tkm),keystat(0) {}
virtual int key(char kbscan)
{
// if (!testing && (!o || !o->active)) return 0;
return km.read(kbscan,keystat);
}
virtual void read()
{
//recalculate stats
stat=0;
if (keystat& ( (1<< 0) + (1<< 1) + (1<< 2) ) ) stat|=ID_UP; else //Up
if (keystat& ( (1<< 5) + (1<< 6) + (1<< 7) ) ) stat|=ID_DOWN; //Down
if (keystat& ( (1<< 2) + (1<< 4) + (1<< 7) ) ) stat|=ID_RIGHT; else //right
if (keystat& ( (1<< 0) + (1<< 3) + (1<< 5) ) ) stat|=ID_LEFT; //Left
//recalculate buttons
oldbut=but;
but=(keystat&0xFF00)>>4;
stat|=((oldbut^but)&but);
}
virtual keymap *getkeymap() {return &km;};
};
class keyboard1:public keyboard
{
public:
keyboard1():keyboard(ID_KEY1,&ids->km[0]) {}
};
class keyboard2:public keyboard
{
public:
keyboard2():keyboard(ID_KEY2,&ids->km[1]) {}
};
//input initialization
input *newinputdevice(int idtype)
{ //if no inputdevicesettings, can't initialize
if (!ids) return 0;
switch (idtype)
{
case ID_NONE: return new inputnone;
case ID_JOY1: return new analogjoy1;
case ID_JOY2: return new analogjoy2;
case ID_GRAVIS: return new gravis;
case ID_KEY1: return new keyboard1;
case ID_KEY2: return new keyboard2;
}
return 0;
}