Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5805 was 5805, checked in by landauf, 15 years ago
  • Enhanced SmartPtr: a) It stores now two pointers, one OrxonoxClass* (for reference counting) and one T* (for normal use). b) Incrementing the reference is now optional in the constructor for raw pointers. This is needed because Scene has a selfreference but should still be destroyable.
  • Changed BaseObject to store a SmartPtr to it's Scene instead of a normal pointer.
  • Changed setScene and getScene to deal directly with SmartPtr instead of a Scene* pointer to avoid casting-to-OrxonoxClass issues.
  • Fixed two problems with lost SceneManagers in CameraManger: a) added a SmartPtr to the Scene for the fallback camera b) added a call to GUIManager in the destructor to release the scene manager
  • Enabled unloading in GSLevel again (after years). Works if playing without bots.
  • 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/GUIManager.h"
37#include "core/ObjectList.h"
38#include "tools/Shader.h"
39#include "graphics/Camera.h"
40#include "Scene.h"
41
42namespace orxonox
43{
44    CameraManager* CameraManager::singletonPtr_s = 0;
45
46    CameraManager::CameraManager(Ogre::Viewport* viewport)
47        : viewport_(viewport)
48    {
49        this->fallbackCamera_ = 0;
50    }
51
52    CameraManager::~CameraManager()
53    {
54        if (this->fallbackCamera_)
55            this->fallbackCameraScene_->getSceneManager()->destroyCamera(this->fallbackCamera_);
56        GUIManager::getInstance().setCamera(0);
57    }
58
59    Camera* CameraManager::getActiveCamera() const
60    {
61        if (this->cameraList_.size() > 0)
62            return this->cameraList_.front();
63        else
64            return 0;
65    }
66
67    void CameraManager::requestFocus(Camera* camera)
68    {
69        if (!GameMode::showsGraphics())
70            return;
71
72        // notify old camera (if it exists)
73        if (this->cameraList_.size() > 0)
74            this->cameraList_.front()->removeFocus();
75        else if (this->fallbackCamera_)
76        {
77            this->fallbackCameraScene_->getSceneManager()->destroyCamera(this->fallbackCamera_);
78            this->fallbackCamera_ = 0;
79        }
80
81        camera->setFocus();
82
83        // make sure we don't add it twice
84        for (std::list<Camera*>::iterator it = this->cameraList_.begin(); it != this->cameraList_.end(); ++it)
85            if ((*it) == camera)
86                return;
87
88        // add to list
89        this->cameraList_.push_front(camera);
90    }
91
92    void CameraManager::releaseFocus(Camera* camera)
93    {
94        if (!GameMode::showsGraphics())
95            return;
96
97        // notify the cam of releasing the focus
98        if (this->cameraList_.front() == camera)
99        {
100            camera->removeFocus();
101            this->cameraList_.pop_front();
102
103            // set new focus if possible
104            if (this->cameraList_.size() > 0)
105                this->cameraList_.front()->setFocus();
106            else
107            {
108                // there are no more cameras, create a fallback
109                if (!this->fallbackCamera_)
110                {
111                    this->fallbackCameraScene_ = camera->getScene();
112                    this->fallbackCamera_ = this->fallbackCameraScene_->getSceneManager()->createCamera(getUniqueNumberString());
113                }
114                this->useCamera(this->fallbackCamera_);
115            }
116        }
117        else
118        {
119            this->cameraList_.remove(camera);
120        }
121    }
122
123    void CameraManager::useCamera(Ogre::Camera* camera)
124    {
125        // This workaround is needed to avoid weird behaviour with active compositors while
126        // switching the camera (like freezing the image)
127        //
128        // Last known Ogre version needing this workaround:
129        // 1.4.8
130
131        // deactivate all compositors
132        {
133            Ogre::ResourceManager::ResourceMapIterator iterator = Ogre::CompositorManager::getSingleton().getResourceIterator();
134            while (iterator.hasMoreElements())
135                Ogre::CompositorManager::getSingleton().setCompositorEnabled(this->viewport_, iterator.getNext()->getName(), false);
136        }
137
138        this->viewport_->setCamera(camera);
139        GUIManager::getInstance().setCamera(camera);
140
141        // reactivate all visible compositors
142        {
143            for (ObjectList<Shader>::iterator it = ObjectList<Shader>::begin(); it != ObjectList<Shader>::end(); ++it)
144                it->updateVisibility();
145        }
146    }
147}
Note: See TracBrowser for help on using the repository browser.