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:
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..
)
Any chance a more clever person can assist me on this one?
If you need more info just ask and I shall deliver!