I tried to create a Button class and use it with SFML, unfortunately, I get the error:
E:\...\something.cpp:154:40: error: no matching function for call to 'sf::RenderWindow::draw(<unresolved overloaded function type>)'
sfmlWin.draw(buttt.Butt);
^
This is the code:
#include <iostream>
#include <functional>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
void onclic(){cout << "Clicked";}
class Button{
public:
string ButtonText;
Color color;
int sizee;
int x;
int y;
RenderWindow winname;
void func(){cout << "Something";}
void Butt(string ButtonText, Color color, int sizee, int x, int y, function<void()> funcc, RenderWindow& winname){
Text tt;
tt.setString(ButtonText);
tt.setColor(color);
tt.setPosition(x, y);
tt.setCharacterSize(sizee);
Event e;
while (winname.pollEvent(e)) {
switch (e.type) {
case Event::MouseButtonPressed:
funcc();
}
}
}
};
int main() {
sf::RenderWindow sfmlWin;
sfmlWin.setTitle("Hello!");
sf::Font font;
while (sfmlWin.isOpen()) {
sf::Event e;
while (sfmlWin.pollEvent(e)) {
switch (e.type) {
case sf::Event::EventType::Closed:
Text t;
t.setString("Something");
sfmlWin.draw(t);
Button buttt;
Color col(0,255,0);
buttt.Butt("CLICK!", col, 20, 50, 50, onclic, sfmlWin);
sfmlWin.draw(buttt.Butt);
}
}
sfmlWin.clear();
sfmlWin.display();
}
return 0;
}
Where did I go wrong?
If a Button class cannot be achieved in this way, is there any other way to implement it?
Your class Button
must be derived from sf::Drawable
and in there you override the draw method:
class Button : public sf::Drawable
{
protected:
void draw (RenderTarget& target, RenderStates states) const override
{
// your draw implementation, which is called Butt() I think
}
}
Then you pass it in your main like this:
Button button;
sfmlWin.draw(button);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.