Clear some things

This commit is contained in:
manzerbredes 2015-05-04 12:55:15 +02:00
parent 6b37ae07ff
commit 3384204edc
4 changed files with 109 additions and 30 deletions

View file

@ -56,7 +56,8 @@ void SFMLController::run(){
m_MainWindow.clearBG();
//m_game.swipe(kbdh::Direction::Left);
m_MainWindow.drawGrid(m_game.getGrid());
std::vector<std::vector<int> > aaa=m_game.getGrid();
m_MainWindow.drawGrid(aaa,m_game.isOver());
m_MainWindow.display();
//keyPress=this->waitArrowKeyPress();

View file

@ -11,6 +11,7 @@ Grid::Grid(): m_size(4), m_grid(4){
m_grid.at(i).push_back(0);
}
}
//m_grid.at(3).at(0)=2048;
}
//Destructor

View file

@ -8,19 +8,31 @@
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)
m_windowSize(),
m_cellSize(105,105),
m_gridSize(0,0),
m_gridPosition(),
m_spaceBetweenCell(15),
m_font()
{
//Set windows size
m_windowSize=RenderWindow::getSize();
m_gridPosition=sf::Vector2u(50,200);
//Load font
m_font.loadFromFile("./src/skin/original/Pragmatica-Medium.ttf");
//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(143,122,102)); //Button color
m_skin.push_back(sf::Color(249,246,242)); //other number font Color
m_skin.push_back(sf::Color(238,228,218,186)); //Game over color bg
//Skin 2 et le 4
m_skin.push_back(sf::Color(238,228,218)); //2
@ -53,36 +65,46 @@ void MainWindow::clearBG(){
}
void MainWindow::drawGrid(std::vector<std::vector<int> > grid){
void MainWindow::drawGrid(std::vector<std::vector<int> > grid, bool gameIsOver){
//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);
int gridX=m_gridPosition.x;
int gridY=m_gridPosition.y;
for(int i=0;i<4;i++){
m_gridSize.x=(grid.at(0).size()*m_cellSize.x)+(grid.at(0).size()*m_spaceBetweenCell)+m_spaceBetweenCell;
m_gridSize.y=(grid.size()*m_cellSize.y)+(grid.size()*m_spaceBetweenCell)+m_spaceBetweenCell;
for(int j=0;j<4;j++){
int x=centerOffset+j*(m_sizeCell+m_spaceBetweenCell);
int y=distanceBetweenTopAndGrid+i*(m_sizeCell+m_spaceBetweenCell);
//Draw the grid
sf::RectangleShape gridShape(sf::Vector2f(m_gridSize.x,m_gridSize.y));
gridShape.setFillColor(m_skin.at(2));
gridShape.setPosition(gridX,gridY);
RenderWindow::draw(gridShape);
for(int i=0;i<grid.size();i++){
for(int j=0;j<grid.at(0).size();j++){
int cellX=(gridX+m_spaceBetweenCell)+j*(m_cellSize.x+m_spaceBetweenCell);
int cellY=(gridY+m_spaceBetweenCell)+i*(m_cellSize.y+m_spaceBetweenCell);
int value=grid.at(i).at(j);
this->drawCell(x,y,value);
this->drawCell(cellX,cellY,value);
}
}
if(gameIsOver)
this->drawGameOver(gridX,gridY);
this->drawATH();
}
void MainWindow::drawCell(int x, int y, int value){
//Init RectangleShape
sf::RectangleShape cell(sf::Vector2f(m_sizeCell, m_sizeCell));
sf::RectangleShape cell(sf::Vector2f(m_cellSize.x, m_cellSize.y));
//Define color, checking skin
cell.setFillColor(this->getCellColor(value));
@ -99,18 +121,20 @@ void MainWindow::drawCell(int x, int y, int value){
std::string valueString(valueStream.str());
int fontSize(m_sizeCell/2);
int fontSize(m_cellSize.x/2);
int fontSizeFact=15;
if(value>=1024)
fontSize-=fontSizeFact;
int valueSize(valueString.size());
int fontX=x+(m_sizeCell/2)-((valueSize*(fontSize-20))/2);
int fontY=y+(m_sizeCell/2)-(fontSize/2)-10;
int fontX=x+(m_cellSize.x/2)-((valueSize*(fontSize-20))/2);
int fontY=y+(m_cellSize.y/2)-(fontSize/2)-10;
sf::Font font;
font.loadFromFile("./src/skin/original/Pragmatica-Medium.ttf");
sf::Text text;
text.setFont(font);
text.setFont(m_font);
text.setStyle(sf::Text::Bold);
text.setCharacterSize(fontSize);
text.setString(valueString);
if(value==2 || value==4)
@ -126,7 +150,7 @@ void MainWindow::drawCell(int x, int y, int value){
sf::Color MainWindow::getCellColor(int value){
//Id of the first cell color skin
int idStart=5;
int idStart=7;
if(value==0){
return m_skin.at(1);
@ -144,4 +168,48 @@ sf::Color MainWindow::getCellColor(int value){
return m_skin.at(idStart+11);
}
void MainWindow::drawGameOver(int gridX, int gridY){
sf::RectangleShape gridShape(sf::Vector2f(m_gridSize.x,m_gridSize.y));
gridShape.setFillColor(m_skin.at(6));
gridShape.setPosition(gridX,gridY);
RenderWindow::draw(gridShape);
}
void MainWindow::drawATH(){
int titleX=m_gridPosition.x;
int titleY=m_gridPosition.y-190;
//==================== Draw title ====================
sf::Text text;
text.setFont(m_font);
text.setStyle(sf::Text::Bold);
text.setCharacterSize(80);
text.setColor(m_skin.at(3));
text.setPosition(titleX,titleY);
text.setString("2048");
RenderWindow::draw(text);
//==================== Draw score ====================
int scoreSizeX=110;
int scoreX=m_gridPosition.x+m_gridSize.x-scoreSizeX;
sf::RectangleShape scoreShape(sf::Vector2f(scoreSizeX,60));
scoreShape.setFillColor(m_skin.at(2));
scoreShape.setPosition(scoreX,titleY+25);
RenderWindow::draw(scoreShape);
//==================== Draw best score ====================
sf::RectangleShape bestScoreShape(sf::Vector2f(scoreSizeX,60));
bestScoreShape.setFillColor(m_skin.at(2));
bestScoreShape.setPosition(scoreX-scoreSizeX-5,titleY+25);
RenderWindow::draw(bestScoreShape);
}

View file

@ -13,19 +13,28 @@ class MainWindow : public sf::RenderWindow{
private:
std::vector<sf::Color> m_skin;
int m_windowMargin;
int m_sizeCell;
int m_spaceBetweenCell;
//int m_windowMargin;
//int m_sizeCell;
sf::Font m_font;
//Coordonates
sf::Vector2u m_windowSize;
sf::Vector2u m_gridSize;
sf::Vector2u m_cellSize;
sf::Vector2u m_gridPosition;
int m_spaceBetweenCell;
public:
MainWindow(int width, int height, std::string title);
~MainWindow();
void clearBG();
void drawGrid(std::vector<std::vector<int> > grid);
void drawGrid(std::vector<std::vector<int> > grid, bool gameIsOver);
void drawCell(int x, int y, int value);
sf::Color getCellColor(int value);
void drawGameOver(int gridX, int griY);
void drawATH();
};