Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include "GameLogic\Classes\Chessboard.h"
- #include "Piece.h"
- #include <SOIL.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream>
- #include <vector>
- #define GLUT_DISABLE_ATEXIT_HACK
- #include <GL/gl.h>
- #include <gl/glu.h>
- #include <GL/glut.h>
- #include "Debug.h"
- #include <sstream>
- #include <ctime>
- #include <fstream>
- #include <string>
- const int WIDTH = 1280;
- const int HEIGHT = 720;
- const float SQU = 14.98f;
- unsigned int fps = 1000/60;
- time_t startTime;
- time_t endTime;
- double whiteTime;
- double blackTime;
- double tempTime;
- bool gameOver = false;
- std::vector<std::string> highscoresList;
- int k = 0;
- int l = 0;
- int nr = 0;
- char out1[21];
- char out2[21];
- char out3[21] = "HIGHSCORES:";
- char out4[21] = "PLAYER 1:";
- char out5[21] = "PLAYER 2:";
- float theta1 = 45.0f;
- float theta2 = 0.0f;
- float theta3 = 1.0f;
- float widok = 0.0f;
- int button_x, button_y;
- const GLdouble left = - 10.0;
- const GLdouble right = 10.0;
- const GLdouble bottom = - 10.0;
- const GLdouble top = 10.0;
- GLfloat WhitePieceMaterialAmbDiff[] = {0.845, 0.845, 0.845, 1.0};
- GLfloat BlackPieceMaterialAmbDiff[] = {0.445, 0.445, 0.845, 1.0};
- GLfloat PieceMaterialSpecular[] = {1.0, 1.0, 1.0, 1.0};
- GLfloat PieceMaterialShininess[] = {78.6};
- GLfloat lightAmb[] = { 0.0, 0.0, 0.0, 1.0 };
- GLfloat lightDif[] = { 1.0, 1.0, 1.0, 1.0 };
- GLfloat lightSpec[] = { 1.0, 1.0, 1.0, 1.0 };
- GLfloat BoardGlassMaterialAmbDiff[] = {0.576, 0.373, 0.0, 1.0};
- GLfloat BoardGlassMaterialSpecular[] = {1.0, 1.0, 1.0, 1.0};
- GLfloat BoardGlassMaterialShininess[] = {120.0};
- GLfloat BoardWoodMaterialAmbDiff[] = {0.416, 0.416, 0.416, 1.0};
- GLfloat BoardWoodMaterialSpecular[] = {1.0, 1.0, 1.0, 1.0};
- GLfloat BoardWoodMaterialShininess[] = {70.6};
- GLfloat TableWoodMaterialAmbDiff[] = {0.3412, 0.8824, 0.3412, 1.0};
- GLfloat TableWoodMaterialSpecular[] = {0.3500, 0.3500, 0.3500, 1.0};
- GLfloat TableWoodMaterialShininess[] = {32.0};
- GLuint tex_2d[5];
- GLuint tex_2d_room[6];
- GLfloat vel = 3;
- GLfloat vel1 = 3;
- GLfloat kat = 0, katOff;
- GLfloat rotX = 1, rotY = 1, rotZ = 1;
- GLuint lista;
- GLUquadricObj *room;
- GameLogic::Chessboard gameboard;
- GameLogic::Chessboard gameboard2;
- std::pair<int, int> selectedSquare;
- std::pair<char, int> sourceMove = {0, 0};
- std::pair<char, int> destinationMove = {0, 0};
- std::list<std::pair<char, int>> greenMoves;
- bool drawGreen = false;
- bool up = false;
- bool bRight = false;
- bool once = true;
- bool once2 = true;
- bool anim = false;
- bool lighRotateOff = false;
- bool highscores = true;
- bool nick1 = true;
- bool nick2 = false;
- int button_state = GLUT_UP;
- std::string white = "TURA GRACZA ";
- std::string black = "TURA GRACZA ";
- std::string s;
- std::string nick1S = "";
- std::string nick2S = "";
- Piece Bishop, King, Knight, Pawn, Queen, Rook;
- Piece Board;
- Piece Table;
- double string_to_double( const std::string& s )
- {
- std::istringstream i(s);
- double x;
- if (!(i >> x))
- return 0;
- return x;
- }
- std::string double_to_string(double d)
- {
- std::ostringstream strs;
- strs << d;
- std::string str = strs.str();
- return str;
- }
- void AddHighScore(const std::string fileName, double seconds, std::string userName);
- std::vector<std::string> ShowHighScores(const std::string fileName);
- void drawPiece(Piece &p);
- void drawChessboard();
- void drawPossibleMoves(std::list<std::pair<char, int>> moves);
- void drawBoard();
- void display();
- void reshape(int w, int h);
- void klawiatura(unsigned char key, int x, int y);
- void special(int key, int x, int y);
- void MouseButton( int button, int state, int x, int y );
- void MouseMotion( int x, int y );
- void fps60(int val);
- void loadTexture(const char* path, GLuint &tex);
- void setupLighting();
- void setupScene();
- void loadObjects();
- void initList();
- void drawRoom();
- void drawTable();
- int main(int argc, char *argv[]) {
- gameboard = GameLogic::Chessboard();
- loadObjects();
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(WIDTH, HEIGHT);
- glutCreateWindow(argv[0]);
- glutPositionWindow(400, 200);
- setupScene();
- initList();
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutKeyboardFunc(klawiatura);
- glutSpecialFunc(special);
- glutMouseFunc(MouseButton);
- glutMotionFunc(MouseMotion);
- glutTimerFunc(100, fps60, 0);
- //PlaySound(TEXT("mjuzik.wav"), NULL, SND_ASYNC|SND_FILENAME|SND_LOOP);
- glutMainLoop();
- return 0;
- }
- void drawChessboard(){
- GameLogic::AbstractPiece* piece;
- for(int i = 0; i < 8; i++){
- for(int j = 0; j < 8; j++){
- if(anim && (sourceMove.first == GameLogic::Chessboard::fileLetterToArrayIndex(i)) && (sourceMove.second == GameLogic::Chessboard::rankNumberToArrayIndex(j))){
- continue;
- }
- if((piece = gameboard.getPieceAt(GameLogic::Chessboard::arrayIndexToFileLetter(i), GameLogic::Chessboard::arrayIndexToRankNumber(j))) != nullptr){
- if(piece->getOwner() == GameLogic::Color::WHITE){
- glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, WhitePieceMaterialAmbDiff);
- }else{
- glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlackPieceMaterialAmbDiff);
- }
- switch (piece -> type()){
- case GameLogic::PieceType::PAWN:
- glPushMatrix();
- glTranslatef(SQU * i, 0, -SQU * j);
- drawPiece(Pawn);
- glPopMatrix();
- break;
- case GameLogic::PieceType::KNIGHT:
- glPushMatrix();
- glTranslatef(SQU * i, 0, -SQU * j);
- drawPiece(Knight);
- glPopMatrix();
- break;
- case GameLogic::PieceType::BISHOP:
- glPushMatrix();
- glTranslatef(SQU * i, 0, -SQU * j);
- drawPiece(Bishop);
- glPopMatrix();
- break;
- case GameLogic::PieceType::ROOK:
- glPushMatrix();
- glTranslatef(SQU * i, 0, -SQU * j);
- drawPiece(Rook);
- glPopMatrix();
- break;
- case GameLogic::PieceType::QUEEN:
- glPushMatrix();
- glTranslatef(SQU * i, 0, -SQU * j);
- drawPiece(Queen);
- glPopMatrix();
- break;
- case GameLogic::PieceType::KING:
- glPushMatrix();
- glTranslatef(SQU * i, 0, -SQU * j);
- drawPiece(King);
- glPopMatrix();;
- break;
- }
- }
- }
- }
- }
- void drawPossibleMoves(std::list<std::pair<char, int>> moves){
- int x;
- for(std::pair<char, int> square : moves){
- switch (square.first)
- {
- case 'A':
- x = 0;
- break;
- case 'B':
- x = 1;
- break;
- case 'C':
- x = 2;
- break;
- case 'D':
- x = 3;
- break;
- case 'E':
- x = 4;
- break;
- case 'F':
- x = 5;
- break;
- case 'G':
- x = 6;
- break;
- case 'H':
- x = 7;
- break;
- }
- glPushMatrix();
- glBegin(GL_QUADS);
- glVertex3f((x*SQU)+(SQU/2.0f), 0.1, ((square.second - 1)* -SQU)+(SQU/2.0f));
- glVertex3f((x*SQU)+(SQU/2.0f), 0.1, ((square.second - 1)* -SQU)-(SQU/2.0f));
- glVertex3f((x*SQU)-(SQU/2.0f), 0.1, ((square.second - 1)* -SQU)-(SQU/2.0f));
- glVertex3f((x*SQU)-(SQU/2.0f), 0.1, ((square.second - 1)* -SQU)+(SQU/2.0f));
- glEnd();
- glPopMatrix();
- }
- }
- void drawBoard()
- {
- glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BoardWoodMaterialAmbDiff);
- glMaterialfv(GL_FRONT, GL_SPECULAR, BoardGlassMaterialSpecular);
- glMaterialfv(GL_FRONT, GL_SHININESS, BoardGlassMaterialShininess);
- glBindTexture(GL_TEXTURE_2D, tex_2d[2]);
- glBegin(GL_QUADS);
- glNormal3f(Board.vn[0].x, Board.vn[0].y, Board.vn[0].z);
- glTexCoord2f(Board.vt[0].x, Board.vt[0].y);
- glVertex3f(Board.v[0].x, Board.v[0].y, Board.v[0].z);
- glNormal3f(Board.vn[1].x, Board.vn[1].y, Board.vn[1].z);
- glTexCoord2f(Board.vt[1].x, Board.vt[1].y);
- glVertex3f(Board.v[1].x, Board.v[1].y, Board.v[1].z);
- glNormal3f(Board.vn[2].x, Board.vn[2].y, Board.vn[2].z);
- glTexCoord2f(Board.vt[2].x, Board.vt[2].y);
- glVertex3f(Board.v[2].x, Board.v[2].y, Board.v[2].z);
- glNormal3f(Board.vn[3].x, Board.vn[3].y, Board.vn[3].z);
- glTexCoord2f(Board.vt[3].x, Board.vt[3].y);
- glVertex3f(Board.v[3].x, Board.v[3].y, Board.v[3].z);
- glEnd();
- glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BoardGlassMaterialAmbDiff);
- glMaterialfv(GL_FRONT, GL_SPECULAR, BoardWoodMaterialSpecular);
- glMaterialfv(GL_FRONT, GL_SHININESS, BoardWoodMaterialShininess);
- glBindTexture(GL_TEXTURE_2D, tex_2d[1]);
- glBegin(GL_QUADS);
- for (unsigned int i = 4; i < Board.v.size(); i += 4)
- {
- glNormal3f(Board.vn[i].x, Board.vn[i].y, Board.vn[i].z);
- glTexCoord2f(Board.vt[i].x, Board.vt[i].y);
- glVertex3f(Board.v[i].x, Board.v[i].y, Board.v[i].z);
- glNormal3f(Board.vn[i+1].x, Board.vn[i+1].y, Board.vn[i+1].z);
- glTexCoord2f(Board.vt[i+1].x, Board.vt[i+1].y);
- glVertex3f(Board.v[i+1].x, Board.v[i+1].y, Board.v[i+1].z);
- glNormal3f(Board.vn[i+2].x, Board.vn[i+2].y, Board.vn[i+2].z);
- glTexCoord2f(Board.vt[i+2].x, Board.vt[i+2].y);
- glVertex3f(Board.v[i+2].x, Board.v[i+2].y, Board.v[i+2].z);
- glNormal3f(Board.vn[i+3].x, Board.vn[i+3].y, Board.vn[i+3].z);
- glTexCoord2f(Board.vt[i+3].x, Board.vt[i+3].y);
- glVertex3f(Board.v[i+3].x, Board.v[i+3].y, Board.v[i+3].z);
- }
- glEnd();
- }
- void drawTable()
- {
- glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, TableWoodMaterialAmbDiff);
- glMaterialfv(GL_FRONT, GL_SPECULAR, TableWoodMaterialSpecular);
- glMaterialfv(GL_FRONT, GL_SHININESS, TableWoodMaterialShininess);
- glBindTexture(GL_TEXTURE_2D, tex_2d[3]);
- glBegin(GL_QUADS);
- for (unsigned int i = 4; i < Table.v.size(); i += 4)
- {
- glNormal3f(Table.vn[i].x, Table.vn[i].y, Table.vn[i].z);
- glTexCoord2f(Table.vt[i].x, Table.vt[i].y);
- glVertex3f(Table.v[i].x, Table.v[i].y, Table.v[i].z);
- glNormal3f(Table.vn[i+1].x, Table.vn[i+1].y, Table.vn[i+1].z);
- glTexCoord2f(Table.vt[i+1].x, Table.vt[i+1].y);
- glVertex3f(Table.v[i+1].x, Table.v[i+1].y, Table.v[i+1].z);
- glNormal3f(Table.vn[i+2].x, Table.vn[i+2].y, Table.vn[i+2].z);
- glTexCoord2f(Table.vt[i+2].x, Table.vt[i+2].y);
- glVertex3f(Table.v[i+2].x, Table.v[i+2].y, Table.v[i+2].z);
- glNormal3f(Table.vn[i+3].x, Table.vn[i+3].y, Table.vn[i+3].z);
- glTexCoord2f(Table.vt[i+3].x, Table.vt[i+3].y);
- glVertex3f(Table.v[i+3].x, Table.v[i+3].y, Table.v[i+3].z);
- }
- glEnd();
- }
- void display() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslated(0, 0, -150);
- if((nick1 || nick2))
- {
- glPushMatrix();
- glDisable(GL_LIGHTING);
- glBegin(GL_QUADS);
- glColor3f(1.0, 0.5, 1.0);
- glVertex3f(-700, -700, 100); //przod
- glVertex3f(-700, 700, 100);
- glVertex3f(700, 700, 100);
- glVertex3f(700, -700, 100);
- glEnd();
- glTranslated(0, 0, 110);
- glColor3f(0, 0, 0);
- for (int i = 0; i < 21; ++i)
- {
- glRasterPos2d(-35 + 1*i, 10);
- glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, out4[i]);
- }
- for (int i = 0; i < 21; ++i)
- {
- glRasterPos2d(-35 + 1*i, -10);
- glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, out5[i]);
- }
- k = 0;
- for (std::string::iterator i = nick1S.begin(); i != nick1S.end(); ++i)
- {
- k++;
- glRasterPos2d(-15 + 1*k, 10);
- char c = *i;
- glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
- }
- k = 0;
- for (std::string::iterator i = nick2S.begin(); i != nick2S.end(); ++i)
- {
- k++;
- glRasterPos2d(-15 + 1*k, -10);
- char c = *i;
- glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
- }
- glEnable(GL_LIGHTING);
- glPopMatrix();
- }
- else if(highscores)
- {
- if(once2)
- {
- once2 = false;
- highscoresList = ShowHighScores("highscores.txt");
- }
- glPushMatrix();
- glDisable(GL_LIGHTING);
- glBegin(GL_QUADS);
- glColor3f(1.0, 0.0, 1.0);
- glVertex3f(-700, -700, 100); //przod
- glVertex3f(-700, 700, 100);
- glVertex3f(700, 700, 100);
- glVertex3f(700, -700, 100);
- glEnd();
- glTranslated(0, 0, 110);
- glColor3f(0, 0, 0);
- for (int i = 0; i < 21; ++i)
- {
- glRasterPos2d(-15 + 3*i, 20);
- glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, out3[i]);
- }
- if(highscoresList.empty())
- {
- printf("empty");
- }
- printf("%d",highscoresList.size());
- l = 20;
- nr = 0;
- for (std::vector<std::string>::iterator it = highscoresList.begin() ; it != highscoresList.end(); ++it)
- {
- l -= 5;
- k = 0;
- nr++;
- char c2 = nr + '0';
- glRasterPos2d(-16, l);
- glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c2);
- for(std::string::iterator it2 = (*it).begin(); it2 != (*it).end(); ++it2)
- {
- k++;
- glRasterPos2d(-15 + 1*k, l);
- char c = *it2;
- glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
- }
- }
- glEnable(GL_LIGHTING);
- glPopMatrix();
- }
- else
- {
- if(once){
- once = false;
- startTime = time(nullptr);
- }
- k = 0;
- glPushMatrix();
- glColor3f(1, 1, 1);
- glDisable(GL_LIGHTING);
- sprintf(out1, "Stracone figury: %d", gameboard.getPiecesTaken(GameLogic::Color::WHITE));
- sprintf(out2, "Stracone figury: %d", gameboard.getPiecesTaken(GameLogic::Color::BLACK));
- for (int i = 0; i < 21; ++i)
- {
- k++;
- glRasterPos2d(-130 + 3*k, 60);
- glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, out1[i]);
- }
- k = 0;
- for (int i = 0; i < 21; ++i)
- {
- k++;
- glRasterPos2d(60 + 3*k, 60);
- glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, out2[i]);
- }
- glEnable(GL_LIGHTING);
- glPopMatrix();
- k = 0;
- glPushMatrix();
- glDisable(GL_LIGHTING);
- if (gameboard.getPlayerToMove() == GameLogic::Color::WHITE)
- {
- s = white + nick1S;
- glColor3f(1, 1, 1);
- }
- else
- {
- s = black + nick2S;
- glColor3f(1, 1, 1);
- k = 45;
- }
- for (std::string::iterator i = s.begin(); i != s.end(); ++i)
- {
- k++;
- glRasterPos2d(-130 + 4*k, 80);
- char c = *i;
- glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
- }
- glEnable(GL_LIGHTING);
- glPopMatrix();
- glRotatef(theta1, 1.0f, 0.0f, 0.0f);
- glRotatef(theta2, 0.0f, 1.0f, 0.0f);
- glRotatef(widok, 0.0f, 1.0f, 0.0f);
- glScalef(theta3, theta3, theta3);
- drawRoom();
- glPushMatrix();
- glScaled(0.3, 0.3, 0.3);
- glTranslatef(0, -805, 0);
- drawTable();
- glPopMatrix();
- glPushMatrix();
- glRotatef(kat, rotX, rotY, rotZ);
- setupLighting();
- glPopMatrix();
- glPushMatrix();
- glScaled(1.2, 1, 1.2);
- drawBoard();
- glPopMatrix();
- glTranslatef(-52.5, 0, 52.5);
- drawChessboard();
- glPushMatrix();
- glDisable(GL_TEXTURE_2D);
- glDisable(GL_LIGHTING);
- if (gameboard.getPlayerToMove() == GameLogic::Color::WHITE)
- {
- glColor3f(0, 0.0f, 1.0f);
- if (widok == 360.f)
- widok = 0.0f;
- if (widok > 0.0f)
- widok += 8.0f;
- }
- else
- {
- if (widok < 180)
- widok += 8.0f;
- glColor3f(1.0f, 0.0f, 0.0f);
- }
- glBegin(GL_QUADS);
- glVertex3f((selectedSquare.first*SQU)+(SQU/2.0f), 0.1, (-selectedSquare.second*SQU)+(SQU/2.0f));
- glVertex3f((selectedSquare.first*SQU)+(SQU/2.0f), 0.1, (-selectedSquare.second*SQU)-(SQU/2.0f));
- glVertex3f((selectedSquare.first*SQU)-(SQU/2.0f), 0.1, (-selectedSquare.second*SQU)-(SQU/2.0f));
- glVertex3f((selectedSquare.first*SQU)-(SQU/2.0f), 0.1, (-selectedSquare.second*SQU)+(SQU/2.0f));
- glEnd();
- glEnable(GL_TEXTURE_2D);
- glEnable(GL_LIGHTING);
- glPopMatrix();
- glPushMatrix();
- glDisable(GL_TEXTURE_2D);
- glDisable(GL_LIGHTING);
- glColor3f(0.0f, 1.0f, 0.0f);
- if(drawGreen){
- drawPossibleMoves(greenMoves);
- }
- glEnable(GL_TEXTURE_2D);
- glEnable(GL_LIGHTING);
- glPopMatrix();
- gluDeleteQuadric(room);
- }
- glutSwapBuffers();
- }
- void reshape(int w, int h) {
- glViewport(0, 0, (GLsizei)w, (GLsizei)h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- //glOrtho(-w, w, -h, h, -700, 700);
- gluPerspective(60, (float)w / h, 1, 1870);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void klawiatura(unsigned char key, int x, int y)
- {
- if(nick1)
- {
- nick1S.push_back(key);
- }
- if(nick2)
- {
- nick2S.push_back(key);
- }
- switch (key)
- {
- case 8:
- if(nick1)
- {
- nick1S.pop_back();
- }
- if(nick2)
- {
- nick2S.pop_back();
- }
- break;
- case 'i':
- if(!gameOver){
- highscores = !highscores;
- }
- break;
- case 27:
- case 'q':
- if(!(nick1 || nick2))
- {
- PlaySound(NULL, NULL, NULL);
- exit(0);
- }
- break;
- case 'x':
- rotX = !rotX;
- break;
- case 'y':
- rotY = !rotY;
- break;
- case 'z':
- rotZ = !rotZ;
- break;
- case 's':
- lighRotateOff = !lighRotateOff;
- break;
- case 1:
- case '+':
- vel1 += 0.5;
- system("cls");
- break;
- case '-':
- if ((vel1 -= 0.5) <= 0)
- vel1 = 0;
- system("cls");
- break;
- case 'j':
- if ((vel -= 1) <= 0)
- {
- vel = 0;
- }
- system("cls");
- break;
- case 'k':
- vel += 1;
- system("cls");
- break;
- case '\\':
- gameOver = true;
- highscores = true;
- if(gameboard.getPlayerToMove() == GameLogic::Color::BLACK){
- tempTime = whiteTime;
- AddHighScore("highscores.txt", tempTime, nick1S);
- }else{
- tempTime = blackTime;
- AddHighScore("highscores.txt", tempTime, nick2S);
- }
- highscoresList = ShowHighScores("highscores.txt");
- break;
- case 32: //SPACE
- if(!(nick1 || nick2))
- {
- if (drawGreen == false)
- {
- drawGreen = true;
- }
- else
- {
- drawGreen = false;
- }
- greenMoves = gameboard.getListOfPossibleMoves(GameLogic::Chessboard::arrayIndexToFileLetter(selectedSquare.first), GameLogic::Chessboard::arrayIndexToRankNumber(selectedSquare.second));
- }
- break;
- case 13: //ENTER
- if(!(nick1 || nick2))
- {
- if (drawGreen == false)
- {
- drawGreen = true;
- }
- else
- {
- drawGreen = false;
- }
- greenMoves = gameboard.getListOfPossibleMoves(GameLogic::Chessboard::arrayIndexToFileLetter(selectedSquare.first), GameLogic::Chessboard::arrayIndexToRankNumber(selectedSquare.second));
- if (up == false)
- {
- up = true;
- anim = false;
- sourceMove.first = GameLogic::Chessboard::arrayIndexToFileLetter(selectedSquare.first);
- sourceMove.second = GameLogic::Chessboard::arrayIndexToRankNumber(selectedSquare.second);
- std::cout<<sourceMove.first<<" "<<sourceMove.second<<"\n";
- }
- else
- {
- up = false;
- destinationMove.first = GameLogic::Chessboard::arrayIndexToFileLetter(selectedSquare.first);
- destinationMove.second = GameLogic::Chessboard::arrayIndexToRankNumber(selectedSquare.second);
- std::cout<<destinationMove.first<<" "<<destinationMove.second<<"\n";
- gameboard2 = gameboard;
- bool tmp = gameboard.tryMove(sourceMove, destinationMove);
- if(tmp){
- endTime = time(nullptr);
- if(gameboard.getPlayerToMove() == GameLogic::Color::BLACK){
- whiteTime += difftime(endTime, startTime);
- startTime = time(nullptr);
- }else{
- blackTime += difftime(endTime, startTime);
- startTime = time(nullptr);
- }
- }
- std::cout<<"tryMove: "<<tmp<<"\n";
- std::cout<<"white: "<<whiteTime<<"\n";
- std::cout<<"black: "<<blackTime<<"\n";
- }
- }
- if(nick2)
- {
- nick2 = false;
- }
- if(nick1)
- {
- nick1 = false;
- nick2 = true;
- }
- break;
- }
- }
- void special(int key, int x, int y)
- {
- if (gameboard.getPlayerToMove() == GameLogic::Color::WHITE)
- {
- switch(key)
- {
- case GLUT_KEY_UP :
- if (selectedSquare.second++ >= 7)
- {
- selectedSquare.second = 7;
- }
- break;
- case GLUT_KEY_DOWN :
- if (selectedSquare.second-- <= 0)
- {
- selectedSquare.second = 0;
- }
- break;
- case GLUT_KEY_LEFT :
- if (selectedSquare.first-- <= 0)
- {
- selectedSquare.first = 0;
- }
- break;
- case GLUT_KEY_RIGHT :
- if (selectedSquare.first++ >= 7 )
- {
- selectedSquare.first = 7;
- }
- break;
- }
- }
- else
- {
- switch(key)
- {
- case GLUT_KEY_DOWN :
- if (selectedSquare.second++ >= 7)
- {
- selectedSquare.second = 7;
- }
- break;
- case GLUT_KEY_UP :
- if (selectedSquare.second-- <= 0)
- {
- selectedSquare.second = 0;
- }
- break;
- case GLUT_KEY_RIGHT :
- if (selectedSquare.first-- <= 0)
- {
- selectedSquare.first = 0;
- }
- break;
- case GLUT_KEY_LEFT :
- if (selectedSquare.first++ >= 7 )
- {
- selectedSquare.first = 7;
- }
- break;
- }
- }
- }
- void MouseButton( int button, int state, int x, int y )
- {
- if( button == GLUT_LEFT_BUTTON )
- {
- bRight = false;
- button_state = state;
- if( state == GLUT_DOWN )
- {
- button_x = x;
- button_y = y;
- }
- }
- if( button == GLUT_RIGHT_BUTTON )
- {
- bRight = true;
- button_state = state;
- if( state == GLUT_DOWN )
- {
- button_x = x;
- button_y = y;
- }
- }
- }
- void MouseMotion( int x, int y )
- {
- if( (button_state == GLUT_DOWN) && !(bRight) )
- {
- theta2 += vel *( right - left ) / glutGet( GLUT_WINDOW_WIDTH ) *( x - button_x );
- button_x = x;
- theta1 += vel *( top - bottom ) / glutGet( GLUT_WINDOW_HEIGHT ) *( button_y - y );
- button_y = y;
- }
- if( (button_state == GLUT_DOWN) && (bRight) )
- {
- theta3 += vel1 *( right - left ) / glutGet( GLUT_WINDOW_WIDTH ) *( x - button_x );
- button_x = x;
- }
- }
- void fps60(int val) {
- if(theta1 >= 360) theta1 -= 360;
- if(theta2 >= 360) theta2 -= 360;
- if(theta3 <= 0.2f) theta3 += 0.2f;
- if (kat >= 360) kat -= 360;
- if(!lighRotateOff) kat = kat + 1;
- glutPostRedisplay();
- glutTimerFunc(fps, fps60, 0);
- }
- void loadTexture(const char* path, GLuint &tex)
- {
- glBindTexture(GL_TEXTURE_2D, tex);
- tex = SOIL_load_OGL_texture
- (
- path,
- SOIL_LOAD_AUTO,
- SOIL_CREATE_NEW_ID,
- SOIL_FLAG_INVERT_Y
- );
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- }
- void setupLighting()
- {
- GLfloat light_position[] = { 0, 100, 0, 1.0 };
- glLightfv(GL_LIGHT1, GL_POSITION, light_position);
- glLightfv(GL_LIGHT1, GL_AMBIENT, lightAmb);
- glLightfv(GL_LIGHT1, GL_DIFFUSE, lightDif);
- glLightfv(GL_LIGHT1, GL_SPECULAR, lightSpec);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT1);
- }
- void setupScene()
- {
- glEnable(GL_DEPTH_TEST);
- glGenTextures(1, &tex_2d[0]);
- glGenTextures(1, &tex_2d[1]);
- glGenTextures(1, &tex_2d[2]);
- glGenTextures(1, &tex_2d[3]);
- glGenTextures(1, &tex_2d[4]);
- glGenTextures(1, &tex_2d_room[0]);
- glGenTextures(1, &tex_2d_room[1]);
- glGenTextures(1, &tex_2d_room[2]);
- glGenTextures(1, &tex_2d_room[3]);
- glGenTextures(1, &tex_2d_room[4]);
- glGenTextures(1, &tex_2d_room[5]);
- loadTexture("Wood.jpg", tex_2d[0]);
- loadTexture("Wood1.bmp", tex_2d[1]);
- loadTexture("cb.jpg", tex_2d[2]);
- loadTexture("bro1.jpg", tex_2d[3]);
- loadTexture("posx.jpg", tex_2d_room[0]);
- loadTexture("posy.jpg", tex_2d_room[1]);
- loadTexture("posz.jpg", tex_2d_room[2]);
- loadTexture("negx.jpg", tex_2d_room[3]);
- loadTexture("negy.jpg", tex_2d_room[4]);
- loadTexture("negz.jpg", tex_2d_room[5]);
- glEnable(GL_TEXTURE_2D);
- }
- void loadObjects()
- {
- Bishop.loadOBJ("Bishop.obj", Bishop.v, Bishop.vt, Bishop.vn);
- Bishop.id = 1;
- King.loadOBJ("King.obj", King.v, King.vt, King.vn);
- King.id = 2;
- Knight.loadOBJ("Knight.obj", Knight.v, Knight.vt, Knight.vn);
- Knight.id = 3;
- Pawn.loadOBJ("Pawn.obj", Pawn.v, Pawn.vt, Pawn.vn);
- Pawn.id = 4;
- Queen.loadOBJ("Queen.obj", Queen.v, Queen.vt, Queen.vn);
- Queen.id = 5;
- Rook.loadOBJ("Rook.obj", Rook.v, Rook.vt, Rook.vn);
- Rook.id = 6;
- Board.loadOBJ("Board.obj", Board.v, Board.vt, Board.vn);
- Table.loadOBJ("Bartable.obj", Table.v, Table.vt, Table.vn);
- std::cout<<"Bishop: "<<Bishop.v.size()<<"\n";
- std::cout<<"King: "<<King.v.size()<<"\n";
- std::cout<<"Knight: "<<Knight.v.size()<<"\n";
- std::cout<<"Pawn: "<<Pawn.v.size()<<"\n";
- std::cout<<"Queen: "<<Queen.v.size()<<"\n";
- std::cout<<"Rook: "<<Rook.v.size()<<"\n";
- }
- void drawPiece(Piece &p)
- {
- glMaterialfv(GL_FRONT, GL_SPECULAR, PieceMaterialSpecular);
- glMaterialfv(GL_FRONT, GL_SHININESS, PieceMaterialShininess);
- glBindTexture(GL_TEXTURE_2D, tex_2d[0]);
- int error;
- // DEBUG_MSG(std::cout, "przed petla");
- switch(p.id){
- case 1:
- glCallList(lista);
- break;
- case 2:
- glCallList(lista+1);
- break;
- case 3:
- glCallList(lista+2);
- break;
- case 4:
- glCallList(lista+3);
- break;
- case 5:
- glCallList(lista+4);
- break;
- case 6:
- glCallList(lista+5);
- break;
- }
- // DEBUG_MSG(std::cout, "po petli");
- glEnd();
- }
- void initList()
- {
- lista = glGenLists(6);
- glNewList(lista, GL_COMPILE);
- glBegin(GL_QUADS);
- for (unsigned int i = 0; i < Bishop.v.size(); i += 4)
- {
- glNormal3f(Bishop.vn[i].x, Bishop.vn[i].y, Bishop.vn[i].z);
- glTexCoord2f(Bishop.vt[i].x, Bishop.vt[i].y);
- glVertex3f(Bishop.v[i].x, Bishop.v[i].y, Bishop.v[i].z);
- glNormal3f(Bishop.vn[i+1].x, Bishop.vn[i+1].y, Bishop.vn[i+1].z);
- glTexCoord2f(Bishop.vt[i+1].x, Bishop.vt[i+1].y);
- glVertex3f(Bishop.v[i+1].x, Bishop.v[i+1].y, Bishop.v[i+1].z);
- glNormal3f(Bishop.vn[i+2].x, Bishop.vn[i+2].y, Bishop.vn[i+2].z);
- glTexCoord2f(Bishop.vt[i+2].x, Bishop.vt[i+2].y);
- glVertex3f(Bishop.v[i+2].x, Bishop.v[i+2].y, Bishop.v[i+2].z);
- glNormal3f(Bishop.vn[i+3].x, Bishop.vn[i+3].y, Bishop.vn[i+3].z);
- glTexCoord2f(Bishop.vt[i+3].x, Bishop.vt[i+3].y);
- glVertex3f(Bishop.v[i+3].x, Bishop.v[i+3].y, Bishop.v[i+3].z);
- }
- glEnd();
- glEndList();
- glNewList(lista+1, GL_COMPILE);
- glBegin(GL_QUADS);
- for (unsigned int i = 0; i < King.v.size(); i += 4)
- {
- glNormal3f(King.vn[i].x, King.vn[i].y, King.vn[i].z);
- glTexCoord2f(King.vt[i].x, King.vt[i].y);
- glVertex3f(King.v[i].x, King.v[i].y, King.v[i].z);
- glNormal3f(King.vn[i+1].x, King.vn[i+1].y, King.vn[i+1].z);
- glTexCoord2f(King.vt[i+1].x, King.vt[i+1].y);
- glVertex3f(King.v[i+1].x, King.v[i+1].y, King.v[i+1].z);
- glNormal3f(King.vn[i+2].x, King.vn[i+2].y, King.vn[i+2].z);
- glTexCoord2f(King.vt[i+2].x, King.vt[i+2].y);
- glVertex3f(King.v[i+2].x, King.v[i+2].y, King.v[i+2].z);
- glNormal3f(King.vn[i+3].x, King.vn[i+3].y, King.vn[i+3].z);
- glTexCoord2f(King.vt[i+3].x, King.vt[i+3].y);
- glVertex3f(King.v[i+3].x, King.v[i+3].y, King.v[i+3].z);
- }
- glEnd();
- glEndList();
- glNewList(lista+2, GL_COMPILE);
- glBegin(GL_QUADS);
- for (unsigned int i = 0; i < Knight.v.size(); i += 4)
- {
- glNormal3f(Knight.vn[i].x, Knight.vn[i].y, Knight.vn[i].z);
- glTexCoord2f(Knight.vt[i].x, Knight.vt[i].y);
- glVertex3f(Knight.v[i].x, Knight.v[i].y, Knight.v[i].z);
- glNormal3f(Knight.vn[i+1].x, Knight.vn[i+1].y, Knight.vn[i+1].z);
- glTexCoord2f(Knight.vt[i+1].x, Knight.vt[i+1].y);
- glVertex3f(Knight.v[i+1].x, Knight.v[i+1].y, Knight.v[i+1].z);
- glNormal3f(Knight.vn[i+2].x, Knight.vn[i+2].y, Knight.vn[i+2].z);
- glTexCoord2f(Knight.vt[i+2].x, Knight.vt[i+2].y);
- glVertex3f(Knight.v[i+2].x, Knight.v[i+2].y, Knight.v[i+2].z);
- glNormal3f(Knight.vn[i+3].x, Knight.vn[i+3].y, Knight.vn[i+3].z);
- glTexCoord2f(Knight.vt[i+3].x, Knight.vt[i+3].y);
- glVertex3f(Knight.v[i+3].x, Knight.v[i+3].y, Knight.v[i+3].z);
- }
- glEnd();
- glEndList();
- glNewList(lista+3, GL_COMPILE);
- glBegin(GL_QUADS);
- for (unsigned int i = 0; i < Pawn.v.size(); i += 4)
- {
- glNormal3f(Pawn.vn[i].x, Pawn.vn[i].y, Pawn.vn[i].z);
- glTexCoord2f(Pawn.vt[i].x, Pawn.vt[i].y);
- glVertex3f(Pawn.v[i].x, Pawn.v[i].y, Pawn.v[i].z);
- glNormal3f(Pawn.vn[i+1].x, Pawn.vn[i+1].y, Pawn.vn[i+1].z);
- glTexCoord2f(Pawn.vt[i+1].x, Pawn.vt[i+1].y);
- glVertex3f(Pawn.v[i+1].x, Pawn.v[i+1].y, Pawn.v[i+1].z);
- glNormal3f(Pawn.vn[i+2].x, Pawn.vn[i+2].y, Pawn.vn[i+2].z);
- glTexCoord2f(Pawn.vt[i+2].x, Pawn.vt[i+2].y);
- glVertex3f(Pawn.v[i+2].x, Pawn.v[i+2].y, Pawn.v[i+2].z);
- glNormal3f(Pawn.vn[i+3].x, Pawn.vn[i+3].y, Pawn.vn[i+3].z);
- glTexCoord2f(Pawn.vt[i+3].x, Pawn.vt[i+3].y);
- glVertex3f(Pawn.v[i+3].x, Pawn.v[i+3].y, Pawn.v[i+3].z);
- }
- glEnd();
- glEndList();
- glNewList(lista+4, GL_COMPILE);
- glBegin(GL_QUADS);
- for (unsigned int i = 0; i < Queen.v.size(); i += 4)
- {
- glNormal3f(Queen.vn[i].x, Queen.vn[i].y, Queen.vn[i].z);
- glTexCoord2f(Queen.vt[i].x, Queen.vt[i].y);
- glVertex3f(Queen.v[i].x, Queen.v[i].y, Queen.v[i].z);
- glNormal3f(Queen.vn[i+1].x, Queen.vn[i+1].y, Queen.vn[i+1].z);
- glTexCoord2f(Queen.vt[i+1].x, Queen.vt[i+1].y);
- glVertex3f(Queen.v[i+1].x, Queen.v[i+1].y, Queen.v[i+1].z);
- glNormal3f(Queen.vn[i+2].x, Queen.vn[i+2].y, Queen.vn[i+2].z);
- glTexCoord2f(Queen.vt[i+2].x, Queen.vt[i+2].y);
- glVertex3f(Queen.v[i+2].x, Queen.v[i+2].y, Queen.v[i+2].z);
- glNormal3f(Queen.vn[i+3].x, Queen.vn[i+3].y, Queen.vn[i+3].z);
- glTexCoord2f(Queen.vt[i+3].x, Queen.vt[i+3].y);
- glVertex3f(Queen.v[i+3].x, Queen.v[i+3].y, Queen.v[i+3].z);
- }
- glEnd();
- glEndList();
- glNewList(lista+5, GL_COMPILE);
- glBegin(GL_QUADS);
- for (unsigned int i = 0; i < Rook.v.size(); i += 4)
- {
- glNormal3f(Rook.vn[i].x, Rook.vn[i].y, Rook.vn[i].z);
- glTexCoord2f(Rook.vt[i].x, Rook.vt[i].y);
- glVertex3f(Rook.v[i].x, Rook.v[i].y, Rook.v[i].z);
- glNormal3f(Rook.vn[i+1].x, Rook.vn[i+1].y, Rook.vn[i+1].z);
- glTexCoord2f(Rook.vt[i+1].x, Rook.vt[i+1].y);
- glVertex3f(Rook.v[i+1].x, Rook.v[i+1].y, Rook.v[i+1].z);
- glNormal3f(Rook.vn[i+2].x, Rook.vn[i+2].y, Rook.vn[i+2].z);
- glTexCoord2f(Rook.vt[i+2].x, Rook.vt[i+2].y);
- glVertex3f(Rook.v[i+2].x, Rook.v[i+2].y, Rook.v[i+2].z);
- glNormal3f(Rook.vn[i+3].x, Rook.vn[i+3].y, Rook.vn[i+3].z);
- glTexCoord2f(Rook.vt[i+3].x, Rook.vt[i+3].y);
- glVertex3f(Rook.v[i+3].x, Rook.v[i+3].y, Rook.v[i+3].z);
- }
- glEnd();
- glEndList();
- }
- void drawRoom()
- {
- glBindTexture(GL_TEXTURE_2D, tex_2d_room[2]);
- glBegin(GL_QUADS);
- glTexCoord2f(0, 0);glVertex3f(-700, -700, -700); //przod
- glTexCoord2f(0, 1);glVertex3f(-700, 700, -700);
- glTexCoord2f(1, 1);glVertex3f(700, 700, -700);
- glTexCoord2f(1, 0);glVertex3f(700, -700, -700);
- glEnd();
- glBindTexture(GL_TEXTURE_2D, tex_2d_room[3]);
- glBegin(GL_QUADS);
- glTexCoord2f(0, 0);glVertex3f(-700, -700, 700); //lewa
- glTexCoord2f(0, 1);glVertex3f(-700, 700, 700);
- glTexCoord2f(1, 1);glVertex3f(-700, 700, -700);
- glTexCoord2f(1, 0);glVertex3f(-700, -700, -700);
- glEnd();
- glBindTexture(GL_TEXTURE_2D, tex_2d_room[5]);
- glBegin(GL_QUADS);
- glTexCoord2f(1, 0);glVertex3f(-700, -700, 700); //tyl
- glTexCoord2f(1, 1);glVertex3f(-700, 700, 700);
- glTexCoord2f(0, 1);glVertex3f(700, 700, 700);
- glTexCoord2f(0, 0);glVertex3f(700, -700, 700);
- glEnd();
- glBindTexture(GL_TEXTURE_2D, tex_2d_room[0]);
- glBegin(GL_QUADS);
- glTexCoord2f(0, 0);glVertex3f(700, -700, -700); //prawa
- glTexCoord2f(0, 1);glVertex3f(700, 700, -700);
- glTexCoord2f(1, 1);glVertex3f(700, 700, 700);
- glTexCoord2f(1, 0);glVertex3f(700, -700, 700);
- glEnd();
- glBindTexture(GL_TEXTURE_2D, tex_2d_room[1]);
- glBegin(GL_QUADS);
- glTexCoord2f(0, 0);glVertex3f(-700, 700, -700); //gora
- glTexCoord2f(0, 1);glVertex3f(-700, 700, 700);
- glTexCoord2f(1, 1);glVertex3f(700, 700, 700);
- glTexCoord2f(1, 0);glVertex3f(700, 700, -700);
- glEnd();
- glBindTexture(GL_TEXTURE_2D, tex_2d_room[4]);
- glBegin(GL_QUADS);
- glTexCoord2f(0, 0);glVertex3f(-700, -700, 700); //dol
- glTexCoord2f(0, 1);glVertex3f(-700, -700, -700);
- glTexCoord2f(1, 1);glVertex3f(700, -700, -700);
- glTexCoord2f(1, 0);glVertex3f(700, -700, 700);
- glEnd();
- }
- void AddHighScore(const std::string fileName, double seconds, std::string userName)
- {
- std::fstream file;
- std::vector<std::pair<std::string, double>> scores;
- int i = 0;
- std::string tmp;
- bool exitFlag = false;
- file.open(fileName, std::ios::in);
- while(std::getline(file, tmp) && (i < 10))
- {
- std::string buf; // Have a buffer string
- std::stringstream ss(tmp); // Insert the string into a stream
- std::vector<std::string> tokens; // Create vector to hold our words
- while (ss >> buf){
- tokens.push_back(buf);
- }
- scores.push_back(std::pair<std::string, double>(tokens[0], string_to_double(tokens[1])));
- i++;
- }
- if(scores.size() < 10)
- {
- for(std::vector<std::pair<std::string, double>>::iterator j = scores.begin(); j!= scores.end(); ++j)
- {
- if(((*j).second) < seconds)
- {
- scores.insert(j, std::pair<std::string, double>(userName, seconds));
- exitFlag = true;
- break;
- }
- }
- if(!exitFlag)
- scores.push_back( std::pair<std::string, double>(userName, seconds));
- }
- else
- {
- for(std::vector<std::pair<std::string, double>>::iterator j = scores.begin(); j!= scores.end(); ++j)
- {
- if(((*j).second) < seconds)
- {
- scores.insert(j, std::pair<std::string, double>(userName, seconds));
- break;
- }
- }
- scores.pop_back();
- }
- file.close();
- file.open(fileName, std::ios::out | std::ios::trunc);
- for(std::vector<std::pair<std::string, double>>::iterator j = scores.begin(); j!= scores.end(); ++j)
- {
- file<<userName + " " + double_to_string((*j).second)<<"\n";
- }
- file.close();
- }
- std::vector<std::string> ShowHighScores(const std::string fileName)
- {
- std::ifstream file;
- std::vector<std::string> scores;
- int i = 0;
- std::string tmp;
- file.open(fileName, std::ios::in);
- if(!file.is_open()){
- }
- while(std::getline(file, tmp) && (i < 10))
- {
- std::cout<<"iteracja " + tmp + "\n";
- scores.push_back(tmp);
- i++;
- }
- if(scores.empty())
- {
- printf("empty");
- }
- return scores;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement