Partage
  • Partager sur Facebook
  • Partager sur Twitter

Erreur de segemtation Text sfml

    30 novembre 2021 à 16:53:07

    bonjour je viens voir sur ce forum si quelqu'un pourrait m'aider,

    je suis sur un projet depuis quelque semaines déjà et je me suis confronté à un problème lié à un de mes objet, j'ai réussi à retrouver une partie de l'erreur mais je n'arrive toujours pas à comprendre comment la résoudre.

    mon erreur est une erreur de segmentation lié à la variable de type sf::Text de ma classe button,

    pouriez vous m'aider ?

    merci d'avance

    main.cpp:

    //
    // Created by Strong21 on 30/10/2021.
    //
    #include <iostream>
    #include "Game.h"
    
    using namespace std;
    
    int main(int argc, char *argv[]) {
        /*/debug
        cout << argv[0] << argv[1] << endl;
        //debug
        if(argc < 2) {
            cerr << "ERROR : use a parameter : -p <PARAMETER>" << endl;
            return EXIT_FAILURE;
        }*/
        /*/debug
        cout << argv[1] << endl;
        //debug*/
        //string arg{argv[1]};
        bool w = true;
        /*if (arg == "fullscreen")
            w = false;
        else if (arg == "window")
            w = true;*/
        Game game("SSSSLIME", 1600, 824, w);
        game.run();
        return 0;
    }
    

    Game.h

    //
    // Created by tidian on 08/11/2021.
    //
    
    #ifndef SSSSLIME_GAME_H
    #define SSSSLIME_GAME_H
    
    #include <iostream>
    #include <SFML/Graphics.hpp>
    
    #include "event.h"
    #include "buttons.h"
    #include "SColors.h"
    
    class Game : Event {
    public:
        Game();
        Game(sf::String name, int width, int height, bool windowed);
    
        void init();
        void tick();
        void clear();
        void draw();
        void run();
    
        //override methode
    private:
        void onInit() override;
    
        void onTick() override;
    
        void onDraw() override;
    
        void onSceneChange() override;
    
        void onRightClick() override;
    
        void onLeftClick() override;
    
        void onMiddleClick() override;
    
        void onKeyPressed(sf::Keyboard::Key key) override;
    
        //variables
        sf::RenderWindow app;
        int scene;
    
        //buttons
        buttons btnExit;
    
    
    
    };
    
    
    #endif //SSSSLIME_GAME_H
    



    Game.cpp

    //
    // Created by tidian on 08/11/2021.
    //
    
    #include "Game.h"
    
    Game::Game() {}
    
    Game::Game(sf::String name, int width, int height, bool windowed) {
        if (windowed) {
            this->app.create(sf::VideoMode(width, height), name, sf::Style::Close);
        } else {
            this->app.create(sf::VideoMode(width, height), name, sf::Style::Fullscreen);
        }
    }
    
    void Game::init() {
        onInit();
        btnExit.setVisible(true);
    }
    
    void Game::tick() {
    
    }
    
    void Game::clear() {
        switch (this->scene) {
            case 0:
                this->app.clear(SColors::mainMenuColor);
                break;
            case 1:
                this->app.clear(SColors::mapBackground);
                break;
            case 2:
                this->app.clear(SColors::alchemyTableBackground);
                break;
            default:
                this->app.clear(sf::Color::Black);
        }
    }
    
    void Game::draw() {
        onDraw();
        btnExit.draw(this->app);
    }
    
    void Game::run() {
        this->init();
        while(this->app.isOpen()) {
            sf::Event e{};
            while (this->app.pollEvent(e)) {
                if (e.type == sf::Event::Closed)
                    this->app.close();
                if (e.type == sf::Event::KeyPressed) {
                    onKeyPressed(e.key.code);
                }
                if (e.type == sf::Event::MouseButtonPressed) {
                    if (e.mouseButton.button == sf::Mouse::Left)
                        onLeftClick();
                    if (e.mouseButton.button == sf::Mouse::Right)
                        onRightClick();
                    if (e.mouseButton.button == sf::Mouse::Middle)
                        onMiddleClick();
                }
            }
            this->tick();
            this->draw();
            this->app.display();
            this->clear();
        }
    
    }
    
    void Game::onInit() {
        this->app.setPosition(sf::Vector2i(50, 50));
        this->scene = 0;
        this->btnExit = buttons(20, 20, 150, 50, "test1");
    
    }
    
    void Game::onTick() {
    
    }
    
    void Game::onDraw() {
    
    }
    
    void Game::onSceneChange() {
    
    }
    
    void Game::onRightClick() {
    
    }
    
    void Game::onLeftClick() {
    
    }
    
    void Game::onMiddleClick() {
    
    }
    
    void Game::onKeyPressed(sf::Keyboard::Key key) {
        if (key == sf::Keyboard::Escape)
            this->app.close();
    }
    
    

    button.h

    //
    // Created by tidian on 09/11/2021.
    //
    
    #ifndef SSSSLIME_BUTTONS_H
    #define SSSSLIME_BUTTONS_H
    
    #include "event.h"
    #include "SColors.h"
    #include <SFML/Graphics.hpp>
    #include <iostream>
    
    class buttons {
    public:
        buttons();
        buttons(buttons &btn);
        buttons(int x, int y, int width, int height, std::string text);
    
    
        void Click(void (*func)(), sf::RenderWindow &app);
        void draw(sf::RenderWindow &app);
        void setVisible(bool visible);
    
    private:
        int x, y, width, heigh;
        bool isVisible;
        std::string text;
        sf::Text btnText;
        sf::RectangleShape btnRectangle, btnBackground;
        sf::Font btnFont;
    };
    
    
    #endif //SSSSLIME_BUTTONS_H
    

    button.cpp

    //
    // Created by tidian on 09/11/2021.
    //
    
    #include "buttons.h"
    
    buttons::buttons() {
    
    }
    
    buttons::buttons(buttons &btn) : x(btn.x), y(btn.y), width(btn.width), heigh(btn.heigh), text(btn.text), btnText(btn.btnText),
                                     btnRectangle(btn.btnRectangle), btnBackground(btn.btnBackground), btnFont(btn.btnFont) {
    
    }
    
    buttons::buttons(int x, int y, int width, int height, std::string text) : x(x), y(y), width(width), heigh(height), text(text), isVisible(false) {
        this->btnBackground = sf::RectangleShape(sf::Vector2f(this->width, this->heigh));
        this->btnBackground.setPosition(this->x, this->y);
        this->btnBackground.setFillColor(SColors::btnBackgroundGrey);
    
        this->btnRectangle = sf::RectangleShape(sf::Vector2f(this->width - 10, this->heigh - 10));
        this->btnRectangle.setPosition(this->x + 5, this->y + 5);
        this->btnRectangle.setFillColor(SColors::btnGrey);
    
        this->btnFont.loadFromFile("setup/fonts/buttons.otf");
    
        this->btnText = sf::Text(text, this->btnFont);
        this->btnText.setPosition(x + 10, y);
        this->btnText.setFillColor(SColors::btnTextGrey);
    }
    
    void buttons::Click(void (*func)(), sf::RenderWindow &app) {
        if (sf::Mouse::getPosition(app).x > this->x && sf::Mouse::getPosition(app).x < this->x + this->width && sf::Mouse::getPosition(app).y > this->y && sf::Mouse::getPosition(app).y < this->y + this->heigh && isVisible)
            func();
    }
    
    void buttons::draw(sf::RenderWindow &app) {
        if (this->isVisible) {
            app.draw(this->btnBackground);
            app.draw(this->btnRectangle);
            app.draw(this->btnText);
        }
    }
    
    void buttons::setVisible(bool visible) {
        this->isVisible = visible;
    }

    event.h

    //
    // Created by tidian on 08/11/2021.
    //
    
    #ifndef SSSSLIME_EVENT_H
    #define SSSSLIME_EVENT_H
    
    #include <SFML/Graphics.hpp>
    
    class Event {
        virtual void onInit();
    
        virtual void onRightClick();
        virtual void onLeftClick();
        virtual void onMiddleClick();
    
        virtual void onKeyPressed(sf::Keyboard::Key key);
    
        virtual void onTick();
        virtual void onDraw();
    
        virtual void onSceneChange();
    };
    
    
    #endif //SSSSLIME_EVENT_H
    

    event.cpp

    //
    // Created by tidian on 08/11/2021.
    //
    
    #include "event.h"
    
    void Event::onInit() {
    
    }
    
    void Event::onRightClick() {
    
    }
    
    void Event::onLeftClick() {
    
    }
    
    void Event::onMiddleClick() {
    
    }
    
    
    void Event::onTick() {
    
    }
    
    void Event::onDraw() {
    
    }
    
    void Event::onSceneChange() {
    
    }
    
    void Event::onKeyPressed(sf::Keyboard::Key) {
    
    }
    

    SColors.h

    //
    // Created by tidian on 09/11/2021.
    //
    
    #ifndef SSSSLIME_SCOLORS_H
    #define SSSSLIME_SCOLORS_H
    
    #include <SFML/Graphics.hpp>
    
    namespace SColors {
        const sf::Color btnBackgroundGrey(10, 10, 10);
        const sf::Color btnGrey(130, 130, 130);
        const sf::Color btnTextGrey(10, 10, 10);
    
        const sf::Color mapBackground(50, 30, 0);
        const sf::Color alchemyTableBackground(80, 50, 0);
        const sf::Color mainMenuColor(0, 120, 140);
    }
    
    #endif //SSSSLIME_SCOLORS_H
    







    • Partager sur Facebook
    • Partager sur Twitter
      30 novembre 2021 à 23:59:09

      Il faut les messages d'erreur complet, la call stack au moment du crash, etc. Un max d'infos.

      C'est étrange d'avoir un constructeur par défaut et par copie pour buttons. Si tu n'as pas une bonne raison d'avoir ca, supprime les.

      • Partager sur Facebook
      • Partager sur Twitter

      Rejoignez le discord NaN pour discuter programmation.

      Erreur de segemtation Text sfml

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      • Editeur
      • Markdown