#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
using namespace std;
class Button : public sf::Drawable
{
sf::VertexArray arr;
sf::Text text;
sf::IntRect location;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(arr, states);
target.draw(text);
}
public:
Button(const sf::Color& c1, const sf::Color& c2 ,
const sf::Color& c3, const sf::Color& c4,
const sf::IntRect rect, string displayedText)
{
arr=sf::VertexArray(sf::Quads, 4);
arr[0].position={rect.left + rect.width, rect.top};
arr[1].position={rect.left, rect.top};
arr[2].position={rect.left, rect.top + rect.height};
arr[3].position={rect.left + rect.width, rect.top + rect.height};
arr[0].color=c1;
arr[1].color=c2;
arr[2].color=c3;
arr[3].color=c4;
location=rect;
text.setString(displayedText);
sf::Font font;
if(!font.loadFromFile("Sanchezregular.otf"))
{
cout << "Loading font file failed";
system("pause");
}
text.setFont(font);
text.setColor(sf::Color::Red);
text.setCharacterSize(location.height/5);
text.setOrigin(sf::Vector2f(text.getGlobalBounds().left+text.getGlobalBounds().width/2,
text.getGlobalBounds().top+text.getGlobalBounds().height/2));
}
//some more functions
};
int main()
{
sf::RenderWindow window(sf::VideoMode(1280, 720), "BUG", sf::Style::Close);
Button button(sf::Color(210, 210, 210), sf::Color(210, 210, 210),
sf::Color(110, 110, 110), sf::Color(110, 110, 110),
{400, 200, 300, 100}, "BUTTON");
while (window.isOpen())
{
window.clear(sf::Color(98, 139, 135));
sf::Event event;
while (window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
window.draw(button);
window.display();
}
}
It's funny because when I remove the while loop which is controlled by the pollevent function (so don't handle events) everything is drawn fine except the text and then it crashes when before a white screen appeared