I am receiving an unhandled exception error when trying to draw a Text object. Here is the error message:
Unhandled exception at 0x59085815 (sfml-graphics-2.dll) in Program.exe: 0xC0000005: Access violation reading location 0x3F800058.
The thing is, I am telling main to draw a Drawable object (InputBox), which is telling another Drawable object (Button) to draw the Text. If I replace the Text object with another Drawable, such as a CircleShape, the program works. Also, if I draw the Button in main directly, it works as well. Here is my code:
My main Program
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
InputBox inputBox;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::Black);
window.draw(inputBox);
window.display();
}
return 0;
}
InputBox class
class InputBox : public sf::Transformable, public sf::Drawable
{
public:
InputBox()
{
button = Button();
}
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const
{
states.transform *= getTransform();
target.draw(button, states);
}
private:
Button button;
};
Button class
class Button : public sf::Transformable, public sf::Drawable
{
public:
Button()
{
font.loadFromFile("arial.ttf");
text.setFont(font);
text.setString("Button");
}
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const
{
states.transform *= getTransform();
target.draw(text, states);
}
private:
sf::Font font;
sf::Text text;
};