Adonthell  0.4
event_list.cc
Go to the documentation of this file.
1 /*
2  $Id: event_list.cc,v 1.8 2003/02/10 20:01:13 ksterker Exp $
3 
4  Copyright (C) 2000/2001/2002/2003 Kai Sterker <kaisterker@linuxgames.com>
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 
16 /**
17  * @file event_list.cc
18  * @author Kai Sterker <kaisterker@linuxgames.com>
19  *
20  * @brief Implements the event_list class.
21  *
22  */
23 
24 #include <algorithm>
25 #include "event_list.h"
26 #include "event_handler.h"
27 
28 // Array with callbacks to return a newly instanciated event
29 new_event event_list::instanciate_event[MAX_EVENTS];
30 
31 // constructor
33 {
34  Paused = false;
35 }
36 
37 // destructor
39 {
40  clear ();
41 }
42 
43 // Unregisters and deletes all events.
45 {
46  event *ev;
47 
48  while (!Events.empty ())
49  {
50  ev = Events.back ();
51  ev->set_list (NULL);
52  Events.pop_back ();
53  delete ev;
54  }
55 }
56 
57 // Adds an event to the list and register it with the event_handler.
59 {
60  ev->set_list (this);
61  Events.push_back (ev);
62 
63  // if the event list is paused, also pause new events
64  if (Paused) ev->pause ();
65 
66  // only register event if not paused
67  else if (!ev->is_paused ()) event_handler::register_event (ev);
68 }
69 
70 // Remove an event from the list
72 {
73  vector<event*>::iterator i;
74 
75  // Search for the event we want to remove
76  i = find (Events.begin (), Events.end (), ev);
77 
78  // found? -> get rid of it :)
79  if (i != Events.end ()) Events.erase (i);
80 }
81 
82 // retrieve event by its id
83 event *event_list::get_event (const string & id)
84 {
85  vector<event*>::iterator i;
86 
87  for (i = Events.begin (); i != Events.end (); i++)
88  if ((*i)->id () == id) return *i;
89 
90  return NULL;
91 }
92 
93 // disable all events in the list
95 {
96  Paused = true;
97  for (vector<event*>::iterator i = Events.begin (); i != Events.end (); i++)
98  (*i)->pause ();
99 }
100 
101 // enable all events in the list
103 {
104  Paused = false;
105  for (vector<event*>::iterator i = Events.begin (); i != Events.end (); i++)
106  (*i)->resume ();
107 }
108 
109 // Register an event for loading
111 {
112  if (type < MAX_EVENTS)
113  instanciate_event[type] = e;
114 }
115 
116 // Save an event_list to file
118 {
119  std::vector <event *>::iterator i;
120  u_int32 nbr_events = Events.size ();
121 
122  nbr_events >> out;
123 
124  for (i = Events.begin (); i != Events.end (); i++)
125  (*i)->put_state (out);
126 }
127 
128 // Loads an event_list from file
130 {
131  u_int32 nbr_events;
132  u_int8 type;
133  event *ev;
134 
135  nbr_events << in;
136 
137  while (nbr_events--)
138  {
139  ev = NULL;
140  type << in;
141 
142  // Instanciate an event of the given type
143  if (type < MAX_EVENTS && instanciate_event[type] != NULL)
144  ev = instanciate_event[type]();
145 
146  // try to load it, ...
147  if (ev != NULL && ev->get_state (in))
148  add_event (ev);
149 
150  // ... otherwise fail.
151  else
152  {
153  fprintf (stderr, "Could not load event #%i. Aborting ...\n", type);
154  return false;
155  }
156  }
157 
158  return true;
159 }
Class to write data from a Gzip compressed file.
Definition: fileops.h:223
Declares the event_handler class.
void clear()
Unregisters and deletes all events owned by this list.
Definition: event_list.cc:44
Class to read data from a Gzip compressed file.
Definition: fileops.h:131
virtual void pause()
Disable the event temporarily.
Definition: event.cc:223
Base class for events.
Definition: event.h:71
event *(* new_event)()
Pointer to a function returning a newly allocated event.
Definition: event_list.h:37
#define u_int32
32 bits long unsigned integer
Definition: types.h:35
#define u_int8
8 bits long unsigned integer
Definition: types.h:29
bool get_state(igzstream &in)
Loads the event_list from a file and registers all loaded events.
Definition: event_list.cc:129
void put_state(ogzstream &out) const
Save the event_list to a file.
Definition: event_list.cc:117
void add_event(event *ev)
Adds an event to this list.
Definition: event_list.cc:58
event_list()
Constructor - creates an empty, unpaused event_list.
Definition: event_list.cc:32
event * get_event(const string &id)
Try to retrieve the event with given id from the list.
Definition: event_list.cc:83
void resume()
Re-enable the events associated with the event_list, thus &#39;awaking&#39; the object to life again...
Definition: event_list.cc:102
std::vector< event * > Events
List of events.
Definition: event_list.h:171
virtual ~event_list()
Destructor - unregisters and deletes all events owned by this list.
Definition: event_list.cc:38
void set_list(event_list *list)
Tell the whether it is kept in an event_list.
Definition: event.cc:217
bool is_paused() const
Check whether the event is temporarily disabled or not.
Definition: event.h:256
static void register_event(event *ev)
Registers an event.
Definition: event_handler.h:75
static void register_event(u_int8 type, new_event e)
Register an event for loading.
Definition: event_list.cc:110
void remove_event(event *ev)
Removes an event from the list.
Definition: event_list.cc:71
Declares the event_list class.
void pause()
Disable any events associated with this event_list.
Definition: event_list.cc:94