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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#include "MainWindow.hpp"
MainWindow::MainWindow(int width, int height, std::string title):
RenderWindow(sf::VideoMode(width,height), title,sf::Style::Titlebar | sf::Style::Close),
m_skin(),
m_windowMargin(10),
m_sizeCell(120),
m_spaceBetweenCell(10)
{
//Set windows size
m_windowSize=RenderWindow::getSize();
//Define original skin:
m_skin.push_back(sf::Color(250,248,239)); //Background MainWindow
m_skin.push_back(sf::Color(205,192,180)); //Background cells
m_skin.push_back(sf::Color(187,173,160)); //Background grid color
m_skin.push_back(sf::Color(119,110,101)); //2 and 4 font color
m_skin.push_back(sf::Color(249,246,242)); //other number font Color
//Skin 2 et le 4
m_skin.push_back(sf::Color(238,228,218)); //2
m_skin.push_back(sf::Color(237,224,200)); //4
//Skin 8 à 64
m_skin.push_back(sf::Color(242,177,121)); //8
m_skin.push_back(sf::Color(245,149,99)); //16
m_skin.push_back(sf::Color(246,124,95)); //32
m_skin.push_back(sf::Color(246,94,59)); //64
//Skin 128 à 2048
m_skin.push_back(sf::Color(237,207,114)); //128
m_skin.push_back(sf::Color(237,204,97)); //256
m_skin.push_back(sf::Color(237,200,80)); //512
m_skin.push_back(sf::Color(237,197,63)); //1024
m_skin.push_back(sf::Color(238,194,46)); //2048
//Skin for other number
m_skin.push_back(sf::Color(60,58,50)); //More than 2048
}
MainWindow::~MainWindow(){
}
void MainWindow::clearBG(){
RenderWindow::clear(m_skin.at(0));
}
void MainWindow::drawGrid(std::vector<std::vector<int> > grid){
//Usefull variable
int centerOffset=(m_windowSize.x-(3*m_spaceBetweenCell+4*m_sizeCell))/2;
int distanceBetweenTopAndGrid=180;
int gridsize=3*m_spaceBetweenCell + 4*m_sizeCell + 2*m_spaceBetweenCell;
//First draw the grid
sf::RectangleShape gridBG(sf::Vector2f(gridsize, gridsize));
gridBG.setFillColor(m_skin.at(2));
gridBG.setPosition(centerOffset-m_spaceBetweenCell,distanceBetweenTopAndGrid - m_spaceBetweenCell);
RenderWindow::draw(gridBG);
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
int x=centerOffset+j*(m_sizeCell+m_spaceBetweenCell);
int y=distanceBetweenTopAndGrid+i*(m_sizeCell+m_spaceBetweenCell);
int value=grid.at(i).at(j);
this->drawCell(x,y,value);
}
}
}
void MainWindow::drawCell(int x, int y, int value){
//Init RectangleShape
sf::RectangleShape cell(sf::Vector2f(m_sizeCell, m_sizeCell));
//Define color, checking skin
cell.setFillColor(this->getCellColor(value));
//Set position
cell.setPosition(x,y);
//Draw the cell
RenderWindow::draw(cell);
std::stringstream valueStream;
valueStream << value;
std::string valueString(valueStream.str());
int fontSize(m_sizeCell/2);
int valueSize(valueString.size());
int fontX=x+(m_sizeCell/2)-((valueSize*(fontSize-20))/2);
int fontY=y+(m_sizeCell/2)-(fontSize/2)-10;
sf::Font font;
font.loadFromFile("./src/skin/original/Pragmatica-Medium.ttf");
sf::Text text;
text.setFont(font);
text.setCharacterSize(fontSize);
text.setString(valueString);
if(value==2 || value==4)
text.setColor(m_skin.at(3));
else
text.setColor(m_skin.at(4));
text.setPosition(fontX,fontY);
if(value != 0)
RenderWindow::draw(text);
}
sf::Color MainWindow::getCellColor(int value){
//Id of the first cell color skin
int idStart=5;
if(value==0){
return m_skin.at(1);
}
else if(value%2==0){
int result(value);
int id(idStart);
while(result!=2){
result/=2;
id++;
}
return m_skin.at(id);
}
return m_skin.at(idStart+11);
}
|