Skip to content

Commit

Permalink
delete, erori
Browse files Browse the repository at this point in the history
  • Loading branch information
mariapreda19 committed Apr 29, 2023
1 parent 7e9c849 commit 17165fd
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 26 deletions.
10 changes: 10 additions & 0 deletions headers/Erori.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ class eroare_fereastra : public std::runtime_error {
explicit eroare_fereastra(const std::string& mesaj);
};

class eroare_imagine : public eroare_fereastra {
public:
[[maybe_unused]] explicit eroare_imagine(const std::string& mesaj);
};

class eroare_font : public eroare_fereastra {
public:
explicit eroare_font(const std::string& mesaj);
};

#endif //OOP_ERORI_H
5 changes: 3 additions & 2 deletions headers/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class Game {
private:
Screen *startScreen;
Screen *startScreen{};
std::vector<Screen *> questions;
std::vector<Player> players;

Expand All @@ -38,7 +38,8 @@ class Game {
void addPlayer(const Player& player);

void play();
};

virtual ~Game();

This comment has been minimized.

Copy link
@mcmarius

mcmarius May 7, 2023

De ce e destructorul virtual? Nu văd să moștenești din clasa Game.

Ar trebui în schimb definite (sau șterse) constructor de copiere și operator= pt că ai atribute pointeri și altfel acele funcții nu se comportă corect

};

#endif //OOP_GAME_H
4 changes: 3 additions & 1 deletion headers/Question.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Button.h"
#include "SFML/Graphics.hpp"
#include "Timer.h"
#include "Erori.h"

class Screen {
std::string text;
Expand All @@ -20,11 +21,12 @@ class Screen {

friend std::ostream& operator<<(std::ostream& os, const Screen& screen);

virtual int display(sf::RenderWindow &window);
virtual int display(sf::RenderWindow &window) = 0;

[[nodiscard]] std::basic_string<char> getText() const;
[[nodiscard]] std::vector<Button> getOptions() const;

virtual ~Screen();

};

Expand Down
3 changes: 0 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

#include "headers/Player.h"
#include "headers/Timer.h"
#include "headers/Question.h"
#include "headers/Button.h"
#include "headers/Game.h"

int main(){

Player p1("A");
Player p2("Maria");
std::vector<Player> players;


Expand Down
8 changes: 7 additions & 1 deletion src/Erori.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ eroare_fisier::eroare_fisier(const std::string& mesaj) :
std::runtime_error("eroare fisier: " + mesaj) {}

eroare_fereastra::eroare_fereastra(const std::string& mesaj) :
std::runtime_error("eroare fereastra: " + mesaj) {}
std::runtime_error("eroare fereastra: " + mesaj) {}

eroare_imagine::eroare_imagine(const std::string& mesaj) :
eroare_fereastra("Nu a fost gasita o imagine " + mesaj) {}

eroare_font::eroare_font(const std::string& mesaj) :
eroare_fereastra("Nu a fost gasit fontul" + mesaj) {}
12 changes: 10 additions & 2 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ void Game::play(){
if (categorie_aleasa != -1) {
bool quit2 = false;
while(!quit2 and window.isOpen()) {
Timer clock(12);
Timer clock(300);
clock.reset();
int number = 0;

std::vector<Question *> category_questions;
for (auto question: questions) {
if (categorie_aleasa == dynamic_cast<Question *>(question)->getCategory()) {
Expand Down Expand Up @@ -183,9 +182,18 @@ void Game::play(){
}
}
}
delete screen;
}
}
}
}
}
}
Game::~Game() {
delete startScreen;
for (auto q : questions) {
delete q;
}
}


77 changes: 60 additions & 17 deletions src/Question.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ std::vector<Button> Screen::getOptions() const {
return options;
}

Screen::~Screen() = default;

Question::Question(std::string question, const std::vector<std::string>& options, const int correct, const int category_):
Screen(std::move(question), options), correctAnswerIndex(correct), category(category_){
}
Expand All @@ -115,23 +117,37 @@ std::ostream& operator<<(std::ostream& os, const Question& question) {


int Question::display(sf::RenderWindow &window) {
// Load background image and button texture
sf::Texture background;
background.loadFromFile("background.jpg");
try {
background.loadFromFile("background.jpg");

This comment has been minimized.

Copy link
@mcmarius

mcmarius May 7, 2023

Un try/catch de felul ăsta semnalează de obicei că de fapt nu ai avea nevoie probabil de excepții pentru că alternativa cu coduri de eroare ar fi mai simplă.

Apropo, din câte știu, funcția respectivă nici nu aruncă excepții 🤔 deci chiar nu înțeleg ce faci pe aici 😕

Folosește excepții doar acolo unde ți-ar simplifica tratarea erorilor; dacă nu simplifici codul, e mai ok cu coduri de eroare

}
catch(eroare_imagine &er){
std::cout << er.what() << std::endl;
}
sf::Sprite background_sprite;
background_sprite.setTexture(background);

sf::Texture texture;
texture.loadFromFile("button2.png");

// Create button sprite and set its position
try {
texture.loadFromFile("button2.png");
}
catch(eroare_imagine &er){
std::cout << er.what() << std::endl;
}

sf::Sprite box;
box.setTexture(texture);
box.setPosition(100, 100);

// Load font and create text object

sf::Font font;
font.loadFromFile(R"(C:\Windows\Fonts\arial.ttf)");
try {
font.loadFromFile(R"(C:\Windows\Fonts\arial.ttf)");

This comment has been minimized.

Copy link
@mcmarius

mcmarius May 7, 2023

Fără căi absolute; configurează-ți working directory-ul și/sau copiază fonturile de care ai nevoie în proiect, altfel nu o să meargă.

În cmakelists.txt la sfârșit trebuie să pui la comanda de install toate fișierele de care e nevoie, adică și fonturile și imaginile (și ce mai e cazul).

De asemenea, precizează te rog în readme sau alt loc vizibil sursele de unde ai luat imaginile sau dacă îți aparțin

}
catch(eroare_font &er){
std::cout << er.what() << std::endl;
}

sf::Text text_;
text_.setString(this -> getText());
Expand Down Expand Up @@ -179,20 +195,34 @@ MenuScreen &MenuScreen::operator=(const MenuScreen &other) = default;

int MenuScreen::display(sf::RenderWindow &window) {
sf::Texture background;
background.loadFromFile("background.jpg");
try {
background.loadFromFile("background.jpg");
}
catch(eroare_imagine &er){
std::cout << er.what() << std::endl;
}
sf::Sprite background_sprite;
background_sprite.setTexture(background);

sf::Sprite box;
sf::Texture texture;
texture.loadFromFile("button2.png");
try {
texture.loadFromFile("button2.png");

This comment has been minimized.

}
catch(eroare_imagine &er){
std::cout << er.what() << std::endl;
}
box.setTexture(texture);
box.setPosition(100, 100);

sf::Text text_;
sf::Font font;
font.loadFromFile(R"(C:\Windows\Fonts\arial.ttf)");

try {
font.loadFromFile(R"(C:\Windows\Fonts\arial.ttf)");

This comment has been minimized.

Copy link
@mcmarius

mcmarius May 7, 2023

Nu pare în regulă; cred că ar trebui să ai într-o singură clasă ajutătoare încărcarea resurselor de pe disc în fonturi/texturi

}
catch(eroare_font &er){
std::cout << er.what() << std::endl;
}
text_.setString(getText());
text_.setCharacterSize(20);
text_.setFont(font);
Expand All @@ -206,8 +236,7 @@ int MenuScreen::display(sf::RenderWindow &window) {
while(window.isOpen())
{
sf::Event e{};
while(window.pollEvent(e))
{
while(window.pollEvent(e)){
if(e.type == sf::Event::Closed)
window.close();
}
Expand All @@ -234,19 +263,34 @@ CategoryScreen::CategoryScreen(const CategoryScreen &other) = default;
CategoryScreen &CategoryScreen::operator=(const CategoryScreen &other) = default;
int CategoryScreen::display(sf::RenderWindow &window) {
sf::Texture background;
background.loadFromFile("background.jpg");
try {
background.loadFromFile("background.jpg");
}
catch(eroare_imagine &er){
std::cout << er.what() << std::endl;
}
sf::Sprite background_sprite;
background_sprite.setTexture(background);

sf::Sprite box;
sf::Texture texture;
texture.loadFromFile("button2.png");
try {
texture.loadFromFile("button2.png");
}
catch(eroare_imagine &er){
std::cout << er.what() << std::endl;
}
box.setTexture(texture);
box.setPosition(100, 100);

sf::Text text_;
sf::Font font;
font.loadFromFile(R"(C:\Windows\Fonts\arial.ttf)");
try {
font.loadFromFile(R"(C:\Windows\Fonts\arial.ttf)");
}
catch(eroare_font &er){
std::cout << er.what() << std::endl;
}

text_.setString(getText());
text_.setCharacterSize(20);
Expand All @@ -261,8 +305,7 @@ int CategoryScreen::display(sf::RenderWindow &window) {
while(window.isOpen())
{
sf::Event e{};
while(window.pollEvent(e))
{
while(window.pollEvent(e)){
if(e.type == sf::Event::Closed)
window.close();
}
Expand Down

0 comments on commit 17165fd

Please sign in to comment.