-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
348 lines (307 loc) · 10.8 KB
/
main.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
341
342
343
344
345
346
347
348
#include "main.h"
GLFWwindow* window;
void error_callback(int error, const char* description)
{
// Print error
fputs(description, stderr);
}
void setup_callbacks()
{
// Set the error callback
glfwSetErrorCallback(error_callback);
// Set the key callback
glfwSetKeyCallback(window, Window::key_callback);
// Set the mouse button callback
glfwSetMouseButtonCallback(window, Window::mouse_callback);
// Set the mouse scrolling callback
glfwSetScrollCallback(window, Window::scroll_callback);
// Sets the cursor callback
glfwSetCursorPosCallback(window, Window::cursor_callback);
// Set the window resize callback
glfwSetFramebufferSizeCallback(window, Window::resize_callback);
}
void setup_glew()
{
// Initialize GLEW. Not needed on OSX systems.
#ifndef __APPLE__
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
glfwTerminate();
}
fprintf(stdout, "Current GLEW version: %s\n", glewGetString(GLEW_VERSION));
#endif
}
void setup_opengl_settings()
{
#ifndef __APPLE__
// Setup GLEW. Don't do this on OSX systems.
setup_glew();
#endif
// Enable depth buffering
glEnable(GL_DEPTH_TEST);
// Related to shaders and z value comparisons for the depth buffer
glDepthFunc(GL_LEQUAL);
// Set polygon drawing mode to fill front and back of each polygon
// You can also use the paramter of GL_LINE instead of GL_FILL to see wireframes
glFrontFace(GL_CW);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// Enable culling
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
// Set clear color
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
}
void print_versions()
{
// Get info of GPU and supported OpenGL version
printf("Renderer: %s\n", glGetString(GL_RENDERER));
printf("OpenGL version supported %s\n", glGetString(GL_VERSION));
//If the shading language symbol is defined
#ifdef GL_SHADING_LANGUAGE_VERSION
std::printf("Supported GLSL version is %s.\n", (char *)glGetString(GL_SHADING_LANGUAGE_VERSION));
#endif
}
int main(void)
{
// Create the GLFW window
window = Window::create_window(640, 480);
// Print OpenGL and GLSL versions
print_versions();
// Setup callbacks
setup_callbacks();
// Setup OpenGL settings, including lighting, materials, etc.
setup_opengl_settings();
// Initialize objects/pointers for rendering
Window::initialize_objects();
// ThinkGear variables
char *comPortName = NULL;
char *comPortName2 = NULL;
int dllVersion = 0;
int connectionId = 0;
int connectionId2 = 0;
int packetsRead = 0;
int errCode = 0;
double secondsToRun = 0;
time_t startTime = 0;
time_t currTime = 0;
char *currTimeStr = NULL;
int set_filter_flag = 0;
int count = 0;
/* Print driver version number */
dllVersion = TG_GetVersion();
printf("Stream SDK for PC version: %d\n", dllVersion);
/* Get a connection ID handle to ThinkGear */
connectionId = TG_GetNewConnectionId();
if (connectionId < 0) {
fprintf(stderr, "ERROR: TG_GetNewConnectionId() returned %d for PLAYER 1.\n",
connectionId);
fprintf(stderr, "Press ENTER to continue...\n", connectionId);
std::cin.ignore();
//exit(EXIT_FAILURE);
}
/* Get a connection ID handle to ThinkGear for player 2 */
/**
connectionId2 = TG_GetNewConnectionId();
if (connectionId2 < 0) {
fprintf(stderr, "ERROR: TG_GetNewConnectionId() returned %d for PLAYER 2.\n",
connectionId);
fprintf(stderr, "Press ENTER to continue...\n", connectionId2);
std::cin.ignore();
//exit(EXIT_FAILURE);
}**/
/* Attempt to connect the connection ID handle to serial port "COM5" */
/* NOTE: On Windows, COM10 and higher must be preceded by \\.\, as in
* "\\\\.\\COM12" (must escape backslashes in strings). COM9
* and lower do not require the \\.\, but are allowed to include
* them. On Mac OS X, COM ports are named like
* "/dev/tty.MindSet-DevB-1".
*/
comPortName = "\\\\.\\COM4";
errCode = TG_Connect(connectionId,
comPortName,
TG_BAUD_57600,
TG_STREAM_PACKETS);
if (errCode < 0) {
fprintf(stderr, "ERROR: TG_Connect() returned %d.\n", errCode);
std::cout << "WARNING: The headset has not been detected!" << std::endl;
fprintf(stderr, "Press ENTER to continue...\n", errCode);
std::cin.ignore();
//exit(EXIT_FAILURE);
}
/* PLAYER 2 */
/**
comPortName2 = "\\\\.\\COM5";
errCode = TG_Connect(connectionId2,
comPortName2,
TG_BAUD_9600,
TG_STREAM_PACKETS);
if (errCode < 0) {
fprintf(stderr, "ERROR: TG_Connect() returned %d for PLAYER 2.\n", errCode);
fprintf(stderr, "Press ENTER to continue...\n", errCode);
std::cin.ignore();
//exit(EXIT_FAILURE);
}**/
// Get names
std::string p1_name;
std::string p2_name;
//std::cout << "\nEnter name for PLAYER 1: ";
//std::getline(std::cin, p1_name);
//std::cout << "Enter name for PLAYER 2: ";
//std::getline(std::cin, p2_name);
p2_name = "BOT";
std::string difficulty;
bool valid_input = false;
while (!valid_input) {
std::cout << "\nPlease choose a difficulty: (e)asy, (m)edium, (h)ard, (v)ery hard, (i)mpossible: ";
std::getline(std::cin, difficulty);
if (difficulty == "e" || difficulty == "m" || difficulty == "h" || difficulty == "v" || difficulty == "i") {
valid_input = true;
}
else {
std::cout << "Invalid input! Try again" << std::endl;
}
}
std::string full_difficulty;
if (difficulty == "e") {
full_difficulty = "Easy";
}
else if (difficulty == "m") {
full_difficulty = "Medium";
}
else if (difficulty == "h") {
full_difficulty = "Hard";
}
else if (difficulty == "v") {
full_difficulty = "Very Hard";
}
else if (difficulty == "i") {
full_difficulty = "Impossible";
}
// Set timers
secondsToRun = 3;
int sec_before_start = 10;
time_t start_delay = time(NULL);
std::cout << "\nSTARTING GAME IN 10 SECONDS...\n" << std::endl;
bool game_over = false;
// Loop while GLFW window should stay open
while (!glfwWindowShouldClose(window))
{
// Wait 10 sec before game starts
if (difftime(time(NULL), start_delay) > sec_before_start && game_over == false) {
startTime = time(NULL);
while (difftime(time(NULL), startTime) < secondsToRun) {
/* Read all currently available Packets, one at a time... */
do {
// Main render display callback. Rendering of objects is done here.
Window::display_callback(window);
/* Read a single Packet from the connection */
packetsRead = TG_ReadPackets(connectionId, 1);
/* If TG_ReadPackets() was able to read a Packet of data... */
if (packetsRead == 1) {
/* If the Packet containted a new raw wave value... */
if (TG_GetValueStatus(connectionId, TG_DATA_ATTENTION) != 0) {
/* Get the current time as a string */
currTime = time(NULL);
currTimeStr = ctime(&currTime);
/* Get and print out the new raw value */
int player1_attention = (int)TG_GetValue(connectionId, TG_DATA_ATTENTION);
int player1_meditation = (int)TG_GetValue(connectionId, TG_DATA_MEDITATION);
//int player2_attention = (int)TG_GetValue(connectionId2, TG_DATA_ATTENTION);
//int player2_meditation = (int)TG_GetValue(connectionId2, TG_DATA_MEDITATION);
int player2_attention;
int player2_meditation;
if (difficulty == "e") {
player2_attention = 0 + (rand() % (int)(35 - 0 + 1));
player2_meditation = 0 + (rand() % (int)(35 - 0 + 1));
}
else if (difficulty == "m") {
player2_attention = 0 + (rand() % (int)(100 - 0 + 1));
player2_meditation = 0 + (rand() % (int)(100 - 0 + 1));
}
else if (difficulty == "h") {
player2_attention = 50 + (rand() % (int)(100 - 50 + 1));
player2_meditation = 50 + (rand() % (int)(100 - 50 + 1));
}
else if (difficulty == "v") {
player2_attention = 75 + (rand() % (int)(100 - 75 + 1));
player2_meditation = 75 + (rand() % (int)(100 - 75 + 1));
}
else if (difficulty == "i") {
player2_attention = 90 + (rand() % (int)(100 - 90 + 1));
player2_meditation = 90 + (rand() % (int)(100 - 90 + 1));
}
// Print player 1 measures
std::string tabs;
if (difficulty == "i") {
tabs = "\t\t\t";
}
else {
tabs = "\t\t";
}
fprintf(stdout, "%sPLAYER%s ATT: %d\n", currTimeStr, tabs, player1_attention);
fprintf(stdout, "PLAYER%s MED: %d\n", tabs, player1_meditation);
// Print player 2 measures
fprintf(stdout, "%s (%s)\t ATT: %d\n", p2_name, full_difficulty, player2_attention);
fprintf(stdout, "%s (%s)\t MED: %d\n\n", p2_name, full_difficulty, player2_meditation);
fflush(stdout);
// Idle callback. Updating objects, etc. can be done here.
game_over = Window::idle_callback(player1_attention, player2_attention,
player1_meditation, player2_meditation, p1_name, p2_name);
// Ball reaches past boundaries
if (game_over) {
std::cout << "GAME OVER, BALL REACHED PAST BOUNDARIES." << std::endl;
bool valid_input = false;
while (!valid_input) {
std::cout << "\nPlease choose a difficulty: (e)asy, (m)edium, (h)ard, (v)ery hard, (i)mpossible: ";
std::getline(std::cin, difficulty);
if (difficulty == "e" || difficulty == "m" || difficulty == "h" || difficulty == "v" || difficulty == "i") {
valid_input = true;
}
else {
std::cout << "Invalid input! Try again." << std::endl;
}
}
if (difficulty == "e") {
full_difficulty = "Easy";
}
else if (difficulty == "m") {
full_difficulty = "Medium";
}
else if (difficulty == "h") {
full_difficulty = "Hard";
}
else if (difficulty == "v") {
full_difficulty = "Very Hard";
}
else if (difficulty == "i") {
full_difficulty = "Impossible";
}
std::cout << "\nPress ENTER to restart game." << std::endl;
std::cin.ignore();
Window::reset_ball();
}
} /* end "If Packet contained a raw wave value..." */
} /* end "If TG_ReadPackets() was able to read a Packet..." */
} while (packetsRead > 0); /* Keep looping until all Packets read */
}
} /* end "Keep reading ThinkGear Packets for 10 seconds..." */
else {
Window::display_callback(window);
}
/**
// Main render display callback. Rendering of objects is done here.
Window::display_callback(window);
// Idle callback. Updating objects, etc. can be done here.
Window::idle_callback();
**/
}
Window::clean_up();
// Destroy the window
glfwDestroyWindow(window);
// Terminate GLFW
glfwTerminate();
exit(EXIT_SUCCESS);
}