hello,
i have a little project with two class inherit drawable and transformable, and a screen class for event loop and draw instructions.
- a class map implement a tilemap with vertex array.
- a class info try to implement an information box.
- a screen class contain event loop and draw call.
I want display a box on same screen than my map with some information updated when user click on map.
theses two class implement a virtual function draw like explain in tutorial
class Map : public sf::Transformable, public sf::Drawable
{
public:
Map();
void setFilename(std::string filename);
void load(sf::Vector2u tileSize);
protected :
void initVertex();
private :
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
sf::Vector2u tileSize;
sf::VertexArray m_secondary_vertices;
sf::VertexArray m_vertices;
std::string filename;
};
void Map::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
states.texture = tilesetCollection.get("tiles"); // give tileset texture
target.draw(m_vertices, states);
target.draw(m_secondary_vertices, states); // for square need a second texture (like coast i.e)
}
class Info : public sf::Transformable, public sf::Drawable
{
public :
Info();
int init();
private :
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
sf::Text test;
};
int Info::init()
{
sf::Font font;
if ( !font.loadFromFile("../Resources/Dustismo.ttf"))
{
std::cout << "Error loading font" << std::endl;
return -1;
}
test.setFont(font);
test.setCharacterSize(30);
test.setPosition(0,0);
test.setString("hello");
test.setColor(sf::Color::Red);
}
void Info::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
target.draw(test, states); // Crash on this instruction.
}
The screen class is build in this way :
int ScreenGame::Run(sf::RenderWindow &window)
{
map.setFilename("../Maps/default.map");
map.load(tilesize); // init all vertices
info.init(); // init sf::Text object
viewMap.setSize(800,600);
viewMap.setCenter(400,300);
bool Running = true;
while ( Running )
{
sf::Event event;
while (App.pollEvent(event))
{
[...]
}
window.setView(viewMap);
window.clear(sf::Color::Black);
window.draw(map);
window.draw(info); // without this call, all work perfect.
window.display();
}
}
When a run this project with only map draw instruction, work well, my map is correctly draw on my screen. But when i add info draw instruction, project crash with return code 0.
Someone could help me ?