Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/orxonox/gamestates/GSRoot.cc @ 2805

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

Added new class: Game
It represents basic operation related to the running game like start and stop or managing the new GameStates.
And since only three lines were left in Main.cc I thought I could move that to the very beginning of 'Game'.

  • Property svn:eol-style set to native
File size: 7.3 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 "OrxonoxStableHeaders.h"
30#include "GSRoot.h"
31
32#include "util/Exception.h"
33#include "util/Debug.h"
34#include "core/Core.h"
35#include "core/ConfigValueIncludes.h"
36#include "core/CoreIncludes.h"
37#include "core/ConsoleCommand.h"
38#include "tools/Timer.h"
39#include "objects/Tickable.h"
40
41namespace orxonox
42{
43    GSRoot::GSRoot()
44        : RootGameState("root")
45        , timeFactor_(1.0f)
46        , bPaused_(false)
47        , timeFactorPauseBackup_(1.0f)
48    {
49        RegisterRootObject(GSRoot);
50        setConfigValues();
51
52        this->ccSetTimeFactor_ = 0;
53        this->ccPause_ = 0;
54    }
55
56    GSRoot::~GSRoot()
57    {
58    }
59
60    void GSRoot::setConfigValues()
61    {
62        SetConfigValue(statisticsRefreshCycle_, 250000)
63            .description("Sets the time in microseconds interval at which average fps, etc. get updated.");
64        SetConfigValue(statisticsAvgLength_, 1000000)
65            .description("Sets the time in microseconds interval at which average fps, etc. gets calculated.");
66    }
67
68    void GSRoot::enter()
69    {
70        // reset game speed to normal
71        timeFactor_ = 1.0f;
72
73        // reset frame counter
74        this->statisticsStartTime_ = 0;
75        this->statisticsTickTimes_.clear();
76        this->periodTickTime_ = 0;
77        this->avgFPS_ = 0.0f;
78        this->avgTickTime_ = 0.0f;
79
80        {
81            // add console commands
82            FunctorMember01<GameStateBase, const std::string&>* functor = createFunctor(&GameStateBase::requestState);
83            functor->setObject(this);
84            this->ccSelectGameState_ = createConsoleCommand(functor, "selectGameState");
85            CommandExecutor::addConsoleCommandShortcut(this->ccSelectGameState_);
86        }
87
88        {
89            // time factor console command
90            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::setTimeFactor);
91            functor->setObject(this);
92            this->ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor");
93            CommandExecutor::addConsoleCommandShortcut(this->ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);
94        }
95
96        {
97            // time factor console command
98            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::pause);
99            functor->setObject(this);
100            this->ccPause_ = createConsoleCommand(functor, "pause");
101            CommandExecutor::addConsoleCommandShortcut(this->ccPause_).accessLevel(AccessLevel::Offline);
102        }
103    }
104
105    void GSRoot::leave()
106    {
107        // destroy console commands
108        delete this->ccSelectGameState_;
109
110        if (this->ccSetTimeFactor_)
111        {
112            delete this->ccSetTimeFactor_;
113            this->ccSetTimeFactor_ = 0;
114        }
115
116        if (this->ccPause_)
117        {
118            delete this->ccPause_;
119            this->ccPause_ = 0;
120        }
121    }
122
123    void GSRoot::ticked(const Clock& time)
124    {
125        uint64_t timeBeforeTick = time.getRealMicroseconds();
126
127        Core::getInstance().update(time);
128
129        for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it)
130            it->tick(time);
131
132        /*** HACK *** HACK ***/
133        // Call the Tickable objects
134        float leveldt = time.getDeltaTime();
135        if (leveldt > 1.0f)
136        {
137            // just loaded
138            leveldt = 0.0f;
139        }
140        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
141            it->tick(leveldt * this->timeFactor_);
142        /*** HACK *** HACK ***/
143
144        uint64_t timeAfterTick = time.getRealMicroseconds();
145
146        // STATISTICS
147        assert(timeAfterTick - timeBeforeTick >= 0 );
148        statisticsTickInfo tickInfo = {timeAfterTick, timeAfterTick - timeBeforeTick};
149        statisticsTickTimes_.push_back(tickInfo);
150        assert(statisticsTickTimes_.back().tickLength==tickInfo.tickLength);
151        this->periodTickTime_ += tickInfo.tickLength;
152
153        // Ticks GSGraphics or GSDedicated
154        this->tickChild(time);
155
156        if (timeAfterTick > statisticsStartTime_ + statisticsRefreshCycle_)
157        {
158            std::list<statisticsTickInfo>::iterator it = this->statisticsTickTimes_.begin();
159            assert(it != this->statisticsTickTimes_.end());
160            int64_t lastTime = timeAfterTick - statisticsAvgLength_;
161            if ((int64_t)it->tickTime < lastTime)
162            {
163                do
164                {
165                    assert(this->periodTickTime_ > it->tickLength);
166                    this->periodTickTime_ -= it->tickLength;
167                    ++it;
168                    assert(it != this->statisticsTickTimes_.end());
169                } while ((int64_t)it->tickTime < lastTime);
170                this->statisticsTickTimes_.erase(this->statisticsTickTimes_.begin(), it);
171            }
172
173            uint32_t framesPerPeriod = this->statisticsTickTimes_.size();
174            this->avgFPS_ = (float)framesPerPeriod / (timeAfterTick - this->statisticsTickTimes_.front().tickTime) * 1000000.0;
175            this->avgTickTime_ = (float)this->periodTickTime_ / framesPerPeriod / 1000.0;
176
177            statisticsStartTime_ = timeAfterTick;
178        }
179
180    }
181
182    /**
183    @brief
184        Changes the speed of Orxonox
185    */
186    void GSRoot::setTimeFactor(float factor)
187    {
188        if (Core::isMaster())
189        {
190            if (!this->bPaused_)
191            {
192                TimeFactorListener::timefactor_s = factor;
193
194                for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it)
195                    it->changedTimeFactor(factor, this->timeFactor_);
196
197                this->timeFactor_ = factor;
198            }
199            else
200                this->timeFactorPauseBackup_ = factor;
201        }
202    }
203
204    void GSRoot::pause()
205    {
206        if (Core::isMaster())
207        {
208            if (!this->bPaused_)
209            {
210                this->timeFactorPauseBackup_ = this->timeFactor_;
211                this->setTimeFactor(0.0f);
212                this->bPaused_ = true;
213            }
214            else
215            {
216                this->bPaused_ = false;
217                this->setTimeFactor(this->timeFactorPauseBackup_);
218            }
219        }
220    }
221
222    ////////////////////////
223    // TimeFactorListener //
224    ////////////////////////
225    float TimeFactorListener::timefactor_s = 1.0f;
226
227    TimeFactorListener::TimeFactorListener()
228    {
229        RegisterRootObject(TimeFactorListener);
230    }
231}
Note: See TracBrowser for help on using the repository browser.