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

Author Topic: Crash when using Font  (Read 2099 times)

0 Members and 1 Guest are viewing this topic.

Xilen

  • Newbie
  • *
  • Posts: 14
    • View Profile
Crash when using Font
« on: July 06, 2014, 08:26:24 am »
I created a font the same way as the wiki said and it just crashes with "whatever.exe Has Stopped Working". Is this a bug with SFML or am I just doing something wrong?

#include <iostream>
#include "SFML/Graphics.hpp"

using namespace std;

sf::Font font;

int main()
{

        sf::RenderWindow window(sf::VideoMode(640, 480, 32), "Test Window");


        if (!font.loadFromFile("AriBlk.ttf"))
                cout << "Error Loading Font" << endl;


        sf::Text text("Hello, World", font, 20);
        text.setColor(sf::Color(100,100,100));


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

 
And yes, it IS Font and not Text and the font file is in the same directory as the exe. I'm not using any IDE, just a text editor (Sublime Text 3 to be specific).

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Crash when using Font
« Reply #1 on: July 06, 2014, 08:49:37 am »
Avoid global variables in general and especially global SFML variables.
And try to avoid "using namespace ...".

Construct the sf::Font where you need it, inside main() : 

#include <iostream>
#include "SFML/Graphics.hpp"

int main()
{
    sf::RenderWindow window(sf::VideoMode(640, 480, 32), "Test Window");

    sf::Font font;
    if (!font.loadFromFile("AriBlk.ttf"))
        std::cout << "Error Loading Font" << std::endl;
 

Xilen

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Crash when using Font
« Reply #2 on: July 06, 2014, 08:54:16 am »
Just tried that; same error.
Side node: Why avoid "using namespace"? Is there a specific reason?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Crash when using Font
« Reply #3 on: July 06, 2014, 08:58:39 am »
Could you post the exact error(s)?

What compiler are you using?

What SFML version?

Pre-build SFML or custom compiled?

The reason to avoid "using namespace" is simply that it pulls everything from the namespace into the global namespace and thus negates the whole point of namespaces in the first place (avoiding name clashes).
It's generally better to explicitly qualify names or only pull in specific things like "using sf::Font;" rather than everything from a namespace.
« Last Edit: July 06, 2014, 09:04:06 am by Jesper Juhl »

Xilen

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Crash when using Font
« Reply #4 on: July 06, 2014, 10:29:12 am »
There are no errors when compiling only when I actually launch the exe that's created.
I'm using MinGW 4.8.
SFML Version 2.1
I compiled SFML myself using cmake and MinGW.

Xilen

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Crash when using Font
« Reply #5 on: July 06, 2014, 11:15:35 am »
I'm actually completely wrong about this. It gets to the clearing of the screen and THEN it crashes.
« Last Edit: July 06, 2014, 11:40:08 am by Xilen »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Crash when using Font
« Reply #6 on: July 06, 2014, 11:18:09 am »
There are no errors when compiling only when I actually launch the exe that's created.
I realize that. Could you quote them, exactly, please?
Without the actual error messages all I can do to help you is look in a really crappy crystal ball.

Xilen

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Crash when using Font
« Reply #7 on: July 06, 2014, 11:39:42 am »
Thanks a bunch for the help, I finally gave in and downloaded the nightly builds of SFML and it works fine. I'm not sure exactly why or how but the window.clear() line was crashing the program.

 

anything