File: tqueue.h

package info (click to toggle)
exult 0.98rc1-2
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 6,924 kB
  • ctags: 8,928
  • sloc: cpp: 83,768; sh: 7,643; ansic: 4,328; makefile: 890; yacc: 618; lex: 255; xml: 19
file content (124 lines) | stat: -rw-r--r-- 3,221 bytes parent folder | download
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
/*
 *	tqueue.h - A queue of time-based events for animation.
 *
 *  Copyright (C) 2000-2001  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 Library 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 "exult_types.h"



/*
 *	An interface for entries in the queue:
 */
class Time_sensitive
	{
	int queue_cnt;			// # of entries for this in queue.
public:
	friend class Time_queue;
	Time_sensitive() : queue_cnt(0)
		{  }
	virtual ~Time_sensitive();
	int in_queue()
		{ return queue_cnt > 0; }
	virtual void handle_event(unsigned long curtime, long udata) = 0;
	};

class Time_queue;

/*
 *	A queue entry:
 */
class Queue_entry
	{
	public:
	// Queue_entry *next, *prev;	// Next, prev. in queue.
	Time_sensitive *handler;	// Object to activate.
	long udata;			// Data to pass to handler.
	uint32 time;			// Time when this is due.
	inline void set(uint32 t, Time_sensitive *h, long 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.

	// Activate head + any others due.
	void activate0(uint32 curtime);
public:
	friend class Time_queue_iterator;
	// Time_queue() : head(0), free_entries(0)
	Time_queue() : pause_time(0)
		{  }
	void clear();			// Remove all entries.
					// Add an entry.
	void add(uint32 t, Time_sensitive *obj, long ud);
					// Remove object's entry.
	int remove(Time_sensitive *obj);
	int remove(Time_sensitive *obj, long udata);
	int find(Time_sensitive *obj);	// Find an entry.
					// Find delay when obj. is due.
	long find_delay(Time_sensitive *obj, uint32 curtime);
					// Activate entries that are 'due'.
	inline void activate(uint32 curtime)
		{
		// if (head && !(curtime < head->time))
		if (data.size() && !(curtime < data.front().time))
			activate0(curtime);
		}
	void pause(uint32 curtime)	// Game paused.
		{
		if (!pause_time)
			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, long& data);
	};

#endif	/* TQUEUE_H */