-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
66 lines (58 loc) · 2.31 KB
/
Board.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stb_image.h>
#include <map>
#include "Board.h"
Board::Board(std::string texturesDirectory, glm::vec3 startingPoint, std::string shadersDirectory, glm::mat4 *projectionMatrix, Camera* camera, Light* light, Material* material) {
m_TexturesDirectory = texturesDirectory;
m_ShadersDirectory = shadersDirectory;
m_GL_ViewMatrix = camera->getViewMatrix();
m_GL_StartingPoint = startingPoint;
m_GL_Camera = camera;
m_GL_Light = light;
m_GL_Material = material;
m_GL_ProjectionMatrix = projectionMatrix;
int i = 0;
for (unsigned int file = 0; file < 8; file++) {
for (unsigned int rank = 0; rank < 8; rank++) {
bool isLight = (file + rank) % 2 != 0;
m_Squares[i] = Square((m_TexturesDirectory + "/dark.jpg").c_str(), (m_TexturesDirectory + "/light.jpg").c_str(), (m_ShadersDirectory + "/square.vert").c_str(), (m_ShadersDirectory + "/square.frag").c_str(), file, rank, isLight, glm::vec3(startingPoint.x + file, startingPoint.y + rank, startingPoint.z), m_GL_ProjectionMatrix, m_GL_Camera, m_GL_Light, m_GL_Material);
i++;
}
}
LoadPositionFromFen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
}
void Board::Draw(glm::vec3 mouseRay, bool mouseIsDown) {
for (int i = 0; i < 64; i++) {
m_Squares[i].Draw(mouseRay, mouseIsDown);
}
}
void Board::LoadPositionFromFen(std::string fenString) {
std::map<char, Type> charToPiece = { {'r', Rook}, {'n', Knight}, {'b', Bishop}, {'q', Queen}, {'k', King}, {'p', Pawn} };
int file = 0;
int rank = 7;
int i = 0;
std::string delimiter = " ";
std::string fen = fenString.substr(0, fenString.find(delimiter));
for (auto& symbol : fen) {
if (symbol == '/') {
file = 0;
rank--;
}
else {
if (isdigit(symbol)) {
file += symbol - '0';
}
else {
Color pieceColor = Black;
if (isupper(symbol)) {
pieceColor = White;
}
Type pieceType = charToPiece[tolower(symbol)];
m_Pieces[i] = Piece((m_ShadersDirectory + "/chesset.vert").c_str(), (m_ShadersDirectory + "/chesset.frag").c_str(), (m_ShadersDirectory + "/outline.vert").c_str(), (m_ShadersDirectory + "/outline.frag").c_str(), pieceColor, pieceType, m_GL_ProjectionMatrix, m_GL_Camera, m_GL_Light, m_GL_Material);
m_Pieces[i].m_GL_Position = m_Squares[file * 8 + rank].m_GL_Position;
m_Squares[file * 8 + rank].m_Piece = &m_Pieces[i];
file++;
i++;
}
}
}
}