blob: e037c0bdd2ac9c6effd6b63ee751d12b28e38950 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
#include "SFMLController.hpp"
SFMLController::SFMLController() : m_game(), m_MainWindow(800,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);
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;
}
|