1 /*
2 This file is part of Caelum.
3 See http://www.ogre3d.org/wiki/index.php/Caelum
4 
5 Copyright (c) 2006-2008 Caelum team. See Contributors.txt for details.
6 
7 Caelum is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Caelum is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with Caelum. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "CaelumDemoCommon.h"
22 #include "ExampleApplication.h"
23 
24 class CaelumSampleFrameListener : public ExampleFrameListener
25 {
26 protected:
27     Caelum::CaelumSystem *mCaelumSystem;
28     Ogre::SceneManager *mScene;
29     float mSpeedFactor;
30     bool mPaused;
31     float mTimeTillNextUpdate;
32 
33 public:
CaelumSampleFrameListener(RenderWindow * win,Camera * cam)34     CaelumSampleFrameListener(RenderWindow* win, Camera* cam): ExampleFrameListener(win, cam)
35     {
36         mScene = cam->getSceneManager();
37         mPaused = false;
38 
39         // Pick components to create in the demo.
40         // You can comment any of those and it should still work
41         // Trying to disable one of these can be useful in finding problems.
42         Caelum::CaelumSystem::CaelumComponent componentMask;
43         componentMask = static_cast<Caelum::CaelumSystem::CaelumComponent> (
44                 Caelum::CaelumSystem::CAELUM_COMPONENT_SUN |
45                 Caelum::CaelumSystem::CAELUM_COMPONENT_MOON |
46                 Caelum::CaelumSystem::CAELUM_COMPONENT_SKY_DOME |
47                 //Caelum::CaelumSystem::CAELUM_COMPONENT_IMAGE_STARFIELD |
48                 Caelum::CaelumSystem::CAELUM_COMPONENT_POINT_STARFIELD |
49                 Caelum::CaelumSystem::CAELUM_COMPONENT_CLOUDS |
50                 0);
51         componentMask = CaelumSystem::CAELUM_COMPONENTS_DEFAULT;
52 
53         // Initialise CaelumSystem.
54         mCaelumSystem = new Caelum::CaelumSystem (Root::getSingletonPtr(), mScene, componentMask);
55 
56         // Set time acceleration.
57         mCaelumSystem->getUniversalClock ()->setTimeScale (512);
58 
59         // Register caelum as a listener.
60         mWindow->addListener (mCaelumSystem);
61         Root::getSingletonPtr()->addFrameListener (mCaelumSystem);
62 
63         UpdateSpeedFactor(mCaelumSystem->getUniversalClock ()->getTimeScale ());
64         mTimeTillNextUpdate = 0;
65     }
66 
~CaelumSampleFrameListener()67     ~CaelumSampleFrameListener() {
68         if (mCaelumSystem) {
69             mCaelumSystem->shutdown (false);
70             mCaelumSystem = 0;
71         }
72     }
73 
74     // Update speed factors.
UpdateSpeedFactor(double factor)75     void UpdateSpeedFactor(double factor)
76     {
77         mSpeedFactor = factor;
78         mCaelumSystem->getUniversalClock ()->setTimeScale (mPaused ? 0 : mSpeedFactor);
79     }
80 
frameEnded(const FrameEvent & evt)81     bool frameEnded(const FrameEvent& evt)
82     {
83         if (!ExampleFrameListener::frameEnded(evt)) {
84             return false;
85         }
86 
87         // Stop key repeat for these keys.
88         mTimeTillNextUpdate -= evt.timeSinceLastFrame;
89         if (mTimeTillNextUpdate <= 0) {
90             if (mKeyboard->isKeyDown (OIS::KC_SPACE)) {
91                 mTimeTillNextUpdate = 1;
92                 mPaused = !mPaused;
93                 UpdateSpeedFactor(mSpeedFactor);
94             }
95             if (mKeyboard->isKeyDown (OIS::KC_X)) {
96                 mTimeTillNextUpdate = 0.25;
97                 UpdateSpeedFactor(mSpeedFactor / 2);
98             }
99             if (mKeyboard->isKeyDown (OIS::KC_C)) {
100                 mTimeTillNextUpdate = 0.25;
101                 UpdateSpeedFactor(mSpeedFactor * 2);
102             }
103             if (mKeyboard->isKeyDown (OIS::KC_Z)) {
104                 mTimeTillNextUpdate = 1;
105                 UpdateSpeedFactor(mSpeedFactor * -1);
106             }
107         }
108 
109         return true;
110     }
111 };
112 
113 class CaelumSampleApplication : public ExampleApplication
114 {
115 public:
CaelumSampleApplication()116     CaelumSampleApplication ()
117     {
118     }
119 
~CaelumSampleApplication()120     ~CaelumSampleApplication ()
121     {
122     }
123 
createFrameListener()124     void createFrameListener ()
125     {
126         mFrameListener = new CaelumSampleFrameListener (mWindow, mCamera);
127         mRoot->addFrameListener (mFrameListener);
128     }
129 
chooseSceneManager(void)130     virtual void chooseSceneManager(void)
131     {
132         mSceneMgr = mRoot->createSceneManager("TerrainSceneManager");
133     }
134 
createCamera(void)135     virtual void createCamera(void)
136     {
137         // Create the camera
138         mCamera = mSceneMgr->createCamera("PlayerCam");
139 
140         // Start the camera on a hill in the middle of the terrain
141         // looking towards Z+ (north).
142         // Sun should rise in the east(left) and set in the west(right).
143         mCamera->setPosition (Vector3 (775, 100, 997));
144         mCamera->lookAt (Vector3 (775, 100, 1000));
145 
146         mCamera->setNearClipDistance(5);
147 
148         // Set camera clip distances. It's important to test with
149         // an infinite clip distance.
150         mCamera->setFarClipDistance(0);
151         //mCamera->setFarClipDistance(10000);
152     }
153 
createScene()154     void createScene ()
155     {
156         // Put some terrain in the scene
157         std::string terrain_cfg("CaelumDemoTerrain.cfg");
158 #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
159         terrain_cfg = mResourcePath + terrain_cfg;
160 #endif
161         mSceneMgr->setWorldGeometry (terrain_cfg);
162     }
163 };
164