Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Moved the singleton creation/destruction mess to the Core class by using just two possible Scopes:

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