Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/orxonox/CameraManager.cc @ 5894

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

Added new an option for the ScopedSingletonManager that can allow the Singleton to fail (throw an exception).
Also improved exception-safety in Scope so that when for a Singleton fails, the Scope will deactivate all activated listeners and properly destroy itself.

  • Property svn:eol-style set to native
File size: 4.7 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 *      Benjamin Knecht
24 *   Co-authors:
25 *      Fabian 'x3n' Landau
26 *
27 */
28
29#include "CameraManager.h"
30
31#include <OgreSceneManager.h>
32#include <OgreViewport.h>
33#include <OgreCompositorManager.h>
34
35#include "util/StringUtils.h"
36#include "core/GameMode.h"
37#include "core/GraphicsManager.h"
38#include "core/GUIManager.h"
39#include "core/ObjectList.h"
40#include "core/ScopedSingletonManager.h"
41#include "tools/Shader.h"
42#include "graphics/Camera.h"
43#include "Scene.h"
44
45namespace orxonox
46{
47    CameraManager* CameraManager::singletonPtr_s = 0;
48    ManageScopedSingleton(CameraManager, ScopeID::Graphics, false);
49
50    CameraManager::CameraManager()
51        : viewport_(GraphicsManager::getInstance().getViewport())
52    {
53        this->fallbackCamera_ = 0;
54    }
55
56    CameraManager::~CameraManager()
57    {
58        if (this->fallbackCamera_)
59            this->fallbackCameraScene_->getSceneManager()->destroyCamera(this->fallbackCamera_);
60        GUIManager::getInstance().setCamera(0);
61    }
62
63    Camera* CameraManager::getActiveCamera() const
64    {
65        if (this->cameraList_.size() > 0)
66            return this->cameraList_.front();
67        else
68            return 0;
69    }
70
71    void CameraManager::requestFocus(Camera* camera)
72    {
73        if (!GameMode::showsGraphics())
74            return;
75
76        // notify old camera (if it exists)
77        if (this->cameraList_.size() > 0)
78            this->cameraList_.front()->removeFocus();
79        else if (this->fallbackCamera_)
80        {
81            this->fallbackCameraScene_->getSceneManager()->destroyCamera(this->fallbackCamera_);
82            this->fallbackCamera_ = 0;
83        }
84
85        camera->setFocus();
86
87        // make sure we don't add it twice
88        for (std::list<Camera*>::iterator it = this->cameraList_.begin(); it != this->cameraList_.end(); ++it)
89            if ((*it) == camera)
90                return;
91
92        // add to list
93        this->cameraList_.push_front(camera);
94    }
95
96    void CameraManager::releaseFocus(Camera* camera)
97    {
98        if (!GameMode::showsGraphics())
99            return;
100
101        // notify the cam of releasing the focus
102        if (this->cameraList_.front() == camera)
103        {
104            camera->removeFocus();
105            this->cameraList_.pop_front();
106
107            // set new focus if possible
108            if (this->cameraList_.size() > 0)
109                this->cameraList_.front()->setFocus();
110            else
111            {
112                // there are no more cameras, create a fallback
113                if (!this->fallbackCamera_)
114                {
115                    this->fallbackCameraScene_ = camera->getScene();
116                    this->fallbackCamera_ = this->fallbackCameraScene_->getSceneManager()->createCamera(getUniqueNumberString());
117                }
118                this->useCamera(this->fallbackCamera_);
119            }
120        }
121        else
122        {
123            this->cameraList_.remove(camera);
124        }
125    }
126
127    void CameraManager::useCamera(Ogre::Camera* camera)
128    {
129        // This workaround is needed to avoid weird behaviour with active compositors while
130        // switching the camera (like freezing the image)
131        //
132        // Last known Ogre version needing this workaround:
133        // 1.4.8
134
135        // deactivate all compositors
136        {
137            Ogre::ResourceManager::ResourceMapIterator iterator = Ogre::CompositorManager::getSingleton().getResourceIterator();
138            while (iterator.hasMoreElements())
139                Ogre::CompositorManager::getSingleton().setCompositorEnabled(this->viewport_, iterator.getNext()->getName(), false);
140        }
141
142        this->viewport_->setCamera(camera);
143        GUIManager::getInstance().setCamera(camera);
144
145        // reactivate all visible compositors
146        {
147            for (ObjectList<Shader>::iterator it = ObjectList<Shader>::begin(); it != ObjectList<Shader>::end(); ++it)
148                it->updateVisibility();
149        }
150    }
151}
Note: See TracBrowser for help on using the repository browser.