root/simulator/trunk/src/simulator/system.cpp

Revision 1468, 14.8 kB (checked in by sehenley, 6 months ago)

Update to Ogre 1.7.0 - removed CEGUI (no mingw support).

  • Property WBS set to 1.1.4.10
  • Property svn:keywords set to
    Url
    Rev
    Author
    Date
    Id
Line 
1 //    OSRail -- a network enabled railroad operations simulator and utilities
2 //    Copyright (C) 2007,2008 Samuel E. Henley sehenley@comcast.net
3 //
4 //    This program is free software; you can redistribute it and/or modify
5 //    it under the terms of the GNU General Public License as published by
6 //    the Free Software Foundation; either version 2 of the License, or
7 //    (at your option) any later version.
8 //
9 //    This program is distributed in the hope that it will be useful,
10 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //    GNU General Public License for more details.
13 //
14 //    You should have received a copy of the GNU General Public License along
15 //    with this program; if not, write to the Free Software Foundation, Inc.,
16 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 //
18 /// \file
19 /// Implementation file for System manager of the simulator and the ISystem factory.
20
21 // Pre-Compiled Header for Simulator
22 #include "presimulator.h"
23
24 // Always include required headers before this: reduce coupling
25 #include "system.h"
26
27
28 /// \addtogroup simulator "Simulator"
29 /// @{
30     /// \addtogroup system "System"
31     /// @{
32
33 extern IAi*              createAi( ISystem& system );
34 extern IConsoleClient*   createConsoleClient( ISystem& system );
35 extern IFactory*         createFactory( ISystem& system );
36 extern IGraphics*        createGraphics( ISystem& system );
37 extern IGui*             createGui( ISystem& system );
38 extern IInput*           createInput( ISystem& system );
39 extern INetwork*         createNetwork( ISystem& system );
40 extern IPropertyManager* createPropertyManager( ISystem& system );
41 extern ISimulation*      createSimulation( ISystem& system );
42 extern ISound*           createSound( ISystem& system );
43 extern ISystem*          createSystem();
44 extern ITask*            createTask( ISystem& system );
45 extern ITest*            createTest( ISystem& system );
46
47 /// The ISystem factory
48 /// \return a System instance.
49 extern ISystem* createSystem()
50 {
51     return new System;
52 }
53
54 System::System()
55 {
56 }
57
58 System::~System()
59 {
60 }
61
62 void System::setOptions( const boost::program_options::variables_map& map )
63 {
64
65 }
66
67
68 //virtual
69 void System::startup()
70 {
71     MESSAGE( "Simulator", "System", "Startup" )
72
73     /// \note Initialize simulation program.
74     /// Initialize
75     /// - console
76     /// - tweaker
77     /// - test
78     /// - factory
79     /// - property manager
80     /// - ogre (graphics)
81     /// - openal (sound)
82     /// - ois (input)
83     /// - gui
84     /// - raknet (network)
85     /// - ai
86     /// - simulation
87     /// - angelcode (simulation scripts)
88     /// - splashscreen - after scripts, ogre and openal
89     /// - go to running state
90
91     states.push_back( boost::shared_ptr<SystemState>( new System::StartupState ) );
92     ASSERT( !states.empty(), "Simulator", "System", "Should be in StartupState state" )
93     states.back()->startup(*this); //state
94
95     /// start console
96     if( !( console = boost::shared_ptr<IConsoleClient>( createConsoleClient( *this ) ) )  )
97         throw std::logic_error( _("Could not create console client"));
98     ASSERT( console.unique(), "Simulator", "System", "Should be only one ConsoleClient" )
99 //    assert( console.unique() );
100     console->startup();
101
102     /// start realtime test
103     if( !( test = boost::shared_ptr<ITest>( createTest( *this ) ) )  )
104         throw std::logic_error( _("Could not create test software"));
105     ASSERT( test.unique(), "Simulator", "System", "Should be only one Test manager" )
106     test->startup();
107
108     /// start factory
109     if( !( factory = boost::shared_ptr<IFactory>( createFactory( *this ) ) )  )
110         throw std::logic_error( _("Could not create factory software"));
111     ASSERT( factory.unique(), "Simulator", "System", "Should be only one Factory" )
112     factory->startup();
113
114     /// start propertymanager
115     if( !( propertymanager = boost::shared_ptr<IPropertyManager>( createPropertyManager( *this ) ) )  )
116         throw std::logic_error( _("Could not create property manager software"));
117     ASSERT( propertymanager.unique(), "Simulator", "System", "Should be only one Property manager" )
118     propertymanager->startup();
119
120     // ogre initialization
121     /// \todo build simulator exception class
122     if( !( graphics = boost::shared_ptr<IGraphics>( createGraphics( *this ) ) )  )
123         throw std::logic_error( _("Could not create 3d graphics device"));
124     ASSERT( graphics.unique(), "Simulator", "System", "Should be only one Graphics manager" )
125     graphics->startup();
126
127     // openal initialization
128     if( !( sound = boost::shared_ptr<ISound>( createSound( *this ) ) )  )
129         throw std::logic_error( _("Could not create sound device"));
130     ASSERT( sound.unique(), "Simulator", "System", "Should be only one Sound manager" )
131     sound->startup();
132
133     // ois initialization
134     if( !( input = boost::shared_ptr<IInput>( createInput( *this ) ) )  )
135         throw std::logic_error( _("Could not create input device"));
136     ASSERT( input.unique(), "Simulator", "System", "Should be only one Input manager" )
137     input->startup();
138
139     // gui initialization
140     if( !( gui = boost::shared_ptr<IGui>( createGui( *this ) ) )  )
141         throw std::logic_error( _("Could not create 2d graphics software"));
142     ASSERT( gui.unique(), "Simulator", "System", "Should be only one GUI manager" )
143     gui->startup();
144
145     // raknet initialization
146     if( !( network = boost::shared_ptr<INetwork>( createNetwork( *this ) ) )  )
147         throw std::logic_error( _("Could not create network device"));
148     ASSERT( network.unique(), "Simulator", "System", "Should be only one Network manager" )
149     network->startup();
150
151     // start ai
152     if( !( ai = boost::shared_ptr<IAi>( createAi( *this ) ) )  )
153         throw std::logic_error( _("Could not create ai client"));
154     ASSERT( ai.unique(), "Simulator", "System", "Should be only one AI manager" )
155     ai->startup();
156
157
158     // start simulation
159     if( !( simulation = boost::shared_ptr<ISimulation>( createSimulation( *this ) ) )  )
160         throw std::logic_error( _("Could not create simulation client"));
161     ASSERT( simulation.unique(), "Simulator", "System", "Should be only one Simulation manager" )
162     simulation->startup();
163
164
165     // start scripts machinery, must be last requires interface pointers
166     if( !( task = boost::shared_ptr<ITask>( createTask( *this ) ) )  )
167         throw std::logic_error( _("Could not create taskl/scripts software"));
168     ASSERT( task.unique(), "Simulator", "System", "Should be only one Task manager" )
169     task->startup();
170
171 }
172
173
174 //virtual
175 void System::shutdown()
176 {
177     MESSAGE( "Simulator", "System", "Shutdown" )
178
179     ASSERT( simulation.unique(), "Simulator", "System", "Should be only one Simulation manager" )
180     simulation->shutdown();
181
182     ASSERT( ai.unique(), "Simulator", "System", "Should be only one AI manager" )
183     ai->shutdown();
184
185     ASSERT( network.unique(), "Simulator", "System", "Should be only one Network manager" )
186     network->shutdown( );
187
188     ASSERT( gui.unique(), "Simulator", "System", "Should be only one GUI manager" )
189     gui->shutdown();
190
191     ASSERT( input.unique(), "Simulator", "System", "Should be only one Input manager" )
192     input->shutdown();
193
194     ASSERT( sound.unique(), "Simulator", "System", "Should be only one Sound manager" )
195     sound->shutdown();
196
197     ASSERT( task.unique(), "Simulator", "System", "Should be only one Task manager" )
198     task->shutdown();
199
200     ASSERT( graphics.unique(), "Simulator", "System", "Should be only one Graphics manager" )
201     graphics->shutdown();
202
203     ASSERT( propertymanager.unique(), "Simulator", "System", "Should be only one Property manager" )
204     propertymanager->shutdown( );
205
206     ASSERT( factory.unique(), "Simulator", "System", "Should be only one Factory" )
207     factory->shutdown();
208
209     ASSERT( test.unique(), "Simulator", "System", "Should be only one Test manager" )
210     test->shutdown();
211
212     ASSERT( console.unique(), "Simulator", "System", "Should be only one ConsoleClient" )
213     console->shutdown();
214 }
215
216 /// Run the current simulator state.
217 void System::operator()()
218 {
219     ASSERT( !states.empty(), "Simulator", "System", "Should be some state" )
220
221     std::vector< boost::shared_ptr<SystemState> >::iterator it = states.begin();
222
223     while( it != states.end() )(*it++)->background( *this );  //do every states background processing
224
225     /// \note System State Mechine - foreground state
226     /// Do only the top state foreground processing.
227     /// Foreground processing can modify states - so
228     /// therefore only one foreground state is allowed.
229     states.back()->operator()( *this );
230
231 }
232
233 bool System::running() const
234 {
235     IGraphics* p = dynamic_cast<IGraphics*>( graphics.get() );
236     return !p->isClosed() && !states.empty();
237 }
238
239 bool System::anyState() const
240 {
241     return !states.empty();
242 }
243
244     /// @}   group system
245 /// @}   group simulator
246
247 // ////////////////////////// System State Machine /////////////////////////////
248 /// \addtogroup simulator "Simulator"
249 /// @{
250     /// \addtogroup system "System"
251     /// @{
252         /// \addtogroup systemstate "System State Machine"
253         /// @{
254
255 /// Simulation progran initization and splish screen.
256 /// Initialize the graphics, show the splash screen and
257 /// move to the GUI state.
258 /// \param system the containing class
259 template<> void System::StartupState::operator()( System& system ) //forground
260 {
261     /// \todo splish screen
262     system.states.back()->shutdown( system );
263 }
264
265 /// Move to the GUI state.
266 /// \param system the containing class
267 template<> void System::StartupState::shutdown( System& system )
268 {
269     MESSAGE( "Simulator", "System::State", "StartupState::shutdown" )
270     system.states.pop_back();
271     system.states.push_back( boost::shared_ptr<SystemState>( new System::GuiState ) );
272     system.states.back()->startup(system);
273 }
274
275 /// No operation
276 /// \param system the containing class
277 template<> void System::StartupState::startup( System& system )
278 {
279     MESSAGE( "Simulator", "System::State", "StartupState::startup" )
280 }
281
282 /// No operation
283 /// \param system the containing class
284 template<> void System::StartupState::background( System& system )
285 {
286 }
287
288 /// Simulation program menu system.
289 /// Bring up the option and route seletion menus.
290 /// Stay in this state until a route or quit is selected.
291 /// If a route is selected request the initialization state.
292 /// \param system the containing class
293 template<> void System::GuiState::operator()( System& system ) //forground
294 {
295
296     /// \todo forground GUI menu processing
297
298
299     system.states.back()->shutdown( system );
300 }
301
302 /// Move to RouteStartupState
303 /// \param system the containing class
304 template<> void System::GuiState::shutdown( System& system )
305 {
306     MESSAGE( "Simulator", "System::State", "GuiState::shutdown" )
307     if( !system.running() )
308     {
309         system.states.pop_back();
310         system.states.push_back( boost::shared_ptr<SystemState>( new System::ShutdownState ) );
311         system.states.back()->startup( system );
312     }
313     else
314     {
315         system.states.pop_back();
316         system.states.push_back( boost::shared_ptr<SystemState>( new System::RouteStartupState ) );
317         system.states.back()->startup( system );
318     }
319 }
320
321 /// Initialize GUI state
322 /// \param system the containing class
323 template<> void System::GuiState::startup( System& system )
324 {
325     MESSAGE( "Simulator", "System::State", "GuiState::startup" )
326     /// \todo Initialize GUI State
327 }
328
329 /// GUI State background processing
330 /// \param system the containing class
331 template<> void System::GuiState::background( System& system )
332 {
333     /// \todo Background processing for visable menus and control keys
334 }
335
336 /// Load and startup a route.
337 /// Loads a selected route, started from GUIState.
338 /// After load is completeed requests Simulation state.
339 template<> void System::RouteStartupState::operator()( System& system )
340 {
341     system.states.back()->shutdown( system );
342 }
343
344 template<> void System::RouteStartupState::startup( System& system )
345 {
346     MESSAGE( "Simulator", "System::State", "RouteStartupState::startup" )
347 }
348
349 template<> void System::RouteStartupState::shutdown( System& system )
350 {
351     MESSAGE( "Simulator", "System::State", "RouteStartupState::shutdown" )
352     system.states.pop_back();
353     system.states.push_back( boost::shared_ptr<SystemState>( new System::SimulationState ) );
354     system.states.back()->startup( system );
355 }
356
357 template<> void System::RouteStartupState::background( System& system )
358 {
359 }
360
361 /// Unload and shutdown a route.
362 /// UnLoads a selected route, and moves to the GUIState.
363 /// After unload is completed requests GUIState state.
364 template<> void System::RouteShutdownState::operator()( System& system )
365 {
366     system.states.back()->shutdown( system );
367 }
368
369 template<> void System::RouteShutdownState::startup( System& system )
370 {
371     MESSAGE( "Simulator", "System::State", "RouteShutdownState::startup" )
372 }
373
374 template<> void System::RouteShutdownState::shutdown( System& system )
375 {
376     MESSAGE( "Simulator", "System::State", "RouteShutdownState::shutdown" )
377     system.states.pop_back();
378     system.states.push_back( boost::shared_ptr<SystemState>( new System::GuiState ) );
379     system.states.back()->startup( system );
380 }
381
382 template<> void System::RouteShutdownState::background( System& system )
383 {
384 }
385
386 /// Run the simulation.
387 /// Runs the simulation and returns to the GUI state.
388 template<> void System::SimulationState::operator()( System& system )
389 {
390     //Input
391     ASSERT( system.input != 0, "Simulator", "SimulationState", "Input should be initialized" )
392     system.input->operator()(); //Will throw errors
393
394     //Graphics
395     ASSERT( system.graphics != 0, "Simulator", "SimulationState", "Graphics should be initialized" )
396     system.graphics->operator()(); //Will throw errors
397
398     if( !system.running() )system.states.back()->shutdown( system );
399 }
400
401 template<> void System::SimulationState::startup( System& system )
402 {
403     MESSAGE( "Simulator", "System::State", "SimulationState::startup" )
404 }
405
406 template<> void System::SimulationState::shutdown( System& system )
407 {
408     MESSAGE( "Simulator", "System::State", "SimulationState::shutdown" )
409     system.states.pop_back();
410     system.states.push_back( boost::shared_ptr<SystemState>( new System::RouteShutdownState ) );
411     system.states.back()->startup( system );
412 }
413
414 template<> void System::SimulationState::background( System& system )
415 {
416 }
417
418 /// Simulation progran shutdown.
419 template<> void System::ShutdownState::operator()( System& system )
420 {
421     system.states.back()->shutdown( system );
422 }
423
424 template<> void System::ShutdownState::startup( System& system )
425 {
426     MESSAGE( "Simulator", "System::State", "ShutdownState::startup" )
427 }
428
429 template<> void System::ShutdownState::shutdown( System& system )
430 {
431     MESSAGE( "Simulator", "System::State", "ShutdownState::shutdown" )
432     system.states.pop_back();
433     ASSERT( system.states.empty(), "Simulator", "System::State", "Should be no system states remainig at ShutdownState" )
434     system.shutdown();
435 }
436
437 template<> void System::ShutdownState::background( System& system )
438 {
439 }
440
441         /// @}   group systemstate
442     /// @}   group system
443 /// @}   group simulator
Note: See TracBrowser for help on using the browser.