SFML community forums

Help => Graphics => Topic started by: LonelyCoder on October 21, 2013, 10:38:40 am

Title: Error to load font
Post by: LonelyCoder on October 21, 2013, 10:38:40 am
Hey, i got a problem to load a font in SFML.. The code is correct. The font is in a right place.. I don't understand what is the problem.. Can you help me?

That is my code:
#include "SFML/Graphics.hpp"
#include "SFML\Window.hpp"
#include "SFML\System.hpp"
#include <iostream>
using namespace std;

class FPS
{
       
public:
        int fps, counter;
        sf::Clock clock;
        FPS(int x = 0, int y = 0) {fps = x;counter = y;}

        void Update()
        { counter++; if(clock.getElapsedTime().asMilliseconds() >= 125)
        {fps = counter*8; counter = 0; clock.restart(); } }

};


int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
        FPS FPS(0,0);
       
        sf::Text text2;
        sf::Font font1;
       
        °°°°°°if(!font1.loadFromFile("arial.ttf"))
        {
                return 1;
        }
        text2.setFont(font1);
        text2.setString("Hello world");
        text2.setColor(sf::Color::White);
        text2.setPosition(0, 0);
        text2.setScale(2, 1);
       
        while (window.isOpen())
    {
               
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

                FPS.Update();
                std::cout << FPS.fps << std::endl;

                window.draw(text2);
                window.clear();
        window.display();
               
    }

    return 0;
}

That is my problem:
(http://i.imgur.com/2t633au.png)

And that is my config and databases:
(http://i.imgur.com/qBTyQP8.png)

(http://i.imgur.com/BWZ0Pa8.png)

Regards. Lonely Coder
Title: Re: Error to load font
Post by: Ixrec on October 21, 2013, 10:45:25 am
It's not in the right place.  You put it next to the code file, but source code doesn't run in place.  What runs is the executable file.  You need to put the font next to the executable.  If you don't know where that is, recompile it and pay attention to the output.

Also, IDEs can sometimes screw with the working directory for various reasons, so if that doesn't work be sure to try running the .exe directly (ie, double-clicking on it).

For the record, arial.ttf isn't a free font, so you'll want to use something else in your final product.  I know it doesn't matter now but the sooner you know the better.
Title: Re: Error to load font
Post by: binary1248 on October 21, 2013, 10:46:07 am
Check here: http://sfml-dev.org/tutorials/2.1/graphics-text.php

Under the section "Drawing text" there is a code example.

You are missing 1 very important line...
Title: Re: Error to load font
Post by: LonelyCoder on October 21, 2013, 12:26:32 pm
The problem keep.. I update the code, the problem it's on the line with ("°"), i'm also try to put the arial.ttf in paste debug and release, but don't work.. At last i didn't know that wasn't free. :S
Title: Re: Error to load font
Post by: Laurent on October 21, 2013, 01:09:25 pm
Make sure that you're not using release SFML libraries in debug mode.
Title: Re: Error to load font
Post by: LonelyCoder on October 21, 2013, 03:24:23 pm
Ty everyone, problem solved..
I had a mistake in my libraries, like Laurent said. Thanks one more time. :)