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

Author Topic: Problem loading font from file  (Read 13066 times)

0 Members and 1 Guest are viewing this topic.

patrick

  • Newbie
  • *
  • Posts: 4
    • View Profile
Problem loading font from file
« on: October 26, 2015, 10:40:16 pm »
Hello,

I've been going through some of the tutorials on SFML, and everything has been working until I got to text and fonts.  When I try to run my program, I get the following error:

"Unhandled exception at 0x70e71f34 in fonttest.exe: 0xC0000005: Access violation reading location 0x00331000."

This is what my code looks like:

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

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Text test");

    sf::Font font;
    if(!font.loadFromFile("arial.ttf"))
        std::cout << "font not loaded" << std::endl;

    sf::Text text("hello, world", font, 16);

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

I've tried putting my font file in the same folder as the executable and in the same folder as the source code, but I still get the same error. 

The error occurs at the line
    if(!font.loadFromFile("arial.ttf"))

I'm using SFML 2.3.2 with Visual Studio 2010
« Last Edit: October 26, 2015, 10:48:07 pm by patrick »

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Problem loading font from file
« Reply #1 on: October 26, 2015, 11:39:08 pm »
The font should be in the same directory as your project (i.e C:/Users/{you}/Documents/Visual Studio 2010/Projects/{your project}/).

Your code should also assert/fail when the font is loaded. Currently, you are still using the font even though it might not be loading.

patrick

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Problem loading font from file
« Reply #2 on: October 27, 2015, 12:26:40 am »
The font should be in the same directory as your project (i.e C:/Users/{you}/Documents/Visual Studio 2010/Projects/{your project}/).

Your code should also assert/fail when the font is loaded. Currently, you are still using the font even though it might not be loading.

I've tried it in the directory you mentioned (same folder as my .sln file) with the same results. 

I'm not familiar with assert/fail, but this wouldn't be a fix so much as a better way to catch the failure, right? 

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Problem loading font from file
« Reply #3 on: October 27, 2015, 04:36:09 am »
I'm not familiar with assert/fail, but this wouldn't be a fix so much as a better way to catch the failure, right? 

Sort of. It wont fix your problem but it will stop your code from breaking in unknown areas. Is anything printed to the console when you try to load the font? I'm thinking its possible that your font might be corrupted or unsupported?

Hapax

  • Hero Member
  • *****
  • Posts: 3350
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem loading font from file
« Reply #4 on: October 27, 2015, 03:20:05 pm »
Are you running it from the IDE or directly from the executable file because the if it's the executable file, the font would need to be in the folder as that instead.

As Gambit said, does the console output any information. Also, does it work without crashing if you don't load any font? You should also try just loading the font in an empty main to help narrow down the problem:
#include <SFML/Graphics.hpp>
int main()
{
    sf::Font font;
    if (!font.loadFromFile("arial.ttf"))
        std::cout << "Could not load font." << std::endl;
    return EXIT_SUCCESS;
}
If your console is closing before you get a chance to see its output, consider delaying the program's closure before returning. This may be of some use.

Try also using a full global path to check if the location is the problem.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

patrick

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Problem loading font from file
« Reply #5 on: October 27, 2015, 07:03:08 pm »
Are you running it from the IDE or directly from the executable file because the if it's the executable file, the font would need to be in the folder as that instead.
I'm running it from the IDE. 
As Gambit said, does the console output any information. Also, does it work without crashing if you don't load any font? You should also try just loading the font in an empty main to help narrow down the problem:
The program works just fine without crashing if I don't try to load any font.  I tried your simplified code and still had the problem.  Interestingly enough, while I never make it to the cout line with "Could not load font" I do get some output on the console.  It says "Failed to load font "P)5 arial.ttf " followed by a bunch of ascii junk.  I've tried a few other fonts now too with the same results. 

I also tried using the full global path with no luck either. 

Rosme

  • Full Member
  • ***
  • Posts: 169
  • Proud member of the shoe club
    • View Profile
    • Code-Concept
Re: Problem loading font from file
« Reply #6 on: October 27, 2015, 08:20:38 pm »
Are you mixing debug and release libraries?
Are you sure you took the right binaries for your IDE/compiler version?
GitHub
Code Concept
Twitter
Rosme on IRC/Discord

patrick

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Problem loading font from file
« Reply #7 on: October 27, 2015, 08:50:39 pm »
Are you mixing debug and release libraries?
Are you sure you took the right binaries for your IDE/compiler version?

It turns out I was indeed mixing the debug and release libraries -.-

I somehow missed that I needed to link the -d versions of the .lib files and it didn't give me an error until I tried to use text. 

Thanks for the help everyone