Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <GL/glut.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. #define FPS 60
  8.  
  9. enum MENU_TYPE
  10. {
  11. MENU_START,
  12. MENU_EXIT,
  13. };
  14.  
  15. GLint count_timer = 0;
  16.  
  17. int i;
  18.  
  19. int timestep = 0;
  20. float gravity = 0.0098;
  21. float velo = 0.0f;
  22.  
  23. int view_pos = 0;
  24.  
  25. int game_w = 400;
  26. int game_h = 700;
  27.  
  28. float square_x1 = -0.06;
  29. float square_x2 = 0.06;
  30.  
  31. float square_y1 = 1;
  32. float square_y2 = 0.9;
  33.  
  34. int a = 0;
  35.  
  36. float x_left[] = { -0.2,-0.1,0.0,-0.1,-0.2,-0.3,-0.2,-0.1,0.0,-0.1,-0.2,-0.3,-0.2,-0.1,0.0,-0.1,-0.2,-0.1 };
  37. float y_left[] = { 0.7,0.6,0.5,0.4,0.3,0.2,0.1,0.0,-0.1,-0.2,-0.3,-0.4,-0.5,-0.6,-0.7,-0.8,-0.9,-1.0 };
  38. float x_right[] = { 0.1,0.2,0.3,0.2,0.1,0.0,0.1,0.2,0.3,0.2,0.1,0.0,0.1,0.2,0.3,0.2,0.1,0.0 };
  39. float y_right[] = { 0.7,0.6,0.5,0.4,0.3,0.2,0.1,0.0,-0.1,-0.2,-0.3,-0.4,-0.5,-0.6,-0.7,-0.8,-0.9,-1.0 };
  40. float timerC[] = { 18,28,38,48,58,68,78,88,98,108,118,128,138,148,158,168,178,198,208 };
  41.  
  42. GLfloat xstep = 0.1f;
  43. GLfloat ystep = 0.01f;
  44.  
  45.  
  46.  
  47.  
  48. void background() {
  49. glBegin(GL_QUADS); // Each set of 4 vertices form a quad
  50.  
  51. glColor3f(0.0f, 0.1, 0.1); //new1
  52. glVertex2f(-1.0f, 0.6f);
  53. glVertex2f(-0.2f, 0.6f);
  54. glVertex2f(-0.2f, 0.7f);
  55. glVertex2f(-1.0f, 0.7f);
  56.  
  57. glColor3f(0.0f, 0.1, 0.1); //new1.1
  58. glVertex2f(0.1f, 0.6f);
  59. glVertex2f(1.0f, 0.6f);
  60. glVertex2f(1.0f, 0.7f);
  61. glVertex2f(0.1f, 0.7f);
  62.  
  63. glColor3f(0.0f, 0.2, 0.2); //new2
  64. glVertex2f(-1.0f, 0.5f);
  65. glVertex2f(-0.1f, 0.5f);
  66. glVertex2f(-0.1f, 0.6f);
  67. glVertex2f(-1.0f, 0.6f);
  68.  
  69. glColor3f(0.0f, 0.2, 0.2); //new2.1
  70. glVertex2f(0.2f, 0.5f);
  71. glVertex2f(1.0f, 0.5f);
  72. glVertex2f(1.0f, 0.6f);
  73. glVertex2f(0.2f, 0.6f);
  74.  
  75. glColor3f(0.0f, 0.3, 0.3); //new3
  76. glVertex2f(-1.0f, 0.4f);
  77. glVertex2f(0.0f, 0.4f);
  78. glVertex2f(0.0f, 0.5f);
  79. glVertex2f(-1.0f, 0.5f);
  80.  
  81. glColor3f(0.0f, 0.4, 0.4); //new3.1
  82. glVertex2f(0.3f, 0.4f);
  83. glVertex2f(1.0f, 0.4f);
  84. glVertex2f(1.0f, 0.5f);
  85. glVertex2f(0.3f, 0.5f);
  86.  
  87. glColor3f(0.0f, 0.5, 0.5); //new4
  88. glVertex2f(-1.0f, 0.3f);
  89. glVertex2f(-0.1f, 0.3f);
  90. glVertex2f(-0.1f, 0.4f);
  91. glVertex2f(-1.0f, 0.4f);
  92.  
  93. glColor3f(0.0f, 0.5, 0.5); //new4.1
  94. glVertex2f(0.2f, 0.3f);
  95. glVertex2f(1.0f, 0.3f);
  96. glVertex2f(1.0f, 0.4f);
  97. glVertex2f(0.2f, 0.4f);
  98.  
  99. glColor3f(0.0f, 0.6, 0.6); //new5
  100. glVertex2f(-1.0f, 0.2f);
  101. glVertex2f(-0.2f, 0.2f);
  102. glVertex2f(-0.2f, 0.3f);
  103. glVertex2f(-1.0f, 0.3f);
  104.  
  105. glColor3f(0.0f, 0.6, 0.6); //new5.1
  106. glVertex2f(0.1f, 0.2f);
  107. glVertex2f(1.0f, 0.2f);
  108. glVertex2f(1.0f, 0.3f);
  109. glVertex2f(0.1f, 0.3f);
  110.  
  111. glColor3f(0.0f, 0.7, 0.7); //new6
  112. glVertex2f(-1.0f, 0.1f);
  113. glVertex2f(-0.3f, 0.1f);
  114. glVertex2f(-0.3f, 0.2f);
  115. glVertex2f(-1.0f, 0.2f);
  116.  
  117. glColor3f(0.0f, 0.7, 0.7); //new6.1
  118. glVertex2f(0.0f, 0.1f);
  119. glVertex2f(1.0f, 0.1f);
  120. glVertex2f(1.0f, 0.2f);
  121. glVertex2f(0.0f, 0.2f);
  122.  
  123. glColor3f(0.0f, 0.8, 0.8); //new7 00 coordinates
  124. glVertex2f(-1.0f, 0.0f);
  125. glVertex2f(-0.2f, 0.0f);
  126. glVertex2f(-0.2f, 0.1f);
  127. glVertex2f(-1.0f, 0.1f);
  128.  
  129. glColor3f(0.0f, 0.8, 0.8); //new7.1
  130. glVertex2f(0.1f, 0.0f);
  131. glVertex2f(1.0f, 0.0f);
  132. glVertex2f(1.0f, 0.1f);
  133. glVertex2f(0.1f, 0.1f);
  134.  
  135. glColor3f(0.0f, 0.9, 0.9); //new8
  136. glVertex2f(-1.0f, -0.1f);
  137. glVertex2f(-0.1f, -0.1f);
  138. glVertex2f(-0.1f, 0.0f);
  139. glVertex2f(-1.0f, 0.0f);
  140.  
  141. glColor3f(0.0f, 0.9, 0.9); //new8.1
  142. glVertex2f(0.2f, -0.1f);
  143. glVertex2f(1.0f, -0.1f);
  144. glVertex2f(1.0f, 0.0f);
  145. glVertex2f(0.2f, 0.0f);
  146.  
  147. glColor3f(0.0f, 1.0, 1.0); //new9
  148. glVertex2f(-1.0f, -0.2f);
  149. glVertex2f(0.0f, -0.2f);
  150. glVertex2f(0.0f, -0.1f);
  151. glVertex2f(-1.0f, -0.1f);
  152.  
  153. glColor3f(0.0f, 1.0, 1.0); //new9.1
  154. glVertex2f(0.3f, -0.2f);
  155. glVertex2f(1.0f, -0.2f);
  156. glVertex2f(1.0f, -0.1f);
  157. glVertex2f(0.3f, -0.1f);
  158.  
  159. glColor3f(0.0f, 0.9, 0.9); //new10
  160. glVertex2f(-1.0f, -0.3f);
  161. glVertex2f(-0.1f, -0.3f);
  162. glVertex2f(-0.1f, -0.2f);
  163. glVertex2f(-1.0f, -0.2f);
  164.  
  165. glColor3f(0.0f, 0.9, 0.9); //new10
  166. glVertex2f(0.2f, -0.3f);
  167. glVertex2f(1.0f, -0.3f);
  168. glVertex2f(1.0f, -0.2f);
  169. glVertex2f(0.2f, -0.2f);
  170.  
  171. glColor3f(0.0f, 0.8, 0.8); //new11
  172. glVertex2f(-1.0f, -0.4f);
  173. glVertex2f(-0.2f, -0.4f);
  174. glVertex2f(-0.2f, -0.3f);
  175. glVertex2f(-1.0f, -0.3f);
  176.  
  177. glColor3f(0.0f, 0.8, 0.8); //new11.1
  178. glVertex2f(0.1f, -0.4f);
  179. glVertex2f(1.0f, -0.4f);
  180. glVertex2f(1.0f, -0.3f);
  181. glVertex2f(0.1f, -0.3f);
  182.  
  183. glColor3f(0.0f, 0.7, 0.7); //new12
  184. glVertex2f(-1.0f, -0.5f);
  185. glVertex2f(-0.3f, -0.5f);
  186. glVertex2f(-0.3f, -0.4f);
  187. glVertex2f(-1.0f, -0.4f);
  188.  
  189. glColor3f(0.0f, 0.7, 0.7); //new12.1
  190. glVertex2f(0.0f, -0.5f);
  191. glVertex2f(1.0f, -0.5f);
  192. glVertex2f(1.0f, -0.4f);
  193. glVertex2f(0.0f, -0.4f);
  194.  
  195. glColor3f(0.0f, 0.6, 0.6); //new13
  196. glVertex2f(-1.0f, -0.6f);
  197. glVertex2f(-0.2f, -0.6f);
  198. glVertex2f(-0.2f, -0.5f);
  199. glVertex2f(-1.0f, -0.5f);
  200.  
  201. glColor3f(0.0f, 0.6, 0.6); //new13.1
  202. glVertex2f(0.1f, -0.6f);
  203. glVertex2f(1.0f, -0.6f);
  204. glVertex2f(1.0f, -0.5f);
  205. glVertex2f(0.1f, -0.5f);
  206.  
  207. glColor3f(0.0f, 0.5, 0.5); //new14
  208. glVertex2f(-1.0f, -0.7f);
  209. glVertex2f(-0.1f, -0.7f);
  210. glVertex2f(-0.1f, -0.6f);
  211. glVertex2f(-1.0f, -0.6f);
  212.  
  213. glColor3f(0.0f, 0.5, 0.5); //new14.1
  214. glVertex2f(0.2f, -0.7f);
  215. glVertex2f(1.0f, -0.7f);
  216. glVertex2f(1.0f, -0.6f);
  217. glVertex2f(0.2f, -0.6f);
  218.  
  219. glColor3f(0.0f, 0.4, 0.4); //new15
  220. glVertex2f(-1.0f, -0.8f);
  221. glVertex2f(0.0f, -0.8f);
  222. glVertex2f(0.0f, -0.7f);
  223. glVertex2f(-1.0f, -0.7f);
  224.  
  225. glColor3f(0.0f, 0.4, 0.4); //new15.1
  226. glVertex2f(0.3f, -0.8f);
  227. glVertex2f(1.0f, -0.8f);
  228. glVertex2f(1.0f, -0.7f);
  229. glVertex2f(0.3f, -0.7f);
  230.  
  231. glColor3f(0.0f, 0.3, 0.3); //new16
  232. glVertex2f(-1.0f, -0.9f);
  233. glVertex2f(-0.1f, -0.9f);
  234. glVertex2f(-0.1f, -0.8f);
  235. glVertex2f(-1.0f, -0.8f);
  236.  
  237. glColor3f(0.0f, 0.3, 0.3); //new16.1
  238. glVertex2f(0.2f, -0.9f);
  239. glVertex2f(1.0f, -0.9f);
  240. glVertex2f(1.0f, -0.8f);
  241. glVertex2f(0.2f, -0.8f);
  242.  
  243. glColor3f(0.0f, 0.2, 0.2); //new17
  244. glVertex2f(-1.0f, -1.0f);
  245. glVertex2f(-0.2f, -1.0f);
  246. glVertex2f(-0.2f, -0.9f);
  247. glVertex2f(-1.0f, -0.9f);
  248.  
  249. glColor3f(0.0f, 0.2, 0.2); //new17.1
  250. glVertex2f(0.1f, -1.0f);
  251. glVertex2f(1.0f, -1.0f);
  252. glVertex2f(1.0f, -0.9f);
  253. glVertex2f(0.1f, -0.9f);
  254.  
  255. glEnd();
  256.  
  257.  
  258.  
  259. glutPostRedisplay();
  260.  
  261.  
  262. glFlush(); // Render now
  263. }
  264.  
  265. void drawText(const char *text, int length, int x, int y) {
  266. glMatrixMode(GL_PROJECTION);
  267. double *matrix = new double[16];
  268. glGetDoublev(GL_PROJECTION_MATRIX, matrix);
  269. glLoadIdentity();
  270. glOrtho(0, 800, 0, 600, -5, 5);
  271. glMatrixMode(GL_MODELVIEW);
  272. glLoadIdentity();
  273. glPushMatrix();
  274. glLoadIdentity();
  275. glRasterPos2i(x, y);
  276. for (int k = 0; k < length; k++) {
  277. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, (int)text[k]);
  278. }
  279. glPopMatrix();
  280. glMatrixMode(GL_PROJECTION);
  281. glLoadMatrixd(matrix);
  282. glMatrixMode(GL_MODELVIEW);
  283. }
  284.  
  285.  
  286.  
  287. float index = 0.8f;
  288.  
  289. void TimerFunction(int value)
  290. {
  291. square_y1 -= 0.002;
  292. square_y2 -= 0.002;
  293.  
  294. // Control time
  295. count_timer++;
  296.  
  297. for (i = 0; i < 16; i++) {
  298. if (count_timer >= timerC[i] & count_timer < timerC[i + 1]) {
  299. if (square_x1 < x_left[i]) {
  300. printf("Left Collision.\n");
  301. printf("Final score : %d", count_timer);
  302. getchar();
  303. exit(0);
  304. }
  305. else if (square_x2 > x_right[i]) {
  306. printf("Right collision.\n");
  307. printf("Final score : %d", count_timer);
  308. getchar();
  309. exit(0);
  310. }
  311. }
  312. }
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319. // Actually move the square
  320. index -= ystep;
  321.  
  322.  
  323. //Redraw the scene with new coordinates
  324. glutPostRedisplay();
  325.  
  326. glutTimerFunc(50, TimerFunction, 1); //The rectangle moves every 33 seconds..1 is for enable the function
  327. }
  328.  
  329.  
  330. void display() {
  331.  
  332. velo += 1.7;
  333.  
  334. //glClear(GL_COLOR_BUFFER_BIT);
  335. background();
  336.  
  337. glColor3f(0.0, 0.5, 0.5); //new17
  338.  
  339.  
  340. glRectd(square_x1, index, square_x2, index + 0.06);
  341.  
  342. //index -= 0.00007;
  343.  
  344. std::string text;
  345. text = "Dancing Lines";
  346. glColor3f(0, 0.5, 0.2);
  347. drawText(text.data(), text.size(), 490, 580);
  348.  
  349. //glViewport(0, -game_h / 2 + velo, game_w, game_h);
  350. glutSwapBuffers();
  351. //glFlush(); // Render now
  352. }
  353.  
  354.  
  355. void SpecialKeys(int key, int x, int y) {
  356.  
  357. if (key == GLUT_KEY_LEFT) {
  358. square_x1 -= 0.10;
  359. square_x2 -= 0.10;
  360.  
  361. }
  362.  
  363. if (key == GLUT_KEY_RIGHT) {
  364. square_x1 += 0.10;
  365. square_x2 += 0.10;
  366.  
  367. }
  368.  
  369.  
  370.  
  371. glutPostRedisplay();
  372. }
  373.  
  374. float epsilon = 0.01;
  375.  
  376.  
  377.  
  378. void timer_callback(int) {
  379. glutPostRedisplay();
  380. glutTimerFunc(1000 / FPS, timer_callback, 0);
  381. }
  382.  
  383.  
  384. // Main function: GLUT runs as a Console Application
  385. int main(int argc, char** argv) {
  386. glutInit(&argc, argv); // Initialize GLUT
  387. glutInitWindowSize(game_w, game_h); // Set the initial Window's width and height
  388. glutInitWindowPosition(50, 50); // Position the initial Window's top-left corner
  389. glutCreateWindow("Marcin & Srison"); // Create window with the given title
  390. glutSpecialFunc(SpecialKeys);
  391. glutTimerFunc(100, TimerFunction, 1);
  392. glutDisplayFunc(display); // Register callback handler for window re-paint event
  393. //initGL(); // Our own OpenGL initialization
  394. glutMainLoop(); // Enter infinitely event-processing loop
  395. return 0;
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement