-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.cpp
More file actions
101 lines (92 loc) · 3.85 KB
/
Copy pathGUI.cpp
File metadata and controls
101 lines (92 loc) · 3.85 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "GUI.h"
GUI::GUI(int width, int height, std::string title,ChessGame& game):m_MainWindow(sf::RenderWindow(sf::VideoMode(width,height),title)),m_Game(game)
{
drawableObjects background;
this->m_DrawableObjects.insert(std::make_pair('0',background));
this->m_DrawableObjects.at('0').texture.loadFromFile("C:\\Dev\\Chess\\assets\\board.PNG");
this->m_DrawableObjects.at('0').sprite.setTexture(this->m_DrawableObjects.at('0').texture);
std::vector<std::string> pieces_identifiers = {"bishop","king","night","pawn","queen","rook"};
for (std::string &ident : pieces_identifiers)
{
drawableObjects curr_obj;
this->m_DrawableObjects.insert(std::make_pair(std::toupper(ident[0]),curr_obj));
this->m_DrawableObjects.at(std::toupper(ident[0])).texture.loadFromFile("C:\\Dev\\Chess\\assets\\pieces\\black_"+ident+".png");
this->m_DrawableObjects.at(std::toupper(ident[0])).texture.setSmooth(true);
this->m_DrawableObjects.at(std::toupper(ident[0])).sprite.setTexture(this->m_DrawableObjects.at(std::toupper(ident[0])).texture);
this->m_DrawableObjects.at(std::toupper(ident[0])).sprite.setScale(scale_pieces,scale_pieces);
}
for (std::string &ident : pieces_identifiers)
{
drawableObjects curr_obj;
this->m_DrawableObjects.insert(std::make_pair(ident[0],curr_obj));
this->m_DrawableObjects.at(ident[0]).texture.loadFromFile("C:\\Dev\\Chess\\assets\\pieces\\white_"+ident+".png");
this->m_DrawableObjects.at(ident[0]).texture.setSmooth(true);
this->m_DrawableObjects.at(ident[0]).sprite.setTexture(this->m_DrawableObjects.at(ident[0]).texture);
this->m_DrawableObjects.at(ident[0]).sprite.setScale(scale_pieces,scale_pieces);
}
}
int GUI::getIndexFromPosition(int x, int y)
{
int row = y/113;
int col = x/113;
return row + col + 7*row;
}
sf::Vector2f GUI::getPositionFromIndex(int index)
{
return sf::Vector2f ((index%8+1)*113-56-sprite_offset,(int(index/8)+1)*113-56-sprite_offset);
}
void GUI::drawSprites()
{
this->m_MainWindow.clear();
this->m_MainWindow.draw(this->m_DrawableObjects.at('0').sprite);
for(auto &board : this->m_Game.getBoardStates())
{
for (auto &index: board.second.getPieceIndexes())
{
if (board.first != '0') {
this->m_DrawableObjects.at(board.first).sprite.setPosition(getPositionFromIndex(index));
this->m_MainWindow.draw(this->m_DrawableObjects.at(board.first).sprite);
}
}
}
}
void GUI::run()
{
int start_index = -1;
bool left_clicked = false;
while(this->m_MainWindow.isOpen())
{
sf::Event event;
while (this->m_MainWindow.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
this->m_MainWindow.close();
break;
case sf::Event::MouseButtonPressed:
if (event.mouseButton.button==sf::Mouse::Left)
{
start_index = getIndexFromPosition(event.mouseButton.x,event.mouseButton.y);
left_clicked=true;
}
break;
case sf::Event::MouseButtonReleased:
if(event.mouseButton.button == sf::Mouse::Left)
{
if (left_clicked && start_index !=-1)
{
this->m_Game.make_move(start_index, getIndexFromPosition(event.mouseButton.x,event.mouseButton.y));
left_clicked = false;
start_index = -1;
}
}
break;
default:
break;
}
}
this->drawSprites();
this->m_MainWindow.display();
}
}