2 // SGEventMgr.cxx -- Event Manager
4 // Written by Bernie Bright, started April 2002.
6 // Copyright (C) 2002 Curtis L. Olson - curt@me.umn.edu
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 # include <simgear_config.h>
28 #include <simgear/compiler.h>
30 #include <simgear/debug/logstream.hxx>
32 #include "event_mgr.hxx"
48 SGEvent::SGEvent( const char* name,
50 interval_type repeat_value,
51 interval_type initial_value )
55 _repeat_value(repeat_value),
56 _initial_value(initial_value),
62 if (_initial_value < 0)
65 _ms_to_go = _repeat_value;
69 _ms_to_go = _initial_value;
73 SGEvent::SGEvent( const char* name,
74 const SGSubsystem* subsystem,
75 interval_type repeat_value,
76 interval_type initial_value )
79 _subsystem((SGSubsystem*)&subsystem),
80 _repeat_value(repeat_value),
81 _initial_value(initial_value),
87 if (_initial_value < 0)
90 _ms_to_go = _repeat_value;
94 _ms_to_go = _initial_value;
107 SGTimeStamp start_time;
108 SGTimeStamp finish_time;
116 } else if (_subsystem)
118 _subsystem->update(_repeat_value);
125 unsigned long duration = finish_time - start_time;
127 _cum_time += duration;
129 if ( duration < _min_time ) {
130 _min_time = duration;
133 if ( duration > _max_time ) {
134 _max_time = duration;
139 SGEvent::print_stats() const
141 SG_LOG( SG_EVENT, SG_INFO,
143 << " int=" << _repeat_value / 1000.0
144 << " cum=" << _cum_time
145 << " min=" << _min_time
146 << " max=" << _max_time
147 << " count=" << _count
148 << " ave=" << _cum_time / (double)_count );
153 SGEventMgr::SGEventMgr()
157 SGEventMgr::~SGEventMgr()
164 SG_LOG( SG_EVENT, SG_INFO, "Initializing event manager" );
186 SGEventMgr::update( double dt )
188 int dt_ms = int(dt * 1000);
192 SG_LOG( SG_GENERAL, SG_ALERT,
193 "SGEventMgr::update() called with negative delta T" );
198 event_container_type::iterator first = event_table.begin();
199 event_container_type::iterator last = event_table.end();
200 event_container_type::iterator event = event_table.end();
202 // Scan all events. Run one whose interval has expired.
203 while (first != last)
205 if (first->update( dt_ms ))
207 if (first->value() < min_value)
209 // Select event with largest negative value.
210 // Its been waiting longest.
211 min_value = first->value();
222 if (event->repeat_value() > 0)
228 SG_LOG( SG_GENERAL, SG_DEBUG, "Deleting event " << event->name() );
229 event_table.erase( event );
235 SGEventMgr::add( const SGEvent& event )
237 event_table.push_back( event );
239 SG_LOG( SG_EVENT, SG_INFO, "registered event " << event.name()
240 << " to run every " << event.repeat_value() << "ms" );
244 SGEventMgr::print_stats() const
246 SG_LOG( SG_EVENT, SG_INFO, "" );
247 SG_LOG( SG_EVENT, SG_INFO, "Event Stats" );
248 SG_LOG( SG_EVENT, SG_INFO, "-----------" );
250 event_container_type::const_iterator first = event_table.begin();
251 event_container_type::const_iterator last = event_table.end();
252 for (; first != last; ++first)
254 first->print_stats();
257 SG_LOG( SG_EVENT, SG_INFO, "" );