2015-05-03 14:19:30 +02:00
|
|
|
#include "SFMLController.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-03 20:23:43 +02:00
|
|
|
SFMLController::SFMLController() : m_game(), m_MainWindow(800,800, "2P11"){
|
2015-05-03 14:19:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SFMLController::~SFMLController(){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SFMLController::run(){
|
|
|
|
|
2015-05-03 20:23:43 +02:00
|
|
|
kbdh::Direction keyPress;
|
|
|
|
|
|
|
|
m_game.popRandomNumber();
|
2015-05-03 14:19:30 +02:00
|
|
|
while(m_MainWindow.isOpen()){
|
|
|
|
|
|
|
|
|
|
|
|
sf::Event event;
|
|
|
|
while (m_MainWindow.pollEvent(event))
|
|
|
|
{
|
|
|
|
// évènement "fermeture demandée" : on ferme la fenêtre
|
|
|
|
if (event.type == sf::Event::Closed)
|
|
|
|
m_MainWindow.close();
|
2015-05-03 20:23:43 +02:00
|
|
|
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.
|
|
|
|
}
|
2015-05-03 14:19:30 +02:00
|
|
|
}
|
2015-05-03 20:23:43 +02:00
|
|
|
|
|
|
|
|
2015-05-03 15:12:46 +02:00
|
|
|
m_MainWindow.clearBG();
|
2015-05-03 20:23:43 +02:00
|
|
|
//m_game.swipe(kbdh::Direction::Left);
|
2015-05-04 12:55:15 +02:00
|
|
|
std::vector<std::vector<int> > aaa=m_game.getGrid();
|
2015-05-04 14:00:54 +02:00
|
|
|
m_MainWindow.drawGame(aaa,m_game.isOver(), m_game.getStats());
|
2015-05-03 14:19:30 +02:00
|
|
|
m_MainWindow.display();
|
2015-05-03 20:23:43 +02:00
|
|
|
|
|
|
|
//keyPress=this->waitArrowKeyPress();
|
|
|
|
m_game.swipe(keyPress);
|
2015-05-03 14:19:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2015-05-03 20:23:43 +02:00
|
|
|
|
|
|
|
//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;
|
|
|
|
}
|