Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] User Event are giving me a hard time.

This thread is locked; no one can reply to it. rss feed Print
[A5] User Event are giving me a hard time.
ryancoboy
Member #14,468
July 2012

So I've been messing around with A5 some more and the next thing I've decided to learn is how to properly use/define User Defined Events in Allegro. So I made a simple project just to learn the basics. And I have been experiencing nothing but problems for a few days now. So, I would be very grateful if someone could tell me why the following does not work as intended. Thanks in advanced.

Note: this is a smaller snippet of my code and hopefully I have included all the relevant code here.

Main.cpp

int main()
{
//all basic allegro stuff has been initialized at this point
  ALLEGRO_EVENT_QUEUE *event_queue = NULL;

  SubMenu_UserEvent::Instance(); //this is here to call the "SubMenu_UserEvent" ctor

  al_register_event_source(event_queue, &SubMenu_UserEvent::Instance()->Get_Source());
  SubMenu_UserEvent::Instance()->TestEvent(); //fails.
//.
//.
//.

Example.h

#SelectExpand
1//Singleton Class to manage this event. 2class SubMenu_UserEvent 3{ 4private: 5 ALLEGRO_EVENT_SOURCE subMenu_event_source; 6 ALLEGRO_EVENT subMenu_event; 7 8public: 9 static SubMenu_UserEvent* Instance(); 10 11 ALLEGRO_EVENT_SOURCE Get_Source() {return subMenu_event_source;} 12 13 void SetDataAndEmit(int Data) { 14 subMenu_event.user.data1 = Data; 15 TestEvent(); 16 } 17 18 void TestEvent(); 19 20private: 21 SubMenu_UserEvent(); 22 //~SubMenu_UserEvent(); 23 SubMenu_UserEvent(const SubMenu_UserEvent&); 24 SubMenu_UserEvent& operator=(const SubMenu_UserEvent&); 25};

Example.cpp

#SelectExpand
1SubMenu_UserEvent::SubMenu_UserEvent() 2{ 3 al_init_user_event_source(&subMenu_event_source); 4 subMenu_event.user.type = ALLEGRO_GET_EVENT_TYPE('M', 'S', 'M', 'E'); 5} 6 7void SubMenu_UserEvent::TestEvent() 8{ 9 if(!al_emit_user_event(&subMenu_event_source, &subMenu_event, NULL)) 10 { 11 std::cout << "fail"; 12 } 13} 14 15SubMenu_UserEvent* SubMenu_UserEvent::Instance() 16{ 17 static SubMenu_UserEvent temp_instance; 18 19 return &temp_instance; 20}

Thanks,

-Ryan
----------------------------------------------------
If I'm asking, it's because I failed for over 2 hours trying to find the answer on Google / trying to fix it myself.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

It's because you're using the address of a temporary copy of your ALLEGRO_EVENT_SOURCE.

Try this :
ALLEGRO_EVENT_SOURCE& Get_Source() {return subMenu_event_source;}

Otherwise you're just registering a copy. Which won't work.

Specifically, this expression fails to give you the address of your event source :
&SubMenu_UserEvent::Instance()->Get_Source()

ryancoboy
Member #14,468
July 2012

hahahaha...I can't believe I missed that. Three days of staring at this thing and I didn't catch that. I guess I just needed a fresh pair of eyes to look at it. Thanks a lot Edgar.

-Ryan
----------------------------------------------------
If I'm asking, it's because I failed for over 2 hours trying to find the answer on Google / trying to fix it myself.

Go to: