[A5] User Event are giving me a hard time.

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.


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.


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};


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}


Edgar Reynaldo

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 :


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.

Thread #612755. Printed from Allegro.cc