Browse Source

implement pause state

pause menu missing
master
noerw 1 year ago
parent
commit
28e63f745a
3 changed files with 34 additions and 7 deletions
  1. 24
    5
      game.cpp
  2. 2
    2
      game.hpp
  3. 8
    0
      main.cpp

+ 24
- 5
game.cpp View File

@@ -14,8 +14,27 @@ namespace Proto4 {
14 14
     }
15 15
 
16 16
     AppState Game::handleKey(const sf::Event &event) {
17
-        if (event.key.code == sf::Keyboard::Q)
18
-            state = GameState::Finished;
17
+        switch (state) {
18
+        case GameState::Paused:
19
+            switch (event.key.code) {
20
+            case sf::Keyboard::Q:
21
+                state = GameState::Finished;
22
+                break;
23
+            case sf::Keyboard::S:
24
+                state = GameState::Running;
25
+                break;
26
+            default:
27
+                break;
28
+            }
29
+            break;
30
+
31
+        case GameState::Running:
32
+            if (event.key.code == sf::Keyboard::Q)
33
+                state = GameState::Paused;
34
+
35
+        default:
36
+            break;
37
+        }
19 38
 
20 39
         return AppState::Game;
21 40
     }
@@ -77,9 +96,9 @@ namespace Proto4 {
77 96
         return true;
78 97
     }
79 98
 
80
-    bool Game::pause() { return true; }
81
-
82
-    bool Game::resume() { return true; }
99
+    // TODO: pause menu
100
+    void Game::pause() { state = GameState::Paused; }
101
+    void Game::resume() { state = GameState::Running; }
83 102
 
84 103
     void Game::resize(sf::Event &resizeEvent) {
85 104
         mainView.setSize(resizeEvent.size.width, resizeEvent.size.height);

+ 2
- 2
game.hpp View File

@@ -20,11 +20,11 @@ namespace Proto4 {
20 20
         sf::View& getMainView();
21 21
 
22 22
         bool init();
23
+        void pause();
24
+        void resume();
23 25
 
24 26
         private:
25 27
         bool start();
26
-        bool pause();
27
-        bool resume();
28 28
         bool reset();
29 29
 
30 30
         sf::View mainView{};

+ 8
- 0
main.cpp View File

@@ -69,6 +69,14 @@ namespace Proto4 {
69 69
         sf::Event event;
70 70
         while (window.pollEvent(event)) {
71 71
             switch (event.type) {
72
+            case sf::Event::LostFocus:
73
+                if (state == AppState::Game) game.pause();
74
+                break;
75
+
76
+            case sf::Event::GainedFocus:
77
+                if (state == AppState::Game) game.resume();
78
+                break;
79
+
72 80
             case sf::Event::Resized:
73 81
                 background.resize(event);
74 82
                 menu.resize(event);

Loading…
Cancel
Save