trying to make a chess program with FLTK and I got callbacks working with one parameter passed, a number specifically, but I want the callback function to include "window->hide();". This means I need to pass the callback function the window object. Has to be cast to original datatype in callback function. I get the error - no instance of overloaded function "Fl_Button::callback" matches the argument list. As well as the error - 'Fl_Widget::callback': no overloaded function takes 3 arguments. Both refer to where I write "wK->callback(....);
int Board::setup(int argc, char** argv, std::vector<Piece*> wow, int turn)
{
Fl_Window* mainWindow = new Fl_Window(640, 640); //create window
//draw background squares
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
if ((x % 2 == 1 && y % 2 == 0) || (x % 2 == 0 && y % 2 == 1)) {
Fl_Box* box = new Fl_Box(x * 80, y * 80, 80, 80);
box->box(FL_FLAT_BOX);
box->color(FL_BLACK);
}
}
}
//draw pieces
if (turn % 2 == 1) {
whiteButtons(wow, argc, argv, (Fl_Window *)mainWindow);
}
if (turn % 2 == 0) {
blackButtons(wow, argc, argv, (Fl_Window *)mainWindow);
}
mainWindow->show();//main loop
return Fl::run();
}
void Board::whiteButtons(std::vector<Piece*> wow, int argc, char** argv, Fl_Window* w){
Fl_Button* wK = new Fl_Button(wow[0]->yPos * 80, wow[0]->xPos * 80, 80, 80, u8"\u2654");//unicode chess piece
wK->box(FL_NO_BOX);
wK->labelsize(48);
wK->callback(firstPClicked, (void *)0, (void *)w);//0 being its pos in the piece vector
//the word callback is redlined in visual studio
}
//blackButtons omitted for simplicity's sake. exactly the same as whiteButtons.
void firstPClicked(Fl_Widget* f, void* d, void* w) {//cast or smthg
if (((int*)d) == 0) {
firstP = 0;
((Fl_Window*)w)->hide();
}
}
Really not sure what I need to change here. Any ideas or people who know what they're doing?
Code aims to call the setup function until one player wins, effectively refreshing the window by closing it. All functions are correctly defined in the .h file. This is the only error I have at the moment.
Many questions ask if you can have multiple buttons callback to the same function, but I want to know if I can pass multiple things to the callback function. regular callback syntax is below.
void balls(int* argc, char** argv){
Fl_Window *deez = new Fl_Window(300, 300);
Fl_Button *nuts = new Fl_Button(100, 100, 100, 100, "Funny");
nuts->callback(imSoFunny, void* 0);
}
void imSoFunny(Fl_Widget *w, (void*) d){
if (((void*)d) == 0){
std::cout << "ahhh";
}
}
FLTK is old and not updated for modern C++. Heck, it's barely even designed or implemented with old pre-C++11 standard in mind.
It's using C-style coding habits for many things, including callbacks, which means you will have to work extra hard and write extra code to work around such things.
For your specific problem, the "simplest" workaround is to gather all your arguments in a structure of some kind, and use a pointer to an object of that structure as the only argument to the callback function. The callback function then converts (though proper
reinterpret_cast) the pointer to the correct type, and uses the structure to get the different arguments.The nice thing about such a workaround, is that one of the structure members could be a reference or pointer to another object, and you can use a member function of that object as the actual callback.