My code was working fine when I was drawing RectangleShapes from a vector, but then I tried doing the same thing with sf::Text and got the error "Unhandled exception at 0x5E7FBA72 (sfml-graphics-d-2.dll) in Dickinson Project.exe: 0xC0000005: Access violation reading location 0x00000004." I'm not sure what's wrong, as the dll's can't be linked incorrectly since the other sfml objects were drawing fine. Here is the problem code: void Engine::Render(){
window.clear(sf::Color::Black);
window.draw(board.GetWalls()[0].GetWallShape());
//comment the next line out, and program runs fine
window.draw(board.GetTexts()[0].GetText());
window.display();
}
Here is the Text class:
#include "stdafx.h"
#include "SFML\Graphics.hpp"
class Text{
public:
Text(std::string str, float posx, float posy, float rotation, int size, sf::Color color);
~Text();
sf::Text GetText();
private:
sf::Text text;
sf::Font font;
};
#include "stdafx.h"
#include "Text.h"
Text::Text(std::string str, float posx, float posy, float rotation, int size, sf::Color color){
font.loadFromFile("fonts/arial.ttf");
text.setFont(font);
text.setString(str);
text.setPosition(posx, posy);
text.setCharacterSize(size);
text.setRotation(rotation);
text.setColor(color);
}
Text::~Text(){
}
sf::Text Text::GetText(){
return text;
}
In another program I have, similar code displays sf::Texts just fine, so what could be the issue?
sf::Text and sf::Font can't be created in the same class declaration.
Do you mean this part:
private:
sf::Text text;
sf::Font font;
because a class can have members of both types.
It looks like you might be trying to draw it before it's been created i.e. board.GetTexts() has a size of zero (doesn't have an element zero). Try outputting the size of that vector instead of drawing it to see if it is indeed zero.