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

Author Topic: sf::Text throws an exception  (Read 2874 times)

0 Members and 1 Guest are viewing this topic.

iride

  • Jr. Member
  • **
  • Posts: 88
    • View Profile
    • Email
sf::Text throws an exception
« on: October 31, 2012, 03:12:09 am »
I figured that the constructor of sf::Text no longer has Font::getDefaultFont() as its second default argument.
so I just put sf::Text("...", sf::Font()), but this throws an exception.
But when I predefine a font object with its default constructor, it doesn't throw an exception. why?

I also just realized that either way doesn't render any text onto the screen.

 #include <SFML/Graphics.hpp>
 
 int main()
 {

     sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
     
     //Exception
     sf::Text text("Exception thrown because of this->", sf::Font());
     
     //no exception
     //sf::Font font;
     //sf::Text text("Exception not thrown here", font);
 
     while (window.isOpen())
     {
         sf::Event event;
         while (window.pollEvent(event))
         {
             if (event.type == sf::Event::Closed)
                 window.close();
         }
         window.clear();
         window.draw(text);
         window.display();
     }
 
     return EXIT_SUCCESS;
 }
 
« Last Edit: October 31, 2012, 03:18:07 am by iride »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: sf::Text throws an exception
« Reply #1 on: October 31, 2012, 03:29:35 am »
Well I guess the exception itself tells you what the problem is (at least in debug mode it will have a 'descriptive' error).

I can't really tell what exactly causes the (to me unknown) exception, but by questioning the sense of your code I automatically will 'resolve' the problem. I mean the constructor of sf::Text takes either nothing, or a string and a reference to a sf::Font object. So what should the text object do with a temporary font object that only exists in the constructor? There's no sense in such code.
The API has changed and you'll have to go with the change and not try to work around it, thus every sf::Text need a valid sf::Font (okay you can also use the default constructor, for which the sf::Text then doesn't have a a reference to a sf::Font object).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

iride

  • Jr. Member
  • **
  • Posts: 88
    • View Profile
    • Email
Re: sf::Text throws an exception
« Reply #2 on: October 31, 2012, 03:47:48 am »
I see the reason why the exception is thrown.
The API has changed and you'll have to go with the change and not try to work around it, thus every sf::Text need a valid sf::Font (okay you can also use the default constructor, for which the sf::Text then doesn't have a a reference to a sf::Font object).
I don't quite understand what you're saying here though.
with only using the default constructor, the text never gets rendered.
sf::Text text //use a default constructor
text.setString(sf::String("hello, world!"));
window.draw(text)//nothing gets rendered without loading a font

So to use sf::Text, I must load a font from a .ttf file?
« Last Edit: October 31, 2012, 03:58:25 am by iride »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text throws an exception
« Reply #3 on: October 31, 2012, 08:18:55 am »
Quote
So to use sf::Text, I must load a font from a .ttf file?
Yes!
Laurent Gomila - SFML developer