Hi,
I'm sort of new to C++ and SFML, so if this is something obvious I apologize. I want to save myself some time by defining a button class, which will contain an instance of an sf::RectangleShape (for the background / body of the button) and an instance of sf::Text (for the tooltip / functional discription, etc). Right now I'm just trying to display both the text and the button, there is no logic for checking for clicks, or anything like that. When I compile and run the code shown below, I get no errors. I expect the code to draw a background (which it does) a small red square (defined within the class button, which it does) a line of text in blue (defined within the class button, which it
doesn't draw). and a line of text in green (defined within main(), which it does, this was primarily to test if I was just using the sf::Font and sf::Text member functions incorrectly). I want to know why the text defined in the class isn't being drawn.
#include <SFML/Graphics.hpp>
sf::RenderWindow window(sf::VideoMode(800,600), "Class Test",sf::Style::Titlebar|sf::Style::Close);
class button
{
private:
sf::RectangleShape rect;
sf::Text text;
public:
void setup(sf::Font font)
{
rect.setFillColor(sf::Color::Red);
rect.setSize(sf::Vector2f(10,10));
rect.setPosition(5,5);
text.setColor(sf::Color::Blue);
text.setString("Hello World");
text.setPosition(30,30);
text.setFont(font);
}
void drawit()
{
window.draw(rect);
window.draw(text);
}
};
int main()
{
sf::Texture backgroundt;
if (!backgroundt.loadFromFile("resources/background.png"))
{
return EXIT_FAILURE;
}
sf::Sprite backgrounds(backgroundt);
sf::Event event;
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
sf::Font fonty;
if(!fonty.loadFromFile("resources/sansation.ttf"))
{
return EXIT_FAILURE;
}
sf::Text texty;
texty.setColor(sf::Color::Green);
texty.setString("Hello World");
texty.setPosition(50,50);
texty.setFont(fonty);
button buddy;
buddy.setup(fonty);
while(window.isOpen())
{
while (window.pollEvent(event))
{
if ((event.type == sf::Event::Closed)||((event.type == sf::Event::KeyPressed)&&(event.key.code == sf::Keyboard::Escape)))
{
window.close();
}
}
window.clear();
window.draw(backgrounds);
window.draw(texty);
buddy.drawit();
window.display();
}
return EXIT_SUCCESS;
}