Bryan
Bryan

Reputation: 3

text on SFML won't displayed(?)

template <class type>
class loader{
private:
    tree/*<type>*/ *seq;
    sf::Sprite BG_img;
    sf::Texture texture;
    int seq_c;
    //node<sf::String> cp1;
    //node<sf::String> *temp;
    //declaring text object
    sf::Text text;
    sf::String path;
    string data

public:
    loader(){
        seq=NULL;
        seq_c=1;
        //setting font
        sf::Font myfont;
        myfont.loadFromFile("fonts/Myriad Pro Regular.ttf");

        //setting text
        //text.SetFont(myfont);
        //text.SetSize(20);
        //text.setColor(sf::Color::White);

        data=""; 
    }

    int load_now(sf::String seq_a='1'){
        ifstream ff;

        string s_path =  "Sequence/" + seq_a + ".txt";
        ff.open(s_path, ios::in);
        getline(ff,data);

        path = "Sequence/" + seq_a +".jpg";
        texture.loadFromFile(path);
        BG_img.setTexture(texture);


        //setting text position
        //text.setPosition(10,0);

        //passing text
        text.setString("Hello");
        text.setCharacterSize(20);
        text.setColor(sf::Color::Red);
        text.setStyle(sf::Text::Regular);

    }

    void draw(sf::RenderWindow &window){
        //window.draw(BG_img);
        //draW TO DISPLAY text
        window.draw(text);
    }
    int choose(int inp){
        return load_now(seq->get_data(inp));
    }
};

Hi, i'm trying to display some text using SFML, i've tried to change sf::String and sf::Text but it seems nothing is displayed, i tried to comment and uncomment text setting on constructor too. When i run the program the picture is displayed but not the text. note:

//declare loader
    loader<sf::String> loader_a;
void play(){
    while(window.isOpen()){
        window.clear();

        sf::Event event;
        while(window.pollEvent(event)){
            if (event.type == sf::Event::Closed){
                window.close();
            }
            //start code here
        }

        if (scene_counter==0){
            mainmenu.draw(window);
        }
        else if(scene_counter==1){
            loader_a.draw(window);
        }
        window.display();
    }
}

};

i use this kind of code for displaying the text and picture

Upvotes: 0

Views: 848

Answers (1)

Antoine Brunner
Antoine Brunner

Reputation: 26

You simply created a font that goes out of scope, and that's wrong. A sf::Text takes a reference to a sf::Font, and if it goes out of scope, which is what you did, your text cant display anything. The solution would be to define your sf::Font as a member of your loader class, and that way it should be fine !

Upvotes: 1

Related Questions