Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Displaying sf::String from a pointer  (Read 3175 times)

0 Members and 1 Guest are viewing this topic.

homie

  • Newbie
  • *
  • Posts: 4
    • View Profile
Displaying sf::String from a pointer
« 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:
Code: [Select]
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:
Code: [Select]
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:
Code: [Select]
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:
Code: [Select]
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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Displaying sf::String from a pointer
« Reply #1 on: May 16, 2010, 09:37:44 am »
or...

3. Use SFML 1.6 ;)
Laurent Gomila - SFML developer

homie

  • Newbie
  • *
  • Posts: 4
    • View Profile
Displaying sf::String from a pointer
« Reply #2 on: May 16, 2010, 05:02:33 pm »
It solved the crash that was caused by a GetDefaultFont(), but with this piece of code:
Code: [Select]
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
Code: [Select]
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:
Code: [Select]
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?

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Displaying sf::String from a pointer
« Reply #3 on: May 17, 2010, 08:42:18 am »
Yes, you're not reading what Laurent says.
And, BTW, you're not using SFML 1.6 or greater... ;)
Mindiell
----

Jack Flower

  • Newbie
  • *
  • Posts: 2
    • View Profile
Displaying sf::String from a pointer
« Reply #4 on: May 17, 2010, 04:37:28 pm »
After installing 1.6 version all is ok.
This solution is correct. I test in my code.

homie

  • Newbie
  • *
  • Posts: 4
    • View Profile
Displaying sf::String from a pointer
« Reply #5 on: May 17, 2010, 07:06:43 pm »
Read more carefully.
Quote
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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Displaying sf::String from a pointer
« Reply #6 on: May 17, 2010, 07:21:23 pm »
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 :)
Laurent Gomila - SFML developer

homie

  • Newbie
  • *
  • Posts: 4
    • View Profile
Displaying sf::String from a pointer
« Reply #7 on: May 17, 2010, 07:36:09 pm »
Aww go to hell : C You just came and solved it in a second. Yep, works fine, thanks : P

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Displaying sf::String from a pointer
« Reply #8 on: May 17, 2010, 07:40:45 pm »
Good :)

I just checked the code, and indeed in SFML 1.6 it crashes. It's handled better in SFML 2.
Laurent Gomila - SFML developer

 

anything