Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/RootGameState.cc @ 2349

Last change on this file since 2349 was 2103, checked in by rgrieder, 16 years ago

Merged r2101 (objecthierarchy) to trunk.

  • Property svn:eol-style set to native
File size: 5.2 KB
RevLine 
[1672]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
[1755]31#include "util/Debug.h"
[1764]32#include "util/Exception.h"
[1755]33#include "Core.h"
[1674]34#include "Clock.h"
[1672]35#include "CommandLine.h"
36
37namespace orxonox
38{
[2087]39    SetCommandLineArgument(state, "gui").shortcut("s");
[1672]40
41    RootGameState::RootGameState(const std::string& name)
[1689]42        : GameState<GameStateBase>(name)
[1672]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    */
[1689]56    void RootGameState::makeTransition(GameStateBase* source, GameStateBase* destination)
[1672]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
[1689]71        std::map<GameStateBase*, GameStateBase*>::const_iterator it
[1672]72            = this->grandchildrenToChildren_.find(destination);
73        if (it != this->grandchildrenToChildren_.end())
74        {
[1689]75            OrxAssert(dynamic_cast<GameStateBase*>(it->second) != 0,
[1672]76                "There was a mix with RootGameState and GameState, could not cast.");
[1689]77            GameStateBase* child = static_cast<GameStateBase*>(it->second);
[1672]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    {
[1689]91        GameStateBase* request = getState(name);
[1672]92        if (request)
93        {
[1689]94            GameStateBase* current = getCurrentState();
[1672]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    */
[2103]130    void RootGameState::start()
[1672]131    {
[1824]132#ifdef NDEBUG
[1802]133        try
134        {
[1824]135#endif
[1802]136            // start global orxonox time
137            Clock clock;
[1695]138
[1802]139            // create the Core settings to configure the output level
140            Core::getInstance();
[1756]141
[1802]142            this->activate();
[1672]143
[1802]144            // get initial state from command line
[2087]145            gotoState(CommandLine::getValue("state"));
[1672]146
[1802]147            while (this->activeChild_)
148            {
149                clock.capture();
[1672]150
[1802]151                this->tick(clock);
[1672]152
[1802]153                if (this->stateRequest_ != "")
154                    gotoState(stateRequest_);
155            }
156
157            this->deactivate();
[1824]158#ifdef NDEBUG
[1672]159        }
[1802]160        // Note: These are all unhandled exceptions that should not have made its way here!
161        // almost complete game catch block to display the messages appropriately.
162        catch (std::exception& ex)
163        {
164            COUT(1) << ex.what() << std::endl;
165            COUT(1) << "Program aborted." << std::endl;
166        }
167        // anything that doesn't inherit from std::exception
168        catch (...)
169        {
170            COUT(1) << "An unidentifiable exception has occured. Program aborted." << std::endl;
171        }
[1824]172#endif
[1672]173    }
174}
Note: See TracBrowser for help on using the repository browser.