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

Author Topic: How to display numbers in a window?  (Read 3234 times)

0 Members and 1 Guest are viewing this topic.

Star

  • Newbie
  • *
  • Posts: 2
    • View Profile
How to display numbers in a window?
« on: March 26, 2015, 12:56:46 pm »
Hi everyone!

I am using SFML 2.2 and VS 13.
My OS: Win 8.1 x64

I am trying to display some numbers in a window. But my window still stay black
I am trying it with the following code
#include <string>
#include <sstream>
#include <iostream>
#include <SFML/Graphics.hpp>

int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Test", sf::Style::Default);

        int number = 1000;

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                sf::Font font;
                if (!font.loadFromFile("font.ttf"))
                {
                        std::cerr << "error";
                }

                std::ostringstream oss;
                oss << number;

                sf::Text text;
                text.setFont(font);
                text.setString(oss.str());
                text.setColor(sf::Color::Red);
                text.setPosition(400, 300);

                window.clear();

                window.draw(text);

                window.display();
        }

        return 0;
}
 

I am sending this code my friends and he work perfect!
What is my problem?

P.S. Sorry for my english, but i hope you understand me!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10823
    • View Profile
    • development blog
    • Email
Re: How to display numbers in a window?
« Reply #1 on: March 26, 2015, 12:59:26 pm »
Does the font get loaded?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to display numbers in a window?
« Reply #2 on: March 26, 2015, 01:06:27 pm »
Load the font outside the game loop.
Laurent Gomila - SFML developer

Dejan Geci

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: How to display numbers in a window?
« Reply #3 on: March 26, 2015, 01:38:46 pm »
As Laurent said, you should load the font outside of the game loop. In your code, the font is loaded many times per second and it totally unnecessary :) Also check if the "font.ttf" is in the same folder as your .exe file. Does this work?

#include <string>
#include <sstream>
#include <iostream>
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Test", sf::Style::Default);

    sf::Font font;
    if (!font.loadFromFile("font.ttf"))
    {
        std::cerr << "error";
    }

    int number = 1000;
    std::ostringstream oss;
    oss << number;

    sf::Text text;
    text.setFont(font);
    text.setString(oss.str());
    text.setColor(sf::Color::Red);
    text.setPosition(400, 300);

    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 0;
}

Star

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: How to display numbers in a window?
« Reply #4 on: March 27, 2015, 12:15:32 am »
The problem was in the font, but not in the location of the font.
Font does not contain numbers, so I guess there was no picture.

Thank you for your participation, dejan.geci, Laurent, eXpl0it3r