File: input.cpp

package info (click to toggle)
stax 1.37-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,768 kB
  • ctags: 809
  • sloc: cpp: 8,151; makefile: 30
file content (482 lines) | stat: -rw-r--r-- 8,342 bytes parent folder | download
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
 * Copyright 1999-2006 Trent Gamblin
 *
 * This file is part of Stax.
 *
 * Stax is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Stax is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Stax; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "stax.h"

bool LeftPressed(void)
{
	return key[KEY_LEFT] || (configuration.joystick_installed ? joy[0].stick[0].axis[0].d1 : false);
}

bool RightPressed(void)
{
	return key[KEY_RIGHT] || (configuration.joystick_installed ? joy[0].stick[0].axis[0].d2 : false);
}

bool UpPressed(void)
{
	return key[KEY_UP] || (configuration.joystick_installed ? joy[0].stick[0].axis[1].d1 : false);
}

bool DownPressed(void)
{
	return key[KEY_DOWN] || (configuration.joystick_installed ? joy[0].stick[0].axis[1].d2 : false);
}

bool ButtonPressed(void)
{
	return key[KEY_ENTER] || (configuration.joystick_installed ? joy[0].button[configuration.joystick1_button].b : false);
}

void WaitForRelease(void)
{
	for (;;) {
		clear_keybuf();
		if (keyboard_needs_poll())
			poll_keyboard();
		if (mouse_needs_poll())
			poll_mouse();
		poll_joystick();
		if (	!key[KEY_ESC] &&
			!key[KEY_ENTER] &&
			(configuration.joystick_installed ?
			(!joy[0].button[0].b &&
			!joy[0].button[1].b &&
			!joy[0].button[2].b &
			!joy[0].button[3].b) : true) &&
			(configuration.mouse_installed ? !mouse_b : true))
			break;
	}
}

bool Input::LeftReady(void)
{
	return left_count <= 0;
}

bool Input::RightReady(void)
{
	return right_count <= 0;
}

bool Input::UpReady(void)
{
	return up_count <= 0;
}

bool Input::DownReady(void)
{
	return down_count <= 0;
}

bool Input::ButtonReady(void)
{
	return button_count <= 0;
}

void Input::PressLeft(void)
{
	left_repeat = MAX(MIN_REPEAT, left_repeat - MOVE_ACCEL);
	left_count = left_repeat;
}

void Input::PressRight(void)
{
	right_repeat = MAX(MIN_REPEAT, right_repeat - MOVE_ACCEL);
	right_count = right_repeat;
}

void Input::PressUp(void)
{
	if (init_up_repeat == 0)
		up_repeat = 0;
	else
		up_repeat = MAX(MIN_REPEAT, up_repeat - MOVE_ACCEL);
	up_count = up_repeat;
}

void Input::PressDown(void)
{
	if (init_down_repeat == 0)
		down_repeat = 0;
	else
		down_repeat = MAX(MIN_REPEAT, down_repeat - MOVE_ACCEL);
	down_count = down_repeat;
}

void Input::PressButton(void)
{
	button_count = button_repeat;
}

void Input::SetRepeatTimes(int left, int right, int up, int down, int button)
{
	left_repeat = left;
	right_repeat = right;
	up_repeat = up;
	down_repeat = down;
	button_repeat = button;
	init_left_repeat = left;
	init_right_repeat = right;
	init_up_repeat = up;
	init_down_repeat = down;
	init_button_repeat = button;
}

void Input::Update(void)
{
	if (left_count > 0)
		left_count--;
	if (right_count > 0)
		right_count--;
	if (up_count > 0)
		up_count--;
	if (down_count > 0)
		down_count--;
	if (button_count > 0)
		button_count--;
}

Input::Input(void)
{
	left_repeat = 0;
	left_count = 0;
	right_repeat = 0;
	right_count = 0;
	up_repeat = 0;
	up_count = 0;
	down_repeat = 0;
	down_count = 0;
	button_repeat = 0;
	button_count = 0;
}

bool LeftKeyboardInput::LeftPressed(void)
{
	if (LeftReady()) {
		if (key[KEY_A]) {
			PressLeft();
			return true;
		}
		else
			left_repeat = init_left_repeat;
	}
	return false;
}

bool LeftKeyboardInput::RightPressed(void)
{
	if (RightReady()) {
		if (key[KEY_D]) {
			PressRight();
			return true;
		}
		else
			right_repeat = init_right_repeat;
	}
	return false;
}

bool LeftKeyboardInput::UpPressed(void)
{
	if (UpReady()) {
		if (key[KEY_W]) {
			PressUp();
			return true;
		}
		else
			up_repeat = init_up_repeat;
	}
	return false;
}

bool LeftKeyboardInput::DownPressed(void)
{
	if (DownReady()) {
		if (key[KEY_S]) {
			PressDown();
			return true;
		}
		else
			down_repeat = init_down_repeat;
	}
	return false;
}

bool LeftKeyboardInput::ButtonPressed(void)
{
	if (ButtonReady()) {
		if (key[KEY_SPACE]) {
			PressButton();
			return true;
		}
		else
			button_repeat = init_button_repeat;
	}
	return false;
}

void LeftKeyboardInput::Update(void)
{
	if (keyboard_needs_poll())
		poll_keyboard();
	Input::Update();
}

bool RightKeyboardInput::LeftPressed(void)
{
	if (LeftReady()) {
		if (key[KEY_LEFT]) {
			PressLeft();
			return true;
		}
		else
			left_repeat = init_left_repeat;
	}
	return false;
}

bool RightKeyboardInput::RightPressed(void)
{
	if (RightReady()) {
		if (key[KEY_RIGHT]) {
			PressRight();
			return true;
		}
		else
			right_repeat = init_right_repeat;
	}
	return false;
}

bool RightKeyboardInput::UpPressed(void)
{
	if (UpReady()) {
		if (key[KEY_UP]) {
			PressUp();
			return true;
		}
		else
			up_repeat = init_up_repeat;
	}
	return false;
}

bool RightKeyboardInput::DownPressed(void)
{
	if (DownReady()) {
		if (key[KEY_DOWN]) {
			PressDown();
			return true;
		}
		else
			down_repeat = init_down_repeat;
	}
	return false;
}

bool RightKeyboardInput::ButtonPressed(void)
{
	if (ButtonReady()) {
		if (key[KEY_RCONTROL] || key[KEY_LCONTROL]) {
			PressButton();
			return true;
		}
		else
			button_repeat = init_button_repeat;
	}
	return false;
}

void RightKeyboardInput::Update(void)
{
	if (keyboard_needs_poll())
		poll_keyboard();
	Input::Update();
}

bool Joystick1Input::LeftPressed(void)
{
	if (LeftReady()) {
		if (joy[0].stick[0].axis[0].d1) {
			PressLeft();
			return true;
		}
		else
			left_repeat = init_left_repeat;
	}
	return false;
}

bool Joystick1Input::RightPressed(void)
{
	if (RightReady()) {
		if (joy[0].stick[0].axis[0].d2) {
			PressRight();
			return true;
		}
		else
			right_repeat = init_right_repeat;
	}
	return false;
}

bool Joystick1Input::UpPressed(void)
{
	if (UpReady()) {
		if (joy[0].stick[0].axis[1].d1) {
			PressUp();
			return true;
		}
		else
			up_repeat = init_up_repeat;
	}
	return false;
}

bool Joystick1Input::DownPressed(void)
{
	if (DownReady()) {
		if (joy[0].stick[0].axis[1].d2) {
			PressDown();
			return true;
		}
		else
			down_repeat = init_down_repeat;
	}
	return false;
}

bool Joystick1Input::ButtonPressed(void)
{
	if (ButtonReady()) {
		if (joy[0].button[configuration.joystick1_button].b) {
			PressButton();
			return true;
		}
		else
			button_repeat = init_button_repeat;
	}
	return false;
}

void Joystick1Input::Update(void)
{
	poll_joystick();
	Input::Update();
}

bool Joystick2Input::LeftPressed(void)
{
	if (LeftReady()) {
		if (joy[1].stick[0].axis[0].d1) {
			PressLeft();
			return true;
		}
		else
			left_repeat = init_left_repeat;
	}
	return false;
}

bool Joystick2Input::RightPressed(void)
{
	if (RightReady()) {
		if (joy[1].stick[0].axis[0].d2) {
			PressRight();
			return true;
		}
		else
			right_repeat = init_right_repeat;
	}
	return false;
}

bool Joystick2Input::UpPressed(void)
{
	if (UpReady()) {
		if (joy[1].stick[0].axis[1].d1) {
			PressUp();
			return true;
		}
		else
			up_repeat = init_up_repeat;
	}
	return false;
}

bool Joystick2Input::DownPressed(void)
{
	if (DownReady()) {
		if (joy[1].stick[0].axis[1].d2) {
			PressDown();
			return true;
		}
		else
			down_repeat = init_down_repeat;
	}
	return false;
}

bool Joystick2Input::ButtonPressed(void)
{
	if (ButtonReady()) {
		if (joy[1].button[configuration.joystick1_button].b) {
			PressButton();
			return true;
		}
		else
			button_repeat = init_button_repeat;
	}
	return false;
}

void Joystick2Input::Update(void)
{
	poll_joystick();
	Input::Update();
}

Input* CreateInput(int player)
{
	InputType input_type;

	if (player == 1)
		input_type = configuration.input_type_1;
	else if (player == 2)
		input_type = configuration.input_type_2;
	else
		return 0;

	switch (input_type) {
		case INPUT_NONE:
			return 0;
		case INPUT_RIGHT_KEYBOARD:
			return new RightKeyboardInput();
		case INPUT_LEFT_KEYBOARD:
			return new LeftKeyboardInput();
		case INPUT_JOYSTICK_1:
			return new Joystick1Input();
		case INPUT_JOYSTICK_2:
			return new Joystick2Input();
		default:
			return 0;
	}
}