Adonthell  0.4
time_event_handler.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002/2003 Kai Sterker <kai.sterker@gmail.com>
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 /**
20  * @file time_event_handler.cc
21  *
22  * @author Kai Sterker
23  * @brief Implements the time_event_handler class.
24  */
25 
26 #include <algorithm>
27 #include "gamedate.h"
28 #include "time_event.h"
29 #include "time_event_handler.h"
30 
31 
32 // See whether a matching event is registered and execute the
33 // according script(s)
35 {
36  s_int32 repeat;
37  event *evt;
38 
39  // As long as matching events are in the list
40  while (!Events.empty () && Events.front ()->equals (e))
41  {
42  evt = Events.front ();
43 
44  // we remove the event in any case, as it needs to be
45  // re-registered at a new position if it repeats
46  Events.erase (Events.begin ());
47  evt->set_registered (false);
48 
49  // events that don't repeat are destroyed automatically
50  repeat = evt->execute (e);
51 
52  // re-register event if it needs be repeated
53  if (repeat) register_event (evt);
54  else delete evt;
55  }
56 
57  return;
58 }
59 
60 // Unregister an event
62 {
63  vector<event*>::iterator i;
64 
65  // Search for the event we want to remove
66  i = find (Events.begin (), Events.end (), e);
67 
68  // found? -> get rid of it :)
69  if (i != Events.end ()) Events.erase (i);
70 }
71 
72 // register an event with the handler
74 {
75  vector<event*>::iterator i = Events.begin ();
76 
77  // search for the proper place to insert new event
78  while (i != Events.end ())
79  {
80  // skip events that are raised earlier than e
81  if (((time_event *) e)->time () <= ((time_event *) (*i))->time ()) break;
82  i++;
83  }
84 
85  Events.insert (i, e);
86 }
#define s_int32
32 bits long signed integer
Definition: types.h:50
void remove_event(event *evnt)
Removes the given event from the event handler.
Base class for events.
Definition: event.h:75
The time event executes the attached script or callback at a certain point in game-time.
Definition: time_event.h:38
void raise_event(const event *evnt)
Raise one or more events in case the given time matches their "alarm" time.
Declares the gamedate class.
Declares the time_event_handler class.
void register_event(event *evnt)
Register a time event with the event handler.
Declares the time_event class.