Add font and fast programming to test the game

This commit is contained in:
manzerbredes 2015-05-03 20:23:43 +02:00
parent 1bf3038477
commit 6b37ae07ff
9 changed files with 178 additions and 15 deletions

View file

@ -12,7 +12,7 @@ MainWindow::MainWindow(int width, int height, std::string title):
m_sizeCell(120),
m_spaceBetweenCell(10)
{
//Set windows size
m_windowSize=RenderWindow::getSize();
//Define original skin:
@ -53,35 +53,85 @@ void MainWindow::clearBG(){
}
void MainWindow::drawCells(){
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 bgsize=3*m_spaceBetweenCell + 4*m_sizeCell + 2*m_spaceBetweenCell;
sf::RectangleShape gridBG(sf::Vector2f(bgsize, bgsize));
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++){
sf::RectangleShape cell(sf::Vector2f(m_sizeCell, m_sizeCell));
cell.setFillColor(m_skin.at(1));
cell.setPosition(centerOffset+j*(m_sizeCell+m_spaceBetweenCell),distanceBetweenTopAndGrid+i*(m_sizeCell+m_spaceBetweenCell));
RenderWindow::draw(cell);
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%2==0){
if(value==0){
return m_skin.at(1);
}
else if(value%2==0){
int result(value);
int id(idStart);
while(result!=2){
@ -91,6 +141,7 @@ sf::Color MainWindow::getCellColor(int value){
return m_skin.at(id);
}
return m_skin.at(idStart+11);
}

View file

@ -3,6 +3,8 @@
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
@ -15,12 +17,15 @@ class MainWindow : public sf::RenderWindow{
int m_sizeCell;
int m_spaceBetweenCell;
sf::Vector2u m_windowSize;
public:
MainWindow(int width, int height, std::string title);
~MainWindow();
void clearBG();
void drawCells();
void drawGrid(std::vector<std::vector<int> > grid);
void drawCell(int x, int y, int value);
sf::Color getCellColor(int value);
};