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

Author Topic: SFML2.1-VS12 - Program crashes when using font loaded from file.  (Read 1761 times)

0 Members and 1 Guest are viewing this topic.

Herter

  • Newbie
  • *
  • Posts: 3
    • View Profile
I'm trying to learn a little C++ and SFML and have started creating a little game. Sofar I've made a little stateengine and I'm currently trying to add stuff on my different states. I'm trying to add text to the window and tried first without using a font, but nothing was showing on the screen so I tried adding a font loaded from a file.

Code:

Game.cpp
void Game::run() {

        window.create(sf::VideoMode(800, 600), "SFML works!");

        stateEngine.run(StateEngine::build<IntroState>(stateEngine, window, true));

        while (stateEngine.isRunning()) {
                stateEngine.nextState();
                stateEngine.update();
                stateEngine.draw();
        }
}

#include "IntroState.hpp"

IntroState::IntroState(StateEngine& engine, sf::RenderWindow& window, bool replace)
        : GameState(engine, window, replace) {
        std::cout << "IntroState initialising.." << std::endl;

        //Set IntroState background
        if (!bgTex.loadFromFile("images/intro_bg.jpg"))
                std::cout << "Failed to load IntroState background image!" << std::endl;

        bg.setTexture(bgTex, true);

        sf::Font font;

        if (!font.loadFromFile("fonts/arial.ttf"))
                std::cout << "Failed to load arial!" << std::endl;

        //Set IntroState text
        introText.setFont(font);
        introText.setString("IntroText!");
        introText.setPosition(sf::Vector2f(10, 10));
        introText.setCharacterSize(30);
        introText.setStyle(sf::Text::Bold);
        introText.setColor(sf::Color::Red);
}

void IntroState::draw() {
        std::cout << "IntroState drawn" << std::endl;
        window.clear();
        window.draw(bg);
        window.draw(introText);
        window.display();
}

When the texture is drawn onto the window the program crashes with the following error message:

Code: [Select]
Unhandled exception at 0x00007FFFBBA8261D (sfml-graphics-d-2.dll) in SfmlTest.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
Callstack:
sfml-graphics-d-2.dll!std::_Tree<std::_Tmap_traits<unsigned int,sf::Font::Page,std::less<unsigned int>,std::allocator<std::pair<unsigned int const ,sf::Font::Page> >,0> >::_Lbound(const unsigned int & _Keyval) Line 2106     C++
        sfml-graphics-d-2.dll!std::_Tree<std::_Tmap_traits<unsigned int,sf::Font::Page,std::less<unsigned int>,std::allocator<std::pair<unsigned int const ,sf::Font::Page> >,0> >::lower_bound(const unsigned int & _Keyval) Line 1575 C++
        sfml-graphics-d-2.dll!std::map<unsigned int,sf::Font::Page,std::less<unsigned int>,std::allocator<std::pair<unsigned int const ,sf::Font::Page> > >::operator[](const unsigned int & _Keyval) Line 226  C++
        sfml-graphics-d-2.dll!sf::Font::getTexture(unsigned int characterSize) Line 314 C++
        sfml-graphics-d-2.dll!sf::Text::draw(sf::RenderTarget & target, sf::RenderStates states) Line 219       C++
        sfml-graphics-d-2.dll!sf::RenderTarget::draw(const sf::Drawable & drawable, const sf::RenderStates & states) Line 148   C++
        SfmlTest.exe!IntroState::draw() Line 82 C++
        SfmlTest.exe!StateEngine::draw() Line 70        C++
        SfmlTest.exe!Game::run() Line 17        C++
        SfmlTest.exe!main() Line 8      C++
        [External Code]
 

I have spent a few hours trying to fix this issue, but sofar I've gotten nowhere.. :/

I'm using Visual Studio 2013 and tried first with the VS11 version of SFML as there is no VS12 version yet. Then I tried compiling the VS12 version myself - no luck..

Now I'm thinking it might be a bug in sfml? or I'm missing something as I've tried everything I can think of (except the solution apparantly.. :D)

Any chance a more clever person can assist me on this one?

If you need more info just ask and I shall deliver!

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: SFML2.1-VS12 - Program crashes when using font loaded from file.
« Reply #1 on: July 17, 2014, 01:46:13 am »
The font your text uses has to be alive when your text is drawn. (fonts are to texts what textures are to sprites)
The way you did it, your sf::Font is detroyed at the end of the IntroState constructor.

Herter

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: SFML2.1-VS12 - Program crashes when using font loaded from file.
« Reply #2 on: July 17, 2014, 02:17:32 am »
Ah..  That would make sense  8)

So moving the font as a public variable of IntroState would work? Have to try that tomorrow..  Thanks for the help :)

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: SFML2.1-VS12 - Program crashes when using font loaded from file.
« Reply #3 on: July 17, 2014, 07:48:38 am »
Preferably a private member, but yes.

Herter

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: SFML2.1-VS12 - Program crashes when using font loaded from file.
« Reply #4 on: July 17, 2014, 11:20:48 pm »
Thank you.. it worked perfectly :)

I knew it was something simple that I had missed..   :o