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.

441 lines
8.6 KiB

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::circle()
  190. {
  191. // glBegin(GL_LINE_LOOP);
  192. // for(int i =0; i <= 300; i++){
  193. // double angle = 2 * PI * i / 300;
  194. // double x = cos(angle);
  195. // double y = sin(angle);
  196. // glVertex2d(x,y);
  197. // }
  198. // glEnd();
  199. // this code (mostly) copied from question:
  200. double radius = 1;
  201. glBegin(GL_POLYGON);
  202. double angle1 = 0.0;
  203. glVertex2d(radius * cos(0.0), radius * sin(0.0));
  204. static const int circle_points = 100;
  205. static const float angle = 2.0f * 3.1416f / circle_points;
  206. int i;
  207. for (i = 0; i < circle_points; i++)
  208. {
  209. glVertex2d(radius * cos(angle1), radius * sin(angle1));
  210. angle1 += angle;
  211. }
  212. glEnd();
  213. }
  214. void Game::pentagon()
  215. {
  216. float angleIncrement = 360.0f / 5.0f;
  217. angleIncrement *= PI / 180.0f;
  218. glBegin(GL_TRIANGLE_FAN);
  219. float angle = 0.0f;
  220. double radius = 1;
  221. for (int k = 0; k < 100; ++k)
  222. {
  223. glVertex3f(radius * cos(angle), radius * sin(angle), 0.0f);
  224. angle += angleIncrement;
  225. }
  226. glEnd();
  227. }
  228. void Game::user_screen()
  229. {
  230. glPushMatrix();
  231. glTranslatef(-1.5f, -1.0f, -6.0f);
  232. this->box();
  233. //this->box();
  234. //glTranslatef(-1.5f, 0.0f, -6.0f); // Move Left 1.5 Units And Into The Screen 6.0
  235. glTranslatef(-1.5f, -1.0f, -6.0f);
  236. // draw a triangle (in smooth coloring mode)
  237. switch (USER_CURRENT)
  238. {
  239. case TRIANGLE:
  240. glColor3f(1.0f, 0.0f, 0.0f);
  241. this->triangle();
  242. break;
  243. case CIRCLE:
  244. glColor3f(0.0f, 1.0f, 0.0f);
  245. this->circle();
  246. break;
  247. case SQUARE:
  248. glColor3f(0.0f, 0.0f, 1.0f);
  249. this->square();
  250. break;
  251. case PENTAGON:
  252. glRotatef(-54.0f, 0.0f, 0.0f, 1.0f);
  253. glColor3f(1.0f, 0.0f, 1.0f);
  254. this->pentagon();
  255. break;
  256. }
  257. // glTranslatef(3.0f, 0.0f, 0.0f); // Move Right 3 Units
  258. // // draw a square (quadrilateral)
  259. // glColor3f(0.5f, 0.5f, 1.0f); // set color to a blue shade.
  260. glPopMatrix();
  261. }
  262. void Game::guest_screen(){
  263. glPushMatrix();
  264. glTranslatef(1.5f, -1.0f, -6.0f);
  265. this->box();
  266. glPopMatrix();
  267. }
  268. void Game::top_screen(){
  269. }
  270. void Game::paint()
  271. {
  272. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
  273. glMatrixMode(GL_MODELVIEW);
  274. glLoadIdentity();
  275. if (this->SHOW_GRID)
  276. {
  277. this->grid();
  278. }
  279. this->user_screen();
  280. this->guest_screen();
  281. SDL_GL_SwapWindow(this->WINDOW);
  282. }
  283. void Game::resize(int width, int height)
  284. {
  285. glViewport(0, 0, width, height); // Reset The USER_CURRENT Viewport And Perspective Transformation
  286. glMatrixMode(GL_PROJECTION);
  287. glLoadIdentity();
  288. gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
  289. glMatrixMode(GL_MODELVIEW);
  290. }
  291. /**
  292. * key down
  293. * SDL_Keycode key_code
  294. */
  295. void Game::key_down(SDL_Keycode key_code)
  296. {
  297. switch (key_code)
  298. {
  299. case SDLK_ESCAPE:
  300. case SDLK_q:
  301. printf("Escape\n");
  302. this->KEEP_ALIVE = false;
  303. break;
  304. case SDLK_UP:
  305. printf("UP Key Pressed\n");
  306. this->on_key_up_arrow();
  307. break;
  308. case SDLK_DOWN:
  309. printf("Down Key Pressed\n");
  310. this->on_key_down_arrow();
  311. break;
  312. case SDLK_LEFT:
  313. printf("Left Key Pressed\n");
  314. this->on_key_left_arrow();
  315. break;
  316. case SDLK_RIGHT:
  317. printf("Right Key Pressed\n");
  318. this->on_key_right_arrow();
  319. break;
  320. case SDLK_g:
  321. this->SHOW_GRID = !SHOW_GRID;
  322. break;
  323. case SDLK_SPACE:
  324. printf("Space Key Pressed\n");
  325. break;
  326. default:
  327. //Do nothing
  328. break;
  329. }
  330. }
  331. void Game::events(SDL_Event &event)
  332. {
  333. while (SDL_PollEvent(&event) != 0)
  334. {
  335. //https://wiki.libsdl.org/SDL_EventType
  336. switch (event.type)
  337. {
  338. case SDL_WINDOWEVENT:
  339. switch (event.window.event)
  340. {
  341. case SDL_WINDOWEVENT_RESIZED:
  342. //resize();
  343. break;
  344. case SDL_WINDOWEVENT_SIZE_CHANGED:
  345. this->resize(event.window.data1, event.window.data2);
  346. break;
  347. }
  348. break;
  349. //user-requested quit
  350. case SDL_QUIT:
  351. this->KEEP_ALIVE = false;
  352. break;
  353. //Mouse events
  354. case SDL_MOUSEBUTTONDOWN:
  355. case SDL_MOUSEMOTION:
  356. case SDL_MOUSEBUTTONUP:
  357. case SDL_MOUSEWHEEL:
  358. break;
  359. //Keyboard Events
  360. case SDL_KEYDOWN:
  361. this->key_down(event.key.keysym.sym);
  362. break;
  363. case SDL_KEYUP:
  364. //Todo Key up commands
  365. break;
  366. } //end switch(event.type)
  367. } //end-while
  368. }
  369. void Game::on_key_up_arrow()
  370. {
  371. // Toggle triangle
  372. this->USER_CURRENT = TRIANGLE;
  373. }
  374. void Game::on_key_down_arrow()
  375. {
  376. this->USER_CURRENT = SQUARE;
  377. }
  378. void Game::on_key_left_arrow()
  379. {
  380. this->USER_CURRENT = PENTAGON;
  381. }
  382. void Game::on_key_right_arrow()
  383. {
  384. this->USER_CURRENT = CIRCLE;
  385. }
  386. void Game::on_key_space()
  387. {
  388. }
  389. void Game::on_key_enter()
  390. {
  391. }
  392. } // namespace betacore
  393. int main(int argc, char *argv[])
  394. {
  395. betacore::Game game;
  396. return 0;
  397. }