-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControls.cpp
More file actions
129 lines (88 loc) · 2.31 KB
/
Controls.cpp
File metadata and controls
129 lines (88 loc) · 2.31 KB
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
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
// Credit to arrow key detection via: http://www.cplusplus.com/forum/beginner/241912/
// getKeyboardInput to pause execution and wait for keyboard input
// Returns:
// string input - The specific key the player pressed
string getKeyboardInput() {
int c;
// _getch() gets the ASCII value of the key
c = _getch();
// If the first value is 224 its an arrow key
if (c == 224) {
// Switch through another _getch() call
switch (c = _getch()) {
case 72:
return "up";
break;
case 80:
return "down";
case 77:
return "right";
break;
case 75:
return "left";
break;
default:
return "invalid";
break;
}
}
// If c is 13 then it's the enter key
else if (c == 13) {
return "enter";
}
// If c is 32 then it's the spacebar
else if (c == 32) {
return "spacebar";
}
// If c is 27 then it's the escape key to exit the program
else if (c == 27) {
exit(0);
}
else {
return "invalid";
}
}
// arrowHandler to take parameters and output the new location of the cursor
// Parameters:
// string arrow - The input the player provides
// int sel - The current position of the cursor
// int last - The maximum position the cursor can be in
// Returns:
// int sel - The new position of the cursor
int arrowHandler(string arrow, int sel, int last) {
if (arrow == "up") {
if (sel > 0) {
sel--;
}
}
else if (arrow == "down") {
if (sel < last) {
sel++;
}
}
return sel;
}
// enterHandler to take parameters and output the new handler variable
// Parameters:
// string arrow - The input the player provides
// int sel - The current position of the cursor
// int last - The maximum position the cursor can be in
// Returns:
// string - The new handler variable
string enterHandler(string key, int sel, int last) {
if (key == "enter") {
return "1" + to_string(sel);
}
return "0" + to_string(sel);
}
// enterPause to pause execution of the program until the enter key is pressed
void enterPause() {
string input = getKeyboardInput();
while (input != "enter") {
input = getKeyboardInput();
}
}