Add font and fast programming to test the game
This commit is contained in:
parent
1bf3038477
commit
6b37ae07ff
9 changed files with 178 additions and 15 deletions
|
@ -5,8 +5,7 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SFMLController::SFMLController() : m_MainWindow(800,800, "2P11"){
|
SFMLController::SFMLController() : m_game(), m_MainWindow(800,800, "2P11"){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,6 +18,9 @@ SFMLController::~SFMLController(){
|
||||||
|
|
||||||
void SFMLController::run(){
|
void SFMLController::run(){
|
||||||
|
|
||||||
|
kbdh::Direction keyPress;
|
||||||
|
|
||||||
|
m_game.popRandomNumber();
|
||||||
while(m_MainWindow.isOpen()){
|
while(m_MainWindow.isOpen()){
|
||||||
|
|
||||||
|
|
||||||
|
@ -28,11 +30,94 @@ void SFMLController::run(){
|
||||||
// évènement "fermeture demandée" : on ferme la fenêtre
|
// évènement "fermeture demandée" : on ferme la fenêtre
|
||||||
if (event.type == sf::Event::Closed)
|
if (event.type == sf::Event::Closed)
|
||||||
m_MainWindow.close();
|
m_MainWindow.close();
|
||||||
|
if (event.type == sf::Event::KeyPressed)
|
||||||
|
{
|
||||||
|
if (event.key.code == sf::Keyboard::Up)
|
||||||
|
{
|
||||||
|
m_game.swipe(kbdh::Direction::Up);
|
||||||
|
}
|
||||||
|
if (event.key.code == sf::Keyboard::Down)
|
||||||
|
{
|
||||||
|
m_game.swipe(kbdh::Direction::Down);
|
||||||
|
// Do something when W is pressed...
|
||||||
|
}
|
||||||
|
if (event.key.code == sf::Keyboard::Left){
|
||||||
|
|
||||||
|
m_game.swipe(kbdh::Direction::Left);
|
||||||
|
}
|
||||||
|
if (event.key.code == sf::Keyboard::Right){
|
||||||
|
m_game.swipe(kbdh::Direction::Right);
|
||||||
|
}
|
||||||
|
|
||||||
|
// And so on.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
m_MainWindow.clearBG();
|
m_MainWindow.clearBG();
|
||||||
m_MainWindow.drawCells();
|
//m_game.swipe(kbdh::Direction::Left);
|
||||||
|
m_MainWindow.drawGrid(m_game.getGrid());
|
||||||
m_MainWindow.display();
|
m_MainWindow.display();
|
||||||
|
|
||||||
|
//keyPress=this->waitArrowKeyPress();
|
||||||
|
m_game.swipe(keyPress);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Wait for keypress and return the keyPress.
|
||||||
|
kbdh::Direction SFMLController::waitArrowKeyPress()
|
||||||
|
{
|
||||||
|
//Initialise keyPress
|
||||||
|
kbdh::Direction keyPress;
|
||||||
|
|
||||||
|
//White space to remove arrows print by the terminal
|
||||||
|
std::string spaces=" ";
|
||||||
|
|
||||||
|
//Wait for keypress
|
||||||
|
while(1){
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
|
||||||
|
{
|
||||||
|
keyPress=kbdh::Left;
|
||||||
|
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
|
||||||
|
{
|
||||||
|
//Wait for release and try to remove arrow printed characters
|
||||||
|
std::cout << "\r" << spaces;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
|
||||||
|
{
|
||||||
|
keyPress=kbdh::Right;
|
||||||
|
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
|
||||||
|
{
|
||||||
|
//Wait for release and try to remove arrow printed characters
|
||||||
|
std::cout << "\r" << spaces;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
|
||||||
|
{
|
||||||
|
keyPress=kbdh::Up;
|
||||||
|
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
|
||||||
|
{
|
||||||
|
//Wait for release and try to remove arrow printed characters
|
||||||
|
std::cout << "\r" << spaces;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
|
||||||
|
{
|
||||||
|
keyPress=kbdh::Down;
|
||||||
|
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
|
||||||
|
{
|
||||||
|
//Wait for release and try to remove arrow printed characters
|
||||||
|
std::cout << "\r" << spaces;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return keyPress;
|
||||||
|
}
|
||||||
|
|
|
@ -5,17 +5,22 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <SFML/Window.hpp>
|
#include <SFML/Window.hpp>
|
||||||
#include "../../View/MainWindow.hpp"
|
#include "../../View/MainWindow.hpp"
|
||||||
|
#include "../../Model/Game.hpp"
|
||||||
|
#include "../../Helpers/Keyboard.hpp"
|
||||||
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
|
|
||||||
class SFMLController{
|
class SFMLController{
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MainWindow m_MainWindow;
|
MainWindow m_MainWindow;
|
||||||
|
Game m_game;
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SFMLController();
|
SFMLController();
|
||||||
~SFMLController();
|
~SFMLController();
|
||||||
|
|
||||||
|
kbdh::Direction waitArrowKeyPress();
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -83,3 +83,12 @@ int Game::getScore(){
|
||||||
int Game::getNbMove(){
|
int Game::getNbMove(){
|
||||||
return m_nbMove;
|
return m_nbMove;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::vector<int> > Game::getGrid(){
|
||||||
|
return m_grid.getGrid();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int Game::maxStrLenInGrid(){
|
||||||
|
return m_grid.maxStrLenInGrid();
|
||||||
|
}
|
||||||
|
|
|
@ -35,6 +35,8 @@ class Game
|
||||||
//Getters and Setters
|
//Getters and Setters
|
||||||
int getScore();
|
int getScore();
|
||||||
int getNbMove();
|
int getNbMove();
|
||||||
|
int maxStrLenInGrid();
|
||||||
|
std::vector<std::vector<int> > getGrid();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -345,3 +345,8 @@ std::vector<int> Grid::getCol(int col){
|
||||||
|
|
||||||
return colVect;
|
return colVect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::vector<std::vector<int> > Grid::getGrid(){
|
||||||
|
return m_grid;
|
||||||
|
}
|
||||||
|
|
|
@ -21,7 +21,6 @@ class Grid
|
||||||
int m_lastMoveScore;
|
int m_lastMoveScore;
|
||||||
|
|
||||||
//Private methods
|
//Private methods
|
||||||
int maxStrLenInGrid();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
//Constructor and Destructor
|
//Constructor and Destructor
|
||||||
|
@ -34,6 +33,7 @@ class Grid
|
||||||
std::vector<int> rightMerge(std::vector<int> line);
|
std::vector<int> rightMerge(std::vector<int> line);
|
||||||
std::vector<int> leftMerge(std::vector<int> line);
|
std::vector<int> leftMerge(std::vector<int> line);
|
||||||
|
|
||||||
|
int maxStrLenInGrid();
|
||||||
//Swipe methods
|
//Swipe methods
|
||||||
bool swipeRight();
|
bool swipeRight();
|
||||||
bool swipeLeft();
|
bool swipeLeft();
|
||||||
|
@ -55,6 +55,7 @@ class Grid
|
||||||
std::vector<int> getCol(int col);
|
std::vector<int> getCol(int col);
|
||||||
void setCol(int col, std::vector<int> colVect);
|
void setCol(int col, std::vector<int> colVect);
|
||||||
int getLastMoveScore();
|
int getLastMoveScore();
|
||||||
|
std::vector<std::vector<int> > getGrid();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ MainWindow::MainWindow(int width, int height, std::string title):
|
||||||
m_sizeCell(120),
|
m_sizeCell(120),
|
||||||
m_spaceBetweenCell(10)
|
m_spaceBetweenCell(10)
|
||||||
{
|
{
|
||||||
|
//Set windows size
|
||||||
m_windowSize=RenderWindow::getSize();
|
m_windowSize=RenderWindow::getSize();
|
||||||
|
|
||||||
//Define original skin:
|
//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 centerOffset=(m_windowSize.x-(3*m_spaceBetweenCell+4*m_sizeCell))/2;
|
||||||
int distanceBetweenTopAndGrid=180;
|
int distanceBetweenTopAndGrid=180;
|
||||||
int bgsize=3*m_spaceBetweenCell + 4*m_sizeCell + 2*m_spaceBetweenCell;
|
int gridsize=3*m_spaceBetweenCell + 4*m_sizeCell + 2*m_spaceBetweenCell;
|
||||||
sf::RectangleShape gridBG(sf::Vector2f(bgsize, bgsize));
|
|
||||||
|
//First draw the grid
|
||||||
|
sf::RectangleShape gridBG(sf::Vector2f(gridsize, gridsize));
|
||||||
gridBG.setFillColor(m_skin.at(2));
|
gridBG.setFillColor(m_skin.at(2));
|
||||||
gridBG.setPosition(centerOffset-m_spaceBetweenCell,distanceBetweenTopAndGrid - m_spaceBetweenCell);
|
gridBG.setPosition(centerOffset-m_spaceBetweenCell,distanceBetweenTopAndGrid - m_spaceBetweenCell);
|
||||||
RenderWindow::draw(gridBG);
|
RenderWindow::draw(gridBG);
|
||||||
|
|
||||||
for(int i=0;i<4;i++){
|
for(int i=0;i<4;i++){
|
||||||
|
|
||||||
for(int j=0;j<4;j++){
|
for(int j=0;j<4;j++){
|
||||||
sf::RectangleShape cell(sf::Vector2f(m_sizeCell, m_sizeCell));
|
int x=centerOffset+j*(m_sizeCell+m_spaceBetweenCell);
|
||||||
cell.setFillColor(m_skin.at(1));
|
int y=distanceBetweenTopAndGrid+i*(m_sizeCell+m_spaceBetweenCell);
|
||||||
cell.setPosition(centerOffset+j*(m_sizeCell+m_spaceBetweenCell),distanceBetweenTopAndGrid+i*(m_sizeCell+m_spaceBetweenCell));
|
int value=grid.at(i).at(j);
|
||||||
RenderWindow::draw(cell);
|
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){
|
sf::Color MainWindow::getCellColor(int value){
|
||||||
|
|
||||||
//Id of the first cell color skin
|
//Id of the first cell color skin
|
||||||
int idStart=5;
|
int idStart=5;
|
||||||
|
|
||||||
if(value%2==0){
|
if(value==0){
|
||||||
|
return m_skin.at(1);
|
||||||
|
}
|
||||||
|
else if(value%2==0){
|
||||||
int result(value);
|
int result(value);
|
||||||
int id(idStart);
|
int id(idStart);
|
||||||
while(result!=2){
|
while(result!=2){
|
||||||
|
@ -91,6 +141,7 @@ sf::Color MainWindow::getCellColor(int value){
|
||||||
return m_skin.at(id);
|
return m_skin.at(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return m_skin.at(idStart+11);
|
return m_skin.at(idStart+11);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
#include <SFML/Window.hpp>
|
#include <SFML/Window.hpp>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
|
@ -15,12 +17,15 @@ class MainWindow : public sf::RenderWindow{
|
||||||
int m_sizeCell;
|
int m_sizeCell;
|
||||||
int m_spaceBetweenCell;
|
int m_spaceBetweenCell;
|
||||||
sf::Vector2u m_windowSize;
|
sf::Vector2u m_windowSize;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
MainWindow(int width, int height, std::string title);
|
MainWindow(int width, int height, std::string title);
|
||||||
~MainWindow();
|
~MainWindow();
|
||||||
|
|
||||||
void clearBG();
|
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);
|
sf::Color getCellColor(int value);
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
BIN
src/skin/original/Pragmatica-Medium.ttf
Normal file
BIN
src/skin/original/Pragmatica-Medium.ttf
Normal file
Binary file not shown.
Loading…
Add table
Reference in a new issue