Skip to content

Commit

Permalink
modified exceptions + dynamic casting
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan-velicu committed Dec 19, 2023
1 parent 73278dd commit 4c289aa
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 36 deletions.
Binary file modified .DS_Store
Binary file not shown.
10 changes: 7 additions & 3 deletions doodle_jump/exceptions/Exceptions.cpp
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";
}
10 changes: 8 additions & 2 deletions doodle_jump/exceptions/Exceptions.h
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;
};
67 changes: 43 additions & 24 deletions doodle_jump/game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
@mcmarius

mcmarius Dec 23, 2023

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)

break;
}
break;
default:
break;
}
break;
default:
break;
}
catch (DoodleJumpException& e) {

This comment has been minimized.

Copy link
@mcmarius

mcmarius Dec 23, 2023

Nu prea e ok cu dynamic_cast dacă poți fără, alternativa e mai simplă:

catch(PlayerOutOfBoundException& e) {
    std::cout << "Player out of bounds: " << e.what() << '\n';
    changeScreen(ScreenType::GAME_OVER);
}
catch(InvalidGameStateException& e) {
    std::cout << "Invalid game state: " << e.what() << '\n';
    changeScreen(ScreenType::CLOSE);
}
catch (DoodleJumpException& e) {
  // altceva

Din moment ce ai virtualizare, dacă chiar ai avea nevoie de ceva stil dynamic_cast în cazul excepțiilor, e bine întâi să vezi dacă te poți de funcții virtuale în excepții.

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) {
Expand Down
7 changes: 0 additions & 7 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <time.h>

#include "./doodle_jump/game/Game.h"
#include "./doodle_jump/exceptions/Exceptions.h"

#ifdef __linux__
#include <X11/Xlib.h>
Expand All @@ -17,12 +16,6 @@ int main() {

srand(time(nullptr));

try {
throw RandomException();
} catch (const std::exception& e) {
std::cout << e.what() << '\n';
}

Game game = Game();
std::cout << game << '\n';

Expand Down

0 comments on commit 4c289aa

Please sign in to comment.