Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/core/RootGameState.cc @ 1929

Last change on this file since 1929 was 1695, checked in by rgrieder, 16 years ago

Changed internal Timer clock from float to uint64_t

  • Property svn:eol-style set to native
File size: 6.4 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
[1690]31#include "util/String.h"
32#include "util/SubString.h"
[1674]33#include "Clock.h"
[1672]34#include "Debug.h"
35#include "Exception.h"
36#include "CommandLine.h"
37
38namespace orxonox
39{
[1673]40    SetCommandLineArgument(state, "gui").setShortcut("s");
[1672]41
42    RootGameState::RootGameState(const std::string& name)
[1689]43        : GameState<GameStateBase>(name)
[1672]44        , stateRequest_("")
45    {
46    }
47
48    RootGameState::~RootGameState()
49    {
50    }
51
52    /**
53    @brief
54        Internal method that actually makes the state transition. Since it is internal,
55        the method can assume certain things to be granted (like 'this' is always active).
56    */
[1689]57    void RootGameState::makeTransition(GameStateBase* source, GameStateBase* destination)
[1672]58    {
59        if (source != 0)
60        {
61            // transition was not initiated by root itself
62            this->activeChild_ = 0;
63        }
64
65        if (destination == this)
66        {
67            // this marks the end of the game.
68            return;
69        }
70
71        // Check for 'destination' in the children map first
[1689]72        std::map<GameStateBase*, GameStateBase*>::const_iterator it
[1672]73            = this->grandchildrenToChildren_.find(destination);
74        if (it != this->grandchildrenToChildren_.end())
75        {
[1689]76            OrxAssert(dynamic_cast<GameStateBase*>(it->second) != 0,
[1672]77                "There was a mix with RootGameState and GameState, could not cast.");
[1689]78            GameStateBase* child = static_cast<GameStateBase*>(it->second);
[1672]79            // child state. Don't use 'state', might be a grandchild!
80            this->activeChild_ = child;
81            child->makeTransition(this, destination);
82        }
83        else
84        {
85            // root doesn't have a parent..
86            OrxAssert(false, "GameState '" + destination->getName() + "' not found in children list of Root.");
87        }
88    }
89
90    void RootGameState::gotoState(const std::string& name)
91    {
[1689]92        GameStateBase* request = getState(name);
[1672]93        if (request)
94        {
[1689]95            GameStateBase* current = getCurrentState();
[1672]96            if (current)
97            {
98                current->makeTransition(0, request);
99            }
100            else
101            {
102                // Root is not yet active. This is a violation.
103                ThrowException(GameState, "Activate Root before requesting a state.");
104            }
105        }
106        else
107        {
108            COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl;
109        }
110    }
111
112    /**
113    @brief
114        Makes a state transition according to the state tree. You can choose any state
115        in the tree to do the call. The function finds the current state on its own.
116    @param state
117        The state to be entered, has to exist in the tree.
118    */
119    void RootGameState::requestState(const std::string& name)
120    {
121        this->stateRequest_ = name;
122    }
123
124    /**
125    @brief
126        Starts the game. The little 'while' denotes the main loop.
127        Whenever the root state is selected, the game ends.
128    @param name
129        State to start with (usually main menu or specified by command line)
130    */
[1689]131    void RootGameState::start(int argc, char** argv)
[1672]132    {
[1695]133        // start global orxonox time
134        Clock clock;
135
[1690]136        parseArguments(argc, argv);
[1689]137
[1672]138        this->activate();
139
140        // get initial state from command line
141        std::string initialState;
142        CommandLine::getValue<std::string>("state", &initialState);
143        gotoState(initialState);
144
145        while (this->activeChild_)
146        {
[1674]147            clock.capture();
[1672]148
[1674]149            this->tick(clock);
[1672]150
151            if (this->stateRequest_ != "")
152                gotoState(stateRequest_);
153        }
154
155        this->deactivate();
156    }
[1689]157
[1690]158    /**
159    @brief
160        Parses both command line and start.ini for CommandLineArguments.
161    */
162    void RootGameState::parseArguments(int argc, char** argv)
[1689]163    {
[1690]164        // parse command line first
[1689]165        std::vector<std::string> args;
166        for (int i = 1; i < argc; ++i)
167            args.push_back(argv[i]);
168
169        try
170        {
171            orxonox::CommandLine::parse(args);
172        }
173        catch (orxonox::ArgumentException& ex)
174        {
175            COUT(1) << ex.what() << std::endl;
176            COUT(0) << "Usage:" << std::endl << "orxonox " << CommandLine::getUsageInformation() << std::endl;
177        }
[1690]178
179        // look for additional arguments in start.ini
180        std::ifstream file;
181        file.open("start.ini");
182        args.clear();
183        if (file)
184        {
185            while (!file.eof())
186            {
187                std::string line;
188                std::getline(file, line);
189                line = removeTrailingWhitespaces(line);
190                //if (!(line[0] == '#' || line[0] == '%'))
191                //{
192                SubString tokens(line, " ", " ", false, 92, false, 34, false, 40, 41, false, '#');
193                for (unsigned i = 0; i < tokens.size(); ++i)
194                    if (tokens[i][0] != '#')
195                        args.push_back(tokens[i]);
196                //args.insert(args.end(), tokens.getAllStrings().begin(), tokens.getAllStrings().end());
197                //}
198            }
199            file.close();
200        }
201
202        try
203        {
204            orxonox::CommandLine::parse(args);
205        }
206        catch (orxonox::ArgumentException& ex)
207        {
208            COUT(1) << "An Exception occured while parsing start.ini" << std::endl;
209            COUT(1) << ex.what() << std::endl;
210            COUT(0) << "Usage:" << std::endl << "orxonox " << CommandLine::getUsageInformation() << std::endl;
211        }
[1689]212    }
[1672]213}
Note: See TracBrowser for help on using the repository browser.