tqueue.h

Go to the documentation of this file.
00001 /*
00002  *  tqueue.h - A queue of time-based events for animation.
00003  *
00004  *  Copyright (C) 2000-2001  The Exult Team
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #ifndef TQUEUE_H
00022 #define TQUEUE_H  1
00023 
00024 
00025 #include <vector>
00026 #include <list>
00027 
00028 #include "exult_types.h"
00029 
00030 
00031 
00032 /*
00033  *  An interface for entries in the queue:
00034  */
00035 class Time_sensitive
00036   {
00037   int queue_cnt;      // # of entries for this in queue.
00038   bool always;      // Always do this, even if paused.
00039 public:
00040   friend class Time_queue;
00041   Time_sensitive() : queue_cnt(0), always(false)
00042     {  }
00043   virtual ~Time_sensitive();
00044   int in_queue()
00045     { return queue_cnt > 0; }
00046   void set_always(bool tf)  // Should be called before placing in
00047           //   queue.
00048     { always = tf; }
00049   virtual void handle_event(unsigned long curtime, long udata) = 0;
00050   };
00051 
00052 class Time_queue;
00053 
00054 /*
00055  *  A queue entry:
00056  */
00057 class Queue_entry
00058   {
00059   public:
00060   // Queue_entry *next, *prev;  // Next, prev. in queue.
00061   Time_sensitive *handler;  // Object to activate.
00062   long udata;     // Data to pass to handler.
00063   uint32 time;      // Time when this is due.
00064   inline void set(uint32 t, Time_sensitive *h, long ud)
00065     {
00066     time = t;
00067     handler = h;
00068     udata = ud;
00069     }
00070   };
00071 
00072 bool  operator <(const Queue_entry &q1,const Queue_entry &q2);
00073 
00074 /*
00075  *  Time-based queue.  The entries are kept sorted in increasing order
00076  *  by time.
00077  */
00078 class Time_queue
00079   {
00080   typedef std::list<Queue_entry>  Temporal_sequence;
00081   Temporal_sequence data;
00082   uint32 pause_time;    // Time when paused.
00083   int paused;     // Count of calls to 'pause()'.
00084 
00085   // Activate head + any others due.
00086   void activate0(uint32 curtime);
00087   void activate_always(uint32 curtime);
00088 public:
00089   friend class Time_queue_iterator;
00090   // Time_queue() : head(0), free_entries(0)
00091   Time_queue() : pause_time(0), paused(0)
00092     {  }
00093   void clear();     // Remove all entries.
00094           // Add an entry.
00095   void add(uint32 t, Time_sensitive *obj, long ud);
00096           // Remove object's entry.
00097   int remove(Time_sensitive *obj);
00098   int remove(Time_sensitive *obj, long udata);
00099   int find(Time_sensitive *obj);  // Find an entry.
00100           // Find delay when obj. is due.
00101   long find_delay(Time_sensitive *obj, uint32 curtime);
00102           // Activate entries that are 'due'.
00103   inline void activate(uint32 curtime)
00104     {
00105     // if (head && !(curtime < head->time))
00106     if (paused)
00107       activate_always(curtime);
00108     else if (data.size() && !(curtime < data.front().time))
00109       activate0(curtime);
00110     }
00111   void pause(uint32 curtime)  // Game paused.
00112     {
00113     if (!paused++)
00114       pause_time = curtime;
00115     }
00116   void resume(uint32 curtime);
00117   };
00118 
00119 
00120 class Time_queue_iterator
00121   {
00122   Time_queue::Temporal_sequence::iterator iter;
00123   Time_queue *tqueue;
00124   Time_sensitive *this_obj; // Only return entries for this obj.
00125 public:
00126   Time_queue_iterator(Time_queue *tq, Time_sensitive *obj)
00127       : iter(tq->data.begin()), tqueue(tq), this_obj(obj)
00128     {  }
00129   int operator()(Time_sensitive *& obj, long& data);
00130   };
00131 
00132 #endif  /* TQUEUE_H */

Generated on Mon Jul 9 14:42:51 2007 for ExultEngine by  doxygen 1.5.1