0

I'm using the Allegro 5 framework. When I need to create an event queue I have to call 'al_create_event_queue' and check for errors, and to destroy it 'al_destroy_event_queue'. Since it is the same mechanism I have to use for each object to be created, it is quite boring.

My question is: there is a way to 'port' a structure to a class so that the constructor of my_event_queue actually calls the 'al_create_event_queue' and the destructor calls the 'al_destroy_event_queue'? If not, how could I track object created by these functions so that they are auto-deleted when my 'Game' main handler class is destructed?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Silver
  • 13
  • 1

2 Answers2

1

Yes. You do exactly what you said. You seem to have figured this one out for your self.

However, you need to make sure you handle copying correctly. You should either disallow copying of this object (via something like boost::noncopyable), or you should write a copy constructor and copy assignment operator for it. Now, Allegro event queue's are not copyable (there's no Allegro function for that), so you should probably just disallow copying.

If you have access to C++0x, a move constructor and move assignment operator would be fine.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
0

Of course you can... simply put the code to create the structure in the constructor, and the code to delete it in the destructor.

struct MyQueue {
  MyQueue() : queue(al_create_event_queue() { }
  ~MyQueue() { al_destroy_event_queue(queue); }

  ALLEGRO_EVENT_QUEUE* queue;

private:
  MyQueue(const MyQueue&);
  MyQueue& operator =(MyQueue);
};

Note that you can't do too much to wrap these types... you pass around those pointers so much in Allegro code that you basically have to expose the underlying queue object to the world.

Dennis Zickefoose
  • 10,791
  • 3
  • 29
  • 38
  • the prototype is "ALLEGRO_EVENT_QUEUE* al_create_event_queue()"..how to match together ALLEGRO_EVENT_QUEUE and MyQueue data types? I mean, I want to call functions which require ALLEGRO_EVENT_QUEUE structure by passing MyQueue instead... And what do '&' at the end of variable and 'operator' stand for? (Sorry, I come from python!) – Silver Jun 27 '11 at 22:45
  • 1
    @Silver: If you have come from Python, this isn't what you need to be doing. You need to learn how C++ works; you can't just jump into game development in C++ without being familiar with the syntax. – Nicol Bolas Jun 27 '11 at 23:18
  • @Silver: I updated to fix the actual interface, but I tend to agree with Nicol. Learning C++ by diving in doesn't work as well as it does with some languages. And using Allegro to do it is even worse... it's a C library that happens to compile with a C++ compiler, and you'll pick up bad habits from code that interfaces with it. – Dennis Zickefoose Jun 28 '11 at 02:20
  • @Dennis Zickefoose: Thank you for your help. Could you suggest me a good reference to C/C++ (as complete as possible)? – Silver Jun 28 '11 at 08:39
  • @Dennis Zickefoose: Thank you again – Silver Jun 28 '11 at 09:15