Ocamler <ocam...@...> writes:
> Hi,I'm trying to use Agar from C++, and am having trouble getting event 
callbacks, like those initiated by AG_Button click events, to call a class 
method properly.  

Try this:

#include <agar/core.h>
#include <agar/gui.h>
#include <agar/dev.h>
#include <stdio.h>

class cppgui
{
private:
        AG_Window *win_;
        int foo;

        // Callbacks must be global (and thus static)!!!
        static void ButtonCallback(AG_Event *event) {
                cppgui *This = (cppgui*)AG_PTR(1); // Let the static metod have 
acces to the calling object instance
                printf("You clicked the button! And foo is %i\n", This->foo);
        }

public:
        cppgui()
        {
                this->foo = 1234; // something...

                win_ = AG_WindowNew(0);
                AG_ButtonNewFn(win_, 0, "Click me", ButtonCallback, "%p", this);
                AG_WindowShow(win_);
        }

        ~cppgui() {
                AG_Destroy();
        }

        void run() {
                AG_EventLoop();
        }
};

void die(const char *format, ...)
{
        va_list arg;
        va_start(arg, format);
        vfprintf(stderr, format, arg);
        va_end(arg);
        exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])
{
        if (AG_InitCore("test", 0) == -1)
                die("%s\n", AG_GetError());

        if (AG_InitVideo(200, 200, 32, 0) == -1)
                die("%s\n", AG_GetError());

        cppgui test;
        test.run();

        return 0;
}

Let me know if you found some other way.
- Eisbaw


_______________________________________________
Agar mailing list
[email protected]
http://libagar.org/lists.html

Reply via email to