-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMSSFMLView.cpp
More file actions
131 lines (124 loc) · 4.4 KB
/
MSSFMLView.cpp
File metadata and controls
131 lines (124 loc) · 4.4 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// Created by c on 4/10/24.
//
#include "MSSFMLView.h"
MSSFMLView::MSSFMLView(MinesweeperBoard & minesweeperboard): board(minesweeperboard) {
offsetX=100;
offsetY=100;
size=50;
font.loadFromFile("assets/mine-sweeper.ttf");
sf::Texture tmp;
tmp.loadFromFile("assets/cellup.png");
texture.push_back(tmp);
tmp.loadFromFile("assets/celldown.png");
texture.push_back(tmp);
}
void MSSFMLView::draw(sf::RenderWindow & win) const{
for (int row = 0; row < board.getBoardHeight(); row++)
for (int col = 0; col < board.getBoardWidth(); col++) {
drawBoard(row, col, win);
drawFont(row, col, win);
}
drawResetButton(win);
if(board.updateGameState()==FINISHED_WIN) drawString(win, "YOU WIN");
if(board.updateGameState()==FINISHED_LOSS) drawString(win, "YOU LOSS");
}
void MSSFMLView::drawBoard(int row, int col, sf::RenderWindow &win) const {
sf::RectangleShape tab(sf::Vector2f(size, size));
int Xo = offsetX + col * size;
int Yo = offsetY + row * size;
tab.setPosition(Xo, Yo);
if(board.getFieldInfo(row,col)=='F' or board.getFieldInfo(row,col)=='_')
tab.setTexture(&texture[0]);
else tab.setTexture(&texture[1]);
if(board.getFieldInfo(row,col)=='x') tab.setFillColor(sf::Color::Red);
win.draw(tab);
}
//"`" - flag; "*" - mine; " " - empty field or not revealed; numbers - numbers of mines around field
void MSSFMLView::drawFont(int row, int col, sf::RenderWindow &win) const{
sf::Text text;
text.setFont(font);
int Xo = offsetX + col * size;
int Yo = offsetY + row * size;
text.setPosition(Xo+0.5f*size, Yo+0.5f*size);
text.setCharacterSize(0.6*size);
switch (board.getFieldInfo(row, col)){
case 'F':
text.setString("`");
text.setFillColor(sf::Color::Black);
break;
case 'x':
text.setString("*");
text.setFillColor(sf::Color::Black);
break;
case '1':
text.setString('1');
text.setFillColor(sf::Color(54,203,233));
break;
case '2':
text.setString('2');
text.setFillColor(sf::Color(60,233,54));
break;
case '3':
text.setString('3');
text.setFillColor(sf::Color(225,145,7));
break;
case '4':
text.setString('4');
text.setFillColor(sf::Color(3,36,114));
break;
case '5':
text.setString('5');
text.setFillColor(sf::Color(115,92,1));
break;
case '6':
text.setString('6');
text.setFillColor(sf::Color(18,133,151));
break;
case '7':
text.setString('7');
text.setFillColor(sf::Color(37,26,8));
break;
case '8':
text.setString('8');
text.setFillColor(sf::Color(98,93,86));
break;
default:
break;
}
sf::FloatRect titlerect=text.getLocalBounds();
text.setOrigin((titlerect.left+titlerect.width)/2.0f,(titlerect.top+titlerect.height)/2.0f);
win.draw(text);
}
void MSSFMLView::drawResetButton(sf::RenderWindow &win) const {
sf::RectangleShape button(sf::Vector2f(3*size,size));
int Xo=offsetX+(board.getBoardWidth()/2-1)*size;
int Yo=offsetY+(board.getBoardHeight()+1)*size;
button.setPosition(Xo,Yo);
button.setFillColor(sf::Color(255, 69, 0));
win.draw(button);
sf::Text text;
text.setFont(font);
text.setPosition(Xo+1.5*size,Yo+0.5*size);
text.setCharacterSize(0.5*size);
text.setString("RESET");
sf::FloatRect titlerect=text.getLocalBounds();
text.setOrigin((titlerect.left+titlerect.width)/2.0f,(titlerect.top+titlerect.height)/2.0f);
win.draw(text);
}
void MSSFMLView::drawString(sf::RenderWindow & win,const std::string & letters) const{
sf::RectangleShape name(sf::Vector2f(5*size, 2*size));
int Xo=offsetX+(board.getBoardWidth()/2-2)*size;
int Yo=offsetY+(board.getBoardHeight()/2-1)*size;
name.setPosition(Xo,Yo);
name.setFillColor(sf::Color(51, 51, 255,70));
win.draw(name);
sf::Text text;
text.setFont(font);
text.setPosition(Xo+2.5*size,Yo+size);
text.setCharacterSize(0.6*size);
text.setString(letters);
sf::FloatRect titlerect=text.getLocalBounds();
text.setOrigin((titlerect.left+titlerect.width)/2.0f,(titlerect.top+titlerect.height)/2.0f);
win.draw(text);
}