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

Author Topic: Cannot display text on screen even though the font file has been loaded  (Read 2742 times)

0 Members and 1 Guest are viewing this topic.

74oshua

  • Newbie
  • *
  • Posts: 4
    • View Profile
Hello, I looked around on the forums and couldn't find any problems like mine.
I am trying to write a journal program, and I want to utilize sf::Text and sf::Font. My program is able to successfully load the font file without error, but when I use text::setString and display the text, I don't get anything. I have noticed that if I setStyle to underline, a line will appear. I have tried this with multiple .ttf files and none have worked. Any help is appreciated  :D

Here is my code:

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <SFML/Audio.hpp>

int main()
{
   int screenWidth = 500;
   int screenHeight = 500;
   sf::Font font;
   sf::Text text;
   sf::RenderWindow window(sf::VideoMode(screenWidth, screenHeight), "Window");
   while (1)
   {
      //refresh window
         window.display();
         window.clear(sf::Color(100,0,0,255));
      //set font
         font.loadFromFile("dotty.ttf");
      //draw text to screen
         text.setFont(font);
         text.setString("Hello world");
         text.setCharacterSize(24);
         text.setFillColor(sf::Color(0,0,0,255));
         text.setStyle(sf::Text::Bold | sf::Text::Underlined);
         window.draw(text);
   }
}

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Well,you could improve your code like this:
-You should use a gameloop and events
while(1)
{
....
}
WRONG
while(window.isOpen()))
    {
        Event event;
        while(window.pollEvent(event))
    {
        if(event.type == Event::Closed)
        {
            window.close();
        }
    }
...
 
CORRECT
Now the error I think you have is that you used window.display(),and everything in the wrong order,the order it should be:
//Outside the gameloop,alongside variables
Font font;
font.loadFromFile("dotty.ttf");
Text text;
text.setFont(font);
text.setString("Hello World");
text.setColor(Color::Red); //Maybe a color
text.setCharacterSize(24);
//Inside the gameloop
//Your events and all that
text.setPosition(250,250);
window.clear(Color::White);
window.draw(text);
window.display();
 
Note:My time is 23:31 now so I am a little sleepy, but the code should be fine.
Guess Who's Back ?

74oshua

  • Newbie
  • *
  • Posts: 4
    • View Profile
thank you! The ultimate problem was that I was putting things in the gameloop that I shouldn't have. (P.S.: setColor is deprecated, the new version is setFillColor)

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Quote
P.S.: setColor is deprecated, the new version is setFillColor
I was a little sleepy + I wasn't writing in C::B or VS, I was writing it here, so I didn't know what was the correct way.
I'm glad,I helped you ;)
Guess Who's Back ?

 

anything