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 "GSGraphics.h" |
---|
31 | |
---|
32 | #include <OgreFrameListener.h> |
---|
33 | #include <OgreRoot.h> |
---|
34 | #include <OgreTimer.h> |
---|
35 | #include <OgreWindowEventUtilities.h> |
---|
36 | #include <OgreRenderWindow.h> |
---|
37 | |
---|
38 | #include "core/ConsoleCommand.h" |
---|
39 | #include "core/ConfigValueIncludes.h" |
---|
40 | #include "core/input/InputManager.h" |
---|
41 | //#include "core/Core.h" |
---|
42 | #include "overlays/console/InGameConsole.h" |
---|
43 | #include "gui/GUIManager.h" |
---|
44 | #include "GraphicsEngine.h" |
---|
45 | |
---|
46 | namespace orxonox |
---|
47 | { |
---|
48 | GSGraphics::GSGraphics() |
---|
49 | : GameState("graphics") |
---|
50 | , debugRefreshTime_(0.0f) |
---|
51 | , inputManager_(0) |
---|
52 | , console_(0) |
---|
53 | , guiManager_(0) |
---|
54 | , frameCount_(0) |
---|
55 | { |
---|
56 | } |
---|
57 | |
---|
58 | GSGraphics::~GSGraphics() |
---|
59 | { |
---|
60 | } |
---|
61 | |
---|
62 | void GSGraphics::setConfigValues() |
---|
63 | { |
---|
64 | SetConfigValue(debugRefreshTime_, 0.2).description("Sets the time interval at which average fps, etc. get updated."); |
---|
65 | } |
---|
66 | |
---|
67 | void GSGraphics::enter() |
---|
68 | { |
---|
69 | this->ogreRoot_ = Ogre::Root::getSingletonPtr(); |
---|
70 | this->graphicsEngine_ = GraphicsEngine::getInstancePtr(); |
---|
71 | |
---|
72 | graphicsEngine_->loadRenderer(); // creates the render window |
---|
73 | |
---|
74 | // TODO: Spread this so that this call only initialises things needed for the Console and GUI |
---|
75 | graphicsEngine_->initialiseResources(); |
---|
76 | |
---|
77 | // Calls the InputManager which sets up the input devices. |
---|
78 | // The render window width and height are used to set up the mouse movement. |
---|
79 | inputManager_ = new InputManager(); |
---|
80 | inputManager_->initialise(graphicsEngine_->getWindowHandle(), |
---|
81 | graphicsEngine_->getWindowWidth(), graphicsEngine_->getWindowHeight(), true); |
---|
82 | |
---|
83 | // Load the InGameConsole |
---|
84 | console_ = new InGameConsole(); |
---|
85 | console_->initialise(); |
---|
86 | |
---|
87 | // load the CEGUI interface |
---|
88 | guiManager_ = new GUIManager(); |
---|
89 | guiManager_->initialise(); |
---|
90 | } |
---|
91 | |
---|
92 | void GSGraphics::leave() |
---|
93 | { |
---|
94 | delete this->guiManager_; |
---|
95 | |
---|
96 | delete this->console_; |
---|
97 | |
---|
98 | delete this->inputManager_; |
---|
99 | |
---|
100 | this->ogreRoot_->detachRenderTarget(GraphicsEngine::getInstance().getRenderWindow()); |
---|
101 | delete GraphicsEngine::getInstance().getRenderWindow(); |
---|
102 | this->ogreRoot_->shutdown |
---|
103 | // TODO: destroy render window |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | Main loop of the orxonox game. |
---|
108 | We use the Ogre::Timer to measure time since it uses the most precise |
---|
109 | method an a platform (however the windows timer lacks time when under |
---|
110 | heavy kernel load!). |
---|
111 | There is a simple mechanism to measure the average time spent in our |
---|
112 | ticks as it may indicate performance issues. |
---|
113 | A note about the Ogre::FrameListener: Even though we don't use them, |
---|
114 | they still get called. However, the delta times are not correct (except |
---|
115 | for timeSinceLastFrame, which is the most important). A little research |
---|
116 | as shown that there is probably only one FrameListener that doesn't even |
---|
117 | need the time. So we shouldn't run into problems. |
---|
118 | */ |
---|
119 | void GSGraphics::ticked(float dt, uint64_t time) |
---|
120 | { |
---|
121 | this->inputManager_->tick(dt); |
---|
122 | |
---|
123 | this->tickChild(dt, time); |
---|
124 | |
---|
125 | // tick console |
---|
126 | this->console_->tick(dt); |
---|
127 | |
---|
128 | //// get current time once again |
---|
129 | //timeAfterTick = timer_->getMicroseconds(); |
---|
130 | |
---|
131 | //tickTime += timeAfterTick - timeBeforeTick; |
---|
132 | //if (timeAfterTick > refreshStartTime + refreshTime) |
---|
133 | //{ |
---|
134 | // GraphicsEngine::getInstance().setAverageTickTime( |
---|
135 | // (float)tickTime * 0.001 / (frameCount - oldFrameCount)); |
---|
136 | // float avgFPS = (float)(frameCount - oldFrameCount) / (timeAfterTick - refreshStartTime) * 1000000.0; |
---|
137 | // GraphicsEngine::getInstance().setAverageFramesPerSecond(avgFPS); |
---|
138 | |
---|
139 | // oldFrameCount = frameCount; |
---|
140 | // tickTime = 0; |
---|
141 | // refreshStartTime = timeAfterTick; |
---|
142 | //} |
---|
143 | |
---|
144 | // don't forget to call _fireFrameStarted in ogre to make sure |
---|
145 | // everything goes smoothly |
---|
146 | Ogre::FrameEvent evt; |
---|
147 | evt.timeSinceLastFrame = dt; |
---|
148 | evt.timeSinceLastEvent = dt; // note: same time, but shouldn't matter anyway |
---|
149 | ogreRoot_->_fireFrameStarted(evt); |
---|
150 | |
---|
151 | // Pump messages in all registered RenderWindows |
---|
152 | // This calls the WindowEventListener objects. |
---|
153 | Ogre::WindowEventUtilities::messagePump(); |
---|
154 | // make sure the window stays active even when not focused |
---|
155 | // (probably only necessary on windows) |
---|
156 | GraphicsEngine::getInstance().setWindowActivity(true); |
---|
157 | |
---|
158 | // render |
---|
159 | ogreRoot_->_updateAllRenderTargets(); |
---|
160 | |
---|
161 | // again, just to be sure ogre works fine |
---|
162 | ogreRoot_->_fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted |
---|
163 | |
---|
164 | ++frameCount_; |
---|
165 | |
---|
166 | //} |
---|
167 | //catch (std::exception& ex) |
---|
168 | //{ |
---|
169 | // // something went wrong. |
---|
170 | // COUT(1) << ex.what() << std::endl; |
---|
171 | // COUT(1) << "Main loop was stopped by an unhandled exception. Shutting down." << std::endl; |
---|
172 | //} |
---|
173 | } |
---|
174 | } |
---|