-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpianotutor.cpp
More file actions
419 lines (371 loc) · 10.4 KB
/
Copy pathpianotutor.cpp
File metadata and controls
419 lines (371 loc) · 10.4 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
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
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include "SDL/SDL.h"
#include "SDL/SDL_joystick.h"
#include "SDL/SDL_keyboard.h"
#undef main // Get rid of SDL_main
#define forloop(i,end) for(unsigned int i=0; i<(end); i++)
typedef unsigned int uint;
enum Mode
{
Mode_setSequence,
Mode_practice
};
// Tutorial text that explains how to use the program
struct DisplayText
{
std::wstring intro;
std::wstring config;
std::wstring practice;
};
struct ButtonInputAction
{
uint buttonIndex;
uint joystickIndex;
};
// D-pads are sometimes mapped to 8-way HAT inputs
struct HatInputAction
{
uint pov;
uint joystickIndex;
};
struct KeyboardInputAction
{
uint keyIndex;
};
struct InputAction
{
enum Type { Type_button, Type_hat, Type_keyboard };
union {
ButtonInputAction button;
HatInputAction hat;
KeyboardInputAction key;
};
Type type;
};
struct PianoState
{
std::vector<InputAction> pianoSequence;
uint frameCount = 0;
uint previousInputFrame = 0;
uint nextExpectedPianoIndex = 0;
};
struct Input
{
struct Button {
bool pressed = 0; // True for one update when the button is first pressed.
bool down = 0; // True while the button is held down.
};
struct Joystick {
uint buttonCount;
Button *buttons;
int hat;
int previousHat;
SDL_Joystick* sdlJoy;
};
static const uint supportedKeyCount = 0xFF;
Button keyboard[supportedKeyCount];
uint joystickCount;
Joystick* joysticks;
};
// Read backslash-delimited strings from an input stream
std::wstring parseString(std::wifstream& inStream)
{
std::wstringstream outStream;
wchar_t c;
while (inStream >> std::noskipws >> c && c != L'\\') outStream << c;
return outStream.str();
}
// Read the tutorial text from a file
DisplayText parseTextFile(std::string fileName)
{
DisplayText result;
std::wifstream input(fileName);
result.intro = parseString(input);
result.config = parseString(input);
result.practice = parseString(input);
return result;
}
void updateButton(Input::Button* inout_button, uint isDown)
{
if (isDown) {
inout_button->pressed = !inout_button->down;
inout_button->down = true;
}
else {
inout_button->pressed = false;
inout_button->down = false;
}
}
void updateInput(Input* input)
{
// Handle joysticks beening plugged in or taken out
int joystickCount = SDL_NumJoysticks();
if (joystickCount != input->joystickCount)
{
// Free joysticks
forloop (i, input->joystickCount)
{
delete[] input->joysticks[i].buttons;
SDL_JoystickClose(input->joysticks[i].sdlJoy);
}
delete[] input->joysticks;
// Allocate joysticks
input->joystickCount = joystickCount;
input->joysticks = new Input::Joystick[joystickCount];
forloop (i, input->joystickCount)
{
input->joysticks[i].sdlJoy = SDL_JoystickOpen(i);
if (input->joysticks[i].sdlJoy)
{
input->joysticks[i].buttonCount = SDL_JoystickNumButtons(input->joysticks[i].sdlJoy);
input->joysticks[i].buttons = new Input::Button[input->joysticks[i].buttonCount];
}
}
}
// Update joystick
SDL_JoystickUpdate();
forloop (joystickIndex, input->joystickCount)
{
Input::Joystick* joystick = &input->joysticks[joystickIndex];
// Buttons
forloop (buttonIndex, joystick->buttonCount)
{
updateButton(&joystick->buttons[buttonIndex], SDL_JoystickGetButton(joystick->sdlJoy, buttonIndex));
}
// Hat
joystick->previousHat = joystick->hat;
joystick->hat = SDL_JoystickGetHat(joystick->sdlJoy, 0);
}
// Update keyboard
int sdlKeyCount;
const Uint8 *keystates = SDL_GetKeyboardState(&sdlKeyCount);
forloop(i, Input::supportedKeyCount)
{
int isKeyDown = 0;
// Make sure we don't read past the end of SDL's key array
if (i < (uint)sdlKeyCount) {
isKeyDown = keystates[i];
}
updateButton(&input->keyboard[i], isKeyDown);
}
}
// Checks if two inputs are equal
bool compareInputActions(InputAction a, InputAction b)
{
if (a.type != b.type) return false;
switch (a.type)
{
case InputAction::Type_button:
return a.button.buttonIndex == b.button.buttonIndex
&& a.button.joystickIndex == b.button.joystickIndex;
case InputAction::Type_hat:
return a.hat.pov == b.hat.pov
&& a.hat.joystickIndex == b.hat.joystickIndex;
case InputAction::Type_keyboard:
return a.key.keyIndex == b.key.keyIndex;
default:
SDL_assert(!"Unimplemented input action type");
return false;
}
}
// Make a list of all inputs that changed this frame
std::vector<InputAction> getActiveInputsList(Input input)
{
std::vector<InputAction> list;
forloop(joystickIndex, input.joystickCount)
{
forloop(buttonIndex, input.joysticks[joystickIndex].buttonCount)
{
if (input.joysticks[joystickIndex].buttons[buttonIndex].pressed)
{
InputAction action;
action.type = InputAction::Type_button;
action.button.joystickIndex = joystickIndex;
action.button.buttonIndex = buttonIndex;
list.push_back(action);
}
}
if (input.joysticks[joystickIndex].hat != input.joysticks[joystickIndex].previousHat)
{
InputAction action;
action.type = InputAction::Type_hat;
action.hat.joystickIndex = joystickIndex;
action.hat.pov = input.joysticks[joystickIndex].hat;
list.push_back(action);
}
}
forloop(keyIndex, input.supportedKeyCount)
{
if (input.keyboard[keyIndex].pressed)
{
if (keyIndex != SDL_SCANCODE_RETURN && keyIndex != SDL_SCANCODE_BACKSPACE)
{
InputAction action;
action.type = InputAction::Type_keyboard;
action.key.keyIndex = keyIndex;
list.push_back(action);
}
}
}
return list;
}
void printPracticeLetter(PianoState* state, InputAction input)
{
// If the sequence contains more than one of the same input,
// print the letter for the one that's next.
// If there isn't one next, print the closest one.
// If the sequence doesn't contain the input, print a dash.
char inputLetter = '-';
forloop(i, state->pianoSequence.size())
{
if (compareInputActions(state->pianoSequence[i], input))
{
inputLetter = 'a'+i;
if (i >= state->nextExpectedPianoIndex) break;
}
}
printf("%c", inputLetter);
}
void printSetSequenceInput(InputAction input, uint index)
{
if (input.type == InputAction::Type_button)
{
printf("Joystick %d, button %d (%c)\n", input.button.joystickIndex, input.button.buttonIndex, 'a'+index);
}
if (input.type == InputAction::Type_hat)
{
// Print the hat number in numpad notation
char hatChar = ' ';
switch (input.hat.pov) {
case SDL_HAT_CENTERED: hatChar = '5'; break;
case SDL_HAT_RIGHT: hatChar = '6'; break;
case SDL_HAT_RIGHTDOWN: hatChar = '3'; break;
case SDL_HAT_DOWN: hatChar = '2'; break;
case SDL_HAT_LEFTDOWN: hatChar = '1'; break;
case SDL_HAT_LEFT: hatChar = '4'; break;
case SDL_HAT_LEFTUP: hatChar = '7'; break;
case SDL_HAT_UP: hatChar = '8'; break;
case SDL_HAT_RIGHTUP: hatChar = '9'; break;
}
printf("Joystick %d, hat %c (%c)\n", input.hat.joystickIndex, hatChar, 'a'+index);
}
if (input.type == InputAction::Type_keyboard)
{
const char* keyName = SDL_GetKeyName(SDL_GetKeyFromScancode((SDL_Scancode)input.key.keyIndex));
printf("%s key (%c)\n", keyName, 'a'+index);
}
}
// Record inputs to create the piano sequence
void updateSetSequence(PianoState* state, Input input)
{
std::vector<InputAction> currentInputList = getActiveInputsList(input);
forloop(i, currentInputList.size())
{
printSetSequenceInput(currentInputList[i], state->pianoSequence.size());
state->pianoSequence.push_back(currentInputList[i]);
}
}
void updatePractice(PianoState* state, Input input)
{
std::vector<InputAction> currentInputList = getActiveInputsList(input);
forloop (i, currentInputList.size())
{
if (state->nextExpectedPianoIndex < state->pianoSequence.size())
{
// Print number of frames since last input
if (state->previousInputFrame != 0)
{
uint frameDelta = state->frameCount - state->previousInputFrame;
printf(" %d ", frameDelta);
if (frameDelta == 0) printf(" MISS ");
}
printPracticeLetter(state, currentInputList[i]);
// If this is the correct input, advance to the next expected input
if (compareInputActions(state->pianoSequence[state->nextExpectedPianoIndex], currentInputList[i]))
{
state->nextExpectedPianoIndex += 1;
}
else printf(" MISS ");
state->previousInputFrame = state->frameCount;
}
}
// If we've reached the end on the expected sequence, or if it's been a while without inputs,
// start over on a new line
if (state->previousInputFrame != 0)
{
uint frameDelta = state->frameCount - state->previousInputFrame;
if (frameDelta >= 15 || state->nextExpectedPianoIndex >= state->pianoSequence.size())
{
state->frameCount = 0;
state->previousInputFrame = 0;
state->nextExpectedPianoIndex = 0;
printf("\n");
}
}
}
int main(int argc, char** argv)
{
// Needs a window for keyboard input and vsynch
SDL_Init(SDL_INIT_VIDEO);
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
SDL_Window* window = SDL_CreateWindow(0, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 250, 0, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
SDL_GLContext graphicsContext = SDL_GL_CreateContext(window);
SDL_SetWindowTitle(window, "Input Capture");
Input input = {0};
updateInput(&input);
Mode mode = Mode_setSequence;
PianoState state;
// Load localized display text from a file
DisplayText text = parseTextFile("en.txt");
// Print tutorial text
std::wcout << text.intro;
std::wcout << text.config;
// Game-style main loop
bool run = true;
while (run)
{
bool enterPressed = false;
bool backspacePressed = false;
// Process window messages
SDL_Event message;
while (SDL_PollEvent(&message)) {
if (message.type == SDL_QUIT) run = false;
if (message.type == SDL_KEYDOWN) {
if (message.key.keysym.sym == SDLK_RETURN) enterPressed = true;
if (message.key.keysym.sym == SDLK_BACKSPACE) backspacePressed = true;
}
}
updateInput(&input);
if (mode == Mode_setSequence)
{
updateSetSequence(&state, input);
if (enterPressed && state.pianoSequence.size() > 0)
{
std::wcout << text.practice;
mode = Mode_practice;
}
}
else if (mode == Mode_practice)
{
updatePractice(&state, input);
}
if (backspacePressed && state.pianoSequence.size() > 0)
{
// Go back to sequence setup
std::wcout << text.config;
mode = Mode_setSequence;
state.pianoSequence.clear();
state.nextExpectedPianoIndex = 0;
}
++state.frameCount;
fflush(stdout);
SDL_GL_SetSwapInterval(1); // Use vsynch
SDL_GL_SwapWindow(window);
}
return 0;
}