I edited my code so that it uses boost::signal
But the crash still occurs
class Button
{
private:
sf::Text text;
boost::signal<void()> onEnter;
boost::signal<void()> onExit;
boost::signal<void()> onClick;
void update(sf::RenderWindow & window);
public:
Button(const char * string, sf::Font & font, sf::Color color, sf::Vector2f position, unsigned int size);
~Button();
sf::Text & getText(){return text;}
void draw(sf::RenderWindow & window);
void connect(std::function<void()> onEnter, std::function<void()> onExit ,std::function<void()> onClick);
};
#include "Button.h"
#include <SFML/Window.hpp>
Button::Button(const char * string, sf::Font & font, sf::Color color, sf::Vector2f position, unsigned int size):text(string, font, size)
{
text.setColor(color);
text.setPosition(position);
}
Button::~Button()
{
}
void Button::update(sf::RenderWindow & window)
{
sf::Vector2i mousePos=sf::Mouse::getPosition(window);
float x1=text.getGlobalBounds().left;
float x2=x1+text.getGlobalBounds().width;
float y1=text.getGlobalBounds().top;
float y2=y1+text.getGlobalBounds().height;
if(x1<=mousePos.x && mousePos.x<=x2 && y1<=mousePos.y && mousePos.y<=y2)
{
(onEnter)();
if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
(onClick)();
}
(onExit)();
}
void Button::connect(std::function<void(void)> onEnter, std::function<void(void)> onExit, std::function<void(void)> onClick)
{
this->onEnter.connect(onEnter);
this->onExit.connect(onExit);
this->onClick.connect(onClick);
}
void Button::draw(sf::RenderWindow & window)
{
update(window);
window.draw(text);
}
Weird thing is that even if I don't call update() or window.draw(), my program still crashes when the Button destructor gets called...
Here's how I'm using my button class
button=new Button("Hello, World!", font(), sf::Color::Black, sf::Vector2f(100, 100), 40);
button->connect([&](){button->getText().setCharacterSize(50);},
[&](){button->getText().setCharacterSize(40);},
[&](){std::cout<<"YOU CLICKED ME\n!";});