SFML community forums
Help => Graphics => Topic started by: homie on May 16, 2010, 01:53:54 am
-
Hi there.
First of all I want to admit that I am just a beginner but I think this doesn't excuse me : P
I wanted to make a class that has an sf::String object as a member. As this object created without the font argument crashes the program after closing I came to those two solutions:
1. Initialize the object in an initialization list like that:
CText(sf::Font& f) : m_string("Sometext", f, 40.f) {}
but with this solution I have to create additional useless instance of sf::Font before I create a CText object like that:
sf::Font _fnt;
_fnt.LoadFromFile("arial.ttf");
CText Foobar(_fnt);
which I wanted to avoid.
2. Create a pointer to the sf::String and create object of it in a constructor:
CText()
{
sf::Font _font;
_font.LoadFromFile("arial.ttf");
this->m_string = new sf::String("Text", _font, 40.f);
}
this one would be cool, but when I try to draw the object:
void Draw(sf::RenderTarget &target)
{
target.Draw(*m_string);
}
I get a crash. I assume that I am making something stupid but my lack of knowledge disallows me to see what am I doing wrong.
-
or...
3. Use SFML 1.6 ;)
-
It solved the crash that was caused by a GetDefaultFont(), but with this piece of code:
CText::CText(void)
{
this->m_string.SetText("Hello");
sf::Font f;
if(!f.LoadFromFile("fonts\\arial.ttf", 50))
{
this->m_string.SetText("Font load failure");
}
else
{
this->m_string.SetText("Font loaded successfully");
this->m_string.SetFont(f);
this->m_string.SetSize(40.f);
}
this->m_string.SetColor(sf::Color(0,0,0));
}
void CText::D(sf::RenderTarget &target)
{
target.Draw(m_string);
}
I'm getting a crash while I'm trying to target.Draw. When I comment out the line
this->m_string.SetFont(f);
the crash doesn't occur. I know that the default font is arial but later I want to load other fonts and this one is only for testing purpose. And the code works well when I'm using it structurally:
sf::String sfSString;
sf::Font f;
if(!f.LoadFromFile("fonts\\arial.ttf", 50))
{
sfSString.SetText("Font load failure");
}
else
{
sfSString.SetText("Font loaded successfully");
sfSString.SetFont(f);
sfSString.SetSize(40.f);
}
Am I doing something wrong?
-
Yes, you're not reading what Laurent says.
And, BTW, you're not using SFML 1.6 or greater... ;)
-
After installing 1.6 version all is ok.
This solution is correct. I test in my code.
-
Read more carefully.
It solved the crash that was caused by a GetDefaultFont()
I've moved to the SFML 1.6 as Laurent said, it solved the crash but I got another problem which I described. Shall I attach the files?
//Edit:
Maybe I've just improperly migrated to the new version but how can this be if the default constructor no longer crashes the app?
-
SFML 1.6 is just a bug fix release, there's nothing to change in your code.
There's one mistake in your code: the font instance is local to the constructor and thus it is destroyed at the ending }. Later when you draw your string, it tries to access a font instance that no longer exists. It should rather be a member of your class.
However SFML is supposed to handle that properly and it shouldn't crash. But fix that and let's see if you still have the same problem :)
-
Aww go to hell : C You just came and solved it in a second. Yep, works fine, thanks : P
-
Good :)
I just checked the code, and indeed in SFML 1.6 it crashes. It's handled better in SFML 2.