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();
}
}
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();
}
}
U indeed have to forward declare it, but it has nothing to do with forward declaration concept, so what u need is just the fact that u need to declare a variable before u can use it. Logic right? U declare ur variable inside an "if" statement. When the "if" statement ends, "uio1" will be deleted (the pointer, not the underlying obj) because it goes "out of scope", which means that when u do "uio1.draw(window)", "uio1" does not exist. U should read more about C++, or do tutorials, or ask ur tutor / teacher if u have one, since this is a simple concept and the already given information shall point u in the right direction. Try to research the problem and come back with questions