-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modified exceptions + dynamic casting
- Loading branch information
1 parent
73278dd
commit 4c289aa
Showing
5 changed files
with
58 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
#include "Exceptions.h" | ||
|
||
const char* DoodleJumpException::what() const noexcept { | ||
return "DoodleJumpException"; | ||
} | ||
|
||
const char* PlayerOutOfBoundException::what() const noexcept { | ||
return "Player is out of bound"; | ||
return "PlayerOutOfBoundException"; | ||
} | ||
|
||
const char* RandomException::what() const noexcept { | ||
return "Random exception"; | ||
const char* InvalidGameStateException::what() const noexcept { | ||
return "InvalidGameStateException"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
#include <iostream> | ||
#include <exception> | ||
|
||
class PlayerOutOfBoundException : public std::exception { | ||
class DoodleJumpException : public std::exception { | ||
public: | ||
[[nodiscard]] const char* what() const noexcept override; | ||
}; | ||
|
||
class RandomException : public std::exception { | ||
class PlayerOutOfBoundException : public DoodleJumpException { | ||
public: | ||
[[nodiscard]] const char* what() const noexcept override; | ||
}; | ||
|
||
class InvalidGameStateException : public DoodleJumpException { | ||
public: | ||
[[nodiscard]] const char* what() const noexcept override; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,36 +107,55 @@ void Game::run() { | |
while (window.isOpen()) { | ||
sf::Event e = sf::Event(); | ||
while (window.pollEvent(e)) { | ||
ScreenType screenCopy = currentScreen; | ||
switch (e.type) { | ||
case sf::Event::Closed: | ||
window.close(); | ||
break; | ||
case sf::Event::Resized: | ||
std::cout << "New width: " << window.getSize().x << '\n' | ||
<< "New height: " << window.getSize().y << '\n'; | ||
break; | ||
case sf::Event::KeyPressed: | ||
switch (currentScreen) { | ||
case ScreenType::MAIN_MENU: | ||
MainMenu::handleInput(e, currentScreen); | ||
try { | ||
ScreenType screenCopy = currentScreen; | ||
switch (e.type) { | ||
case sf::Event::Closed: | ||
window.close(); | ||
break; | ||
case ScreenType::GAME_OVER: | ||
GameOver::handleInput(e, currentScreen); | ||
if (currentScreen == ScreenType::PLAY) | ||
reset(); | ||
case sf::Event::Resized: | ||
std::cout << "New width: " << window.getSize().x << '\n' | ||
<< "New height: " << window.getSize().y << '\n'; | ||
break; | ||
case ScreenType::PLAY: | ||
PlayScreen::handleInput(e, currentScreen); | ||
if (screenCopy != currentScreen) | ||
reset(); | ||
case sf::Event::KeyPressed: | ||
switch (currentScreen) { | ||
case ScreenType::MAIN_MENU: | ||
MainMenu::handleInput(e, currentScreen); | ||
break; | ||
case ScreenType::GAME_OVER: | ||
GameOver::handleInput(e, currentScreen); | ||
if (currentScreen == ScreenType::PLAY) | ||
reset(); | ||
break; | ||
case ScreenType::PLAY: | ||
PlayScreen::handleInput(e, currentScreen); | ||
if (screenCopy != currentScreen) | ||
reset(); | ||
break; | ||
default: | ||
throw InvalidGameStateException(); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
break; | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
catch (DoodleJumpException& e) { | ||
This comment has been minimized.
Sorry, something went wrong.
mcmarius
|
||
std::cout << e.what() << '\n'; | ||
|
||
auto* p = dynamic_cast<PlayerOutOfBoundException*>(&e); | ||
auto* p2 = dynamic_cast<InvalidGameStateException*>(&e); | ||
|
||
if (p != nullptr) { | ||
std::cout << "Player out of bounds: " << p->what() << '\n'; | ||
changeScreen(ScreenType::GAME_OVER); | ||
} | ||
|
||
if (p2 != nullptr) { | ||
std::cout << "Invalid game state: " << p2->what() << '\n'; | ||
changeScreen(ScreenType::CLOSE); | ||
} | ||
} | ||
} | ||
switch (currentScreen) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Aici e așa și așa, la limită să zicem că mergem. Eventual să mai arunci o excepție în vreun constructor (să ai validări)