diff options
Diffstat (limited to 'src/Controllers')
| -rw-r--r-- | src/Controllers/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/Controllers/ConsoleController/ConsoleController.cpp | 11 | ||||
| -rw-r--r-- | src/Controllers/SFMLController/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | src/Controllers/SFMLController/SFMLController.cpp | 124 | ||||
| -rw-r--r-- | src/Controllers/SFMLController/SFMLController.hpp | 26 |
5 files changed, 161 insertions, 4 deletions
diff --git a/src/Controllers/CMakeLists.txt b/src/Controllers/CMakeLists.txt index 7ebeb19..13b1c20 100644 --- a/src/Controllers/CMakeLists.txt +++ b/src/Controllers/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(./ConsoleController/) +add_subdirectory(./SFMLController/) diff --git a/src/Controllers/ConsoleController/ConsoleController.cpp b/src/Controllers/ConsoleController/ConsoleController.cpp index 1bc9b84..57f626f 100644 --- a/src/Controllers/ConsoleController/ConsoleController.cpp +++ b/src/Controllers/ConsoleController/ConsoleController.cpp @@ -70,6 +70,9 @@ kbdh::Direction ConsoleController::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)) @@ -78,7 +81,7 @@ kbdh::Direction ConsoleController::waitArrowKeyPress() while(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { //Wait for release and try to remove arrow printed characters - std::cout << "\r" << " "; + std::cout << "\r" << spaces; } break; } @@ -88,7 +91,7 @@ kbdh::Direction ConsoleController::waitArrowKeyPress() while(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { //Wait for release and try to remove arrow printed characters - std::cout << "\r" << " "; + std::cout << "\r" << spaces; } break; } @@ -98,7 +101,7 @@ kbdh::Direction ConsoleController::waitArrowKeyPress() while(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { //Wait for release and try to remove arrow printed characters - std::cout << "\r" << " "; + std::cout << "\r" << spaces; } break; } @@ -108,7 +111,7 @@ kbdh::Direction ConsoleController::waitArrowKeyPress() while(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { //Wait for release and try to remove arrow printed characters - std::cout << "\r" << " "; + std::cout << "\r" << spaces; } break; } diff --git a/src/Controllers/SFMLController/CMakeLists.txt b/src/Controllers/SFMLController/CMakeLists.txt new file mode 100644 index 0000000..e3e60c4 --- /dev/null +++ b/src/Controllers/SFMLController/CMakeLists.txt @@ -0,0 +1,3 @@ +#Make Model lib +add_library(SFMLController ./SFMLController.cpp) +target_link_libraries(SFMLController Model View) diff --git a/src/Controllers/SFMLController/SFMLController.cpp b/src/Controllers/SFMLController/SFMLController.cpp new file mode 100644 index 0000000..1c21753 --- /dev/null +++ b/src/Controllers/SFMLController/SFMLController.cpp @@ -0,0 +1,124 @@ +#include "SFMLController.hpp" + + + + + + +SFMLController::SFMLController() : m_game(), m_MainWindow(600,800, "2P11"){ +} + + +SFMLController::~SFMLController(){ + +} + + + + +void SFMLController::run(){ + + kbdh::Direction keyPress; + + m_game.popRandomNumber(); + 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(); + 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_game.swipe(kbdh::Direction::Left); + std::vector<std::vector<int> > aaa=m_game.getGrid(); + m_MainWindow.drawGame(aaa,m_game.isOver(), m_game.getStats()); + 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 new file mode 100644 index 0000000..0bb0eea --- /dev/null +++ b/src/Controllers/SFMLController/SFMLController.hpp @@ -0,0 +1,26 @@ + + + +#include <iostream> +#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(); + +}; |
