Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 38.99 KB | None | 0 0
  1. #include <Windows.h>
  2. #include "GameLogic\Classes\Chessboard.h"
  3. #include "Piece.h"
  4. #include <SOIL.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <iostream>
  8. #include <vector>
  9. #define GLUT_DISABLE_ATEXIT_HACK
  10. #include <GL/gl.h>
  11. #include <gl/glu.h>
  12. #include <GL/glut.h>
  13. #include "Debug.h"
  14. #include <sstream>
  15. #include <ctime>
  16. #include <fstream>
  17. #include <string>
  18.  
  19. const int WIDTH = 1280;
  20. const int HEIGHT = 720;
  21.  
  22. const float SQU = 14.98f;
  23.  
  24. unsigned int fps = 1000/60;
  25. time_t startTime;
  26. time_t endTime;
  27. double whiteTime;
  28. double blackTime;
  29. double tempTime;
  30. bool gameOver = false;
  31. std::vector<std::string> highscoresList;
  32.  
  33. int k = 0;
  34. int l = 0;
  35. int nr = 0;
  36. char out1[21];
  37. char out2[21];
  38. char out3[21] = "HIGHSCORES:";
  39. char out4[21] = "PLAYER 1:";
  40. char out5[21] = "PLAYER 2:";
  41. float theta1 = 45.0f;
  42. float theta2 = 0.0f;
  43. float theta3 = 1.0f;
  44. float widok = 0.0f;
  45.  
  46.  
  47. int button_x, button_y;
  48.  
  49.  
  50. const GLdouble left = - 10.0;
  51. const GLdouble right = 10.0;
  52. const GLdouble bottom = - 10.0;
  53. const GLdouble top = 10.0;
  54.  
  55. GLfloat WhitePieceMaterialAmbDiff[] = {0.845, 0.845, 0.845, 1.0};
  56. GLfloat BlackPieceMaterialAmbDiff[] = {0.445, 0.445, 0.845, 1.0};
  57. GLfloat PieceMaterialSpecular[] = {1.0, 1.0, 1.0, 1.0};
  58. GLfloat PieceMaterialShininess[] = {78.6};
  59.  
  60.  
  61. GLfloat lightAmb[] = { 0.0, 0.0, 0.0, 1.0 };
  62. GLfloat lightDif[] = { 1.0, 1.0, 1.0, 1.0 };
  63. GLfloat lightSpec[] = { 1.0, 1.0, 1.0, 1.0 };
  64.  
  65. GLfloat BoardGlassMaterialAmbDiff[] = {0.576, 0.373, 0.0, 1.0};
  66. GLfloat BoardGlassMaterialSpecular[] = {1.0, 1.0, 1.0, 1.0};
  67. GLfloat BoardGlassMaterialShininess[] = {120.0};
  68.  
  69. GLfloat BoardWoodMaterialAmbDiff[] = {0.416, 0.416, 0.416, 1.0};
  70. GLfloat BoardWoodMaterialSpecular[] = {1.0, 1.0, 1.0, 1.0};
  71. GLfloat BoardWoodMaterialShininess[] = {70.6};
  72.  
  73. GLfloat TableWoodMaterialAmbDiff[] = {0.3412, 0.8824, 0.3412, 1.0};
  74. GLfloat TableWoodMaterialSpecular[] = {0.3500, 0.3500, 0.3500, 1.0};
  75. GLfloat TableWoodMaterialShininess[] = {32.0};
  76.  
  77. GLuint tex_2d[5];
  78. GLuint tex_2d_room[6];
  79. GLfloat vel = 3;
  80. GLfloat vel1 = 3;
  81. GLfloat kat = 0, katOff;
  82. GLfloat rotX = 1, rotY = 1, rotZ = 1;
  83. GLuint lista;
  84. GLUquadricObj *room;
  85.  
  86. GameLogic::Chessboard gameboard;
  87. GameLogic::Chessboard gameboard2;
  88. std::pair<int, int> selectedSquare;
  89. std::pair<char, int> sourceMove = {0, 0};
  90. std::pair<char, int> destinationMove = {0, 0};
  91. std::list<std::pair<char, int>> greenMoves;
  92. bool drawGreen = false;
  93. bool up = false;
  94. bool bRight = false;
  95. bool once = true;
  96. bool once2 = true;
  97. bool anim = false;
  98. bool lighRotateOff = false;
  99. bool highscores = true;
  100. bool nick1 = true;
  101. bool nick2 = false;
  102. int button_state = GLUT_UP;
  103. std::string white = "TURA GRACZA ";
  104. std::string black = "TURA GRACZA ";
  105. std::string s;
  106.  
  107. std::string nick1S = "";
  108. std::string nick2S = "";
  109.  
  110. Piece Bishop, King, Knight, Pawn, Queen, Rook;
  111. Piece Board;
  112. Piece Table;
  113.  
  114. double string_to_double( const std::string& s )
  115. {
  116. std::istringstream i(s);
  117. double x;
  118. if (!(i >> x))
  119. return 0;
  120. return x;
  121. }
  122.  
  123. std::string double_to_string(double d)
  124. {
  125. std::ostringstream strs;
  126. strs << d;
  127. std::string str = strs.str();
  128. return str;
  129. }
  130.  
  131.  
  132. void AddHighScore(const std::string fileName, double seconds, std::string userName);
  133. std::vector<std::string> ShowHighScores(const std::string fileName);
  134. void drawPiece(Piece &p);
  135. void drawChessboard();
  136. void drawPossibleMoves(std::list<std::pair<char, int>> moves);
  137. void drawBoard();
  138. void display();
  139. void reshape(int w, int h);
  140. void klawiatura(unsigned char key, int x, int y);
  141. void special(int key, int x, int y);
  142. void MouseButton( int button, int state, int x, int y );
  143. void MouseMotion( int x, int y );
  144. void fps60(int val);
  145. void loadTexture(const char* path, GLuint &tex);
  146. void setupLighting();
  147. void setupScene();
  148. void loadObjects();
  149. void initList();
  150. void drawRoom();
  151. void drawTable();
  152.  
  153. int main(int argc, char *argv[]) {
  154. gameboard = GameLogic::Chessboard();
  155. loadObjects();
  156. glutInit(&argc, argv);
  157. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  158. glutInitWindowSize(WIDTH, HEIGHT);
  159. glutCreateWindow(argv[0]);
  160. glutPositionWindow(400, 200);
  161. setupScene();
  162. initList();
  163. glutDisplayFunc(display);
  164. glutReshapeFunc(reshape);
  165. glutKeyboardFunc(klawiatura);
  166. glutSpecialFunc(special);
  167. glutMouseFunc(MouseButton);
  168. glutMotionFunc(MouseMotion);
  169. glutTimerFunc(100, fps60, 0);
  170.  
  171. //PlaySound(TEXT("mjuzik.wav"), NULL, SND_ASYNC|SND_FILENAME|SND_LOOP);
  172. glutMainLoop();
  173.  
  174. return 0;
  175. }
  176.  
  177.  
  178.  
  179.  
  180. void drawChessboard(){
  181. GameLogic::AbstractPiece* piece;
  182.  
  183. for(int i = 0; i < 8; i++){
  184. for(int j = 0; j < 8; j++){
  185. if(anim && (sourceMove.first == GameLogic::Chessboard::fileLetterToArrayIndex(i)) && (sourceMove.second == GameLogic::Chessboard::rankNumberToArrayIndex(j))){
  186. continue;
  187. }
  188. if((piece = gameboard.getPieceAt(GameLogic::Chessboard::arrayIndexToFileLetter(i), GameLogic::Chessboard::arrayIndexToRankNumber(j))) != nullptr){
  189. if(piece->getOwner() == GameLogic::Color::WHITE){
  190. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, WhitePieceMaterialAmbDiff);
  191. }else{
  192. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlackPieceMaterialAmbDiff);
  193. }
  194. switch (piece -> type()){
  195. case GameLogic::PieceType::PAWN:
  196. glPushMatrix();
  197. glTranslatef(SQU * i, 0, -SQU * j);
  198. drawPiece(Pawn);
  199. glPopMatrix();
  200. break;
  201. case GameLogic::PieceType::KNIGHT:
  202. glPushMatrix();
  203. glTranslatef(SQU * i, 0, -SQU * j);
  204. drawPiece(Knight);
  205. glPopMatrix();
  206. break;
  207. case GameLogic::PieceType::BISHOP:
  208. glPushMatrix();
  209. glTranslatef(SQU * i, 0, -SQU * j);
  210. drawPiece(Bishop);
  211. glPopMatrix();
  212. break;
  213. case GameLogic::PieceType::ROOK:
  214. glPushMatrix();
  215. glTranslatef(SQU * i, 0, -SQU * j);
  216. drawPiece(Rook);
  217. glPopMatrix();
  218. break;
  219. case GameLogic::PieceType::QUEEN:
  220. glPushMatrix();
  221. glTranslatef(SQU * i, 0, -SQU * j);
  222. drawPiece(Queen);
  223. glPopMatrix();
  224. break;
  225. case GameLogic::PieceType::KING:
  226. glPushMatrix();
  227. glTranslatef(SQU * i, 0, -SQU * j);
  228. drawPiece(King);
  229. glPopMatrix();;
  230. break;
  231. }
  232. }
  233. }
  234. }
  235. }
  236.  
  237. void drawPossibleMoves(std::list<std::pair<char, int>> moves){
  238. int x;
  239. for(std::pair<char, int> square : moves){
  240. switch (square.first)
  241. {
  242. case 'A':
  243. x = 0;
  244. break;
  245. case 'B':
  246. x = 1;
  247. break;
  248. case 'C':
  249. x = 2;
  250. break;
  251. case 'D':
  252. x = 3;
  253. break;
  254. case 'E':
  255. x = 4;
  256. break;
  257. case 'F':
  258. x = 5;
  259. break;
  260. case 'G':
  261. x = 6;
  262. break;
  263. case 'H':
  264. x = 7;
  265. break;
  266. }
  267. glPushMatrix();
  268. glBegin(GL_QUADS);
  269. glVertex3f((x*SQU)+(SQU/2.0f), 0.1, ((square.second - 1)* -SQU)+(SQU/2.0f));
  270. glVertex3f((x*SQU)+(SQU/2.0f), 0.1, ((square.second - 1)* -SQU)-(SQU/2.0f));
  271. glVertex3f((x*SQU)-(SQU/2.0f), 0.1, ((square.second - 1)* -SQU)-(SQU/2.0f));
  272. glVertex3f((x*SQU)-(SQU/2.0f), 0.1, ((square.second - 1)* -SQU)+(SQU/2.0f));
  273. glEnd();
  274. glPopMatrix();
  275. }
  276. }
  277.  
  278. void drawBoard()
  279. {
  280. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BoardWoodMaterialAmbDiff);
  281. glMaterialfv(GL_FRONT, GL_SPECULAR, BoardGlassMaterialSpecular);
  282. glMaterialfv(GL_FRONT, GL_SHININESS, BoardGlassMaterialShininess);
  283. glBindTexture(GL_TEXTURE_2D, tex_2d[2]);
  284. glBegin(GL_QUADS);
  285. glNormal3f(Board.vn[0].x, Board.vn[0].y, Board.vn[0].z);
  286. glTexCoord2f(Board.vt[0].x, Board.vt[0].y);
  287. glVertex3f(Board.v[0].x, Board.v[0].y, Board.v[0].z);
  288.  
  289. glNormal3f(Board.vn[1].x, Board.vn[1].y, Board.vn[1].z);
  290. glTexCoord2f(Board.vt[1].x, Board.vt[1].y);
  291. glVertex3f(Board.v[1].x, Board.v[1].y, Board.v[1].z);
  292.  
  293. glNormal3f(Board.vn[2].x, Board.vn[2].y, Board.vn[2].z);
  294. glTexCoord2f(Board.vt[2].x, Board.vt[2].y);
  295. glVertex3f(Board.v[2].x, Board.v[2].y, Board.v[2].z);
  296.  
  297. glNormal3f(Board.vn[3].x, Board.vn[3].y, Board.vn[3].z);
  298. glTexCoord2f(Board.vt[3].x, Board.vt[3].y);
  299. glVertex3f(Board.v[3].x, Board.v[3].y, Board.v[3].z);
  300. glEnd();
  301.  
  302. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BoardGlassMaterialAmbDiff);
  303. glMaterialfv(GL_FRONT, GL_SPECULAR, BoardWoodMaterialSpecular);
  304. glMaterialfv(GL_FRONT, GL_SHININESS, BoardWoodMaterialShininess);
  305. glBindTexture(GL_TEXTURE_2D, tex_2d[1]);
  306. glBegin(GL_QUADS);
  307. for (unsigned int i = 4; i < Board.v.size(); i += 4)
  308. {
  309. glNormal3f(Board.vn[i].x, Board.vn[i].y, Board.vn[i].z);
  310. glTexCoord2f(Board.vt[i].x, Board.vt[i].y);
  311. glVertex3f(Board.v[i].x, Board.v[i].y, Board.v[i].z);
  312.  
  313. glNormal3f(Board.vn[i+1].x, Board.vn[i+1].y, Board.vn[i+1].z);
  314. glTexCoord2f(Board.vt[i+1].x, Board.vt[i+1].y);
  315. glVertex3f(Board.v[i+1].x, Board.v[i+1].y, Board.v[i+1].z);
  316.  
  317. glNormal3f(Board.vn[i+2].x, Board.vn[i+2].y, Board.vn[i+2].z);
  318. glTexCoord2f(Board.vt[i+2].x, Board.vt[i+2].y);
  319. glVertex3f(Board.v[i+2].x, Board.v[i+2].y, Board.v[i+2].z);
  320.  
  321. glNormal3f(Board.vn[i+3].x, Board.vn[i+3].y, Board.vn[i+3].z);
  322. glTexCoord2f(Board.vt[i+3].x, Board.vt[i+3].y);
  323. glVertex3f(Board.v[i+3].x, Board.v[i+3].y, Board.v[i+3].z);
  324.  
  325. }
  326. glEnd();
  327. }
  328.  
  329. void drawTable()
  330. {
  331. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, TableWoodMaterialAmbDiff);
  332. glMaterialfv(GL_FRONT, GL_SPECULAR, TableWoodMaterialSpecular);
  333. glMaterialfv(GL_FRONT, GL_SHININESS, TableWoodMaterialShininess);
  334. glBindTexture(GL_TEXTURE_2D, tex_2d[3]);
  335. glBegin(GL_QUADS);
  336. for (unsigned int i = 4; i < Table.v.size(); i += 4)
  337. {
  338. glNormal3f(Table.vn[i].x, Table.vn[i].y, Table.vn[i].z);
  339. glTexCoord2f(Table.vt[i].x, Table.vt[i].y);
  340. glVertex3f(Table.v[i].x, Table.v[i].y, Table.v[i].z);
  341.  
  342. glNormal3f(Table.vn[i+1].x, Table.vn[i+1].y, Table.vn[i+1].z);
  343. glTexCoord2f(Table.vt[i+1].x, Table.vt[i+1].y);
  344. glVertex3f(Table.v[i+1].x, Table.v[i+1].y, Table.v[i+1].z);
  345.  
  346. glNormal3f(Table.vn[i+2].x, Table.vn[i+2].y, Table.vn[i+2].z);
  347. glTexCoord2f(Table.vt[i+2].x, Table.vt[i+2].y);
  348. glVertex3f(Table.v[i+2].x, Table.v[i+2].y, Table.v[i+2].z);
  349.  
  350. glNormal3f(Table.vn[i+3].x, Table.vn[i+3].y, Table.vn[i+3].z);
  351. glTexCoord2f(Table.vt[i+3].x, Table.vt[i+3].y);
  352. glVertex3f(Table.v[i+3].x, Table.v[i+3].y, Table.v[i+3].z);
  353.  
  354. }
  355. glEnd();
  356. }
  357.  
  358. void display() {
  359. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  360. glMatrixMode(GL_MODELVIEW);
  361.  
  362. glLoadIdentity();
  363. glTranslated(0, 0, -150);
  364.  
  365. if((nick1 || nick2))
  366. {
  367. glPushMatrix();
  368. glDisable(GL_LIGHTING);
  369.  
  370. glBegin(GL_QUADS);
  371. glColor3f(1.0, 0.5, 1.0);
  372.  
  373. glVertex3f(-700, -700, 100); //przod
  374. glVertex3f(-700, 700, 100);
  375. glVertex3f(700, 700, 100);
  376. glVertex3f(700, -700, 100);
  377. glEnd();
  378.  
  379. glTranslated(0, 0, 110);
  380.  
  381. glColor3f(0, 0, 0);
  382. for (int i = 0; i < 21; ++i)
  383. {
  384. glRasterPos2d(-35 + 1*i, 10);
  385. glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, out4[i]);
  386. }
  387. for (int i = 0; i < 21; ++i)
  388. {
  389. glRasterPos2d(-35 + 1*i, -10);
  390. glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, out5[i]);
  391. }
  392. k = 0;
  393. for (std::string::iterator i = nick1S.begin(); i != nick1S.end(); ++i)
  394. {
  395. k++;
  396. glRasterPos2d(-15 + 1*k, 10);
  397. char c = *i;
  398. glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
  399. }
  400. k = 0;
  401. for (std::string::iterator i = nick2S.begin(); i != nick2S.end(); ++i)
  402. {
  403. k++;
  404. glRasterPos2d(-15 + 1*k, -10);
  405. char c = *i;
  406. glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
  407. }
  408.  
  409. glEnable(GL_LIGHTING);
  410. glPopMatrix();
  411. }
  412. else if(highscores)
  413. {
  414. if(once2)
  415. {
  416. once2 = false;
  417. highscoresList = ShowHighScores("highscores.txt");
  418. }
  419. glPushMatrix();
  420. glDisable(GL_LIGHTING);
  421.  
  422. glBegin(GL_QUADS);
  423. glColor3f(1.0, 0.0, 1.0);
  424.  
  425. glVertex3f(-700, -700, 100); //przod
  426. glVertex3f(-700, 700, 100);
  427. glVertex3f(700, 700, 100);
  428. glVertex3f(700, -700, 100);
  429. glEnd();
  430.  
  431. glTranslated(0, 0, 110);
  432.  
  433. glColor3f(0, 0, 0);
  434. for (int i = 0; i < 21; ++i)
  435. {
  436. glRasterPos2d(-15 + 3*i, 20);
  437. glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, out3[i]);
  438. }
  439. if(highscoresList.empty())
  440. {
  441. printf("empty");
  442. }
  443. printf("%d",highscoresList.size());
  444. l = 20;
  445. nr = 0;
  446. for (std::vector<std::string>::iterator it = highscoresList.begin() ; it != highscoresList.end(); ++it)
  447. {
  448. l -= 5;
  449. k = 0;
  450. nr++;
  451. char c2 = nr + '0';
  452. glRasterPos2d(-16, l);
  453. glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c2);
  454. for(std::string::iterator it2 = (*it).begin(); it2 != (*it).end(); ++it2)
  455. {
  456. k++;
  457. glRasterPos2d(-15 + 1*k, l);
  458. char c = *it2;
  459. glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
  460. }
  461. }
  462.  
  463. glEnable(GL_LIGHTING);
  464. glPopMatrix();
  465. }
  466. else
  467. {
  468. if(once){
  469. once = false;
  470. startTime = time(nullptr);
  471. }
  472. k = 0;
  473. glPushMatrix();
  474. glColor3f(1, 1, 1);
  475. glDisable(GL_LIGHTING);
  476. sprintf(out1, "Stracone figury: %d", gameboard.getPiecesTaken(GameLogic::Color::WHITE));
  477. sprintf(out2, "Stracone figury: %d", gameboard.getPiecesTaken(GameLogic::Color::BLACK));
  478. for (int i = 0; i < 21; ++i)
  479. {
  480. k++;
  481. glRasterPos2d(-130 + 3*k, 60);
  482. glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, out1[i]);
  483. }
  484. k = 0;
  485. for (int i = 0; i < 21; ++i)
  486. {
  487. k++;
  488. glRasterPos2d(60 + 3*k, 60);
  489. glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, out2[i]);
  490. }
  491. glEnable(GL_LIGHTING);
  492. glPopMatrix();
  493. k = 0;
  494. glPushMatrix();
  495. glDisable(GL_LIGHTING);
  496. if (gameboard.getPlayerToMove() == GameLogic::Color::WHITE)
  497. {
  498. s = white + nick1S;
  499. glColor3f(1, 1, 1);
  500. }
  501. else
  502. {
  503. s = black + nick2S;
  504. glColor3f(1, 1, 1);
  505. k = 45;
  506. }
  507. for (std::string::iterator i = s.begin(); i != s.end(); ++i)
  508. {
  509. k++;
  510. glRasterPos2d(-130 + 4*k, 80);
  511. char c = *i;
  512. glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
  513. }
  514. glEnable(GL_LIGHTING);
  515. glPopMatrix();
  516.  
  517. glRotatef(theta1, 1.0f, 0.0f, 0.0f);
  518. glRotatef(theta2, 0.0f, 1.0f, 0.0f);
  519. glRotatef(widok, 0.0f, 1.0f, 0.0f);
  520. glScalef(theta3, theta3, theta3);
  521.  
  522. drawRoom();
  523.  
  524. glPushMatrix();
  525. glScaled(0.3, 0.3, 0.3);
  526. glTranslatef(0, -805, 0);
  527. drawTable();
  528. glPopMatrix();
  529.  
  530. glPushMatrix();
  531. glRotatef(kat, rotX, rotY, rotZ);
  532. setupLighting();
  533. glPopMatrix();
  534.  
  535. glPushMatrix();
  536. glScaled(1.2, 1, 1.2);
  537. drawBoard();
  538. glPopMatrix();
  539.  
  540. glTranslatef(-52.5, 0, 52.5);
  541.  
  542.  
  543. drawChessboard();
  544.  
  545. glPushMatrix();
  546. glDisable(GL_TEXTURE_2D);
  547. glDisable(GL_LIGHTING);
  548. if (gameboard.getPlayerToMove() == GameLogic::Color::WHITE)
  549. {
  550. glColor3f(0, 0.0f, 1.0f);
  551. if (widok == 360.f)
  552. widok = 0.0f;
  553. if (widok > 0.0f)
  554. widok += 8.0f;
  555. }
  556. else
  557. {
  558. if (widok < 180)
  559. widok += 8.0f;
  560. glColor3f(1.0f, 0.0f, 0.0f);
  561. }
  562. glBegin(GL_QUADS);
  563. glVertex3f((selectedSquare.first*SQU)+(SQU/2.0f), 0.1, (-selectedSquare.second*SQU)+(SQU/2.0f));
  564. glVertex3f((selectedSquare.first*SQU)+(SQU/2.0f), 0.1, (-selectedSquare.second*SQU)-(SQU/2.0f));
  565. glVertex3f((selectedSquare.first*SQU)-(SQU/2.0f), 0.1, (-selectedSquare.second*SQU)-(SQU/2.0f));
  566. glVertex3f((selectedSquare.first*SQU)-(SQU/2.0f), 0.1, (-selectedSquare.second*SQU)+(SQU/2.0f));
  567. glEnd();
  568. glEnable(GL_TEXTURE_2D);
  569. glEnable(GL_LIGHTING);
  570. glPopMatrix();
  571.  
  572. glPushMatrix();
  573. glDisable(GL_TEXTURE_2D);
  574. glDisable(GL_LIGHTING);
  575. glColor3f(0.0f, 1.0f, 0.0f);
  576. if(drawGreen){
  577. drawPossibleMoves(greenMoves);
  578. }
  579. glEnable(GL_TEXTURE_2D);
  580. glEnable(GL_LIGHTING);
  581. glPopMatrix();
  582.  
  583. gluDeleteQuadric(room);
  584. }
  585. glutSwapBuffers();
  586. }
  587.  
  588. void reshape(int w, int h) {
  589. glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  590. glMatrixMode(GL_PROJECTION);
  591. glLoadIdentity();
  592. //glOrtho(-w, w, -h, h, -700, 700);
  593. gluPerspective(60, (float)w / h, 1, 1870);
  594. glMatrixMode(GL_MODELVIEW);
  595. glLoadIdentity();
  596. }
  597.  
  598. void klawiatura(unsigned char key, int x, int y)
  599. {
  600. if(nick1)
  601. {
  602. nick1S.push_back(key);
  603. }
  604. if(nick2)
  605. {
  606. nick2S.push_back(key);
  607. }
  608. switch (key)
  609. {
  610. case 8:
  611. if(nick1)
  612. {
  613. nick1S.pop_back();
  614. }
  615. if(nick2)
  616. {
  617. nick2S.pop_back();
  618. }
  619. break;
  620. case 'i':
  621. if(!gameOver){
  622. highscores = !highscores;
  623. }
  624.  
  625. break;
  626. case 27:
  627. case 'q':
  628. if(!(nick1 || nick2))
  629. {
  630. PlaySound(NULL, NULL, NULL);
  631. exit(0);
  632. }
  633. break;
  634. case 'x':
  635. rotX = !rotX;
  636. break;
  637. case 'y':
  638. rotY = !rotY;
  639. break;
  640. case 'z':
  641. rotZ = !rotZ;
  642. break;
  643. case 's':
  644. lighRotateOff = !lighRotateOff;
  645. break;
  646. case 1:
  647.  
  648. case '+':
  649.  
  650. vel1 += 0.5;
  651. system("cls");
  652. break;
  653. case '-':
  654. if ((vel1 -= 0.5) <= 0)
  655. vel1 = 0;
  656. system("cls");
  657. break;
  658. case 'j':
  659. if ((vel -= 1) <= 0)
  660. {
  661. vel = 0;
  662. }
  663. system("cls");
  664. break;
  665. case 'k':
  666. vel += 1;
  667. system("cls");
  668. break;
  669. case '\\':
  670. gameOver = true;
  671. highscores = true;
  672. if(gameboard.getPlayerToMove() == GameLogic::Color::BLACK){
  673. tempTime = whiteTime;
  674. AddHighScore("highscores.txt", tempTime, nick1S);
  675. }else{
  676. tempTime = blackTime;
  677. AddHighScore("highscores.txt", tempTime, nick2S);
  678. }
  679. highscoresList = ShowHighScores("highscores.txt");
  680. break;
  681.  
  682. case 32: //SPACE
  683. if(!(nick1 || nick2))
  684. {
  685. if (drawGreen == false)
  686. {
  687. drawGreen = true;
  688. }
  689. else
  690. {
  691. drawGreen = false;
  692. }
  693. greenMoves = gameboard.getListOfPossibleMoves(GameLogic::Chessboard::arrayIndexToFileLetter(selectedSquare.first), GameLogic::Chessboard::arrayIndexToRankNumber(selectedSquare.second));
  694. }
  695. break;
  696. case 13: //ENTER
  697. if(!(nick1 || nick2))
  698. {
  699. if (drawGreen == false)
  700. {
  701. drawGreen = true;
  702. }
  703. else
  704. {
  705. drawGreen = false;
  706. }
  707. greenMoves = gameboard.getListOfPossibleMoves(GameLogic::Chessboard::arrayIndexToFileLetter(selectedSquare.first), GameLogic::Chessboard::arrayIndexToRankNumber(selectedSquare.second));
  708. if (up == false)
  709. {
  710. up = true;
  711. anim = false;
  712. sourceMove.first = GameLogic::Chessboard::arrayIndexToFileLetter(selectedSquare.first);
  713. sourceMove.second = GameLogic::Chessboard::arrayIndexToRankNumber(selectedSquare.second);
  714. std::cout<<sourceMove.first<<" "<<sourceMove.second<<"\n";
  715. }
  716. else
  717. {
  718.  
  719. up = false;
  720.  
  721. destinationMove.first = GameLogic::Chessboard::arrayIndexToFileLetter(selectedSquare.first);
  722. destinationMove.second = GameLogic::Chessboard::arrayIndexToRankNumber(selectedSquare.second);
  723.  
  724. std::cout<<destinationMove.first<<" "<<destinationMove.second<<"\n";
  725. gameboard2 = gameboard;
  726. bool tmp = gameboard.tryMove(sourceMove, destinationMove);
  727. if(tmp){
  728. endTime = time(nullptr);
  729. if(gameboard.getPlayerToMove() == GameLogic::Color::BLACK){
  730. whiteTime += difftime(endTime, startTime);
  731. startTime = time(nullptr);
  732. }else{
  733. blackTime += difftime(endTime, startTime);
  734. startTime = time(nullptr);
  735. }
  736.  
  737. }
  738. std::cout<<"tryMove: "<<tmp<<"\n";
  739. std::cout<<"white: "<<whiteTime<<"\n";
  740. std::cout<<"black: "<<blackTime<<"\n";
  741. }
  742. }
  743. if(nick2)
  744. {
  745. nick2 = false;
  746. }
  747. if(nick1)
  748. {
  749. nick1 = false;
  750. nick2 = true;
  751. }
  752. break;
  753. }
  754. }
  755.  
  756. void special(int key, int x, int y)
  757. {
  758.  
  759. if (gameboard.getPlayerToMove() == GameLogic::Color::WHITE)
  760. {
  761. switch(key)
  762. {
  763. case GLUT_KEY_UP :
  764. if (selectedSquare.second++ >= 7)
  765. {
  766. selectedSquare.second = 7;
  767. }
  768. break;
  769. case GLUT_KEY_DOWN :
  770. if (selectedSquare.second-- <= 0)
  771. {
  772. selectedSquare.second = 0;
  773. }
  774. break;
  775. case GLUT_KEY_LEFT :
  776. if (selectedSquare.first-- <= 0)
  777. {
  778. selectedSquare.first = 0;
  779. }
  780. break;
  781. case GLUT_KEY_RIGHT :
  782. if (selectedSquare.first++ >= 7 )
  783. {
  784. selectedSquare.first = 7;
  785. }
  786. break;
  787. }
  788. }
  789. else
  790. {
  791. switch(key)
  792. {
  793. case GLUT_KEY_DOWN :
  794. if (selectedSquare.second++ >= 7)
  795. {
  796. selectedSquare.second = 7;
  797. }
  798. break;
  799. case GLUT_KEY_UP :
  800. if (selectedSquare.second-- <= 0)
  801. {
  802. selectedSquare.second = 0;
  803. }
  804. break;
  805. case GLUT_KEY_RIGHT :
  806. if (selectedSquare.first-- <= 0)
  807. {
  808. selectedSquare.first = 0;
  809. }
  810. break;
  811. case GLUT_KEY_LEFT :
  812. if (selectedSquare.first++ >= 7 )
  813. {
  814. selectedSquare.first = 7;
  815. }
  816. break;
  817. }
  818. }
  819. }
  820.  
  821. void MouseButton( int button, int state, int x, int y )
  822. {
  823. if( button == GLUT_LEFT_BUTTON )
  824. {
  825. bRight = false;
  826. button_state = state;
  827. if( state == GLUT_DOWN )
  828. {
  829. button_x = x;
  830. button_y = y;
  831. }
  832. }
  833. if( button == GLUT_RIGHT_BUTTON )
  834. {
  835. bRight = true;
  836. button_state = state;
  837. if( state == GLUT_DOWN )
  838. {
  839. button_x = x;
  840. button_y = y;
  841. }
  842. }
  843. }
  844.  
  845. void MouseMotion( int x, int y )
  846. {
  847. if( (button_state == GLUT_DOWN) && !(bRight) )
  848. {
  849. theta2 += vel *( right - left ) / glutGet( GLUT_WINDOW_WIDTH ) *( x - button_x );
  850. button_x = x;
  851. theta1 += vel *( top - bottom ) / glutGet( GLUT_WINDOW_HEIGHT ) *( button_y - y );
  852. button_y = y;
  853. }
  854. if( (button_state == GLUT_DOWN) && (bRight) )
  855. {
  856. theta3 += vel1 *( right - left ) / glutGet( GLUT_WINDOW_WIDTH ) *( x - button_x );
  857. button_x = x;
  858. }
  859. }
  860.  
  861. void fps60(int val) {
  862. if(theta1 >= 360) theta1 -= 360;
  863. if(theta2 >= 360) theta2 -= 360;
  864. if(theta3 <= 0.2f) theta3 += 0.2f;
  865. if (kat >= 360) kat -= 360;
  866. if(!lighRotateOff) kat = kat + 1;
  867. glutPostRedisplay();
  868. glutTimerFunc(fps, fps60, 0);
  869. }
  870.  
  871. void loadTexture(const char* path, GLuint &tex)
  872. {
  873. glBindTexture(GL_TEXTURE_2D, tex);
  874. tex = SOIL_load_OGL_texture
  875. (
  876. path,
  877. SOIL_LOAD_AUTO,
  878. SOIL_CREATE_NEW_ID,
  879. SOIL_FLAG_INVERT_Y
  880. );
  881. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  882. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  883. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  884. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  885. }
  886. void setupLighting()
  887. {
  888. GLfloat light_position[] = { 0, 100, 0, 1.0 };
  889. glLightfv(GL_LIGHT1, GL_POSITION, light_position);
  890. glLightfv(GL_LIGHT1, GL_AMBIENT, lightAmb);
  891. glLightfv(GL_LIGHT1, GL_DIFFUSE, lightDif);
  892. glLightfv(GL_LIGHT1, GL_SPECULAR, lightSpec);
  893.  
  894. glEnable(GL_LIGHTING);
  895. glEnable(GL_LIGHT1);
  896. }
  897.  
  898. void setupScene()
  899. {
  900. glEnable(GL_DEPTH_TEST);
  901.  
  902. glGenTextures(1, &tex_2d[0]);
  903. glGenTextures(1, &tex_2d[1]);
  904. glGenTextures(1, &tex_2d[2]);
  905. glGenTextures(1, &tex_2d[3]);
  906. glGenTextures(1, &tex_2d[4]);
  907. glGenTextures(1, &tex_2d_room[0]);
  908. glGenTextures(1, &tex_2d_room[1]);
  909. glGenTextures(1, &tex_2d_room[2]);
  910. glGenTextures(1, &tex_2d_room[3]);
  911. glGenTextures(1, &tex_2d_room[4]);
  912. glGenTextures(1, &tex_2d_room[5]);
  913. loadTexture("Wood.jpg", tex_2d[0]);
  914. loadTexture("Wood1.bmp", tex_2d[1]);
  915. loadTexture("cb.jpg", tex_2d[2]);
  916. loadTexture("bro1.jpg", tex_2d[3]);
  917. loadTexture("posx.jpg", tex_2d_room[0]);
  918. loadTexture("posy.jpg", tex_2d_room[1]);
  919. loadTexture("posz.jpg", tex_2d_room[2]);
  920. loadTexture("negx.jpg", tex_2d_room[3]);
  921. loadTexture("negy.jpg", tex_2d_room[4]);
  922. loadTexture("negz.jpg", tex_2d_room[5]);
  923. glEnable(GL_TEXTURE_2D);
  924. }
  925.  
  926. void loadObjects()
  927. {
  928. Bishop.loadOBJ("Bishop.obj", Bishop.v, Bishop.vt, Bishop.vn);
  929. Bishop.id = 1;
  930. King.loadOBJ("King.obj", King.v, King.vt, King.vn);
  931. King.id = 2;
  932. Knight.loadOBJ("Knight.obj", Knight.v, Knight.vt, Knight.vn);
  933. Knight.id = 3;
  934. Pawn.loadOBJ("Pawn.obj", Pawn.v, Pawn.vt, Pawn.vn);
  935. Pawn.id = 4;
  936. Queen.loadOBJ("Queen.obj", Queen.v, Queen.vt, Queen.vn);
  937. Queen.id = 5;
  938. Rook.loadOBJ("Rook.obj", Rook.v, Rook.vt, Rook.vn);
  939. Rook.id = 6;
  940. Board.loadOBJ("Board.obj", Board.v, Board.vt, Board.vn);
  941. Table.loadOBJ("Bartable.obj", Table.v, Table.vt, Table.vn);
  942. std::cout<<"Bishop: "<<Bishop.v.size()<<"\n";
  943. std::cout<<"King: "<<King.v.size()<<"\n";
  944. std::cout<<"Knight: "<<Knight.v.size()<<"\n";
  945. std::cout<<"Pawn: "<<Pawn.v.size()<<"\n";
  946. std::cout<<"Queen: "<<Queen.v.size()<<"\n";
  947. std::cout<<"Rook: "<<Rook.v.size()<<"\n";
  948. }
  949.  
  950. void drawPiece(Piece &p)
  951. {
  952. glMaterialfv(GL_FRONT, GL_SPECULAR, PieceMaterialSpecular);
  953. glMaterialfv(GL_FRONT, GL_SHININESS, PieceMaterialShininess);
  954. glBindTexture(GL_TEXTURE_2D, tex_2d[0]);
  955. int error;
  956. // DEBUG_MSG(std::cout, "przed petla");
  957. switch(p.id){
  958. case 1:
  959. glCallList(lista);
  960. break;
  961. case 2:
  962. glCallList(lista+1);
  963. break;
  964. case 3:
  965. glCallList(lista+2);
  966. break;
  967. case 4:
  968. glCallList(lista+3);
  969. break;
  970. case 5:
  971. glCallList(lista+4);
  972. break;
  973. case 6:
  974. glCallList(lista+5);
  975. break;
  976. }
  977.  
  978. // DEBUG_MSG(std::cout, "po petli");
  979. glEnd();
  980. }
  981.  
  982. void initList()
  983. {
  984. lista = glGenLists(6);
  985. glNewList(lista, GL_COMPILE);
  986. glBegin(GL_QUADS);
  987. for (unsigned int i = 0; i < Bishop.v.size(); i += 4)
  988. {
  989. glNormal3f(Bishop.vn[i].x, Bishop.vn[i].y, Bishop.vn[i].z);
  990. glTexCoord2f(Bishop.vt[i].x, Bishop.vt[i].y);
  991. glVertex3f(Bishop.v[i].x, Bishop.v[i].y, Bishop.v[i].z);
  992.  
  993. glNormal3f(Bishop.vn[i+1].x, Bishop.vn[i+1].y, Bishop.vn[i+1].z);
  994. glTexCoord2f(Bishop.vt[i+1].x, Bishop.vt[i+1].y);
  995. glVertex3f(Bishop.v[i+1].x, Bishop.v[i+1].y, Bishop.v[i+1].z);
  996.  
  997. glNormal3f(Bishop.vn[i+2].x, Bishop.vn[i+2].y, Bishop.vn[i+2].z);
  998. glTexCoord2f(Bishop.vt[i+2].x, Bishop.vt[i+2].y);
  999. glVertex3f(Bishop.v[i+2].x, Bishop.v[i+2].y, Bishop.v[i+2].z);
  1000.  
  1001. glNormal3f(Bishop.vn[i+3].x, Bishop.vn[i+3].y, Bishop.vn[i+3].z);
  1002. glTexCoord2f(Bishop.vt[i+3].x, Bishop.vt[i+3].y);
  1003. glVertex3f(Bishop.v[i+3].x, Bishop.v[i+3].y, Bishop.v[i+3].z);
  1004.  
  1005. }
  1006. glEnd();
  1007. glEndList();
  1008.  
  1009. glNewList(lista+1, GL_COMPILE);
  1010. glBegin(GL_QUADS);
  1011. for (unsigned int i = 0; i < King.v.size(); i += 4)
  1012. {
  1013. glNormal3f(King.vn[i].x, King.vn[i].y, King.vn[i].z);
  1014. glTexCoord2f(King.vt[i].x, King.vt[i].y);
  1015. glVertex3f(King.v[i].x, King.v[i].y, King.v[i].z);
  1016.  
  1017. glNormal3f(King.vn[i+1].x, King.vn[i+1].y, King.vn[i+1].z);
  1018. glTexCoord2f(King.vt[i+1].x, King.vt[i+1].y);
  1019. glVertex3f(King.v[i+1].x, King.v[i+1].y, King.v[i+1].z);
  1020.  
  1021. glNormal3f(King.vn[i+2].x, King.vn[i+2].y, King.vn[i+2].z);
  1022. glTexCoord2f(King.vt[i+2].x, King.vt[i+2].y);
  1023. glVertex3f(King.v[i+2].x, King.v[i+2].y, King.v[i+2].z);
  1024.  
  1025. glNormal3f(King.vn[i+3].x, King.vn[i+3].y, King.vn[i+3].z);
  1026. glTexCoord2f(King.vt[i+3].x, King.vt[i+3].y);
  1027. glVertex3f(King.v[i+3].x, King.v[i+3].y, King.v[i+3].z);
  1028.  
  1029. }
  1030. glEnd();
  1031. glEndList();
  1032.  
  1033. glNewList(lista+2, GL_COMPILE);
  1034. glBegin(GL_QUADS);
  1035. for (unsigned int i = 0; i < Knight.v.size(); i += 4)
  1036. {
  1037. glNormal3f(Knight.vn[i].x, Knight.vn[i].y, Knight.vn[i].z);
  1038. glTexCoord2f(Knight.vt[i].x, Knight.vt[i].y);
  1039. glVertex3f(Knight.v[i].x, Knight.v[i].y, Knight.v[i].z);
  1040.  
  1041. glNormal3f(Knight.vn[i+1].x, Knight.vn[i+1].y, Knight.vn[i+1].z);
  1042. glTexCoord2f(Knight.vt[i+1].x, Knight.vt[i+1].y);
  1043. glVertex3f(Knight.v[i+1].x, Knight.v[i+1].y, Knight.v[i+1].z);
  1044.  
  1045. glNormal3f(Knight.vn[i+2].x, Knight.vn[i+2].y, Knight.vn[i+2].z);
  1046. glTexCoord2f(Knight.vt[i+2].x, Knight.vt[i+2].y);
  1047. glVertex3f(Knight.v[i+2].x, Knight.v[i+2].y, Knight.v[i+2].z);
  1048.  
  1049. glNormal3f(Knight.vn[i+3].x, Knight.vn[i+3].y, Knight.vn[i+3].z);
  1050. glTexCoord2f(Knight.vt[i+3].x, Knight.vt[i+3].y);
  1051. glVertex3f(Knight.v[i+3].x, Knight.v[i+3].y, Knight.v[i+3].z);
  1052.  
  1053. }
  1054. glEnd();
  1055. glEndList();
  1056.  
  1057. glNewList(lista+3, GL_COMPILE);
  1058. glBegin(GL_QUADS);
  1059. for (unsigned int i = 0; i < Pawn.v.size(); i += 4)
  1060. {
  1061. glNormal3f(Pawn.vn[i].x, Pawn.vn[i].y, Pawn.vn[i].z);
  1062. glTexCoord2f(Pawn.vt[i].x, Pawn.vt[i].y);
  1063. glVertex3f(Pawn.v[i].x, Pawn.v[i].y, Pawn.v[i].z);
  1064.  
  1065. glNormal3f(Pawn.vn[i+1].x, Pawn.vn[i+1].y, Pawn.vn[i+1].z);
  1066. glTexCoord2f(Pawn.vt[i+1].x, Pawn.vt[i+1].y);
  1067. glVertex3f(Pawn.v[i+1].x, Pawn.v[i+1].y, Pawn.v[i+1].z);
  1068.  
  1069. glNormal3f(Pawn.vn[i+2].x, Pawn.vn[i+2].y, Pawn.vn[i+2].z);
  1070. glTexCoord2f(Pawn.vt[i+2].x, Pawn.vt[i+2].y);
  1071. glVertex3f(Pawn.v[i+2].x, Pawn.v[i+2].y, Pawn.v[i+2].z);
  1072.  
  1073. glNormal3f(Pawn.vn[i+3].x, Pawn.vn[i+3].y, Pawn.vn[i+3].z);
  1074. glTexCoord2f(Pawn.vt[i+3].x, Pawn.vt[i+3].y);
  1075. glVertex3f(Pawn.v[i+3].x, Pawn.v[i+3].y, Pawn.v[i+3].z);
  1076.  
  1077. }
  1078. glEnd();
  1079. glEndList();
  1080.  
  1081. glNewList(lista+4, GL_COMPILE);
  1082. glBegin(GL_QUADS);
  1083. for (unsigned int i = 0; i < Queen.v.size(); i += 4)
  1084. {
  1085. glNormal3f(Queen.vn[i].x, Queen.vn[i].y, Queen.vn[i].z);
  1086. glTexCoord2f(Queen.vt[i].x, Queen.vt[i].y);
  1087. glVertex3f(Queen.v[i].x, Queen.v[i].y, Queen.v[i].z);
  1088.  
  1089. glNormal3f(Queen.vn[i+1].x, Queen.vn[i+1].y, Queen.vn[i+1].z);
  1090. glTexCoord2f(Queen.vt[i+1].x, Queen.vt[i+1].y);
  1091. glVertex3f(Queen.v[i+1].x, Queen.v[i+1].y, Queen.v[i+1].z);
  1092.  
  1093. glNormal3f(Queen.vn[i+2].x, Queen.vn[i+2].y, Queen.vn[i+2].z);
  1094. glTexCoord2f(Queen.vt[i+2].x, Queen.vt[i+2].y);
  1095. glVertex3f(Queen.v[i+2].x, Queen.v[i+2].y, Queen.v[i+2].z);
  1096.  
  1097. glNormal3f(Queen.vn[i+3].x, Queen.vn[i+3].y, Queen.vn[i+3].z);
  1098. glTexCoord2f(Queen.vt[i+3].x, Queen.vt[i+3].y);
  1099. glVertex3f(Queen.v[i+3].x, Queen.v[i+3].y, Queen.v[i+3].z);
  1100.  
  1101. }
  1102. glEnd();
  1103. glEndList();
  1104.  
  1105. glNewList(lista+5, GL_COMPILE);
  1106. glBegin(GL_QUADS);
  1107. for (unsigned int i = 0; i < Rook.v.size(); i += 4)
  1108. {
  1109. glNormal3f(Rook.vn[i].x, Rook.vn[i].y, Rook.vn[i].z);
  1110. glTexCoord2f(Rook.vt[i].x, Rook.vt[i].y);
  1111. glVertex3f(Rook.v[i].x, Rook.v[i].y, Rook.v[i].z);
  1112.  
  1113. glNormal3f(Rook.vn[i+1].x, Rook.vn[i+1].y, Rook.vn[i+1].z);
  1114. glTexCoord2f(Rook.vt[i+1].x, Rook.vt[i+1].y);
  1115. glVertex3f(Rook.v[i+1].x, Rook.v[i+1].y, Rook.v[i+1].z);
  1116.  
  1117. glNormal3f(Rook.vn[i+2].x, Rook.vn[i+2].y, Rook.vn[i+2].z);
  1118. glTexCoord2f(Rook.vt[i+2].x, Rook.vt[i+2].y);
  1119. glVertex3f(Rook.v[i+2].x, Rook.v[i+2].y, Rook.v[i+2].z);
  1120.  
  1121. glNormal3f(Rook.vn[i+3].x, Rook.vn[i+3].y, Rook.vn[i+3].z);
  1122. glTexCoord2f(Rook.vt[i+3].x, Rook.vt[i+3].y);
  1123. glVertex3f(Rook.v[i+3].x, Rook.v[i+3].y, Rook.v[i+3].z);
  1124.  
  1125. }
  1126. glEnd();
  1127. glEndList();
  1128.  
  1129. }
  1130.  
  1131. void drawRoom()
  1132. {
  1133.  
  1134. glBindTexture(GL_TEXTURE_2D, tex_2d_room[2]);
  1135. glBegin(GL_QUADS);
  1136. glTexCoord2f(0, 0);glVertex3f(-700, -700, -700); //przod
  1137. glTexCoord2f(0, 1);glVertex3f(-700, 700, -700);
  1138. glTexCoord2f(1, 1);glVertex3f(700, 700, -700);
  1139. glTexCoord2f(1, 0);glVertex3f(700, -700, -700);
  1140. glEnd();
  1141.  
  1142. glBindTexture(GL_TEXTURE_2D, tex_2d_room[3]);
  1143. glBegin(GL_QUADS);
  1144. glTexCoord2f(0, 0);glVertex3f(-700, -700, 700); //lewa
  1145. glTexCoord2f(0, 1);glVertex3f(-700, 700, 700);
  1146. glTexCoord2f(1, 1);glVertex3f(-700, 700, -700);
  1147. glTexCoord2f(1, 0);glVertex3f(-700, -700, -700);
  1148. glEnd();
  1149.  
  1150. glBindTexture(GL_TEXTURE_2D, tex_2d_room[5]);
  1151. glBegin(GL_QUADS);
  1152. glTexCoord2f(1, 0);glVertex3f(-700, -700, 700); //tyl
  1153. glTexCoord2f(1, 1);glVertex3f(-700, 700, 700);
  1154. glTexCoord2f(0, 1);glVertex3f(700, 700, 700);
  1155. glTexCoord2f(0, 0);glVertex3f(700, -700, 700);
  1156. glEnd();
  1157.  
  1158. glBindTexture(GL_TEXTURE_2D, tex_2d_room[0]);
  1159. glBegin(GL_QUADS);
  1160. glTexCoord2f(0, 0);glVertex3f(700, -700, -700); //prawa
  1161. glTexCoord2f(0, 1);glVertex3f(700, 700, -700);
  1162. glTexCoord2f(1, 1);glVertex3f(700, 700, 700);
  1163. glTexCoord2f(1, 0);glVertex3f(700, -700, 700);
  1164. glEnd();
  1165.  
  1166. glBindTexture(GL_TEXTURE_2D, tex_2d_room[1]);
  1167. glBegin(GL_QUADS);
  1168. glTexCoord2f(0, 0);glVertex3f(-700, 700, -700); //gora
  1169. glTexCoord2f(0, 1);glVertex3f(-700, 700, 700);
  1170. glTexCoord2f(1, 1);glVertex3f(700, 700, 700);
  1171. glTexCoord2f(1, 0);glVertex3f(700, 700, -700);
  1172. glEnd();
  1173.  
  1174. glBindTexture(GL_TEXTURE_2D, tex_2d_room[4]);
  1175. glBegin(GL_QUADS);
  1176. glTexCoord2f(0, 0);glVertex3f(-700, -700, 700); //dol
  1177. glTexCoord2f(0, 1);glVertex3f(-700, -700, -700);
  1178. glTexCoord2f(1, 1);glVertex3f(700, -700, -700);
  1179. glTexCoord2f(1, 0);glVertex3f(700, -700, 700);
  1180.  
  1181. glEnd();
  1182. }
  1183.  
  1184.  
  1185.  
  1186.  
  1187. void AddHighScore(const std::string fileName, double seconds, std::string userName)
  1188. {
  1189. std::fstream file;
  1190. std::vector<std::pair<std::string, double>> scores;
  1191. int i = 0;
  1192. std::string tmp;
  1193. bool exitFlag = false;
  1194. file.open(fileName, std::ios::in);
  1195. while(std::getline(file, tmp) && (i < 10))
  1196. {
  1197. std::string buf; // Have a buffer string
  1198. std::stringstream ss(tmp); // Insert the string into a stream
  1199.  
  1200. std::vector<std::string> tokens; // Create vector to hold our words
  1201.  
  1202. while (ss >> buf){
  1203. tokens.push_back(buf);
  1204. }
  1205.  
  1206. scores.push_back(std::pair<std::string, double>(tokens[0], string_to_double(tokens[1])));
  1207. i++;
  1208. }
  1209. if(scores.size() < 10)
  1210. {
  1211. for(std::vector<std::pair<std::string, double>>::iterator j = scores.begin(); j!= scores.end(); ++j)
  1212. {
  1213. if(((*j).second) < seconds)
  1214. {
  1215. scores.insert(j, std::pair<std::string, double>(userName, seconds));
  1216. exitFlag = true;
  1217. break;
  1218. }
  1219. }
  1220. if(!exitFlag)
  1221. scores.push_back( std::pair<std::string, double>(userName, seconds));
  1222.  
  1223. }
  1224. else
  1225. {
  1226. for(std::vector<std::pair<std::string, double>>::iterator j = scores.begin(); j!= scores.end(); ++j)
  1227. {
  1228. if(((*j).second) < seconds)
  1229. {
  1230. scores.insert(j, std::pair<std::string, double>(userName, seconds));
  1231. break;
  1232. }
  1233. }
  1234.  
  1235. scores.pop_back();
  1236. }
  1237.  
  1238. file.close();
  1239. file.open(fileName, std::ios::out | std::ios::trunc);
  1240. for(std::vector<std::pair<std::string, double>>::iterator j = scores.begin(); j!= scores.end(); ++j)
  1241. {
  1242. file<<userName + " " + double_to_string((*j).second)<<"\n";
  1243. }
  1244. file.close();
  1245. }
  1246.  
  1247.  
  1248. std::vector<std::string> ShowHighScores(const std::string fileName)
  1249. {
  1250. std::ifstream file;
  1251. std::vector<std::string> scores;
  1252. int i = 0;
  1253. std::string tmp;
  1254. file.open(fileName, std::ios::in);
  1255. if(!file.is_open()){
  1256.  
  1257. }
  1258. while(std::getline(file, tmp) && (i < 10))
  1259. {
  1260. std::cout<<"iteracja " + tmp + "\n";
  1261. scores.push_back(tmp);
  1262. i++;
  1263. }
  1264. if(scores.empty())
  1265. {
  1266. printf("empty");
  1267. }
  1268. return scores;
  1269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement