Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core4/src/orxonox/gamestates/GSRoot.cc @ 3238

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

Improved exception-safety in the Game class and fixed some issues and bugs resulting from the changes.

  • Property svn:eol-style set to native
File size: 6.0 KB
RevLine 
[1661]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 "GSRoot.h"
30
[2896]31#include "core/Clock.h"
[3196]32#include "core/CommandLine.h"
33#include "core/ConsoleCommand.h"
[2896]34#include "core/Game.h"
35#include "core/GameMode.h"
[1826]36#include "tools/Timer.h"
[3196]37#include "interfaces/TimeFactorListener.h"
38#include "interfaces/Tickable.h"
[1661]39
40namespace orxonox
41{
[3238]42    AddGameState(GSRoot, "root", true);
[2896]43    SetCommandLineSwitch(console);
44    // Shortcuts for easy direct loading
45    SetCommandLineSwitch(server);
46    SetCommandLineSwitch(client);
47    SetCommandLineSwitch(dedicated);
48    SetCommandLineSwitch(standalone);
[1663]49
[3084]50    GSRoot::GSRoot(const std::string& name, bool countTickTime)
51        : GameState(name, countTickTime)
[2662]52        , timeFactor_(1.0f)
53        , bPaused_(false)
54        , timeFactorPauseBackup_(1.0f)
[1661]55    {
[2662]56        this->ccSetTimeFactor_ = 0;
57        this->ccPause_ = 0;
[1661]58    }
59
60    GSRoot::~GSRoot()
61    {
62    }
63
[2896]64    void GSRoot::activate()
[1686]65    {
[2662]66        // reset game speed to normal
[2896]67        this->timeFactor_ = 1.0f;
[2662]68
69        {
70            // time factor console command
71            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::setTimeFactor);
72            functor->setObject(this);
73            this->ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor");
74            CommandExecutor::addConsoleCommandShortcut(this->ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);
75        }
76
77        {
78            // time factor console command
79            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::pause);
80            functor->setObject(this);
81            this->ccPause_ = createConsoleCommand(functor, "pause");
82            CommandExecutor::addConsoleCommandShortcut(this->ccPause_).accessLevel(AccessLevel::Offline);
83        }
[2896]84
85        // Load level directly?
86        bool loadLevel = false;
87        if (CommandLine::getValue("standalone").getBool())
88        {
89            Game::getInstance().requestStates("graphics, standalone, level");
90            loadLevel = true;
91        }
92        if (CommandLine::getValue("server").getBool())
93        {
94            Game::getInstance().requestStates("graphics, server, level");
95            loadLevel = true;
96        }
97        if (CommandLine::getValue("client").getBool())
98        {
99            Game::getInstance().requestStates("graphics, client, level");
100            loadLevel = true;
101        }
102        if (CommandLine::getValue("dedicated").getBool())
103        {
104            Game::getInstance().requestStates("dedicated, level");
105            loadLevel = true;
106        }
107       
108        // Determine where to start otherwise
109        if (!loadLevel && !CommandLine::getValue("console").getBool())
110        {
111            // Also load graphics
112            Game::getInstance().requestState("graphics");
113        }
[1661]114    }
115
[2896]116    void GSRoot::deactivate()
[1661]117    {
[2928]118/*
[2662]119        if (this->ccSetTimeFactor_)
120        {
121            delete this->ccSetTimeFactor_;
122            this->ccSetTimeFactor_ = 0;
123        }
124
125        if (this->ccPause_)
126        {
127            delete this->ccPause_;
128            this->ccPause_ = 0;
129        }
[2928]130*/
[1661]131    }
132
[2896]133    void GSRoot::update(const Clock& time)
[1661]134    {
[2896]135        if (this->getActivity().topState)
136        {
137            // This state can not 'survive' on its own.
138            // Load a user interface therefore
139            Game::getInstance().requestState("ioConsole");
140        }
141
[2662]142        uint64_t timeBeforeTick = time.getRealMicroseconds();
143
[1826]144        for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it)
145            it->tick(time);
146
[2171]147        /*** HACK *** HACK ***/
148        // Call the Tickable objects
[2662]149        float leveldt = time.getDeltaTime();
150        if (leveldt > 1.0f)
151        {
152            // just loaded
153            leveldt = 0.0f;
154        }
[2171]155        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
[2662]156            it->tick(leveldt * this->timeFactor_);
[2171]157        /*** HACK *** HACK ***/
158
[2662]159        uint64_t timeAfterTick = time.getRealMicroseconds();
160
[2896]161        // Also add our tick time
162        Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick);
[1662]163    }
[1674]164
165    /**
[2662]166    @brief
167        Changes the speed of Orxonox
168    */
169    void GSRoot::setTimeFactor(float factor)
170    {
[2896]171        if (GameMode::isMaster())
[2662]172        {
173            if (!this->bPaused_)
174            {
175                TimeFactorListener::timefactor_s = factor;
176
177                for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it)
178                    it->changedTimeFactor(factor, this->timeFactor_);
179
180                this->timeFactor_ = factor;
181            }
182            else
183                this->timeFactorPauseBackup_ = factor;
184        }
185    }
186
187    void GSRoot::pause()
188    {
[2896]189        if (GameMode::isMaster())
[2662]190        {
191            if (!this->bPaused_)
192            {
193                this->timeFactorPauseBackup_ = this->timeFactor_;
194                this->setTimeFactor(0.0f);
195                this->bPaused_ = true;
196            }
197            else
198            {
199                this->bPaused_ = false;
200                this->setTimeFactor(this->timeFactorPauseBackup_);
201            }
202        }
203    }
[1661]204}
Note: See TracBrowser for help on using the repository browser.