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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/*
* tqueue.cc - 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 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.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "tqueue.h"
#include <algorithm>
#include <SDL_timer.h>
/*
* Be sure no copies are still in queue when deleted.
*/
Time_sensitive::~Time_sensitive
(
)
{
#if 0 /*+++++++For finding bugs. */
if (queue_cnt > 0)
{
char *p = 0;
char c = *p; // Force crash.
}
#endif
}
/*
* Remove all entries.
*/
void Time_queue::clear
(
)
{
Temporal_sequence::iterator it;
while ((it=data.begin()) != data.end())
{
Queue_entry ent = *it;
Time_sensitive *obj = ent.handler;
data.erase(it);
obj->queue_cnt--;
}
}
/*
* Add an entry to the queue.
*/
void Time_queue::add
(
uint32 t, // When entry is to be activated.
Time_sensitive *obj, // Object to be added.
long ud // User data.
)
{
obj->queue_cnt++; // It's going in, no matter what.
Queue_entry newent;
if (paused && !obj->always) // Paused?
// Messy, but we need to fix time.
t -= SDL_GetTicks() - pause_time;
newent.set(t,obj,ud);
if(!data.size())
{
data.push_back(newent);
return;
}
for(Temporal_sequence::iterator it=data.begin();
it!=data.end(); ++it)
{
if(newent<*it)
{
data.insert(it,newent);
return;
}
}
data.push_back(newent);
}
bool operator <(const Queue_entry &q1,const Queue_entry &q2)
{
if(q1.time<q2.time)
return true;
return false;
}
/*
* Remove first entry containing a given object.
*
* Output: 1 if found, else 0.
*/
int Time_queue::remove
(
Time_sensitive *obj
)
{
if(data.size()==0)
return 0;
for(Temporal_sequence::iterator it=data.begin();
it!=data.end(); ++it)
{
if(it->handler==obj)
{
obj->queue_cnt--;
data.erase(it);
return 1;
}
}
return (0); // Not found.
}
/*
* Remove first entry containing a given object and data.
*
* Output: 1 if found, else 0.
*/
int Time_queue::remove
(
Time_sensitive *obj,
long udata
)
{
if(data.size()==0)
return 0;
for(Temporal_sequence::iterator it=data.begin();
it!=data.end(); ++it)
{
if(it->handler==obj && it->udata == udata)
{
obj->queue_cnt--;
data.erase(it);
return 1;
}
}
return (0); // Not found.
}
/*
* See if a given entry is in the queue.
*
* Output: 1 if found, else 0.
*/
int Time_queue::find
(
Time_sensitive *obj
)
{
if(data.size()==0)
return 0;
for(Temporal_sequence::iterator it=data.begin();
it!=data.end(); ++it)
{
if(it->handler==obj)
return 1;
}
return 0;
}
/*
* Find when an entry is due.
*
* Output: delay in msecs. when due, or -1 if not in queue.
*/
long Time_queue::find_delay
(
Time_sensitive *obj,
uint32 curtime
)
{
if(data.size()==0)
return -1;
for(Temporal_sequence::iterator it=data.begin();
it!=data.end(); ++it)
{
if(it->handler==obj)
{
if (pause_time) // Watch for case when paused.
curtime = pause_time;
long delay = it->time - curtime;
return delay >= 0 ? delay : 0;
}
}
return -1;
}
/*
* Remove & activate entries that are due, starting with head (already
* known to be due).
*/
void Time_queue::activate0
(
uint32 curtime // Current time.
)
{
if(data.size()==0)
return;
Queue_entry ent;
do
{
ent=data.front();
Time_sensitive *obj = ent.handler;
long udata = ent.udata;
data.pop_front(); // Remove from chain.
obj->queue_cnt--;
obj->handle_event(curtime, udata);
}
while (data.size() && !(curtime < data.front().time));
}
/*
* Remove & activate entries marked 'always'. This is called when
* the queue is paused.
*/
void Time_queue::activate_always
(
uint32 curtime // Current time.
)
{
if(data.size()==0)
return;
Queue_entry ent;
for(Temporal_sequence::iterator it=data.begin();
it!=data.end() && !(curtime < (*it).time); )
{
Temporal_sequence::iterator next = it;
++next; // Get ->next in case we erase.
ent = *it;
Time_sensitive *obj = ent.handler;
if (obj->always)
{
obj->queue_cnt--;
long udata = ent.udata;
data.erase(it);
obj->handle_event(curtime, udata);
}
it = next;
}
}
/*
* Resume after a pause.
*/
void Time_queue::resume
(
uint32 curtime
)
{
if (!paused || --paused > 0) // Only unpause when stack empty.
return; // Not paused.
int diff = curtime - pause_time;
pause_time = 0;
if (diff < 0) // Should not happen.
return;
for(Temporal_sequence::iterator it=data.begin();
it!=data.end(); ++it)
{
if (!(*it).handler->always)
it->time += diff; // Push entries ahead.
}
}
/*
* Get next element in queue.
*/
int Time_queue_iterator::operator()
(
Time_sensitive *& obj, // Main object.
long& data // Data that was added with it.
)
{
while (iter != tqueue->data.end() && this_obj &&
(*iter).handler != this_obj)
++iter;
if (iter == tqueue->data.end())
return (0);
obj = (*iter).handler; // Return fields.
data = (*iter).udata;
++iter; // On to the next.
return (1);
}
|