summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Controllers/SFMLController/SFMLController.cpp91
-rw-r--r--src/Controllers/SFMLController/SFMLController.hpp5
-rw-r--r--src/Model/Game.cpp9
-rw-r--r--src/Model/Game.hpp2
-rw-r--r--src/Model/Grid.cpp5
-rw-r--r--src/Model/Grid.hpp3
-rw-r--r--src/View/MainWindow.cpp71
-rw-r--r--src/View/MainWindow.hpp7
-rw-r--r--src/skin/original/Pragmatica-Medium.ttfbin0 -> 87416 bytes
9 files changed, 178 insertions, 15 deletions
diff --git a/src/Controllers/SFMLController/SFMLController.cpp b/src/Controllers/SFMLController/SFMLController.cpp
index cd676a9..e037c0b 100644
--- a/src/Controllers/SFMLController/SFMLController.cpp
+++ b/src/Controllers/SFMLController/SFMLController.cpp
@@ -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(){
+ kbdh::Direction keyPress;
+
+ m_game.popRandomNumber();
while(m_MainWindow.isOpen()){
@@ -28,11 +30,94 @@ void SFMLController::run(){
// évènement "fermeture demandée" : on ferme la fenêtre
if (event.type == sf::Event::Closed)
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.drawCells();
+ //m_game.swipe(kbdh::Direction::Left);
+ m_MainWindow.drawGrid(m_game.getGrid());
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;
+}
diff --git a/src/Controllers/SFMLController/SFMLController.hpp b/src/Controllers/SFMLController/SFMLController.hpp
index 94a22e3..0bb0eea 100644
--- a/src/Controllers/SFMLController/SFMLController.hpp
+++ b/src/Controllers/SFMLController/SFMLController.hpp
@@ -5,17 +5,22 @@
#include <string>
#include <SFML/Window.hpp>
#include "../../View/MainWindow.hpp"
+#include "../../Model/Game.hpp"
+#include "../../Helpers/Keyboard.hpp"
+#include <SFML/Window/Keyboard.hpp>
class SFMLController{
private:
MainWindow m_MainWindow;
+ Game m_game;
public:
SFMLController();
~SFMLController();
+ kbdh::Direction waitArrowKeyPress();
void run();
};
diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp
index 284d82b..e2ac799 100644
--- a/src/Model/Game.cpp
+++ b/src/Model/Game.cpp
@@ -83,3 +83,12 @@ int Game::getScore(){
int Game::getNbMove(){
return m_nbMove;
}
+
+std::vector<std::vector<int> > Game::getGrid(){
+ return m_grid.getGrid();
+}
+
+
+int Game::maxStrLenInGrid(){
+ return m_grid.maxStrLenInGrid();
+}
diff --git a/src/Model/Game.hpp b/src/Model/Game.hpp
index 0b2ee4d..e5d19e5 100644
--- a/src/Model/Game.hpp
+++ b/src/Model/Game.hpp
@@ -35,6 +35,8 @@ class Game
//Getters and Setters
int getScore();
int getNbMove();
+ int maxStrLenInGrid();
+ std::vector<std::vector<int> > getGrid();
};
#endif
diff --git a/src/Model/Grid.cpp b/src/Model/Grid.cpp
index cc0fe60..7d23c6c 100644
--- a/src/Model/Grid.cpp
+++ b/src/Model/Grid.cpp
@@ -345,3 +345,8 @@ std::vector<int> Grid::getCol(int col){
return colVect;
}
+
+
+std::vector<std::vector<int> > Grid::getGrid(){
+ return m_grid;
+}
diff --git a/src/Model/Grid.hpp b/src/Model/Grid.hpp
index 51168a9..604a6ff 100644
--- a/src/Model/Grid.hpp
+++ b/src/Model/Grid.hpp
@@ -21,7 +21,6 @@ class Grid
int m_lastMoveScore;
//Private methods
- int maxStrLenInGrid();
public:
//Constructor and Destructor
@@ -34,6 +33,7 @@ class Grid
std::vector<int> rightMerge(std::vector<int> line);
std::vector<int> leftMerge(std::vector<int> line);
+ int maxStrLenInGrid();
//Swipe methods
bool swipeRight();
bool swipeLeft();
@@ -55,6 +55,7 @@ class Grid
std::vector<int> getCol(int col);
void setCol(int col, std::vector<int> colVect);
int getLastMoveScore();
+ std::vector<std::vector<int> > getGrid();
};
diff --git a/src/View/MainWindow.cpp b/src/View/MainWindow.cpp
index 179b737..784effb 100644
--- a/src/View/MainWindow.cpp
+++ b/src/View/MainWindow.cpp
@@ -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);
}
+
+
diff --git a/src/View/MainWindow.hpp b/src/View/MainWindow.hpp
index 89cce0f..aabdb09 100644
--- a/src/View/MainWindow.hpp
+++ b/src/View/MainWindow.hpp
@@ -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);
+
};
diff --git a/src/skin/original/Pragmatica-Medium.ttf b/src/skin/original/Pragmatica-Medium.ttf
new file mode 100644
index 0000000..f5fee8d
--- /dev/null
+++ b/src/skin/original/Pragmatica-Medium.ttf
Binary files differ