Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/gamestates/GSLevel.cc @ 8061

Last change on this file since 8061 was 7879, checked in by landauf, 14 years ago

moved compositor unloading to avoid crashes in rare cases
disabled compositor unloading for Ogre 1.7.0+ which fixes some issues with shaders

I'm unsure if this belongs into GSLevel anyway, since it's resource related and removeAll() requires quite some attention to be called in the right place.

  • Property svn:eol-style set to native
File size: 6.8 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:
[1662]25 *      Fabian 'x3n' Landau
[2896]26 *      Benjamin Knecht
[1661]27 *
28 */
29
30#include "GSLevel.h"
31
[5695]32#include <OgreCompositorManager.h>
33
[5855]34#include "util/Clock.h"
[1661]35#include "core/input/InputManager.h"
[3327]36#include "core/input/InputState.h"
[5863]37#include "core/input/KeyBinderManager.h"
[7870]38#include "core/Core.h"
[2896]39#include "core/Game.h"
40#include "core/GameMode.h"
[3370]41#include "core/GUIManager.h"
[3196]42#include "core/Loader.h"
43#include "core/XMLFile.h"
[7284]44#include "core/command/ConsoleCommand.h"
[3196]45
[2087]46#include "LevelManager.h"
[2662]47#include "PlayerManager.h"
[1661]48
49namespace orxonox
50{
[3370]51    DeclareGameState(GSLevel, "level", false, false);
[1934]52
[7876]53    static const std::string __CC_startMainMenu_name = "startMainMenu";
54    static const std::string __CC_changeGame_name = "changeGame";
55
56    SetConsoleCommand(__CC_startMainMenu_name, &GSLevel::startMainMenu).deactivate();
57    SetConsoleCommand(__CC_changeGame_name, &GSLevel::changeGame).defaultValues(BLANKSTRING).deactivate();
58
[3370]59    GSLevel::GSLevel(const GameStateInfo& info)
60        : GameState(info)
[2896]61        , gameInputState_(0)
62        , guiMouseOnlyInputState_(0)
63        , guiKeysOnlyInputState_(0)
[5876]64        , startFile_(0)
[6417]65        , bShowIngameGUI_(false)
[1661]66    {
67    }
68
69    GSLevel::~GSLevel()
70    {
71    }
72
[2896]73    void GSLevel::activate()
[1661]74    {
[2896]75        if (GameMode::showsGraphics())
[2087]76        {
[3327]77            gameInputState_ = InputManager::getInstance().createInputState("game");
[6746]78            gameInputState_->setMouseExclusive(TriBool::True);
[5863]79            gameInputState_->setHandler(KeyBinderManager::getInstance().getDefaultAsHandler());
80            KeyBinderManager::getInstance().setToDefault();
[1661]81
[3327]82            guiMouseOnlyInputState_ = InputManager::getInstance().createInputState("guiMouseOnly");
[6746]83            guiMouseOnlyInputState_->setMouseExclusive(TriBool::True);
84            guiMouseOnlyInputState_->setMouseHandler(&GUIManager::getInstance());
[2896]85
[3327]86            guiKeysOnlyInputState_ = InputManager::getInstance().createInputState("guiKeysOnly");
[6746]87            guiKeysOnlyInputState_->setKeyHandler(&GUIManager::getInstance());
[2087]88        }
[1662]89
[2896]90        if (GameMode::isMaster())
[2087]91        {
92            this->loadLevel();
93        }
[1755]94
[2896]95        if (GameMode::showsGraphics())
[2087]96        {
97            // level is loaded: we can start capturing the input
[3327]98            InputManager::getInstance().enterState("game");
[6417]99
[5820]100            // connect the HumanPlayer to the game
[5850]101            PlayerManager::getInstance().clientConnected(0);
[7876]102
103            ModifyConsoleCommand(__CC_startMainMenu_name).activate();
[2087]104        }
[7876]105
106        if (GameMode::isStandalone())
107            ModifyConsoleCommand(__CC_changeGame_name).activate();
[2662]108    }
[2087]109
[2896]110    void GSLevel::deactivate()
111    {
[5695]112        if (GameMode::showsGraphics())
[3327]113            InputManager::getInstance().leaveState("game");
[6417]114
[5966]115        // disconnect all HumanPlayers
116        PlayerManager::getInstance().disconnectAllClients();
[1662]117
[2896]118        if (GameMode::isMaster())
[2087]119            this->unloadLevel();
[1662]120
[2896]121        if (GameMode::showsGraphics())
[2087]122        {
[7879]123#if OGRE_VERSION < 0x010700
124            // unload all compositors (this is only necessary because we don't yet destroy all resources!)
125            Ogre::CompositorManager::getSingleton().removeAll();
126#endif
127
[2896]128            gameInputState_->setHandler(0);
129            guiMouseOnlyInputState_->setHandler(0);
130            guiKeysOnlyInputState_->setHandler(0);
[3327]131            InputManager::getInstance().destroyState("game");
[5818]132            InputManager::getInstance().destroyState("guiKeysOnly");
133            InputManager::getInstance().destroyState("guiMouseOnly");
[7876]134
135            ModifyConsoleCommand(__CC_startMainMenu_name  ).deactivate();
[2087]136        }
[7876]137
138        if (GameMode::isStandalone())
139            ModifyConsoleCommand(__CC_changeGame_name).deactivate();
[1661]140    }
141
[2896]142    void GSLevel::update(const Clock& time)
[1661]143    {
[5876]144        // Note: Temporarily moved to GSRoot.
[2087]145        //// Call the scene objects
146        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
147        //    it->tick(time.getDeltaTime() * this->timeFactor_);
[1661]148    }
149
[1670]150    void GSLevel::loadLevel()
151    {
[5922]152        for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it != ObjectList<BaseObject>::end(); ++it)
153            this->staticObjects_.insert(*it);
[6417]154
[1670]155        // call the loader
156        COUT(0) << "Loading level..." << std::endl;
[5876]157        startFile_ = new XMLFile(LevelManager::getInstance().getDefaultLevel());
158        Loader::open(startFile_);
[7870]159
160        Core::getInstance().updateLastLevelTimestamp();
[1670]161    }
162
163    void GSLevel::unloadLevel()
164    {
[5876]165        Loader::unload(startFile_);
166        delete startFile_;
[5922]167
168        COUT(3) << "Unloaded level. Remaining objects:" << std::endl;
169        unsigned int i = 0;
170        for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it != ObjectList<BaseObject>::end(); ++it)
171        {
172            std::set<BaseObject*>::const_iterator find = this->staticObjects_.find(*it);
173            if (find == this->staticObjects_.end())
174            {
[7854]175                COUT(3) << ++i << ": " << it->getIdentifier()->getName() << " (" << *it << "), references: " << it->getReferenceCount() << std::endl;
[5922]176            }
177        }
178        COUT(3) << i << " objects remaining.";
179        if (i == 0)
180            COUT(3) << " Well done!" << std::endl;
181        else
182            COUT(3) << " Try harder!" << std::endl;
[1670]183    }
[7876]184
185    /**
186    @brief
187        Starts the MainMenu.
188    */
189    /*static*/ void GSLevel::startMainMenu(void)
190    {
191        // HACK
192        Game::getInstance().popState();
193        Game::getInstance().popState();
194    }
195
196    /**
197    @brief
198        Terminates the current game and starts a new game.
199    @param level
200        The filename of the level to be started.
201    */
202    /*static*/ void GSLevel::changeGame(const std::string& level)
203    {
204        if(level != BLANKSTRING)
205            LevelManager::getInstance().setDefaultLevel(level);
206
207        // HACK
208        Game::getInstance().popState();
209        Game::getInstance().popState();
210        Game::getInstance().requestStates("standalone, level");
211    }
[1661]212}
Note: See TracBrowser for help on using the repository browser.