You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

753 lines
15 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #include "game.hpp"
  2. void gluPerspective(double fovy,double aspect, double zNear, double zFar)
  3. {
  4. // Start in projection mode.
  5. glMatrixMode(GL_PROJECTION);
  6. glLoadIdentity();
  7. double xmin, xmax, ymin, ymax;
  8. ymax = zNear * tan(fovy * M_PI / 360.0);
  9. ymin = -ymax;
  10. xmin = ymin * aspect;
  11. xmax = ymax * aspect;
  12. glFrustum(xmin, xmax, ymin, ymax, zNear, zFar);
  13. }
  14. namespace betacore
  15. {
  16. Game::Game(MODE mode,
  17. int port,
  18. std::string url): ME(mode)
  19. {
  20. this->TITLE = Parser::mode(mode).c_str();
  21. printf("%s\t%s\t%d\t%d\n",this->TITLE, Parser::mode(mode).c_str(),
  22. this->MONITOR_WIDTH, this->MONITOR_HEIGHT);
  23. if (init())
  24. {
  25. printf("Error loading init\n");
  26. return;
  27. }
  28. using namespace std::placeholders;
  29. std::function<void(SHAPE & s, MODE & m)> fn =
  30. std::bind(&Game::update, this, _1, _2);
  31. this->client = new Client(mode, port, url, fn);
  32. // SDL_DisplayMode DM;
  33. // SDL_GetCurrentDisplayMode(0, &DM);
  34. this->MONITOR_HEIGHT = 1080;
  35. this->MONITOR_WIDTH = 1920;
  36. SDL_Event event;
  37. while (this->KEEP_ALIVE)
  38. {
  39. //Handle Events
  40. this->events(event);
  41. this->paint();
  42. }
  43. close();
  44. }
  45. Game::~Game()
  46. {
  47. if (this->client != nullptr)
  48. delete this->client;
  49. this->client = nullptr;
  50. }
  51. int Game::init()
  52. {
  53. int result = 0;
  54. //Initialize SDL
  55. if (SDL_Init(SDL_INIT_VIDEO) < 0)
  56. {
  57. printf("Problem loading SLD Error: %s\n", SDL_GetError());
  58. }
  59. else
  60. {
  61. //OPEN GL VERSION 2.X
  62. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  63. //OPEN GL VERSION ^.1
  64. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
  65. //Create window
  66. int videoFlags = SDL_WINDOW_OPENGL;
  67. //videoFlags |= SDL_WINDOW_FULLSCREEN;
  68. videoFlags |= SDL_WINDOW_SHOWN;
  69. videoFlags |= SDL_WINDOW_RESIZABLE;
  70. //videoFlags |= SDL_WINDOW_BORDERLESS;
  71. // videoFlags |= SDL_WINDOW_INPUT_GRABBED;
  72. this->WINDOW = SDL_CreateWindow(
  73. this->TITLE,
  74. SDL_WINDOWPOS_CENTERED,
  75. SDL_WINDOWPOS_CENTERED,
  76. this->SCREEN_WIDTH,
  77. this->SCREEN_HEIGHT,
  78. videoFlags);
  79. if (WINDOW == NULL)
  80. {
  81. printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
  82. result = -1;
  83. }
  84. else
  85. {
  86. glContext = SDL_GL_CreateContext(WINDOW);
  87. if (glContext == NULL)
  88. {
  89. printf("Unable to crate Open GL Context: %s\n", SDL_GetError());
  90. result = -2;
  91. }
  92. else
  93. {
  94. //Try Vsync
  95. if (SDL_GL_SetSwapInterval(1) < 0)
  96. {
  97. printf("Vsync error:%s\n", SDL_GetError());
  98. }
  99. if (initGL())
  100. {
  101. printf("Problem working with OpenGL");
  102. }
  103. }
  104. }
  105. }
  106. return result;
  107. }
  108. int Game::initGL()
  109. {
  110. int result = 0;
  111. glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
  112. glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
  113. glDepthFunc(GL_LESS);
  114. glEnable(GL_DEPTH_TEST); // Enables Depth Testing
  115. glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
  116. glMatrixMode(GL_PROJECTION);
  117. /* Really Nice Perspective Calculations */
  118. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  119. glLoadIdentity();
  120. GLenum error = glGetError();
  121. if (error != GL_NO_ERROR)
  122. {
  123. printf("OpenGL Error %s\n");
  124. result = -1;
  125. return result;
  126. }
  127. glMatrixMode(GL_MODELVIEW);
  128. // Set up the view port
  129. glViewport(0, 0, this->SCREEN_WIDTH, this->SCREEN_HEIGHT);
  130. glMatrixMode(GL_PROJECTION);
  131. glLoadIdentity();
  132. gluPerspective(
  133. 45.0f,
  134. (GLfloat)this->SCREEN_WIDTH / (GLfloat)this->SCREEN_HEIGHT,
  135. 0.1f,
  136. 100.0f);
  137. glMatrixMode(GL_MODELVIEW);
  138. return result;
  139. }
  140. void Game::close()
  141. {
  142. SDL_DestroyWindow(this->WINDOW);
  143. WINDOW = NULL;
  144. SDL_Quit();
  145. }
  146. void Game::grid()
  147. {
  148. glPushMatrix();
  149. //glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,gray);
  150. //glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
  151. //glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64);
  152. glTranslatef(0.0f, 0.0f, 0.0f);
  153. glLineWidth(2.5);
  154. glColor3f(1.0, 1.0, 1.0);
  155. /*grid*/
  156. float GRIDE_SIZE = 100;
  157. //X
  158. glBegin(GL_LINES);
  159. glVertex2f(-GRIDE_SIZE, 0);
  160. glVertex2f(GRIDE_SIZE, 0);
  161. glEnd();
  162. //Y
  163. glBegin(GL_LINES);
  164. glVertex2f(0, -GRIDE_SIZE);
  165. glVertex2f(0, GRIDE_SIZE);
  166. glEnd();
  167. //Z
  168. glBegin(GL_LINES);
  169. glVertex3f(0, 0, -GRIDE_SIZE);
  170. glVertex3f(0, 0, GRIDE_SIZE);
  171. glEnd();
  172. //X ticks
  173. float TICK_SIZE = .5;
  174. for (float f = -GRIDE_SIZE; f <= GRIDE_SIZE; f += 1)
  175. {
  176. //X
  177. glBegin(GL_LINES);
  178. glVertex3f(f, -TICK_SIZE, 0);
  179. glVertex3f(f, TICK_SIZE, 0);
  180. glEnd();
  181. //Y
  182. glBegin(GL_LINES);
  183. glVertex2f(-TICK_SIZE, f);
  184. glVertex2f(TICK_SIZE, f);
  185. glEnd();
  186. //Z
  187. glBegin(GL_LINES);
  188. glVertex3f(0, -TICK_SIZE, f);
  189. glVertex3f(0, TICK_SIZE, f);
  190. glEnd();
  191. }
  192. glPopMatrix();
  193. }
  194. void Game::triangle()
  195. {
  196. this->triangle(true);
  197. }
  198. void Game::triangle(bool filled)
  199. {
  200. glColor3f(1.0f, 0.0f, 0.0f);
  201. if (filled)
  202. glBegin(GL_POLYGON); // start drawing a polygon
  203. else
  204. glBegin(GL_LINE_LOOP);
  205. glColor3f(1.0f, 0.0f, 0.0f); // Set The Color To Red
  206. glVertex3f(0.0f, 1.0f, 0.0f); // Top
  207. glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
  208. glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
  209. glEnd(); // we're done with the polygon (smooth color interpolation)
  210. }
  211. void Game::square()
  212. {
  213. this->square(true);
  214. }
  215. void Game::square(bool filled)
  216. {
  217. glColor3f(0.0f, 0.0f, 1.0f);
  218. if (filled)
  219. glBegin(GL_QUADS); // start drawing a polygon (4 sided)
  220. else
  221. glBegin(GL_LINE_LOOP);
  222. glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
  223. glVertex3f(1.0f, 1.0f, 0.0f); // Top Right
  224. glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
  225. glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
  226. glEnd(); // done with the polygon
  227. }
  228. void Game::box()
  229. {
  230. glColor3f(1.0f, 1.0f, 1.0f);
  231. glBegin(GL_LINE_LOOP);
  232. glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
  233. glVertex3f(1.0f, 1.0f, 0.0f); // Top Right
  234. glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
  235. glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
  236. glEnd();
  237. }
  238. void Game::rectangle()
  239. {
  240. glColor3f(1.0f, 1.0f, 1.0f);
  241. glBegin(GL_LINE_LOOP);
  242. glVertex3f(-2.5f, 1.0f, 0.0f); // Top Left
  243. glVertex3f(2.5f, 1.0f, 0.0f); // Top Right
  244. glVertex3f(2.5f, -1.0f, 0.0f); // Bottom Right
  245. glVertex3f(-2.5f, -1.0f, 0.0f); // Bottom Left
  246. glEnd();
  247. }
  248. void Game::cross(bool filled)
  249. {
  250. double width = 0.15;
  251. if (filled)
  252. glBegin(GL_QUADS);
  253. else
  254. glBegin(GL_LINE_LOOP);
  255. glVertex3f(-0.5f, width, 0.0f); // Top Left
  256. glVertex3f(0.5f, width, 0.0f); // Top Right
  257. glVertex3f(0.5f, -width, 0.0f); // Bottom Right
  258. glVertex3f(-0.5f, -width, 0.0f); // Bottom Left
  259. glEnd();
  260. glPushMatrix();
  261. glRotatef(-90.0f, 0.0f, 0.0f, 1.0f);
  262. if (filled)
  263. glBegin(GL_QUADS);
  264. else
  265. glBegin(GL_LINE_LOOP);
  266. glVertex3f(-0.5f, width, 0.0f); // Top Left
  267. glVertex3f(0.5f, width, 0.0f); // Top Right
  268. glVertex3f(0.5f, -width, 0.0f); // Bottom Right
  269. glVertex3f(-0.5f, -width, 0.0f); // Bottom Left
  270. glEnd();
  271. glPopMatrix();
  272. }
  273. void Game::compass()
  274. {
  275. glPushMatrix();
  276. glTranslatef(-1.5f, 0.0f, 0.0f);
  277. glColor3f(1.0f, 1.0f, 1.0f);
  278. this->cross(false);
  279. //MINI TRIANGLE
  280. glPushMatrix();
  281. glScalef(0.12f, 0.12f, 0.0f);
  282. glTranslatef(0.0f, 2.6f, 0.0f);
  283. this->triangle(this->KEY_UP_ARROW_ACTIVE);
  284. glPopMatrix();
  285. // MINI SQUARE
  286. glPushMatrix();
  287. glScalef(0.12f, 0.12f, 0.0f);
  288. glTranslatef(0.0f, -2.6f, 0.0f);
  289. this->square(this->KEY_DOWN_ARROW_ACTIVE);
  290. glPopMatrix();
  291. // MINI PENTAGON
  292. glPushMatrix();
  293. glScalef(0.12f, 0.12f, 0.0f);
  294. glTranslatef(-2.56f, 0.0f, 0.0f);
  295. this->pentagon(this->KEY_LEFT_ARROW_ACTIVE);
  296. glPopMatrix();
  297. // MINI CIRCLE
  298. glPushMatrix();
  299. glScalef(0.12f, 0.12f, 0.0f);
  300. glTranslatef(2.56f, 0.0f, 0.0f);
  301. glColor3f(0.0f, 1.0f, 0.0f);
  302. this->circle(this->KEY_RIGHT_ARROW_ACTIVE);
  303. glPopMatrix();
  304. glPushMatrix();
  305. glScalef(0.35f, 0.35f, 0.0f);
  306. glTranslatef(7.0f, 0.0f, 0.0f);
  307. glColor3f(0.0f, 1.0f, 1.0f);
  308. this->circle(this->KEY_SPACE_ACTIVE);
  309. glPopMatrix();
  310. glPushMatrix();
  311. glScalef(0.35f, 0.35f, 0.0f);
  312. glTranslatef(10.0f, 0.0f, 0.0f);
  313. glColor3f(1.0f, 1.0f, 0.0f);
  314. this->circle(this->KEY_RETURN_ACTIVE);
  315. glPopMatrix();
  316. glPopMatrix();
  317. }
  318. void Game::circle()
  319. {
  320. this->circle(true);
  321. }
  322. void Game::circle(bool filled)
  323. {
  324. //glBegin(GL_LINE_LOOP);
  325. // for (int i = 0; i <= 300; i++)
  326. // {
  327. // double angle = 2 * PI * i / 300;
  328. // double x = cos(angle);
  329. // double y = sin(angle);
  330. // glVertex2d(x, y);
  331. // }
  332. // glEnd();
  333. double radius = 1;
  334. if (filled)
  335. glBegin(GL_POLYGON);
  336. else
  337. glBegin(GL_LINE_LOOP);
  338. double angle1 = 0.0;
  339. glVertex2d(radius * cos(0.0), radius * sin(0.0));
  340. static const int circle_points = 100;
  341. static const float angle = 2.0f * 3.1416f / circle_points;
  342. int i;
  343. for (i = 0; i < circle_points; i++)
  344. {
  345. glVertex2d(radius * cos(angle1), radius * sin(angle1));
  346. angle1 += angle;
  347. }
  348. glEnd();
  349. }
  350. void Game::pentagon()
  351. {
  352. this->pentagon(true);
  353. }
  354. void Game::pentagon(bool filled)
  355. {
  356. glRotatef(-54.0f, 0.0f, 0.0f, 1.0f);
  357. glColor3f(1.0f, 0.0f, 1.0f);
  358. float angleIncrement = 360.0f / 5.0f;
  359. angleIncrement *= PI / 180.0f;
  360. if (filled)
  361. glBegin(GL_TRIANGLE_FAN);
  362. else
  363. glBegin(GL_LINE_LOOP);
  364. float angle = 0.0f;
  365. double radius = 1;
  366. for (int k = 0; k < 100; ++k)
  367. {
  368. glVertex3f(radius * cos(angle), radius * sin(angle), 0.0f);
  369. angle += angleIncrement;
  370. }
  371. glEnd();
  372. }
  373. void Game::user_screen()
  374. {
  375. glPushMatrix();
  376. glTranslatef(-1.5f, -1.0f, -6.0f);
  377. this->box();
  378. //this->box();
  379. //glTranslatef(-1.5f, 0.0f, -6.0f); // Move Left 1.5 Units And Into The Screen 6.0
  380. glTranslatef(-1.5f, -1.0f, -6.0f);
  381. // draw a triangle (in smooth coloring mode)
  382. switch (USER_CURRENT)
  383. {
  384. case TRIANGLE:
  385. this->triangle();
  386. break;
  387. case CIRCLE:
  388. glColor3f(0.0f, 1.0f, 0.0f);
  389. this->circle();
  390. break;
  391. case SQUARE:
  392. this->square();
  393. break;
  394. case PENTAGON:
  395. this->pentagon();
  396. break;
  397. case UNKOWN:
  398. float gray = 105.0 / 255.0;
  399. glColor3f(gray, gray, gray);
  400. glPushMatrix();
  401. glRotatef(-45.0f, 0.0f, 0.0f, 1.0f);
  402. glScalef(2.0f, 2.0f, 0.0f);
  403. this->cross(true);
  404. glPopMatrix();
  405. break;
  406. }
  407. // glTranslatef(3.0f, 0.0f, 0.0f); // Move Right 3 Units
  408. // // draw a square (quadrilateral)
  409. // glColor3f(0.5f, 0.5f, 1.0f); // set color to a blue shade.
  410. glPopMatrix();
  411. }
  412. void Game::guest_screen()
  413. {
  414. glPushMatrix();
  415. glTranslatef(1.5f, -1.0f, -6.0f);
  416. this->box();
  417. glTranslatef(1.5f, -1.0f, -6.0f);
  418. // draw a triangle (in smooth coloring mode)
  419. switch (GUST_CURRENT)
  420. {
  421. case TRIANGLE:
  422. this->triangle();
  423. break;
  424. case CIRCLE:
  425. glColor3f(0.0f, 1.0f, 0.0f);
  426. this->circle();
  427. break;
  428. case SQUARE:
  429. this->square();
  430. break;
  431. case PENTAGON:
  432. this->pentagon();
  433. break;
  434. case UNKOWN:
  435. float gray = 105.0 / 255.0;
  436. glColor3f(gray, gray, gray);
  437. glPushMatrix();
  438. glRotatef(-45.0f, 0.0f, 0.0f, 1.0f);
  439. glScalef(2.0f, 2.0f, 0.0f);
  440. this->cross(true);
  441. glPopMatrix();
  442. break;
  443. }
  444. glPopMatrix();
  445. }
  446. void Game::top_screen()
  447. {
  448. glPushMatrix();
  449. glTranslatef(-0.0f, 1.25f, -6.0f);
  450. this->rectangle();
  451. this->compass();
  452. glPopMatrix();
  453. }
  454. void Game::paint()
  455. {
  456. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
  457. glMatrixMode(GL_MODELVIEW);
  458. glLoadIdentity();
  459. if (this->SHOW_GRID)
  460. {
  461. this->grid();
  462. }
  463. this->top_screen();
  464. this->user_screen();
  465. this->guest_screen();
  466. SDL_GL_SwapWindow(this->WINDOW);
  467. }
  468. void Game::resize(int width, int height)
  469. {
  470. glViewport(0, 0, width, height); // Reset The USER_CURRENT Viewport And Perspective Transformation
  471. glMatrixMode(GL_PROJECTION);
  472. glLoadIdentity();
  473. gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
  474. glMatrixMode(GL_MODELVIEW);
  475. }
  476. /**
  477. * key down
  478. * SDL_Keycode key_code
  479. */
  480. void Game::key_down(SDL_Keycode key_code)
  481. {
  482. switch (key_code)
  483. {
  484. case SDLK_ESCAPE:
  485. case SDLK_q:
  486. printf("Escape\n");
  487. this->KEEP_ALIVE = false;
  488. break;
  489. case SDLK_UP:
  490. printf("UP Key Pressed\n");
  491. this->on_key_up_arrow();
  492. this->KEY_UP_ARROW_ACTIVE = true;
  493. break;
  494. case SDLK_DOWN:
  495. printf("Down Key Pressed\n");
  496. this->on_key_down_arrow();
  497. this->KEY_DOWN_ARROW_ACTIVE = true;
  498. break;
  499. case SDLK_LEFT:
  500. printf("Left Key Pressed\n");
  501. this->on_key_left_arrow();
  502. this->KEY_LEFT_ARROW_ACTIVE = true;
  503. break;
  504. case SDLK_RIGHT:
  505. printf("Right Key Pressed\n");
  506. this->on_key_right_arrow();
  507. this->KEY_RIGHT_ARROW_ACTIVE = true;
  508. break;
  509. case SDLK_g:
  510. this->SHOW_GRID = !SHOW_GRID;
  511. break;
  512. case SDLK_SPACE:
  513. this->KEY_SPACE_ACTIVE = true;
  514. this->on_key_space();
  515. break;
  516. case SDLK_RETURN:
  517. this->KEY_RETURN_ACTIVE = true;
  518. this->on_key_enter();
  519. break;
  520. case SDLK_F11:
  521. if (FULL_SCREEN)
  522. {
  523. SDL_SetWindowPosition(this->WINDOW, 100, 100);
  524. SDL_RestoreWindow(this->WINDOW);
  525. //SDL_SetWindowResizable(this->WINDOW, SDL_TRUE);
  526. //SDL_SetWindowBordered(this->WINDOW, 1);
  527. SDL_SetWindowSize(this->WINDOW, SCREEN_WIDTH, SCREEN_HEIGHT);
  528. //SDL_SetWindowFullscreen(this->WINDOW,SDL_WINDOW_FULLSCREEN );
  529. }
  530. else
  531. {
  532. SDL_SetWindowPosition(this->WINDOW, 0, 0);
  533. //SDL_SetWindowResizable(this->WINDOW, SDL_FALSE);
  534. //SDL_SetWindowBordered(this->WINDOW, 0);
  535. SDL_SetWindowSize(this->WINDOW, MONITOR_WIDTH, MONITOR_HEIGHT);
  536. //SDL_SetWindowFullscreen(this->WINDOW,0 );
  537. }
  538. this->FULL_SCREEN = !this->FULL_SCREEN;
  539. SDL_ShowCursor(this->FULL_SCREEN);
  540. break;
  541. default:
  542. //Do nothing
  543. break;
  544. }
  545. }
  546. /**
  547. * key down
  548. * SDL_Keycode key_code
  549. */
  550. void Game::key_up(SDL_Keycode key_code)
  551. {
  552. switch (key_code)
  553. {
  554. case SDLK_UP:
  555. this->KEY_UP_ARROW_ACTIVE = false;
  556. break;
  557. case SDLK_DOWN:
  558. this->KEY_DOWN_ARROW_ACTIVE = false;
  559. break;
  560. case SDLK_LEFT:
  561. this->KEY_LEFT_ARROW_ACTIVE = false;
  562. break;
  563. case SDLK_RIGHT:
  564. this->KEY_RIGHT_ARROW_ACTIVE = false;
  565. break;
  566. case SDLK_SPACE:
  567. this->KEY_SPACE_ACTIVE = false;
  568. break;
  569. case SDLK_RETURN:
  570. this->KEY_RETURN_ACTIVE = false;
  571. break;
  572. case SDLK_F11:
  573. // SDL_DisplayMode dm;
  574. // SDL_RestoreWindow(this->WINDOW); //Incase it's maximized...
  575. // SDL_SetWindowSize(this->WINDOW, dm.w, dm.h + 10);
  576. // SDL_SetWindowPosition(this->WINDOW, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
  577. default:
  578. //Do nothing
  579. break;
  580. }
  581. }
  582. void Game::events(SDL_Event &event)
  583. {
  584. while (SDL_PollEvent(&event) != 0)
  585. {
  586. //https://wiki.libsdl.org/SDL_EventType
  587. switch (event.type)
  588. {
  589. case SDL_WINDOWEVENT:
  590. switch (event.window.event)
  591. {
  592. case SDL_WINDOWEVENT_RESIZED:
  593. //resize();
  594. break;
  595. case SDL_WINDOWEVENT_SIZE_CHANGED:
  596. this->resize(event.window.data1, event.window.data2);
  597. break;
  598. }
  599. break;
  600. //user-requested quit
  601. case SDL_QUIT:
  602. this->KEEP_ALIVE = false;
  603. break;
  604. //Mouse events
  605. case SDL_MOUSEBUTTONDOWN:
  606. case SDL_MOUSEMOTION:
  607. case SDL_MOUSEBUTTONUP:
  608. case SDL_MOUSEWHEEL:
  609. break;
  610. //Keyboard Events
  611. case SDL_KEYDOWN:
  612. this->key_down(event.key.keysym.sym);
  613. break;
  614. case SDL_KEYUP:
  615. this->key_up(event.key.keysym.sym);
  616. break;
  617. } //end switch(event.type)
  618. } //end-while
  619. }
  620. void Game::on_key_up_arrow()
  621. {
  622. // Toggle triangle
  623. this->USER_CURRENT = TRIANGLE;
  624. }
  625. void Game::on_key_down_arrow()
  626. {
  627. this->USER_CURRENT = SQUARE;
  628. }
  629. void Game::on_key_left_arrow()
  630. {
  631. this->USER_CURRENT = PENTAGON;
  632. }
  633. void Game::on_key_right_arrow()
  634. {
  635. this->USER_CURRENT = CIRCLE;
  636. }
  637. void Game::on_key_space()
  638. {
  639. this->client->send(false,this->USER_CURRENT);
  640. }
  641. void Game::on_key_enter()
  642. {
  643. this->client->send(true,this->USER_CURRENT);
  644. }
  645. void Game::update(SHAPE &s, MODE &m)
  646. {
  647. if (m == ME || (ME==EVE && m == ALICE))
  648. this->USER_CURRENT = s;
  649. else
  650. this->GUST_CURRENT = s;
  651. }
  652. } // namespace betacore
  653. void ussage(){
  654. std::cout
  655. << "Ussage:\n"
  656. << "\tMODE: 1-3\n"
  657. << "\t\t1)Alice\n"
  658. << "\t\t2)Bob\n"
  659. << "\t\t2)Eve\n"
  660. << "\tport\n"
  661. << "\taddress"
  662. << std::endl;
  663. }
  664. int main(int argc, char *argv[])
  665. {
  666. if(argc < 4){
  667. ussage();
  668. return 1;
  669. }
  670. betacore::MODE mode = betacore::EVE;
  671. int a = atoi(argv[1]);
  672. std::cout << "Running mode" << a << std::endl;
  673. switch( a){
  674. case 1:
  675. mode = betacore::ALICE;
  676. break;
  677. case 2:
  678. mode = betacore::BOB;
  679. break;
  680. default:
  681. mode = betacore::EVE;
  682. break;
  683. }
  684. betacore::Game game(mode, atoi(argv[2]),argv[3]);
  685. return 0;
  686. }