[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 "OrxonoxStableHeaders.h" |
---|
| 30 | #include "GSRoot.h" |
---|
| 31 | |
---|
[1764] | 32 | #include "util/Exception.h" |
---|
| 33 | #include "util/Debug.h" |
---|
[2662] | 34 | #include "core/Core.h" |
---|
[1661] | 35 | #include "core/ConfigValueIncludes.h" |
---|
[1686] | 36 | #include "core/CoreIncludes.h" |
---|
[1661] | 37 | #include "core/ConsoleCommand.h" |
---|
[1826] | 38 | #include "tools/Timer.h" |
---|
[2171] | 39 | #include "objects/Tickable.h" |
---|
[1661] | 40 | |
---|
| 41 | namespace orxonox |
---|
| 42 | { |
---|
| 43 | GSRoot::GSRoot() |
---|
[1672] | 44 | : RootGameState("root") |
---|
[2662] | 45 | , timeFactor_(1.0f) |
---|
| 46 | , bPaused_(false) |
---|
| 47 | , timeFactorPauseBackup_(1.0f) |
---|
[1661] | 48 | { |
---|
[1686] | 49 | RegisterRootObject(GSRoot); |
---|
[1891] | 50 | setConfigValues(); |
---|
[2662] | 51 | |
---|
| 52 | this->ccSetTimeFactor_ = 0; |
---|
| 53 | this->ccPause_ = 0; |
---|
[1661] | 54 | } |
---|
| 55 | |
---|
| 56 | GSRoot::~GSRoot() |
---|
| 57 | { |
---|
| 58 | } |
---|
| 59 | |
---|
[1686] | 60 | void GSRoot::setConfigValues() |
---|
| 61 | { |
---|
[2662] | 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."); |
---|
[1686] | 66 | } |
---|
| 67 | |
---|
[1661] | 68 | void GSRoot::enter() |
---|
| 69 | { |
---|
[2662] | 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 | FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::exitGame); |
---|
| 83 | functor->setObject(this); |
---|
| 84 | this->ccExit_ = createConsoleCommand(functor, "exit"); |
---|
| 85 | CommandExecutor::addConsoleCommandShortcut(this->ccExit_); |
---|
| 86 | } |
---|
[1664] | 87 | |
---|
[2662] | 88 | { |
---|
| 89 | // add console commands |
---|
| 90 | FunctorMember01<GameStateBase, const std::string&>* functor = createFunctor(&GameStateBase::requestState); |
---|
| 91 | functor->setObject(this); |
---|
| 92 | this->ccSelectGameState_ = createConsoleCommand(functor, "selectGameState"); |
---|
| 93 | CommandExecutor::addConsoleCommandShortcut(this->ccSelectGameState_); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | { |
---|
| 97 | // time factor console command |
---|
| 98 | FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::setTimeFactor); |
---|
| 99 | functor->setObject(this); |
---|
| 100 | this->ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor"); |
---|
| 101 | CommandExecutor::addConsoleCommandShortcut(this->ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | { |
---|
| 105 | // time factor console command |
---|
| 106 | FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::pause); |
---|
| 107 | functor->setObject(this); |
---|
| 108 | this->ccPause_ = createConsoleCommand(functor, "pause"); |
---|
| 109 | CommandExecutor::addConsoleCommandShortcut(this->ccPause_).accessLevel(AccessLevel::Offline); |
---|
| 110 | } |
---|
[1661] | 111 | } |
---|
| 112 | |
---|
| 113 | void GSRoot::leave() |
---|
| 114 | { |
---|
[2662] | 115 | // destroy console commands |
---|
| 116 | delete this->ccExit_; |
---|
| 117 | delete this->ccSelectGameState_; |
---|
[1792] | 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 | } |
---|
[1661] | 130 | } |
---|
| 131 | |
---|
[1674] | 132 | void GSRoot::ticked(const Clock& time) |
---|
[1661] | 133 | { |
---|
[2662] | 134 | uint64_t timeBeforeTick = time.getRealMicroseconds(); |
---|
| 135 | |
---|
[2800] | 136 | Core::getInstance().update(time); |
---|
[1662] | 137 | |
---|
[1826] | 138 | for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it) |
---|
| 139 | it->tick(time); |
---|
| 140 | |
---|
[2171] | 141 | /*** HACK *** HACK ***/ |
---|
| 142 | // Call the Tickable objects |
---|
[2662] | 143 | float leveldt = time.getDeltaTime(); |
---|
| 144 | if (leveldt > 1.0f) |
---|
| 145 | { |
---|
| 146 | // just loaded |
---|
| 147 | leveldt = 0.0f; |
---|
| 148 | } |
---|
[2171] | 149 | for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) |
---|
[2662] | 150 | it->tick(leveldt * this->timeFactor_); |
---|
[2171] | 151 | /*** HACK *** HACK ***/ |
---|
| 152 | |
---|
[2662] | 153 | uint64_t timeAfterTick = time.getRealMicroseconds(); |
---|
| 154 | |
---|
| 155 | // STATISTICS |
---|
| 156 | assert(timeAfterTick - timeBeforeTick >= 0 ); |
---|
| 157 | statisticsTickInfo tickInfo = {timeAfterTick, timeAfterTick - timeBeforeTick}; |
---|
| 158 | statisticsTickTimes_.push_back(tickInfo); |
---|
| 159 | assert(statisticsTickTimes_.back().tickLength==tickInfo.tickLength); |
---|
| 160 | this->periodTickTime_ += tickInfo.tickLength; |
---|
| 161 | |
---|
| 162 | // Ticks GSGraphics or GSDedicated |
---|
[1674] | 163 | this->tickChild(time); |
---|
[2662] | 164 | |
---|
| 165 | if (timeAfterTick > statisticsStartTime_ + statisticsRefreshCycle_) |
---|
| 166 | { |
---|
| 167 | std::list<statisticsTickInfo>::iterator it = this->statisticsTickTimes_.begin(); |
---|
| 168 | assert(it != this->statisticsTickTimes_.end()); |
---|
| 169 | int64_t lastTime = timeAfterTick - statisticsAvgLength_; |
---|
| 170 | if ((int64_t)it->tickTime < lastTime) |
---|
| 171 | { |
---|
| 172 | do |
---|
| 173 | { |
---|
| 174 | assert(this->periodTickTime_ > it->tickLength); |
---|
| 175 | this->periodTickTime_ -= it->tickLength; |
---|
| 176 | ++it; |
---|
| 177 | assert(it != this->statisticsTickTimes_.end()); |
---|
| 178 | } while ((int64_t)it->tickTime < lastTime); |
---|
| 179 | this->statisticsTickTimes_.erase(this->statisticsTickTimes_.begin(), it); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | uint32_t framesPerPeriod = this->statisticsTickTimes_.size(); |
---|
| 183 | this->avgFPS_ = (float)framesPerPeriod / (timeAfterTick - this->statisticsTickTimes_.front().tickTime) * 1000000.0; |
---|
| 184 | this->avgTickTime_ = (float)this->periodTickTime_ / framesPerPeriod / 1000.0; |
---|
| 185 | |
---|
| 186 | statisticsStartTime_ = timeAfterTick; |
---|
| 187 | } |
---|
| 188 | |
---|
[1662] | 189 | } |
---|
[1674] | 190 | |
---|
| 191 | /** |
---|
[2662] | 192 | @brief |
---|
| 193 | Changes the speed of Orxonox |
---|
| 194 | */ |
---|
| 195 | void GSRoot::setTimeFactor(float factor) |
---|
| 196 | { |
---|
| 197 | if (Core::isMaster()) |
---|
| 198 | { |
---|
| 199 | if (!this->bPaused_) |
---|
| 200 | { |
---|
| 201 | TimeFactorListener::timefactor_s = factor; |
---|
| 202 | |
---|
| 203 | for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it) |
---|
| 204 | it->changedTimeFactor(factor, this->timeFactor_); |
---|
| 205 | |
---|
| 206 | this->timeFactor_ = factor; |
---|
| 207 | } |
---|
| 208 | else |
---|
| 209 | this->timeFactorPauseBackup_ = factor; |
---|
| 210 | } |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | void GSRoot::pause() |
---|
| 214 | { |
---|
| 215 | if (Core::isMaster()) |
---|
| 216 | { |
---|
| 217 | if (!this->bPaused_) |
---|
| 218 | { |
---|
| 219 | this->timeFactorPauseBackup_ = this->timeFactor_; |
---|
| 220 | this->setTimeFactor(0.0f); |
---|
| 221 | this->bPaused_ = true; |
---|
| 222 | } |
---|
| 223 | else |
---|
| 224 | { |
---|
| 225 | this->bPaused_ = false; |
---|
| 226 | this->setTimeFactor(this->timeFactorPauseBackup_); |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | //////////////////////// |
---|
| 232 | // TimeFactorListener // |
---|
| 233 | //////////////////////// |
---|
| 234 | float TimeFactorListener::timefactor_s = 1.0f; |
---|
| 235 | |
---|
| 236 | TimeFactorListener::TimeFactorListener() |
---|
| 237 | { |
---|
| 238 | RegisterRootObject(TimeFactorListener); |
---|
| 239 | } |
---|
[1661] | 240 | } |
---|