SFML community forums

Help => General => Topic started by: Canvas on July 21, 2012, 05:01:27 am

Title: sf::Text weird display?
Post by: Canvas on July 21, 2012, 05:01:27 am
Hey there guys, ive got a small piece of code
tmpPos.x = 162; tmpPos.y = 34;
                                        tmpText.setUpText(tmpPos,"hair",font);
                                        tmpText.textDisplay.setColor(sf::Color(25,25,25));
                                        tmpText.textDisplay.setCharacterSize(38);
                                        texts.push_back(tmpText);
                                        tmpPos.x = 136; tmpPos.y = 141;
                                        tmpText.setUpText(tmpPos,"colour",font);
                                        tmpText.textDisplay.setColor(sf::Color(25,25,25));
                                        tmpText.textDisplay.setCharacterSize(38);
                                        texts.push_back(tmpText);
 

tmpText is made in the header file so its global to the class, now all this does it sets up a position and passed in a font and a string.
Here is my setUpText Method
void gText::setUpText(rect rectPos,string stringDisplay,sf::Font &font)
{
        timer = 0;
        alive = true;
        fontStyle = font;
        pos = rectPos;
        stringText.clear();
        stringText.insert(0,stringDisplay);
        textDisplay.setFont(fontStyle);
        textDisplay.setString(stringText);
        textDisplay.setPosition(pos.x,pos.y);
        std::cout << stringDisplay << std::endl;
}
 

now on my screen i get this

hair is = fi
colour = colour,

why is hair fi? i cleared the sf::Text but im just really confused on why it isnt the correct string i told it to be, anyone see my problem?

cheers
Title: Re: sf::Text weird display?
Post by: eXpl0it3r on July 21, 2012, 09:16:48 am
Don't use the text as global object. This can lead to problems with loading the correct glyphs from the font afaik. ;)
Title: Re: sf::Text weird display?
Post by: Canvas on July 21, 2012, 12:20:14 pm
ok should i just create my tmpText as in a new instance everytime i want to push a new gText onto the texts stack? Also wouldnt that take more process power? in my header file i have this
gText tmpText;
 

now with it being global i get the random hair is fi, but if i removed it and do this instead
gText tmpText2;
                                tmpPos.x = 325; tmpPos.y = -5;
                                tmpText2.setUpText(tmpPos,"Phor",font);
                                tmpText2.textDisplay.setColor(sf::Color(82,47,12));
                                tmpText2.textDisplay.setCharacterSize(150);
                                texts.push_back(tmpText2);
 

in my draw function
for(int i=0;i<texts.size();i++){if(texts.at(i).rtnAlive() == true){screen.draw(texts.at(i).rtnText());}}
 
i get a memory access violation because the tmpText is deleted as it went out of scope, so how can i fix this without using a global variable?
Title: Re: sf::Text weird display?
Post by: eXpl0it3r on July 21, 2012, 09:10:37 pm
Sorry I can't follow you. :o
What class do you have where, how..?

Anyways to prevent having to deal with global objects you often need to create your class that holds the object as member.
Title: Re: sf::Text weird display?
Post by: Canvas on July 21, 2012, 11:30:27 pm
--FIXED--

ok, in my program i have a class known as gText (game text)
my header file has the sf::Text and sf::String,
now the class just simply stores a string and the object has a method which allows access to the sf::Text to be drawn, i split this up so i could have one class deal with anything that is related to text.
now in my gState (game state) it has a vector of gText so i can push as many as i like on to the vector (stack) and all of them will be read from a for loop and will be drawn to the screen.

But the problem i have with a global gText called tmpText is if i use that push it onto the vector of gText then push another tmpText again but with a different string, the first gText in the vector will not display the correct string that was stored. so if i then remove the global tmpText and create a instance of gText when i want to use it like so
gText tmpText;
tmpPos.x = 325; tmpPos.y = -5;
tmpText.setUpText(tmpPos,"Phor",font);
tmpText.textDisplay.setColor(sf::Color(82,47,12));
tmpText.textDisplay.setCharacterSize(150);
texts.push_back(tmpText);
 
it will push that onto the stack, but once it my program goes down the main loop, it will then check the size of the vector of gText, it will find that there is one to be drawn, but when it actually goes to try and draw it, it breaks my program and gives the error (access memory violiation), so with a global i cant push more than one gText onto my vector of gText and if i create a instance of gtext then push it on to the vector, i get access violiation... I dont know how I can get around this :(

its ok, what i do is i populate my vector first, so if i want two strings on screen i would just do a simple
texts.push_back(tmpText);
texts.push_back(tmpText);
 

then from that i can just use the vector.at() method. cheers for the help but i got it now :) if i have anymore problems i will let you know