Hi,
I'm struggling with drawing a class object, which is to be created during runtime by hitting key A.
Creating is no problem, but how to draw it as it is not created yet. With vectors it's no problem, but how would you handle this case? As I understand it I have to forward declare the object somehow, but how?
#include <SFML/Graphics.hpp>
#include <iostream>
class UserInterface
{
private:
sf::Texture uitexture;
sf::Sprite uisprite;
public:
UserInterface(std::string loadFromFileString, float x, float y)
{
this->uitexture.loadFromFile(loadFromFileString);
this->uisprite.setTexture(uitexture);
this->uisprite.setPosition(x, y);
}
~UserInterface()
{
std::cout << "deleted" << std::endl;
}
void draw(sf::RenderTarget& target)
{
target.draw(uisprite);
}
};
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 500), "Map", sf::Style::Close);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::A)
{
UserInterface* uio1 = new UserInterface("ui.png", 100, 100);
}
break;
}
}
window.clear();
uio1.draw(window); // how to draw that?
window.display();
}
}