2 * Copyright 2008 Jacek Caban
3 * Copyright 2008 Piotr Caban
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "application.h"
23 class ExitListener
: public FrameListener
{
25 ExitListener(OIS::Keyboard
*keyboard
, OIS::Mouse
*mouse
, SceneManager
*mgr
) {
30 mCamera
= mgr
->getCamera("Camera");
36 bool frameStarted(const FrameEvent
&evt
) {
39 mToggle
-= evt
.timeSinceLastFrame
;
41 if(mToggle
<0.0f
&& mKeyboard
->isKeyDown(OIS::KC_C
)) {
43 mCamera
->getParentSceneNode()->detachObject(mCamera
);
44 mCameraLocation
= !mCameraLocation
;
45 if(mCameraLocation
) mSceneManager
->getSceneNode("CameraTableNode")->attachObject(mCamera
);
46 else mSceneManager
->getSceneNode("CameraWhiteNode")->attachObject(mCamera
);
48 else if(mToggle
<0.0f
&& mKeyboard
->isKeyDown(OIS::KC_1
)) {
50 mCamera
->getParentSceneNode()->detachObject(mCamera
);
51 mSceneManager
->getSceneNode("CameraWhiteNode")->attachObject(mCamera
);
54 else if(mToggle
<0.0f
&& mKeyboard
->isKeyDown(OIS::KC_2
)) {
56 mCamera
->getParentSceneNode()->detachObject(mCamera
);
57 mSceneManager
->getSceneNode("CameraTableNode")->attachObject(mCamera
);
60 else if(mToggle
<0.0f
&& mKeyboard
->isKeyDown(OIS::KC_S
)) {
62 mSceneManager
->getLight("Light1")->setCastShadows(!mSceneManager
->getLight("Light1")->getCastShadows());
64 else if(mToggle
<0.0f
&& mKeyboard
->isKeyDown(OIS::KC_L
)) {
66 mSceneManager
->getLight("Light1")->setVisible(!mSceneManager
->getLight("Light1")->getVisible());
69 #define ROTATION_SPEED 0.5f
70 if(mKeyboard
->isKeyDown(OIS::KC_UP
) && vertical
<160) {
71 mSceneManager
->getSceneNode("CameraLookAtTableNode")->rotate(Vector3::UNIT_Y
, -Degree(ROTATION_SPEED
));
72 mSceneManager
->getSceneNode("CameraLookAtWhiteNode")->rotate(Vector3::UNIT_Y
, -Degree(ROTATION_SPEED
));
75 else if(mKeyboard
->isKeyDown(OIS::KC_DOWN
) && vertical
>20) {
76 mSceneManager
->getSceneNode("CameraLookAtTableNode")->rotate(Vector3::UNIT_Y
, Degree(ROTATION_SPEED
));
77 mSceneManager
->getSceneNode("CameraLookAtWhiteNode")->rotate(Vector3::UNIT_Y
, Degree(ROTATION_SPEED
));
80 else if(mKeyboard
->isKeyDown(OIS::KC_LEFT
)) {
81 if(mCameraLocation
) mSceneManager
->getSceneNode("CameraTableRotationNode")->rotate(Vector3::UNIT_Z
, -Degree(ROTATION_SPEED
));
82 else mSceneManager
->getSceneNode("CameraWhiteRotationNode")->rotate(Vector3::UNIT_Z
, -Degree(ROTATION_SPEED
));
84 else if(mKeyboard
->isKeyDown(OIS::KC_RIGHT
)) {
85 if(mCameraLocation
) mSceneManager
->getSceneNode("CameraTableRotationNode")->rotate(Vector3::UNIT_Z
, Degree(ROTATION_SPEED
));
86 else mSceneManager
->getSceneNode("CameraWhiteRotationNode")->rotate(Vector3::UNIT_Z
, Degree(ROTATION_SPEED
));
89 #define ZOOM_SPEED 0.025f
92 if(mKeyboard
->isKeyDown(OIS::KC_EQUALS
) && zoom
>ZOOM_MIN
) {
94 mSceneManager
->getSceneNode("CameraWhiteNode")->setPosition(50.0f
*zoom
, 0.0f
, 0.0f
);
95 mSceneManager
->getSceneNode("CameraTableNode")->setPosition(70.0f
*zoom
, 0.0f
, 0.0f
);
97 else if(mKeyboard
->isKeyDown(OIS::KC_MINUS
) && zoom
<ZOOM_MAX
) {
99 mSceneManager
->getSceneNode("CameraWhiteNode")->setPosition(50.0f
*zoom
, 0.0f
, 0.0f
);
100 mSceneManager
->getSceneNode("CameraTableNode")->setPosition(70.0f
*zoom
, 0.0f
, 0.0f
);
103 return !mKeyboard
->isKeyDown(OIS::KC_ESCAPE
);
107 OIS::Keyboard
*mKeyboard
;
111 SceneManager
*mSceneManager
;
117 void Application::go() {
121 createRenderWindow();
122 initializeResourceGroups();
126 createFrameListener();
130 Application::~Application() {
131 mInputManager
->destroyInputObject(mKeyboard
);
132 mInputManager
->destroyInputObject(mMouse
);
133 OIS::InputManager::destroyInputSystem(mInputManager
);
142 void Application::createRoot() {
146 void Application::defineResources() {
147 String secName
, typeName
, archName
;
149 cf
.load("resources.cfg");
151 ConfigFile::SectionIterator seci
= cf
.getSectionIterator();
152 while (seci
.hasMoreElements()) {
153 secName
= seci
.peekNextKey();
154 ConfigFile::SettingsMultiMap
*settings
= seci
.getNext();
155 ConfigFile::SettingsMultiMap::iterator i
;
156 for (i
= settings
->begin(); i
!= settings
->end(); ++i
) {
158 archName
= i
->second
;
159 ResourceGroupManager::getSingleton().addResourceLocation(archName
, typeName
, secName
);
164 void Application::setupRenderSystem() {
165 if(!mRoot
->showConfigDialog())
166 throw Exception(52, "User canceled the config dialog!", "Application::setupRenderSystem()");
169 void Application::createRenderWindow() {
170 mRoot
->initialise(true, "Pool Simulation Game");
173 void Application::initializeResourceGroups() {
174 TextureManager::getSingleton().setDefaultNumMipmaps(5);
175 ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
178 void Application::setupScene() {
179 SceneManager
*mgr
= mRoot
->createSceneManager(ST_GENERIC
, "SceneManager");
180 Camera
*cam
= mgr
->createCamera("Camera");
181 Viewport
*vp
= mRoot
->getAutoCreatedWindow()->addViewport(cam
);
183 cam
->lookAt(Vector3(-1.0f
, 0.0f
, 0.0f
));
184 cam
->setNearClipDistance(0.01f
);
185 cam
->roll(Degree(90));
186 mgr
->setAmbientLight(ColourValue(1.0f
, 1.0f
, 1.0f
));
188 mgr
->setShadowTechnique(SHADOWTYPE_STENCIL_ADDITIVE
);//SHADOWTYPE_TEXTURE_MODULATIVE);
189 mgr
->setShadowColour(ColourValue(0.5f
,0.5f
,0.5f
));
191 /* Rozmiar stolu 60x30 */
192 Entity
*table
= mgr
->createEntity("PoolTable", "table.mesh");
193 table
->setCastShadows(false);
194 Entity
*white
= mgr
->createEntity("White", "white.mesh");
195 Entity
*ball1
= mgr
->createEntity("Ball1", "ball1.mesh");
196 Entity
*ball2
= mgr
->createEntity("Ball2", "ball2.mesh");
197 Entity
*ball3
= mgr
->createEntity("Ball3", "ball3.mesh");
198 Entity
*ball4
= mgr
->createEntity("Ball4", "ball4.mesh");
199 Entity
*ball5
= mgr
->createEntity("Ball5", "ball5.mesh");
200 Entity
*ball6
= mgr
->createEntity("Ball6", "ball6.mesh");
201 Entity
*ball7
= mgr
->createEntity("Ball7", "ball7.mesh");
202 Entity
*ball8
= mgr
->createEntity("Ball8", "ball8.mesh");
203 Entity
*ball9
= mgr
->createEntity("Ball9", "ball9.mesh");
204 Entity
*ball10
= mgr
->createEntity("Ball10", "ball10.mesh");
205 Entity
*ball11
= mgr
->createEntity("Ball11", "ball11.mesh");
206 Entity
*ball12
= mgr
->createEntity("Ball12", "ball12.mesh");
207 Entity
*ball13
= mgr
->createEntity("Ball13", "ball13.mesh");
208 Entity
*ball14
= mgr
->createEntity("Ball14", "ball14.mesh");
209 Entity
*ball15
= mgr
->createEntity("Ball15", "ball15.mesh");
211 SceneNode
*tableNode
= mgr
->getRootSceneNode()->createChildSceneNode("TableNode");
212 SceneNode
*tableCenterNode
= tableNode
->createChildSceneNode("TableCenterNode", Vector3(1.1f
, -2.0f
, -20.0f
));
213 SceneNode
*whiteNode
= mgr
->getRootSceneNode()->createChildSceneNode("WhiteNode", Vector3(0.0f
, 0.0f
, 0.6f
));
214 SceneNode
*whiteCenterNode
= whiteNode
->createChildSceneNode("WhiteCenterNode", Vector3(-12.96f
, -4.2f
, -20.6f
));
215 SceneNode
*cameraTableRotationNode
= tableNode
->createChildSceneNode("CameraTableRotationNode", Vector3(0.0f
, 0.0f
, 1.0f
));
216 SceneNode
*cameraWhiteRotationNode
= whiteNode
->createChildSceneNode("CameraWhiteRotationNode", Vector3(0.0f
, 0.0f
, 1.0f
));
217 SceneNode
*cameraLookAtTableNode
= cameraTableRotationNode
->createChildSceneNode("CameraLookAtTableNode");
218 SceneNode
*cameraLookAtWhiteNode
= cameraWhiteRotationNode
->createChildSceneNode("CameraLookAtWhiteNode");
219 SceneNode
*cameraWhiteNode
= cameraLookAtWhiteNode
->createChildSceneNode("CameraWhiteNode", Vector3(50.0f
, 0.0f
, 0.0f
));
220 SceneNode
*cameraTableNode
= cameraLookAtTableNode
->createChildSceneNode("CameraTableNode", Vector3(70.0f
, 0.0f
, 0.0f
));
221 cameraLookAtTableNode
->rotate(Vector3::UNIT_Y
, -Degree(10));
222 cameraLookAtWhiteNode
->rotate(Vector3::UNIT_Y
, -Degree(10));
223 tableCenterNode
->attachObject(table
);
224 whiteCenterNode
->attachObject(white
);
225 cameraWhiteNode
->attachObject(cam
);
227 SceneNode
*ball1Node
= mgr
->getRootSceneNode()->createChildSceneNode("Ball1Node", Vector3(0.0f
, 0.0f
, 0.6f
));
228 SceneNode
*ball1RotationNode
= ball1Node
->createChildSceneNode("Ball1RotationNode");
229 SceneNode
*ball1CenterNode
= ball1RotationNode
->createChildSceneNode("Ball1CenterNode", Vector3(18.7f
, -2.15f
, -20.6f
));
230 ball1CenterNode
->attachObject(ball1
);
232 SceneNode
*ball2Node
= mgr
->getRootSceneNode()->createChildSceneNode("Ball2Node", Vector3(0.0f
, 0.0f
, 0.6f
));
233 SceneNode
*ball2RotationNode
= ball2Node
->createChildSceneNode("Ball2RotationNode");
234 SceneNode
*ball2CenterNode
= ball2RotationNode
->createChildSceneNode("Ball2CenterNode", Vector3(25.82f
, -0.33f
, -20.6f
));
235 ball2CenterNode
->attachObject(ball2
);
237 SceneNode
*ball3Node
= mgr
->getRootSceneNode()->createChildSceneNode("Ball3Node", Vector3(0.0f
, 0.0f
, 0.6f
));
238 SceneNode
*ball3RotationNode
= ball3Node
->createChildSceneNode("Ball3RotationNode");
239 SceneNode
*ball3CenterNode
= ball3RotationNode
->createChildSceneNode("Ball3CenterNode", Vector3(20.40f
, -1.17f
, -20.6f
));
240 ball3CenterNode
->attachObject(ball3
);
242 SceneNode
*ball4Node
= mgr
->getRootSceneNode()->createChildSceneNode("Ball4Node", Vector3(0.0f
, 0.0f
, 0.6f
));
243 SceneNode
*ball4RotationNode
= ball4Node
->createChildSceneNode("Ball4RotationNode");
244 SceneNode
*ball4CenterNode
= ball4RotationNode
->createChildSceneNode("Ball4CenterNode", Vector3(22.08f
, -4.30f
, -20.6f
));
245 ball4CenterNode
->attachObject(ball4
);
247 SceneNode
*ball5Node
= mgr
->getRootSceneNode()->createChildSceneNode("Ball5Node", Vector3(0.0f
, 0.0f
, 0.6f
));
248 SceneNode
*ball5RotationNode
= ball5Node
->createChildSceneNode("Ball5RotationNode");
249 SceneNode
*ball5CenterNode
= ball5RotationNode
->createChildSceneNode("Ball5CenterNode", Vector3(23.83f
, -5.37f
, -20.6f
));
250 ball5CenterNode
->attachObject(ball5
);
252 SceneNode
*ball6Node
= mgr
->getRootSceneNode()->createChildSceneNode("Ball6Node", Vector3(0.0f
, 0.0f
, 0.6f
));
253 SceneNode
*ball6RotationNode
= ball6Node
->createChildSceneNode("Ball6RotationNode");
254 SceneNode
*ball6CenterNode
= ball6RotationNode
->createChildSceneNode("Ball6CenterNode", Vector3(25.74f
, -4.27f
, -20.6f
));
255 ball6CenterNode
->attachObject(ball6
);
257 SceneNode
*ball7Node
= mgr
->getRootSceneNode()->createChildSceneNode("Ball7Node", Vector3(0.0f
, 0.0f
, 0.6f
));
258 SceneNode
*ball7RotationNode
= ball7Node
->createChildSceneNode("Ball7RotationNode");
259 SceneNode
*ball7CenterNode
= ball7RotationNode
->createChildSceneNode("Ball7CenterNode", Vector3(23.83f
, 0.58f
, -20.6f
));
260 ball7CenterNode
->attachObject(ball7
);
262 SceneNode
*ball8Node
= mgr
->getRootSceneNode()->createChildSceneNode("Ball8Node", Vector3(0.0f
, 0.0f
, 0.6f
));
263 SceneNode
*ball8RotationNode
= ball8Node
->createChildSceneNode("Ball8RotationNode");
264 SceneNode
*ball8CenterNode
= ball8RotationNode
->createChildSceneNode("Ball8CenterNode", Vector3(22.15f
, -2.36f
, -20.6f
));
265 ball8CenterNode
->attachObject(ball8
);
267 SceneNode
*ball9Node
= mgr
->getRootSceneNode()->createChildSceneNode("Ball9Node", Vector3(0.0f
, 0.0f
, 0.6f
));
268 SceneNode
*ball9RotationNode
= ball9Node
->createChildSceneNode("Ball9RotationNode");
269 SceneNode
*ball9CenterNode
= ball9RotationNode
->createChildSceneNode("Ball9CenterNode", Vector3(25.75f
, -2.33f
, -20.6f
));
270 ball9CenterNode
->attachObject(ball9
);
272 SceneNode
*ball10Node
= mgr
->getRootSceneNode()->createChildSceneNode("Ball10Node", Vector3(0.0f
, 0.0f
, 0.6f
));
273 SceneNode
*ball10RotationNode
= ball10Node
->createChildSceneNode("Ball10RotationNode");
274 SceneNode
*ball10CenterNode
= ball10RotationNode
->createChildSceneNode("Ball10CenterNode", Vector3(23.9f
, -1.38f
, -20.6f
));
275 ball10CenterNode
->attachObject(ball10
);
277 SceneNode
*ball11Node
= mgr
->getRootSceneNode()->createChildSceneNode("Ball11Node", Vector3(0.0f
, 0.0f
, 0.6f
));
278 SceneNode
*ball11RotationNode
= ball11Node
->createChildSceneNode("Ball11RotationNode");
279 SceneNode
*ball11CenterNode
= ball11RotationNode
->createChildSceneNode("Ball11CenterNode", Vector3(22.12f
, -0.4f
, -20.6f
));
280 ball11CenterNode
->attachObject(ball11
);
282 SceneNode
*ball12Node
= mgr
->getRootSceneNode()->createChildSceneNode("Ball12Node", Vector3(0.0f
, 0.0f
, 0.6f
));
283 SceneNode
*ball12RotationNode
= ball12Node
->createChildSceneNode("Ball12RotationNode");
284 SceneNode
*ball12CenterNode
= ball12RotationNode
->createChildSceneNode("Ball12CenterNode", Vector3(25.59f
, -6.18f
, -20.6f
));
285 ball12CenterNode
->attachObject(ball12
);
287 SceneNode
*ball13Node
= mgr
->getRootSceneNode()->createChildSceneNode("Ball13Node", Vector3(0.0f
, 0.0f
, 0.6f
));
288 SceneNode
*ball13RotationNode
= ball13Node
->createChildSceneNode("Ball13RotationNode");
289 SceneNode
*ball13CenterNode
= ball13RotationNode
->createChildSceneNode("Ball13CenterNode", Vector3(25.66f
, 1.61f
, -20.6f
));
290 ball13CenterNode
->attachObject(ball13
);
292 SceneNode
*ball14Node
= mgr
->getRootSceneNode()->createChildSceneNode("Ball14Node", Vector3(0.0f
, 0.0f
, 0.6f
));
293 SceneNode
*ball14RotationNode
= ball14Node
->createChildSceneNode("Ball14RotationNode");
294 SceneNode
*ball14CenterNode
= ball14RotationNode
->createChildSceneNode("Ball14CenterNode", Vector3(23.86f
, -3.42f
, -20.6f
));
295 ball14CenterNode
->attachObject(ball14
);
297 SceneNode
*ball15Node
= mgr
->getRootSceneNode()->createChildSceneNode("Ball15Node", Vector3(0.0f
, 0.0f
, 0.6f
));
298 SceneNode
*ball15RotationNode
= ball15Node
->createChildSceneNode("Ball15RotationNode");
299 SceneNode
*ball15CenterNode
= ball15RotationNode
->createChildSceneNode("Ball15CenterNode", Vector3(20.37f
, -3.19f
, -20.6f
));
300 ball15CenterNode
->attachObject(ball15
);
302 Light
* light
= mgr
->createLight("Light1");
303 light
->setType(Light::LT_SPOTLIGHT
);
304 light
->setDiffuseColour(ColourValue(0.25f
,0.25f
,0.0f
));
305 light
->setSpecularColour(ColourValue(0.25f
,0.25f
,0.0f
));
306 light
->setSpotlightRange(Degree(60), Degree(70));
307 light
->setDirection(Vector3::NEGATIVE_UNIT_Z
);
308 SceneNode
*lightNode
= mgr
->getRootSceneNode()->createChildSceneNode("LightNode", Vector3(0.0f
, 0.0f
, 30.0f
));
309 lightNode
->attachObject(light
);
310 light
->setVisible(false);
312 /* Place white ball on the start dot */
313 whiteNode
->translate(Vector3(16.0f
, 0.0f
, 0.0f
));
316 void Application::setupInputSystem() {
317 size_t windowHnd
= 0;
318 std::ostringstream windowHndStr
;
320 RenderWindow
*win
= mRoot
->getAutoCreatedWindow();
322 win
->getCustomAttribute("WINDOW", &windowHnd
);
323 windowHndStr
<< windowHnd
;
324 pl
.insert(std::make_pair(std::string("WINDOW"), windowHndStr
.str()));
325 mInputManager
= OIS::InputManager::createInputSystem(pl
);
328 mKeyboard
= static_cast<OIS::Keyboard
*>(mInputManager
->createInputObject(OIS::OISKeyboard
, false));
329 mMouse
= static_cast<OIS::Mouse
*>(mInputManager
->createInputObject(OIS::OISMouse
, false));
331 catch (const OIS::Exception
&e
) {
332 throw Exception(42, e
.eText
, "Application::setupInputSystem");
336 void Application::setupCEGUI() {
337 SceneManager
*mgr
= mRoot
->getSceneManager("SceneManager");
338 RenderWindow
*win
= mRoot
->getAutoCreatedWindow();
340 mRenderer
= new CEGUI::OgreCEGUIRenderer(win
, Ogre::RENDER_QUEUE_OVERLAY
, false, 3000, mgr
);
341 mSystem
= new CEGUI::System(mRenderer
);
344 void Application::createFrameListener() {
345 mListener
= new ExitListener(mKeyboard
, mMouse
, mRoot
->getSceneManager("SceneManager"));
346 mRoot
->addFrameListener(mListener
);
349 void Application::startRenderLoop() {
350 mRoot
->startRendering();