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 {
}

AppState Game::handleKey(const sf::Event &event) {
if (event.key.code == sf::Keyboard::Q)
state = GameState::Finished;
switch (state) {
case GameState::Paused:
switch (event.key.code) {
case sf::Keyboard::Q:
state = GameState::Finished;
break;
case sf::Keyboard::S:
state = GameState::Running;
break;
default:
break;
}
break;

case GameState::Running:
if (event.key.code == sf::Keyboard::Q)
state = GameState::Paused;

default:
break;
}

return AppState::Game;
}
@@ -77,9 +96,9 @@ namespace Proto4 {
return true;
}

bool Game::pause() { return true; }
bool Game::resume() { return true; }
// TODO: pause menu
void Game::pause() { state = GameState::Paused; }
void Game::resume() { state = GameState::Running; }

void Game::resize(sf::Event &resizeEvent) {
mainView.setSize(resizeEvent.size.width, resizeEvent.size.height);

+ 2
- 2
game.hpp View File

@@ -20,11 +20,11 @@ namespace Proto4 {
sf::View& getMainView();

bool init();
void pause();
void resume();

private:
bool start();
bool pause();
bool resume();
bool reset();

sf::View mainView{};

+ 8
- 0
main.cpp View File

@@ -69,6 +69,14 @@ namespace Proto4 {
sf::Event event;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::LostFocus:
if (state == AppState::Game) game.pause();
break;

case sf::Event::GainedFocus:
if (state == AppState::Game) game.resume();
break;

case sf::Event::Resized:
background.resize(event);
menu.resize(event);

Loading…
Cancel
Save