1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
/*
* tqueue.h - A queue of time-based events for animation.
*
* Copyright (C) 2000-2013 The Exult Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef TQUEUE_H
#define TQUEUE_H 1
#include <vector>
#include <list>
#include "common_types.h"
/*
* An interface for entries in the queue:
*/
class Time_sensitive {
int queue_cnt; // # of entries for this in queue.
bool always; // Always do this, even if paused.
protected:
virtual void dequeue() {
queue_cnt--;
}
public:
friend class Time_queue;
Time_sensitive() : queue_cnt(0), always(false)
{ }
virtual ~Time_sensitive();
int in_queue() const {
return queue_cnt > 0;
}
void set_always(bool tf) // Should be called before placing in
// queue.
{
always = tf;
}
virtual void handle_event(unsigned long curtime, uintptr udata) = 0;
};
class Time_queue;
/*
* A queue entry:
*/
struct Queue_entry {
// Queue_entry *next, *prev; // Next, prev. in queue.
Time_sensitive *handler; // Object to activate.
uintptr udata; // Data to pass to handler.
uint32 time; // Time when this is due.
inline void set(uint32 t, Time_sensitive *h, uintptr ud) {
time = t;
handler = h;
udata = ud;
}
};
bool operator <(const Queue_entry &q1, const Queue_entry &q2);
/*
* Time-based queue. The entries are kept sorted in increasing order
* by time.
*/
class Time_queue {
typedef std::list<Queue_entry> Temporal_sequence;
Temporal_sequence data;
uint32 pause_time; // Time when paused.
int paused; // Count of calls to 'pause()'.
// Activate head + any others due.
void activate0(uint32 curtime);
void activate_always(uint32 curtime);
public:
friend class Time_queue_iterator;
// Time_queue() : head(0), free_entries(0)
Time_queue() : pause_time(0), paused(0)
{ }
void clear(); // Remove all entries.
// Add an entry.
void add(uint32 t, Time_sensitive *obj) {
add(t, obj, static_cast<uintptr>(0));
}
void add(uint32 t, Time_sensitive *obj, void *ud) {
add(t, obj, reinterpret_cast<uintptr>(ud));
}
void add(uint32 t, Time_sensitive *obj, uintptr ud);
// Remove object's entry.
int remove(Time_sensitive *obj);
void remove(Time_sensitive *obj, void *ud) {
remove(obj, reinterpret_cast<uintptr>(ud));
}
int remove(Time_sensitive *obj, uintptr udata);
int find(Time_sensitive const *obj) const; // Find an entry.
// Find delay when obj. is due.
long find_delay(Time_sensitive const *obj, uint32 curtime) const;
// Activate entries that are 'due'.
inline void activate(uint32 curtime) {
// if (head && !(curtime < head->time))
if (paused)
activate_always(curtime);
else if (data.size() && !(curtime < data.front().time))
activate0(curtime);
}
void pause(uint32 curtime) { // Game paused.
if (!paused++)
pause_time = curtime;
}
void resume(uint32 curtime);
};
class Time_queue_iterator {
Time_queue::Temporal_sequence::iterator iter;
Time_queue *tqueue;
Time_sensitive *this_obj; // Only return entries for this obj.
public:
Time_queue_iterator(Time_queue *tq, Time_sensitive *obj)
: iter(tq->data.begin()), tqueue(tq), this_obj(obj)
{ }
int operator()(Time_sensitive *&obj, uintptr &data);
};
#endif /* TQUEUE_H */
|