Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.81 KB | None | 0 0
  1. #ifdef __APPLE__
  2. #include <GLUT/glut.h>
  3. #else
  4. #include <GL/glut.h>
  5. #endif
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <GL/freeglut.h>
  10. #include <math.h>
  11.  
  12. #define PI 3.1415927
  13. //-------------- VARIAVEIS GLOBAIS -------------------------
  14.  
  15. float _angle = 45;
  16. float i,j, selX=0, selY=0; //selX e selY = coordenadas do "cursor"
  17. int k=0;
  18. int board[8][8];
  19. int player = 1; //1 = vermelhas 2= azuis
  20. int pickInProgress = 0;
  21. float atualX, atualy;
  22. int pickingX, pickingY;
  23.  
  24.  
  25. GLuint _displayListId_blackArea; //The OpenGL id of the display list
  26. GLuint _displayListId_whiteArea; //The OpenGL id of the display list
  27. GLuint _displayListId_redArea;
  28. GLuint _displayListId_blueArea;
  29. GLuint _displayListId_clearArea;
  30.  
  31. GLuint _displayListId_whitePiece;
  32. GLuint _displayListId_blackPiece;
  33.  
  34.  
  35. //--------------- PROTOTIPOS DE FUNCOES ---------------------
  36. void getCoord();
  37. void checkLimits();
  38. void drawPieces();
  39. void drawMatrix();
  40. void drawScene();
  41. void showBoard();
  42. int checkPecas();
  43. int movValido(int jogador, int xDestino, int yDestino);
  44. int destinoValido( int x, int y, int destX, int destY);
  45. void draw_cylinder(GLfloat radius, GLfloat height, GLubyte R, GLubyte G, GLubyte B);
  46. void movePeca(int origemX, int origemY, int destX, int destY);
  47.  
  48. void comePeca(int origemX, int origemY, int destX, int destY);
  49. //Compilar: gcc -o chess chess.c -lglut -lGLU -lGL -lm
  50.  
  51. void handleResize(int w,int h)
  52. {
  53. glViewport(0,0,w,h);
  54. glMatrixMode(GL_PROJECTION);
  55. glLoadIdentity();
  56. gluPerspective(65.0,(double)w/(double)h,1.0,200);
  57. //gluLookAt(0.0f,5.5f, 15.0f, 0.0f,0.0f,0.0f, 0.0f,1.0f,0.0f);
  58. gluLookAt(0.0,13 ,10.0,0.0,0.0,0.0,0.0,0.0,-1.0);
  59. }
  60.  
  61. void draw_BlackArea()
  62. {
  63. // glPushMatrix();
  64. //glTranslatef(1.5f,0.0f,0.0f);
  65.  
  66. glBegin(GL_QUADS);
  67. glColor3f(0.05f,0.05f,0.05f);
  68. glTranslatef(0.0f,0.0f,0.0f);
  69. glVertex3f(0.0f,0.0f,0.0f);
  70. glVertex3f(1.50f,0.0f,0.0f);
  71. glVertex3f(1.5f,0.3f,0.0f);
  72. glVertex3f(0.0f,0.3f,0.0f);
  73. glEnd();
  74.  
  75. glBegin(GL_QUADS);
  76. glColor3f(0.05f,0.05f,0.05f);
  77. glVertex3f(0.0f,0.0f,0.0f);
  78. glVertex3f(0.0f,0.0f,-1.5f);
  79. glVertex3f(0.0f,0.3f,-1.5f);
  80. glVertex3f(0.0f,0.3f,0.0f);
  81. glEnd();
  82.  
  83. glBegin(GL_QUADS);
  84. glColor3f(0.05f,0.05f,0.05f);
  85. glVertex3f(1.5f,0.0f,0.0f);
  86. glVertex3f(1.5f,0.0f,-1.5f);
  87. glVertex3f(1.5f,0.3f,-1.5f);
  88. glVertex3f(1.5f,0.3f,0.0f);
  89. glEnd();
  90.  
  91. glBegin(GL_QUADS);
  92. glColor3f(0.05f,0.05f,0.05f);
  93. glVertex3f(0.0f,0.0f,-1.5f);
  94. glVertex3f(1.50f,0.0f,-1.5f);
  95. glVertex3f(1.5f,0.3f,-1.5f);
  96. glVertex3f(0.0f,0.3f,-1.5f);
  97. glEnd();
  98.  
  99. glBegin(GL_QUADS);
  100. glColor3f(0.05f,0.05f,0.05f);
  101. glVertex3f(0.0f,0.0f,0.0f);
  102. glVertex3f(1.50f,0.0f,0.0f);
  103. glVertex3f(1.5f,0.0f,-1.5f);
  104. glVertex3f(0.0f,0.0f,-1.5f);
  105. glEnd();
  106.  
  107. glBegin(GL_QUADS);
  108. glColor3f(0.0f,0.0f,0.0f);
  109. glVertex3f(0.0f,0.3f,0.0f);
  110. glVertex3f(1.50f,0.3f,0.0f);
  111. glVertex3f(1.5f,0.3f,-1.5f);
  112. glVertex3f(0.0f,0.3f,-1.5f);
  113. glEnd();
  114.  
  115. // glPopMatrix();
  116.  
  117. }
  118. void draw_whiteArea()
  119. {
  120. //glPushMatrix();
  121.  
  122. glBegin(GL_QUADS);
  123. glColor3f(0.05f,0.05f,0.05f);
  124. glTranslatef(0.0f,0.0f,0.0f);
  125. glVertex3f(0.0f,0.0f,0.0f);
  126. glVertex3f(1.50f,0.0f,0.0f);
  127. glVertex3f(1.5f,0.3f,0.0f);
  128. glVertex3f(0.0f,0.3f,0.0f);
  129. glEnd();
  130.  
  131. glBegin(GL_QUADS);
  132. glColor3f(0.05f,0.05f,0.05f);
  133. glVertex3f(0.0f,0.0f,0.0f);
  134. glVertex3f(0.0f,0.0f,-1.5f);
  135. glVertex3f(0.0f,0.3f,-1.5f);
  136. glVertex3f(0.0f,0.3f,0.0f);
  137. glEnd();
  138.  
  139. glBegin(GL_QUADS);
  140. glColor3f(0.05f,0.05f,0.05f);
  141. glVertex3f(1.5f,0.0f,0.0f);
  142. glVertex3f(1.5f,0.0f,-1.5f);
  143. glVertex3f(1.5f,0.3f,-1.5f);
  144. glVertex3f(1.5f,0.3f,0.0f);
  145. glEnd();
  146.  
  147. glBegin(GL_QUADS);
  148. glColor3f(0.05f,0.05f,0.05f);
  149. glVertex3f(0.0f,0.0f,-1.5f);
  150. glVertex3f(1.50f,0.0f,-1.5f);
  151. glVertex3f(1.5f,0.3f,-1.5f);
  152. glVertex3f(0.0f,0.3f,-1.5f);
  153. glEnd();
  154.  
  155. glBegin(GL_QUADS);
  156. glColor3f(0.05f,0.05f,0.05f);
  157. glVertex3f(0.0f,0.0f,0.0f);
  158. glVertex3f(1.50f,0.0f,0.0f);
  159. glVertex3f(1.5f,0.0f,-1.5f);
  160. glVertex3f(0.0f,0.0f,-1.5f);
  161. glEnd();
  162.  
  163. glBegin(GL_QUADS);
  164. glColor3f(1.0f,1.0f,1.0f);
  165. glVertex3f(0.0f,0.3f,0.0f);
  166. glVertex3f(1.50f,0.3f,0.0f);
  167. glVertex3f(1.5f,0.3f,-1.5f);
  168. glVertex3f(0.0f,0.3f,-1.5f);
  169. glEnd();
  170.  
  171. // glPopMatrix();
  172.  
  173. }
  174.  
  175. void draw_redArea()
  176. {
  177.  
  178. glBegin(GL_QUADS);
  179. glColor3f(0.7f,0.0f,0.0f);
  180. glTranslatef(0.0f,0.0f,0.0f);
  181. glVertex3f(0.0f,0.0f,0.0f);
  182. glVertex3f(1.50f,0.0f,0.0f);
  183. glVertex3f(1.5f,0.3f,0.0f);
  184. glVertex3f(0.0f,0.3f,0.0f);
  185. glTranslatef(0.001+selX, 0.001+selY, 0.0);
  186. glEnd();
  187.  
  188.  
  189.  
  190. glBegin(GL_QUADS);
  191. glColor3f(0.7f,0.0f,0.0f);
  192. glVertex3f(0.0f,0.3f,0.0f);
  193. glVertex3f(1.50f,0.3f,0.f);
  194. glVertex3f(1.5f,0.3f,-1.5f);
  195. glVertex3f(0.0f,0.3f,-1.5f);
  196. glTranslatef(0.001+selX, 0.001+selY, 0.0);
  197. glEnd();
  198.  
  199. }
  200.  
  201.  
  202. void draw_clearArea()
  203. {
  204.  
  205. glBegin(GL_QUADS);
  206. glColor3f(0.0f,0.7f,0.0f);
  207. glTranslatef(0.0f,0.0f,0.0f);
  208. glVertex3f(0.0f,0.0f,0.0f);
  209. glVertex3f(1.50f,0.0f,0.0f);
  210. glVertex3f(1.5f,0.3f,0.0f);
  211. glVertex3f(0.0f,0.3f,0.0f);
  212. glTranslatef(0.001+selX, 0.001+selY, 0.0);
  213. glEnd();
  214.  
  215.  
  216.  
  217. glBegin(GL_QUADS);
  218. glColor3f(0.f,0.7f,0.0f);
  219. glVertex3f(0.0f,0.3f,0.0f);
  220. glVertex3f(1.50f,0.3f,0.f);
  221. glVertex3f(1.5f,0.3f,-1.5f);
  222. glVertex3f(0.0f,0.3f,-1.5f);
  223. glTranslatef(0.001+selX, 0.001+selY, 0.0);
  224. glEnd();
  225.  
  226. }
  227.  
  228.  
  229. void draw_blueArea()
  230. {
  231.  
  232. glBegin(GL_QUADS);
  233. glColor3f(0.0f,0.0f,0.7f);
  234. glTranslatef(0.0f,0.0f,0.0f);
  235. glVertex3f(0.0f,0.0f,0.0f);
  236. glVertex3f(1.50f,0.0f,0.0f);
  237. glVertex3f(1.5f,0.3f,0.0f);
  238. glVertex3f(0.0f,0.3f,0.0f);
  239. glTranslatef(0.002+selX, 0.002+selY, 0.0);
  240. glEnd();
  241.  
  242.  
  243.  
  244. glBegin(GL_QUADS);
  245. glColor3f(0.0f,0.0f,0.7f);
  246. glVertex3f(0.0f,0.3f,0.0f);
  247. glVertex3f(1.50f,0.3f,0.f);
  248. glVertex3f(1.5f,0.3f,-1.5f);
  249. glVertex3f(0.0f,0.3f,-1.5f);
  250. glTranslatef(0.002+selX, 0.002+selY, 0.0);
  251. glEnd();
  252.  
  253. }
  254.  
  255. void draw_cylinder(GLfloat radius, GLfloat height, GLubyte R, GLubyte G, GLubyte B)
  256. {
  257. GLfloat x = 0.0;
  258. GLfloat y = 0.0;
  259. GLfloat angle = 0.0;
  260. GLfloat angle_stepsize = 0.1;
  261.  
  262. //Tubo do cilindro
  263. glColor3ub(0.6*R,0.6*G,0.6*B);
  264. glBegin(GL_QUAD_STRIP);
  265. angle = 0.0;
  266. while( angle <= 2*PI ) {
  267. x = radius * cos(angle);
  268. y = radius * sin(angle);
  269. glVertex3f(x, y , 1);
  270. glVertex3f(x, y , 0.0);
  271. angle = angle + angle_stepsize;
  272. }
  273. glVertex3f(radius, 0.0, 1);
  274. glVertex3f(radius, 0.0, 0.0);
  275. glEnd();
  276.  
  277. //Circulo do topo
  278. glColor3ub(R,G,B);
  279. glBegin(GL_POLYGON);
  280. angle = 0.0;
  281. while( angle <= 2*PI ) {
  282. x = radius * cos(angle);
  283. y = radius * sin(angle);
  284. glVertex3f(x, y , 1);
  285. angle = angle + angle_stepsize;
  286. }
  287. glVertex3f(radius, 0.0, height);
  288. glEnd();
  289. }
  290.  
  291.  
  292. void initRendering()
  293. {
  294. glEnable(GL_DEPTH_TEST);
  295. glEnable(GL_COLOR_MATERIAL);
  296. glClearColor(0.0f,0.0f,0.2f,1.0f);
  297.  
  298.  
  299. _displayListId_blackArea = glGenLists(1); //Make room for the display list
  300. glNewList(_displayListId_blackArea, GL_COMPILE); //Begin the display list
  301. draw_BlackArea(); //Add commands for drawing a black area to the display list
  302. glEndList(); //End the display list
  303.  
  304. //Set up a display list for drawing a cube
  305. _displayListId_whiteArea = glGenLists(2); //Make room for the display list
  306. glNewList(_displayListId_whiteArea, GL_COMPILE); //Begin the display list
  307. draw_whiteArea(); //Add commands for drawing a black to the display list
  308. glEndList(); //End the display list
  309.  
  310. _displayListId_redArea = glGenLists(3); //Make room for the display list
  311. glNewList(_displayListId_redArea, GL_COMPILE); //Begin the display list
  312. draw_redArea(); //Add commands for drawing a black to the display list
  313. glEndList(); //End the display list
  314.  
  315. _displayListId_clearArea = glGenLists(5); //Make room for the display list
  316. glNewList(_displayListId_clearArea, GL_COMPILE); //Begin the display list
  317. draw_clearArea(); //Add commands for drawing a black to the display list
  318. glEndList(); //End the display list
  319.  
  320.  
  321. _displayListId_blueArea = glGenLists(5); //Make room for the display list
  322. glNewList(_displayListId_blueArea, GL_COMPILE); //Begin the display list
  323. draw_blueArea(); //Add commands for drawing a black to the display list
  324. glEndList(); //End the display list
  325.  
  326.  
  327.  
  328.  
  329. _displayListId_whitePiece = glGenLists(4); //Make room for the display list
  330. glNewList(_displayListId_whitePiece, GL_COMPILE); //Begin the display list
  331. glTranslatef(0.75,0.0,-0.75);
  332. glRotatef(-90, 1.0, 0.0, 0.0);
  333. draw_cylinder(0.5, 0.0, 255, 1, 1); //Add commands for drawing a black to the display list
  334. glEndList(); //End the display list
  335.  
  336. _displayListId_blackPiece = glGenLists(4); //Make room for the display list
  337. glNewList(_displayListId_blackPiece, GL_COMPILE); //Begin the display list
  338. glTranslatef(0.75,0.0,-0.75);
  339. glRotatef(-90, 1.0, 0.0, 0.0);
  340. draw_cylinder(0.5, 0.0, 29, 66, 214); //Add commands for drawing a black to the display list
  341. glEndList(); //End the display list
  342.  
  343.  
  344. }
  345.  
  346. void showBoard(){ //cenas da matriz
  347. int a=0, i=0;
  348.  
  349. for (a = 0; a < 8 ; a++){
  350. for(i = 0;i < 8; i++){
  351. printf(" %d ", board[a][i]);
  352. }
  353. printf(" \n");
  354. }
  355.  
  356. }
  357.  
  358. void drawPieces(){ //cenas da matriz
  359. int a=0, i=0; // a = linhas; i = colunas
  360. // 1 = brancas; 2 = pretas
  361.  
  362. for( a = 0; a < 8; a++){ // cria a matriz "em branco"
  363. for(i=0; i < 8; i++){
  364. board[a][i]= 0;
  365. }
  366. }
  367. // ----------------- coloca as brancas na matriz
  368. for( a = 1; a < 3; a += 2){ //fila 2
  369. for(i=0; i < 8; i += 2){
  370. board[a][i]= 1;
  371.  
  372. glPushMatrix();
  373. glCallList(_displayListId_whitePiece);
  374. glRotated(_angle, 1.0, 0.0, 0.0);
  375. glPopMatrix();
  376.  
  377. glutSwapBuffers();
  378. glutDisplayFunc(drawScene);
  379.  
  380.  
  381. //glRotated(_angle, 1.0, 0.0, 0.0);
  382. //glPopMatrix();
  383.  
  384. }
  385. }
  386.  
  387. for( a = 0; a < 3; a += 2){ //fila 1 e 3
  388. for(i=1; i < 8; i += 2){
  389. board[a][i]= 1;
  390.  
  391. }
  392. }
  393.  
  394. // ----------------- coloca as pretas na matriz
  395. for( a = 5; a < 8; a += 2){ // 6 e 8 filas
  396. for(i=0; i < 8; i += 2){
  397. board[a][i]= 2;
  398.  
  399. }
  400. }
  401.  
  402. for( a = 6; a < 8; a += 2){ // 7 fila
  403. for(i=1; i < 8; i += 2){
  404. board[a][i]= 2;
  405.  
  406. }
  407. }
  408. //chamar a funcao para fazer a primeira leitura da matriz aqui
  409.  
  410. drawMatrix();
  411.  
  412. }
  413.  
  414. void drawMatrix(){ //nao faz nada
  415. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  416. glMatrixMode(GL_MODELVIEW);
  417. glLoadIdentity();
  418.  
  419. glTranslatef(0.75,0.0,-0.75);
  420. glRotatef(-90, 1.0, 0.0, 0.0);
  421. draw_cylinder(0.5, 0.0, 255, 1, 1); //Add commands for drawing a black to the display list
  422.  
  423.  
  424.  
  425. for(j=0.0;j>(-8*1.5);j-=1.5)
  426. {
  427. k++;
  428. for(i=0.0;i<(4*3.0);i+=3.0)
  429. {
  430. if(k%2==0) //Se for par
  431. {
  432. glPushMatrix();
  433. glTranslatef(i,0.0,j);
  434. glCallList(_displayListId_blackArea);
  435. glPopMatrix();
  436.  
  437. }
  438.  
  439. else //Se for impar
  440. {
  441. glPushMatrix();
  442. glTranslatef(i+1.5,0.0,j); //Espaçamento de 1.5 entre cada peça
  443. glCallList(_displayListId_blackArea);
  444. glPopMatrix();
  445.  
  446. }
  447.  
  448. }
  449. }
  450.  
  451. for(float j=0.0;j>(-8*1.5);j-=1.5)
  452. {
  453. k++;
  454. for(i=0.0;i<(4*3.0);i+=3.0)
  455. {
  456. if(k%2!=0) //Se for impar
  457. {
  458. glPushMatrix();
  459. glTranslatef(i,0.0,j);
  460. glCallList(_displayListId_whiteArea);
  461. glPopMatrix();
  462.  
  463. }
  464.  
  465. else //Se for par
  466. {
  467. glPushMatrix();
  468. glTranslatef(i+1.5,0.0,j); //Espaçamento de 1.5 entre cada peça
  469. glCallList(_displayListId_whiteArea);
  470. glPopMatrix();
  471. }
  472.  
  473.  
  474.  
  475. }
  476. }
  477.  
  478. showBoard();
  479. /*
  480. int linha = 0, coluna = 0;
  481. for(linha = 0; linha < 8; linha ++){
  482. for(coluna = 0; coluna < 8; coluna ++){
  483. if(board[linha][coluna] == 1){
  484. printf(" encountrou uns \n");
  485. }
  486. }
  487. }*/
  488.  
  489. }
  490.  
  491. void drawScene(){
  492. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  493. glMatrixMode(GL_MODELVIEW);
  494. glLoadIdentity();
  495. glRotatef(-90, 0.0f, 1.0f, 0.0f); //rodar automaticamente com o angulo: -_angle
  496. glTranslatef(-4*1.5, 0.0, 4*1.5);
  497.  
  498.  
  499. for(j=0.0;j>(-8*1.5);j-=1.5)
  500. {
  501. k++;
  502. for(i=0.0;i<(4*3.0);i+=3.0)
  503. {
  504. if(k%2==0) //Se for par
  505. {
  506. glPushMatrix();
  507. glTranslatef(i,0.0,j);
  508. glCallList(_displayListId_blackArea);
  509. glPopMatrix();
  510.  
  511. }
  512.  
  513. else //Se for impar
  514. {
  515. glPushMatrix();
  516. glTranslatef(i+1.5,0.0,j); //Espaçamento de 1.5 entre cada peça
  517. glCallList(_displayListId_blackArea);
  518. glPopMatrix();
  519.  
  520. }
  521.  
  522. }
  523. }
  524.  
  525. for(float j=0.0;j>(-8*1.5);j-=1.5)
  526. {
  527. k++;
  528. for(i=0.0;i<(4*3.0);i+=3.0)
  529. {
  530. if(k%2!=0) //Se for impar
  531. {
  532. glPushMatrix();
  533. glTranslatef(i,0.0,j);
  534. glCallList(_displayListId_whiteArea);
  535. glPopMatrix();
  536.  
  537. }
  538.  
  539. else //Se for par
  540. {
  541. glPushMatrix();
  542. glTranslatef(i+1.5,0.0,j); //Espaçamento de 1.5 entre cada peça
  543. glCallList(_displayListId_whiteArea);
  544. glPopMatrix();
  545. }
  546.  
  547.  
  548.  
  549. }
  550. }
  551.  
  552. if(player == 1 && pickInProgress == 0){
  553.  
  554. glPushMatrix();
  555. if(selX <0){
  556. selX = 0;
  557. }
  558. if(selX > 10.5){
  559. selX =10.5;
  560. }
  561.  
  562. if(selY > 0){
  563. selY =0;
  564. }
  565. if(selY < -10.5){
  566. selY =-10.5;
  567. }
  568. glTranslated(0.001+selX, 0.001, 0.001+selY);
  569.  
  570. glCallList(_displayListId_redArea);
  571.  
  572.  
  573. glPopMatrix();
  574.  
  575. }
  576.  
  577.  
  578. if (player == 1 && pickInProgress == 1){
  579. glPushMatrix();
  580. if(selX <0){
  581. selX = 0;
  582. }
  583. if(selX > 10.5){
  584. selX =10.5;
  585. }
  586.  
  587. if(selY > 0){
  588. selY =0;
  589. }
  590. if(selY < -10.5){
  591. selY =-10.5;
  592. }
  593. glColor3f(0.6f,0.3f,0.7f);
  594. glTranslated(0.001+selX, 0.001, 0.001+selY);
  595.  
  596. glCallList(_displayListId_clearArea);
  597.  
  598. glPopMatrix();
  599.  
  600. }
  601.  
  602.  
  603. if (player == 2 && pickInProgress == 0){
  604. glPushMatrix();
  605. if(selX <0){
  606. selX = 0;
  607. }
  608. if(selX > 10.5){
  609. selX =10.5;
  610. }
  611.  
  612. if(selY > 0){
  613. selY =0;
  614. }
  615. if(selY < -10.5){
  616. selY =-10.5;
  617. }
  618. glColor3f(0.6f,0.3f,0.7f);
  619. glTranslated(0.001+selX, 0.001, 0.001+selY);
  620.  
  621. glCallList(_displayListId_blueArea);
  622.  
  623. glPopMatrix();
  624.  
  625. }
  626.  
  627.  
  628. if (player == 2 && pickInProgress == 1){
  629. glPushMatrix();
  630. if(selX <0){
  631. selX = 0;
  632. }
  633. if(selX > 10.5){
  634. selX =10.5;
  635. }
  636.  
  637. if(selY > 0){
  638. selY =0;
  639. }
  640. if(selY < -10.5){
  641. selY =-10.5;
  642. }
  643. glTranslated(0.001+selX, 0.001, 0.001+selY);
  644.  
  645. glCallList(_displayListId_clearArea);
  646.  
  647. glPopMatrix();
  648.  
  649. }
  650.  
  651.  
  652. //-------- pecas brancas
  653. int a = 0, i = 0;
  654. float offX = 0, offY = 0; //off y coloca nas co
  655.  
  656. for (a = 0; a < 8 ; a++){
  657. for(i = 0;i < 8; i++){
  658. if(board[a][i] == 1 ){
  659. glPushMatrix();
  660. //glRotated(_angle, 1.0, 0.0, 0.0);
  661. glTranslated(offX, 0, offY);
  662. glCallList(_displayListId_whitePiece);
  663. glPopMatrix();
  664. }
  665. offY -= 1.5;
  666. }
  667. offY = 0;
  668. offX += 1.5;
  669. }
  670.  
  671. //-------- ciclo para as pretas
  672. offX = 0;
  673. offY = 0;
  674. for (a = 0; a < 8 ; a++){
  675. for(i = 0;i < 8; i++){
  676. if(board[a][i] == 2 ){
  677. glPushMatrix();
  678. //glRotated(_angle, 1.0, 0.0, 0.0);
  679. glTranslated(offX, 0, offY);
  680. glCallList(_displayListId_blackPiece);
  681. glPopMatrix();
  682. }
  683. offY -= 1.5;
  684. }
  685. offY = 0;
  686. offX += 1.5;
  687. }
  688.  
  689. glutSwapBuffers();
  690. glFlush();
  691.  
  692. }
  693.  
  694. int checkPecas(){
  695. int a=0, i=0;
  696.  
  697. for (a = 0; a < 8 ; a++){
  698. for(i = 0;i < 8; i++){
  699. if(board[a][i] == 1 ){
  700. return 1;
  701. }
  702. if(board[a][i] == 2){
  703. return 2;
  704. }
  705. }
  706. }
  707. return 0;
  708. }
  709.  
  710. void update(int value)
  711. {
  712. _angle += 1.0f;
  713. if (_angle > 360)
  714. {
  715. _angle -= 360;
  716. }
  717. glutPostRedisplay();
  718. glutTimerFunc(25, update, 0);
  719. }
  720.  
  721. void handleKeypress(unsigned char key,int x,int y)
  722. {
  723. switch(key)
  724. {
  725. case('d'):
  726. glPushMatrix();
  727. //selX -= 1.5;
  728. selY -= 1.5;
  729. glCallList(_displayListId_redArea);
  730. glPopMatrix();
  731. //getCoord();
  732.  
  733. glutSwapBuffers();
  734. glutDisplayFunc(drawScene);
  735. break;
  736. case('a'):
  737. glPushMatrix();
  738. //selX -= 1.5;
  739. selY += 1.5;
  740. glCallList(_displayListId_redArea);
  741. glPopMatrix();
  742. //getCoord();
  743.  
  744. glutSwapBuffers();
  745. glutDisplayFunc(drawScene);
  746. break;
  747. case('w'):
  748. glPushMatrix();
  749. selX -= 1.5;
  750. //selY += 1.5;
  751. glCallList(_displayListId_redArea);
  752. glPopMatrix();
  753. //getCoord();
  754.  
  755. glutSwapBuffers();
  756. glutDisplayFunc(drawScene);
  757. break;
  758.  
  759. case('s'):
  760. glPushMatrix();
  761. selX += 1.5;
  762. //selY += 1.5;
  763. glCallList(_displayListId_redArea);
  764. glPopMatrix();
  765. getCoord();
  766.  
  767. glutSwapBuffers();
  768. glutDisplayFunc(drawScene);
  769. break;
  770. case('q'):
  771. checkLimits();
  772. atualX = selX / 1.5;
  773. atualy = fabsf (selY/1.5);
  774. printf("%d -- Posicao de X Selecionada: %f \n Posicao de Y Selecionada: %f \n",player, atualX, atualy);
  775.  
  776. if(pickInProgress == 0){
  777. if(movValido(player, (int) abs(atualX), (int) abs(atualy)) == 1){
  778. printf("Peca do tipo certa encontrada \n");
  779. pickInProgress = 1;
  780. pickingX = (int) abs(atualX); //variaveis temporarias por causa da selecao
  781. pickingY = (int) abs(atualy);
  782. //fazer aqui a mudanca de cor
  783. break;
  784. }
  785. }
  786.  
  787. if(pickInProgress == 1){
  788. printf("escolha em curso para o jogador %d \n", player);
  789. if(destinoValido(pickingX, pickingY,(int) abs(atualX), (int) abs(atualy)) == 1){
  790. printf("Destino valido!\n");
  791. movePeca(pickingX, pickingY,(int) abs(atualX), (int) abs(atualy));
  792. pickInProgress = 0;
  793. }
  794. if(destinoValido(pickingX, pickingY,(int) abs(atualX), (int) abs(atualy)) == 2){
  795. printf("Destino válido - comer Peca \n");
  796. comePeca(pickingX, pickingY,(int) abs(atualX), (int) abs(atualy));
  797. pickInProgress = 0;
  798. }else{
  799. printf("destino invalido i dk \n");
  800. pickInProgress = 0;
  801. }
  802. }
  803.  
  804. printf("rip \n");
  805.  
  806.  
  807. break;
  808. case('e'):
  809. break;
  810. case 27:
  811. exit(0);
  812. }
  813. }
  814. void comePeca(int origemX, int origemY, int destX, int destY){
  815. int offX, offY;
  816. offX = destX-origemX;
  817. offY = destY-origemY;
  818. board[destX+offX][destY+offY] = board[origemX][origemY];
  819. board[origemX][origemY] = 0;
  820. board[destX][destY] = 0;
  821.  
  822. if (player == 1){
  823. printf("esta no jogador %d \n", player);
  824. player = 2;
  825. return;
  826. }
  827. if (player == 2){
  828. printf("esta no jogador %d \n", player);
  829. player = 1;
  830. return;
  831. }
  832.  
  833. }
  834.  
  835. int movValido(int jogador, int xDestino, int yDestino){ //retorna 1 se se a peca selecionada é do jogador atual
  836.  
  837. printf("X recebido = %d \n Y recebido = %d \n", xDestino, yDestino);
  838.  
  839. if(board[xDestino][yDestino] == jogador){
  840. return 1;
  841. }
  842.  
  843. return 0;
  844. }
  845.  
  846. int destinoValido( int x, int y, int destX, int destY){
  847. int jogador = board[x][y];
  848. printf("--------\n ");
  849. printf("X origem: %d \n Y origem: %d\n", x, y);
  850.  
  851. printf("X destino: %d \n Y destino: %d \n", destX, destY);
  852.  
  853. printf("--------\n ");
  854.  
  855.  
  856. if(player == 1 ){
  857. if(destX == x + 1 && destY == y + 1){
  858. if(board[destX][destY] == 0){
  859. return 1;
  860. }
  861. if(board[destX][destY] == 2){
  862. if(board[destX+1][destY+1] == 0 && (destX+1)<8 && (destY+1)<8 ){
  863. return 2;
  864. }
  865. }
  866. }
  867. if(destX == x + 1 && destY == y - 1){
  868. if(board[destX][destY] == 0)
  869. return 1;
  870. if(board[destX][destY] == 2){
  871. if(board[destX+1][destY-1] == 0 && (destX+1)<8 && (destY-1)>=0){
  872. return 2;
  873. }
  874. }
  875. }
  876. }
  877.  
  878. if(player == 2){
  879. if(destX == x - 1 && destY == y - 1){
  880. if(board[destX][destY] == 0)
  881. return 1;
  882. if(board[destX][destY] == 1){
  883. if(board[destX-1][destY-1] == 0 && (destX-1)>=0 && (destY-1)>=0 ){
  884. return 2;
  885. }
  886. }
  887.  
  888. }
  889.  
  890. if(destX == x - 1 && destY == y + 1){
  891. if(board[destX][destY] == 0)
  892. return 1;
  893. if(board[destX][destY] == 1){
  894. if(board[destX-1][destY+1] == 0 && (destX-1)>=0 && (destY+1)<8 ){
  895. return 2;
  896. }
  897. }
  898. }
  899. }
  900.  
  901.  
  902. return 0;
  903. }
  904.  
  905. void movePeca(int origemX, int origemY, int destX, int destY){
  906. board[destX][destY] = board[origemX][origemY];
  907. board[origemX][origemY] = 0;
  908.  
  909. if (player == 1){
  910. printf("esta no jogador %d \n", player);
  911. player = 2;
  912. return;
  913. }
  914. if (player == 2){
  915. printf("esta no jogador %d \n", player);
  916. player = 1;
  917. return;
  918. }
  919. }
  920.  
  921. int comeSequencia(int origemX, int origemY, int destX, int destY){
  922. int counter = 0;
  923.  
  924. int offX, offY;
  925. offX = destX-origemX;
  926. offY = destY-origemY;
  927. return 1;
  928. }
  929.  
  930. void getCoord(){
  931. checkLimits();
  932. atualX = selX / 1.5;
  933. atualy = selY / 1.5;
  934. printf("Posicao de X: %f \n Posicao de Y: %f \n", atualX, fabsf(atualy));
  935. }
  936.  
  937. void checkLimits(){ //limita as coordenadas
  938. if(selX > 10.5){
  939. selX = 10.5;
  940. }
  941. if(selX < 0){
  942. selX = 0;
  943. }
  944. if(selY < -10.5){
  945. selY = -10.5;
  946. }
  947. if(selY > 0){
  948. selY = 0;
  949. }
  950.  
  951. }
  952.  
  953.  
  954. int main(int argc,char** argv)
  955. {
  956. glutInit(&argc,argv);
  957. glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
  958. glutInitWindowSize(800,800);
  959. glutCreateWindow("ChessBoard");
  960. initRendering();
  961. //glutFullScreen();
  962. glutDisplayFunc(drawScene);
  963. drawPieces();
  964. glutKeyboardFunc(handleKeypress);
  965. glutReshapeFunc(handleResize);
  966. glutTimerFunc(25, update, 0);
  967. glutMainLoop();
  968.  
  969.  
  970. return 0;
  971. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement