I'm trying to create a central event system for my game where I add some events to a priority_queue and pull off the first one as soon as it expires. But I'm having a problem of understanding if I'm adding them to the queue correctly, if they're in order, or how to pull them off. Can anyone lend a hand here?
Initialize the queue.
my_queue = new MyEventQueue();
Here I create 10 events and put them on a queue after assigning the current timestamp, and then the later timestamp to execute this event.
for (int i = 0; i < 10; i++) {
MyEvent *event = new MyEvent();
event->set_start_timestamp(); // now time
event->set_execute_timestamp(); // time to execute from now
my_queue->push_event(event);
}
Then in each frame of my game, i want to check the queue to see if the event is ready to be executed.
if (my_queue->count() > 0) {
MyEvent *evnt = my_queue->top_event();
// is_expired() is how i was doing it, but didn't work
if (evnt->is_expired()) {
execute_event(evnt->id());
my_queue->pop_event();
}
}
Here are my event classes ...
class MyEvent
{
...
public:
...
bool is_expired();
struct timeval start_timestamp();
double execute_timestamp();
};
struct EventPointerCompare {
bool operator() (MyEvent* lhs, MyEvent* rhs) const {
return ( lhs->_execute_time < rhs->_execute_time );
}
};
// this is my priority_queue wrapper
class MyEventQueue
{
private:
priority_queue<MyEvent *, vector<MyEvent *>,
EventPointerCompare> event_queue;
public:
check_queue();
has_events_of_type_x();
...
Can anyone help me use priority_queue's correctly?