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.

454 lines
9.0 KiB

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. if (init())
  7. {
  8. printf("Error loading init\n");
  9. return;
  10. }
  11. SDL_Event event;
  12. while (this->KEEP_ALIVE)
  13. {
  14. //Handle Events
  15. this->events(event);
  16. this->paint();
  17. }
  18. close();
  19. }
  20. Game::~Game() {}
  21. int Game::init()
  22. {
  23. int result = 0;
  24. //Initialize SDL
  25. if (SDL_Init(SDL_INIT_VIDEO) < 0)
  26. {
  27. printf("Problem loading SLD Error: %s\n", SDL_GetError());
  28. }
  29. else
  30. {
  31. //OPEN GL VERSION 2.X
  32. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  33. //OPEN GL VERSION ^.1
  34. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
  35. //Create window
  36. int videoFlags = SDL_WINDOW_OPENGL;
  37. videoFlags |= SDL_WINDOW_SHOWN;
  38. videoFlags |= SDL_WINDOW_RESIZABLE;
  39. this->WINDOW = SDL_CreateWindow(
  40. this->TITLE,
  41. SDL_WINDOWPOS_UNDEFINED,
  42. SDL_WINDOWPOS_UNDEFINED,
  43. this->SCREEN_WIDTH,
  44. this->SCREEN_HEIGHT,
  45. videoFlags);
  46. if (WINDOW == NULL)
  47. {
  48. printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
  49. result = -1;
  50. }
  51. else
  52. {
  53. glContext = SDL_GL_CreateContext(WINDOW);
  54. if (glContext == NULL)
  55. {
  56. printf("Unable to crate Open GL Context: %s\n", SDL_GetError());
  57. result = -2;
  58. }
  59. else
  60. {
  61. //Try Vsync
  62. if (SDL_GL_SetSwapInterval(1) < 0)
  63. {
  64. printf("Vsync error:%s\n", SDL_GetError());
  65. }
  66. if (initGL())
  67. {
  68. printf("Problem working with OpenGL");
  69. }
  70. }
  71. }
  72. }
  73. return result;
  74. }
  75. int Game::initGL()
  76. {
  77. int result = 0;
  78. glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
  79. glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
  80. glDepthFunc(GL_LESS);
  81. glEnable(GL_DEPTH_TEST); // Enables Depth Testing
  82. glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
  83. glMatrixMode(GL_PROJECTION);
  84. /* Really Nice Perspective Calculations */
  85. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  86. glLoadIdentity();
  87. GLenum error = glGetError();
  88. if (error != GL_NO_ERROR)
  89. {
  90. printf("OpenGL Error %s\n", gluErrorString(error));
  91. result = -1;
  92. return result;
  93. }
  94. glMatrixMode(GL_MODELVIEW);
  95. // Set up the view port
  96. glViewport(0, 0, this->SCREEN_WIDTH, this->SCREEN_HEIGHT);
  97. glMatrixMode(GL_PROJECTION);
  98. glLoadIdentity();
  99. gluPerspective(
  100. 45.0f,
  101. (GLfloat)this->SCREEN_WIDTH / (GLfloat)this->SCREEN_HEIGHT,
  102. 0.1f,
  103. 100.0f);
  104. glMatrixMode(GL_MODELVIEW);
  105. return result;
  106. }
  107. void Game::close()
  108. {
  109. SDL_DestroyWindow(this->WINDOW);
  110. WINDOW = NULL;
  111. SDL_Quit();
  112. }
  113. void Game::grid()
  114. {
  115. glPushMatrix();
  116. //glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,gray);
  117. //glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
  118. //glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64);
  119. glTranslatef(0.0f, 0.0f, 0.0f);
  120. glLineWidth(2.5);
  121. glColor3f(1.0, 1.0, 1.0);
  122. /*grid*/
  123. float GRIDE_SIZE = 100;
  124. //X
  125. glBegin(GL_LINES);
  126. glVertex2f(-GRIDE_SIZE, 0);
  127. glVertex2f(GRIDE_SIZE, 0);
  128. glEnd();
  129. //Y
  130. glBegin(GL_LINES);
  131. glVertex2f(0, -GRIDE_SIZE);
  132. glVertex2f(0, GRIDE_SIZE);
  133. glEnd();
  134. //Z
  135. glBegin(GL_LINES);
  136. glVertex3f(0, 0, -GRIDE_SIZE);
  137. glVertex3f(0, 0, GRIDE_SIZE);
  138. glEnd();
  139. //X ticks
  140. float TICK_SIZE = .5;
  141. for (float f = -GRIDE_SIZE; f <= GRIDE_SIZE; f += 1)
  142. {
  143. //X
  144. glBegin(GL_LINES);
  145. glVertex3f(f, -TICK_SIZE, 0);
  146. glVertex3f(f, TICK_SIZE, 0);
  147. glEnd();
  148. //Y
  149. glBegin(GL_LINES);
  150. glVertex2f(-TICK_SIZE, f);
  151. glVertex2f(TICK_SIZE, f);
  152. glEnd();
  153. //Z
  154. glBegin(GL_LINES);
  155. glVertex3f(0, -TICK_SIZE, f);
  156. glVertex3f(0, TICK_SIZE, f);
  157. glEnd();
  158. }
  159. glPopMatrix();
  160. }
  161. void Game::triangle()
  162. {
  163. glBegin(GL_POLYGON); // start drawing a polygon
  164. glColor3f(1.0f, 0.0f, 0.0f); // Set The Color To Red
  165. glVertex3f(0.0f, 1.0f, 0.0f); // Top
  166. glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
  167. glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
  168. glEnd(); // we're done with the polygon (smooth color interpolation)
  169. }
  170. void Game::square()
  171. {
  172. glBegin(GL_QUADS); // start drawing a polygon (4 sided)
  173. glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
  174. glVertex3f(1.0f, 1.0f, 0.0f); // Top Right
  175. glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
  176. glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
  177. glEnd(); // done with the polygon
  178. }
  179. void Game::box()
  180. {
  181. glColor3f(1.0f, 1.0f, 1.0f);
  182. glBegin(GL_LINE_LOOP);
  183. glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
  184. glVertex3f(1.0f, 1.0f, 0.0f); // Top Right
  185. glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
  186. glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
  187. glEnd();
  188. }
  189. void Game::rectangle(){
  190. glColor3f(1.0f, 1.0f, 1.0f);
  191. glBegin(GL_LINE_LOOP);
  192. glVertex3f(-2.5f, 1.0f, 0.0f); // Top Left
  193. glVertex3f(2.5f, 1.0f, 0.0f); // Top Right
  194. glVertex3f(2.5f, -1.0f, 0.0f); // Bottom Right
  195. glVertex3f(-2.5f, -1.0f, 0.0f); // Bottom Left
  196. glEnd();
  197. }
  198. void Game::circle()
  199. {
  200. // glBegin(GL_LINE_LOOP);
  201. // for(int i =0; i <= 300; i++){
  202. // double angle = 2 * PI * i / 300;
  203. // double x = cos(angle);
  204. // double y = sin(angle);
  205. // glVertex2d(x,y);
  206. // }
  207. // glEnd();
  208. // this code (mostly) copied from question:
  209. double radius = 1;
  210. glBegin(GL_POLYGON);
  211. double angle1 = 0.0;
  212. glVertex2d(radius * cos(0.0), radius * sin(0.0));
  213. static const int circle_points = 100;
  214. static const float angle = 2.0f * 3.1416f / circle_points;
  215. int i;
  216. for (i = 0; i < circle_points; i++)
  217. {
  218. glVertex2d(radius * cos(angle1), radius * sin(angle1));
  219. angle1 += angle;
  220. }
  221. glEnd();
  222. }
  223. void Game::pentagon()
  224. {
  225. float angleIncrement = 360.0f / 5.0f;
  226. angleIncrement *= PI / 180.0f;
  227. glBegin(GL_TRIANGLE_FAN);
  228. float angle = 0.0f;
  229. double radius = 1;
  230. for (int k = 0; k < 100; ++k)
  231. {
  232. glVertex3f(radius * cos(angle), radius * sin(angle), 0.0f);
  233. angle += angleIncrement;
  234. }
  235. glEnd();
  236. }
  237. void Game::user_screen()
  238. {
  239. glPushMatrix();
  240. glTranslatef(-1.5f, -1.0f, -6.0f);
  241. this->box();
  242. //this->box();
  243. //glTranslatef(-1.5f, 0.0f, -6.0f); // Move Left 1.5 Units And Into The Screen 6.0
  244. glTranslatef(-1.5f, -1.0f, -6.0f);
  245. // draw a triangle (in smooth coloring mode)
  246. switch (USER_CURRENT)
  247. {
  248. case TRIANGLE:
  249. glColor3f(1.0f, 0.0f, 0.0f);
  250. this->triangle();
  251. break;
  252. case CIRCLE:
  253. glColor3f(0.0f, 1.0f, 0.0f);
  254. this->circle();
  255. break;
  256. case SQUARE:
  257. glColor3f(0.0f, 0.0f, 1.0f);
  258. this->square();
  259. break;
  260. case PENTAGON:
  261. glRotatef(-54.0f, 0.0f, 0.0f, 1.0f);
  262. glColor3f(1.0f, 0.0f, 1.0f);
  263. this->pentagon();
  264. break;
  265. }
  266. // glTranslatef(3.0f, 0.0f, 0.0f); // Move Right 3 Units
  267. // // draw a square (quadrilateral)
  268. // glColor3f(0.5f, 0.5f, 1.0f); // set color to a blue shade.
  269. glPopMatrix();
  270. }
  271. void Game::guest_screen(){
  272. glPushMatrix();
  273. glTranslatef(1.5f, -1.0f, -6.0f);
  274. this->box();
  275. glPopMatrix();
  276. }
  277. void Game::top_screen(){
  278. glPushMatrix();
  279. glTranslatef(-0.0f, 1.25f, -6.0f);
  280. this->rectangle();
  281. glPopMatrix();
  282. }
  283. void Game::paint()
  284. {
  285. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
  286. glMatrixMode(GL_MODELVIEW);
  287. glLoadIdentity();
  288. if (this->SHOW_GRID)
  289. {
  290. this->grid();
  291. }
  292. this->top_screen();
  293. this->user_screen();
  294. this->guest_screen();
  295. SDL_GL_SwapWindow(this->WINDOW);
  296. }
  297. void Game::resize(int width, int height)
  298. {
  299. glViewport(0, 0, width, height); // Reset The USER_CURRENT Viewport And Perspective Transformation
  300. glMatrixMode(GL_PROJECTION);
  301. glLoadIdentity();
  302. gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
  303. glMatrixMode(GL_MODELVIEW);
  304. }
  305. /**
  306. * key down
  307. * SDL_Keycode key_code
  308. */
  309. void Game::key_down(SDL_Keycode key_code)
  310. {
  311. switch (key_code)
  312. {
  313. case SDLK_ESCAPE:
  314. case SDLK_q:
  315. printf("Escape\n");
  316. this->KEEP_ALIVE = false;
  317. break;
  318. case SDLK_UP:
  319. printf("UP Key Pressed\n");
  320. this->on_key_up_arrow();
  321. break;
  322. case SDLK_DOWN:
  323. printf("Down Key Pressed\n");
  324. this->on_key_down_arrow();
  325. break;
  326. case SDLK_LEFT:
  327. printf("Left Key Pressed\n");
  328. this->on_key_left_arrow();
  329. break;
  330. case SDLK_RIGHT:
  331. printf("Right Key Pressed\n");
  332. this->on_key_right_arrow();
  333. break;
  334. case SDLK_g:
  335. this->SHOW_GRID = !SHOW_GRID;
  336. break;
  337. case SDLK_SPACE:
  338. printf("Space Key Pressed\n");
  339. break;
  340. default:
  341. //Do nothing
  342. break;
  343. }
  344. }
  345. void Game::events(SDL_Event &event)
  346. {
  347. while (SDL_PollEvent(&event) != 0)
  348. {
  349. //https://wiki.libsdl.org/SDL_EventType
  350. switch (event.type)
  351. {
  352. case SDL_WINDOWEVENT:
  353. switch (event.window.event)
  354. {
  355. case SDL_WINDOWEVENT_RESIZED:
  356. //resize();
  357. break;
  358. case SDL_WINDOWEVENT_SIZE_CHANGED:
  359. this->resize(event.window.data1, event.window.data2);
  360. break;
  361. }
  362. break;
  363. //user-requested quit
  364. case SDL_QUIT:
  365. this->KEEP_ALIVE = false;
  366. break;
  367. //Mouse events
  368. case SDL_MOUSEBUTTONDOWN:
  369. case SDL_MOUSEMOTION:
  370. case SDL_MOUSEBUTTONUP:
  371. case SDL_MOUSEWHEEL:
  372. break;
  373. //Keyboard Events
  374. case SDL_KEYDOWN:
  375. this->key_down(event.key.keysym.sym);
  376. break;
  377. case SDL_KEYUP:
  378. //Todo Key up commands
  379. break;
  380. } //end switch(event.type)
  381. } //end-while
  382. }
  383. void Game::on_key_up_arrow()
  384. {
  385. // Toggle triangle
  386. this->USER_CURRENT = TRIANGLE;
  387. }
  388. void Game::on_key_down_arrow()
  389. {
  390. this->USER_CURRENT = SQUARE;
  391. }
  392. void Game::on_key_left_arrow()
  393. {
  394. this->USER_CURRENT = PENTAGON;
  395. }
  396. void Game::on_key_right_arrow()
  397. {
  398. this->USER_CURRENT = CIRCLE;
  399. }
  400. void Game::on_key_space()
  401. {
  402. }
  403. void Game::on_key_enter()
  404. {
  405. }
  406. } // namespace betacore
  407. int main(int argc, char *argv[])
  408. {
  409. betacore::Game game;
  410. return 0;
  411. }