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.

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