OpenGL help glutMouseFun () function

0

I would like someone to help me or give me an explanation regarding a problem presented with the glutMouseFunc () function, I have created a class called Button

class Button{ 
private:
    char *texto;
    int iniX;
    int iniY;
    int tamX;
    int tamY;
    void (*accion)();
public:
    Button(char *, int, int);
    void dibujar();
    void onMouseClick(int, int, int, int);
    void mouseClickLeft(void()); 
};

What happens is that when I implement the functions mouseClickLeft () and onMouseClick () I see an error.

void Button::mouseClickLeft(void func())
{
    accion = func;
    glutMouseFunc(onMouseClick);
}

void Button::onMouseClick(int button, int state, int x, int y)
{
    if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        if((x>iniX && x<(iniX+tamX))&&(y<iniY && y>(iniY-tamY)))
        {
            accion;
        }
    }
}

wanting to pass the onMouseClick function to the glutMouseFun () function, tells me there is a conversion error. Someone can guide me to a possible solution, since I would like to generalize the function and so when creating an instance of objects, I can call the mouseClickLeft () function and do all the work.

    
asked by Nick JAG 01.12.2018 в 23:07
source

1 answer

1

The problem I'm seeing is when you make this call:

glutMouseFunc(onMouseClick);

Although the function onMouseClick () has declared the function with four integers, since it is a method of the object, it implicitly carries another parameter that is the pointer this .

The problem would be to declare the onMouseClick () method as static . But another problem appears is that you can not access the attributes of the Button object from this function.

I would solve it in the following way;

class Button;
std::vector<Button*> allButtons;
//--------------------------------------------------------------------------------------------------
class Rect
{
public:
    int rx;
    int ry;
    int rw;
    int rh;
public:
    Rect()
    {
    }
    bool contains(int x, int y) const
    {
        if(rx > x && ry > y && (rx+rw) < x && (ry+rh) < y)
        {
            return true;
        }
        return false;
    }
};
//--------------------------------------------------------------------------------------------------
class Button
{
private:
    Rect rect;
    const char* texto;
    void (*action)();
public:
    Button(int x, int y, const char* texto, void (*action)());
    ~Button();
    const Rect& getRect() const
    {
        return rect;
    }
    void dibujar();
    void onMouseClick(int button, int state, int x, int y);
};

Button::Button(int x, int y, const char* _texto, void (*_action)())
{
    rect.rx = x;
    rect.ry = y;
    rect.rw = 100;
    rect.rh = 50;
    texto = _texto;
    action = _action;
    allButtons.push_back(this);
}
Button::~Button()
{
    std::vector<Button*>::iterator it;
    for(it = allButtons.begin(); it != allButtons.end(); it++)
    {
        if((*it) == this)
            allButtons.erase(it);
        break;
    }
}
void Button::onMouseClick(int button, int state, int x, int y)
{
    action();
}
//--------------------------------------------------------------------------------------------------
void onGlutMouseClick(int button, int state, int x, int y)
{
    std::vector<Button*>::const_iterator it;
    for(it = allButtons.begin(); it != allButtons.end(); it++)
    {
        if((*it)->getRect().contains(x, y))
        {
            (*it)->onMouseClick(button, state, x, y);
        }
    }
}

Register when you start the program the event capture click

glutMouseFunc(onGlutMouseClick);
    
answered by 04.12.2018 / 23:23
source