Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics_merge/src/core/RootGameState.cc @ 2442

Last change on this file since 2442 was 2442, checked in by rgrieder, 15 years ago

Finally merged physics stuff. Target is physics_merge because I'll have to do some testing first.

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "RootGameState.h"
30
31#include "util/Debug.h"
32#include "util/Exception.h"
33#include "Core.h"
34#include "Clock.h"
35#include "CommandLine.h"
36
37namespace orxonox
38{
39    SetCommandLineArgument(state, "gui").shortcut("s");
40
41    RootGameState::RootGameState(const std::string& name)
42        : GameState<GameStateBase>(name)
43        , stateRequest_("")
44    {
45    }
46
47    RootGameState::~RootGameState()
48    {
49    }
50
51    /**
52    @brief
53        Internal method that actually makes the state transition. Since it is internal,
54        the method can assume certain things to be granted (like 'this' is always active).
55    */
56    void RootGameState::makeTransition(GameStateBase* source, GameStateBase* destination)
57    {
58        if (source != 0)
59        {
60            // transition was not initiated by root itself
61            this->activeChild_ = 0;
62        }
63
64        if (destination == this)
65        {
66            // this marks the end of the game.
67            return;
68        }
69
70        // Check for 'destination' in the children map first
71        std::map<GameStateBase*, GameStateBase*>::const_iterator it
72            = this->grandchildrenToChildren_.find(destination);
73        if (it != this->grandchildrenToChildren_.end())
74        {
75            OrxAssert(dynamic_cast<GameStateBase*>(it->second) != 0,
76                "There was a mix with RootGameState and GameState, could not cast.");
77            GameStateBase* child = static_cast<GameStateBase*>(it->second);
78            // child state. Don't use 'state', might be a grandchild!
79            this->activeChild_ = child;
80            child->makeTransition(this, destination);
81        }
82        else
83        {
84            // root doesn't have a parent..
85            OrxAssert(false, "GameState '" + destination->getName() + "' not found in children list of Root.");
86        }
87    }
88
89    void RootGameState::gotoState(const std::string& name)
90    {
91        GameStateBase* request = getState(name);
92        if (request)
93        {
94            GameStateBase* current = getCurrentState();
95            if (current)
96            {
97                current->makeTransition(0, request);
98            }
99            else
100            {
101                // Root is not yet active. This is a violation.
102                ThrowException(GameState, "Activate Root before requesting a state.");
103            }
104        }
105        else
106        {
107            COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl;
108        }
109    }
110
111    /**
112    @brief
113        Makes a state transition according to the state tree. You can choose any state
114        in the tree to do the call. The function finds the current state on its own.
115    @param state
116        The state to be entered, has to exist in the tree.
117    */
118    void RootGameState::requestState(const std::string& name)
119    {
120        this->stateRequest_ = name;
121    }
122
123    /**
124    @brief
125        Starts the game. The little 'while' denotes the main loop.
126        Whenever the root state is selected, the game ends.
127    @param name
128        State to start with (usually main menu or specified by command line)
129    */
130    void RootGameState::start()
131    {
132        // Don't catch errors when having a debugger in msvc
133#if ORXONOX_COMPILER != ORXONOX_COMPILER_MSVC || defined(NDEBUG)
134        try
135        {
136#endif
137            // start global orxonox time
138            Clock clock;
139
140            // create the Core settings to configure the output level
141            Core::getInstance();
142
143            this->activate();
144
145            // get initial state from command line
146            gotoState(CommandLine::getValue("state"));
147
148            while (this->activeChild_)
149            {
150                clock.capture();
151
152                this->tick(clock);
153
154                if (this->stateRequest_ != "")
155                    gotoState(stateRequest_);
156            }
157
158            this->deactivate();
159#if ORXONOX_COMPILER != ORXONOX_COMPILER_MSVC || defined(NDEBUG)
160        }
161        // Note: These are all unhandled exceptions that should not have made its way here!
162        // almost complete game catch block to display the messages appropriately.
163        catch (std::exception& ex)
164        {
165            COUT(0) << ex.what() << std::endl;
166            COUT(0) << "Program aborted." << std::endl;
167            abort();
168        }
169        // anything that doesn't inherit from std::exception
170        catch (...)
171        {
172            COUT(0) << "An unidentifiable exception has occured. Program aborted." << std::endl;
173            abort();
174        }
175#endif
176    }
177}
Note: See TracBrowser for help on using the repository browser.