Remove power displaying from application.cpp
[psg.git] / src / application.cpp
blobb752b0ff9e9534a7008f0c6992a4bfc29e04d539
1 /*
2 * Copyright 2008 Jacek Caban
3 * Copyright 2008 Piotr Caban
4 * Copyright 2008 Jarek Sobiecki
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "rules.h"
21 #include "ui.h"
23 #include <Ogre.h>
24 #include <OIS/OIS.h>
25 #include <CEGUI/CEGUI.h>
26 #include <OgreCEGUIRenderer.h>
28 using namespace Ogre;
30 /**
31 * This is the main program class. It is created when the program is run and destroyed when the program quits.
33 class Application {
34 public:
35 void go();
36 ~Application();
38 private:
39 Ogre::Root *mRoot;
40 OIS::Keyboard *mKeyboard;
41 OIS::Mouse *mMouse;
42 OIS::InputManager *mInputManager;
43 CEGUI::OgreCEGUIRenderer *mRenderer;
44 CEGUI::System *mSystem;
45 UI *mUI;
46 bool mContinue;
47 CEGUI::WindowManager *mWindowManager;
48 Ogre::FrameListener *mListener;
49 MenuFrameListener *mMenuListener;
50 PoolGame *game;
52 void createRoot();
53 void defineResources();
54 void setupRenderSystem();
55 void createRenderWindow();
56 void initializeResourceGroups();
57 void setupScene();
58 void setupInputSystem();
59 void setupCEGUI();
60 void createFrameListener();
61 void startRenderLoop();
64 /**
65 * This is the main game listener. It is responsible for interaction with user during the game (not in menu).
66 * More informations about keys/how to play can be found in documentation.pdf.
67 * This class interacts with PoolGame class (rules) and UI (for menu integration).
69 class GameListener : public FrameListener, public OIS::MouseListener {
70 private:
71 Real getMoveScale();
73 static const Real ROTATION_SPEED = 10.0;
74 static const Real ZOOM_SPEED = 0.2;
75 static const Real MOVE_SPEED = 0.1;
76 static const Real ZOOM_MIN = 0.1;
77 static const Real ZOOM_MAX = 2.0;
79 public:
80 /**
81 * GameListener class constructor.
83 GameListener(OIS::Keyboard *keyboard, OIS::Mouse *mouse, SceneManager *mgr, PoolGame *sGame, UI *sUI, MouseListener *mouseListener) {
84 mUI = sUI;
85 mKeyboard = keyboard;
86 mMouse = mouse;
87 mToggle = 0.0f;
88 mSceneManager = mgr;
89 game = sGame;
90 mCamera = mgr->getCamera("Camera");
91 mCameraLocation = 0;
92 vertical = 10.0f; horizontal = 0.0f;
93 ballV = 0.0f; ballH = 0.0f;
94 zoom = 1.0f;
95 mMenuListener = mouseListener;
96 mMousePressed = false;
97 mLastWhellPosition = 0;
98 mLastXPosition = 0;
99 mLastYPosition = 0;
103 * Implementation of MouseListener::MouseMoved event.
104 * It translates OIS event to CEGUI event. If right mouse button is pressed the function
105 * rotates whole scene. It's responsible for handling mouse whell move too.
107 bool mouseMoved(const OIS::MouseEvent &arg)
109 Real deg = 0.0;
110 int sgn = 1;
111 int delta_zoom = arg.state.Z.abs - mLastWhellPosition;
112 int delta_x = arg.state.X.abs - mLastXPosition;
113 int delta_y = arg.state.Y.abs - mLastYPosition;
114 //first, we have to handle mouse whell move
115 if (delta_zoom < 0 && (zoom + (delta_zoom)*mTimeSinceLastFrame*0.2) > ZOOM_MIN ||
116 delta_zoom > 0 && (zoom + (delta_zoom)*mTimeSinceLastFrame*0.2) < ZOOM_MAX)
118 zoom += (delta_zoom)*mTimeSinceLastFrame*0.2;
120 mSceneManager->getSceneNode("CameraWhiteNode")->setPosition(50.0f*zoom, 0.0f, 0.0f);
121 mSceneManager->getSceneNode("CameraTableNode")->setPosition(70.0f*zoom, 0.0f, 0.0f);
122 mSceneManager->getSceneNode("CrossNode")->setScale(Vector3(0.05f*zoom, 0.05f*zoom, 0.05f*zoom));
123 mLastWhellPosition = arg.state.Z.abs;
125 if (!mMousePressed)
127 CEGUI::System::getSingleton().injectMousePosition(arg.state.X.abs*2, arg.state.Y.abs*2);
129 else
132 //TODO: ARCBALL
133 if((delta_y < 0 && vertical > 10.0f) ||
134 (delta_y > 0 && vertical < 80.0f)) {
135 sgn = delta_y > 0 ? -1.0 : +1.0;
136 deg = 360.0*fabs(delta_y)*mTimeSinceLastFrame;
137 mSceneManager->getSceneNode("CameraLookAtTableNode")->rotate(Vector3::UNIT_Y, sgn * Degree(deg));
138 mSceneManager->getSceneNode("CameraLookAtWhiteNode")->rotate(Vector3::UNIT_Y, sgn * Degree(deg));
139 mSceneManager->getSceneNode("CrossVerticalRotationNode")->rotate(Vector3::UNIT_Y, sgn * Degree(deg));
140 vertical += -1.0 * sgn * deg;
143 sgn = delta_x > 0 ? -1.0 : +1.0;
145 deg = 360.0*fabs(delta_x)*mTimeSinceLastFrame;
146 if(mCameraLocation) mSceneManager->getSceneNode("CameraTableRotationNode")->rotate(Vector3::UNIT_Z, sgn *Degree(deg));
147 else {
148 mSceneManager->getSceneNode("CameraWhiteRotationNode")->rotate(Vector3::UNIT_Z, sgn * Degree(deg));
149 horizontal += sgn * deg;
154 mLastYPosition = arg.state.Y.abs;
155 mLastXPosition = arg.state.X.abs;
157 return true;
162 * This method is implementation of MouseListener::MousePressed method.
163 * It detects right mouse button press and change mouse cursor.
164 * It also translate OIS click event to CEGUI event
166 bool mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
168 switch (id)
170 case OIS::MB_Button3:
171 break;
172 case OIS::MB_Button4:
173 break;
174 case OIS::MB_Button5:
175 break;
176 case OIS::MB_Button6:
177 break;
178 case OIS::MB_Button7:
179 break;
180 case OIS::MB_Right:
181 CEGUI::System::getSingletonPtr()->setDefaultMouseCursor((CEGUI::utf8*) "PSG",
182 (CEGUI::utf8*) "MouseMoveCursor");
184 mMousePressed = true;
186 break;
187 case OIS::MB_Middle:
188 break;
190 CEGUI::System::getSingleton().injectMouseButtonDown(
191 MenuFrameListener::convertButton(id));
192 return true;
197 * It's implementation of MouseListener::MouseReleased method
198 * It't responsible for changing view of cursor
200 bool mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
202 switch (id)
204 case OIS::MB_Button3:
205 break;
206 case OIS::MB_Button4:
207 break;
208 case OIS::MB_Button5:
209 break;
210 case OIS::MB_Button6:
211 break;
212 case OIS::MB_Button7:
213 break;
214 case OIS::MB_Right:
215 mMousePressed = false;
216 CEGUI::System::getSingletonPtr()->setDefaultMouseCursor((CEGUI::utf8*) "PSG",
217 (CEGUI::utf8*) "MouseArrow");
218 break;
219 case OIS::MB_Middle:
220 break;
222 CEGUI::System::getSingleton().injectMouseButtonUp(
223 MenuFrameListener::convertButton(id));
224 return true;
226 return true;
227 return false;
231 * This function is started every time before the frame is rendered. It checks if the user is pressing any key.
232 * It is responsible for ball placing as well.
234 * frameStarted function do following things:
235 * - rotates camera
236 * - zomms camera in/out
237 * - sets current camera
238 * - places withe ball after a faul
239 * - changes camera
240 * - switches on/off lights and shadows
241 * - sets balls orientation and position
243 bool frameStarted(const FrameEvent &evt) {
244 Real deg;
245 mTimeSinceLastFrame = evt.timeSinceLastFrame;
247 if(mUI->getScene() != M_DISABLED)
249 mMouse->setEventCallback(mMenuListener);
250 return true;
252 mMouse->setEventCallback(this);
253 mKeyboard->capture();
254 mMouse->capture();
256 mToggle -= evt.timeSinceLastFrame;
258 if(mToggle<0.0f && mKeyboard->isKeyDown(OIS::KC_C)) {
259 mToggle = 0.5f;
260 mCamera->getParentSceneNode()->detachObject(mCamera);
261 mCameraLocation = !mCameraLocation;
262 if(mCameraLocation) mSceneManager->getSceneNode("CameraTableNode")->attachObject(mCamera);
263 else mSceneManager->getSceneNode("CameraWhiteNode")->attachObject(mCamera);
265 else if(mToggle<0.0f && mKeyboard->isKeyDown(OIS::KC_1)) {
266 mToggle = 0.5f;
267 mCamera->getParentSceneNode()->detachObject(mCamera);
268 mSceneManager->getSceneNode("CameraWhiteNode")->attachObject(mCamera);
269 mCameraLocation = 0;
271 else if(mToggle<0.0f && mKeyboard->isKeyDown(OIS::KC_2)) {
272 mToggle = 0.5f;
273 mCamera->getParentSceneNode()->detachObject(mCamera);
274 mSceneManager->getSceneNode("CameraTableNode")->attachObject(mCamera);
275 mCameraLocation = 1;
277 else if(!game->isBallOnTable(0) || (mToggle<0.0f && mKeyboard->isKeyDown(OIS::KC_3))) {
278 mToggle = 0.5f;
279 mCamera->getParentSceneNode()->detachObject(mCamera);
280 mSceneManager->getSceneNode("CameraTableNode")->attachObject(mCamera);
281 mCameraLocation = 1;
282 mSceneManager->getSceneNode("CameraLookAtTableNode")->rotate(Vector3::UNIT_Y, Degree(vertical-80.0));
283 mSceneManager->getSceneNode("CameraLookAtWhiteNode")->rotate(Vector3::UNIT_Y, Degree(vertical-80.0));
284 mSceneManager->getSceneNode("CrossVerticalRotationNode")->rotate(Vector3::UNIT_Y, -Degree(vertical-80.0));
285 vertical += 80.0-vertical;
286 zoom = (ZOOM_MAX+ZOOM_MIN)/2.0;
287 mSceneManager->getSceneNode("CameraWhiteNode")->setPosition(50.0f*zoom, 0.0f, 0.0f);
288 mSceneManager->getSceneNode("CameraTableNode")->setPosition(70.0f*zoom, 0.0f, 0.0f);
289 mSceneManager->getSceneNode("CrossNode")->setScale(Vector3(0.05f*zoom, 0.05f*zoom, 0.05f*zoom));
291 else if(mToggle<0.0f && mKeyboard->isKeyDown(OIS::KC_K)) {
292 mToggle = 0.5f;
293 mSceneManager->getLight("Light1")->setCastShadows(!mSceneManager->getLight("Light1")->getCastShadows());
294 mSceneManager->getLight("Light2")->setCastShadows(!mSceneManager->getLight("Light2")->getCastShadows());
295 mSceneManager->getLight("Light3")->setCastShadows(!mSceneManager->getLight("Light3")->getCastShadows());
297 else if(mToggle<0.0f && mKeyboard->isKeyDown(OIS::KC_L)) {
298 mToggle = 0.5f;
299 mSceneManager->getLight("Light1")->setVisible(!mSceneManager->getLight("Light1")->getVisible());
300 mSceneManager->getLight("Light2")->setVisible(!mSceneManager->getLight("Light2")->getVisible());
301 mSceneManager->getLight("Light3")->setVisible(!mSceneManager->getLight("Light3")->getVisible());
304 if(mKeyboard->isKeyDown(OIS::KC_DOWN) && vertical<80.0f) {
305 deg = ROTATION_SPEED*evt.timeSinceLastFrame*getMoveScale();
306 mSceneManager->getSceneNode("CameraLookAtTableNode")->rotate(Vector3::UNIT_Y, -Degree(deg));
307 mSceneManager->getSceneNode("CameraLookAtWhiteNode")->rotate(Vector3::UNIT_Y, -Degree(deg));
308 mSceneManager->getSceneNode("CrossVerticalRotationNode")->rotate(Vector3::UNIT_Y, -Degree(deg));
309 vertical += deg;
311 else if(mKeyboard->isKeyDown(OIS::KC_UP) && vertical>10.0f) {
312 deg = ROTATION_SPEED*evt.timeSinceLastFrame*getMoveScale();;
313 mSceneManager->getSceneNode("CameraLookAtTableNode")->rotate(Vector3::UNIT_Y, Degree(deg));
314 mSceneManager->getSceneNode("CameraLookAtWhiteNode")->rotate(Vector3::UNIT_Y, Degree(deg));
315 mSceneManager->getSceneNode("CrossVerticalRotationNode")->rotate(Vector3::UNIT_Y, Degree(deg));
316 vertical -= deg;
318 else if(mKeyboard->isKeyDown(OIS::KC_RIGHT)) {
319 deg = ROTATION_SPEED*evt.timeSinceLastFrame*getMoveScale();
320 if(mCameraLocation) mSceneManager->getSceneNode("CameraTableRotationNode")->rotate(Vector3::UNIT_Z, -Degree(deg));
321 else {
322 mSceneManager->getSceneNode("CameraWhiteRotationNode")->rotate(Vector3::UNIT_Z, -Degree(deg));
323 horizontal -= deg;
326 else if(mKeyboard->isKeyDown(OIS::KC_LEFT)) {
327 deg = ROTATION_SPEED*evt.timeSinceLastFrame*getMoveScale();
328 if(mCameraLocation) mSceneManager->getSceneNode("CameraTableRotationNode")->rotate(Vector3::UNIT_Z, Degree(deg));
329 else {
330 mSceneManager->getSceneNode("CameraWhiteRotationNode")->rotate(Vector3::UNIT_Z, Degree(deg));
331 horizontal += deg;
334 else if(mKeyboard->isKeyDown(OIS::KC_W) && !mCameraLocation && ballV<40.0f) {
335 deg = ROTATION_SPEED*evt.timeSinceLastFrame*getMoveScale();
336 mSceneManager->getSceneNode("CrossVerticalRotationNode")->rotate(Vector3::UNIT_Y, -Degree(deg));
337 ballV += deg;
339 else if(mKeyboard->isKeyDown(OIS::KC_S) && !mCameraLocation && ballV>-40.0f) {
340 deg = ROTATION_SPEED*evt.timeSinceLastFrame*getMoveScale();
341 mSceneManager->getSceneNode("CrossVerticalRotationNode")->rotate(Vector3::UNIT_Y, Degree(deg));
342 ballV -= deg;
344 else if(mKeyboard->isKeyDown(OIS::KC_A) && !mCameraLocation && ballH<40.0f) {
345 deg = ROTATION_SPEED*evt.timeSinceLastFrame*getMoveScale();
346 mSceneManager->getSceneNode("CrossRotationNode")->rotate(Vector3::UNIT_Z, -Degree(deg));
347 ballH += deg;
349 else if(mKeyboard->isKeyDown(OIS::KC_D) && !mCameraLocation && ballH>-40.0f) {
350 deg = ROTATION_SPEED*evt.timeSinceLastFrame*getMoveScale();
351 mSceneManager->getSceneNode("CrossRotationNode")->rotate(Vector3::UNIT_Z, Degree(deg));
352 ballH -= deg;
355 if(mKeyboard->isKeyDown(OIS::KC_EQUALS) && zoom>ZOOM_MIN) {
356 zoom -= ZOOM_SPEED*evt.timeSinceLastFrame;
357 mSceneManager->getSceneNode("CameraWhiteNode")->setPosition(50.0f*zoom, 0.0f, 0.0f);
358 mSceneManager->getSceneNode("CameraTableNode")->setPosition(70.0f*zoom, 0.0f, 0.0f);
359 mSceneManager->getSceneNode("CrossNode")->setScale(Vector3(0.05f*zoom, 0.05f*zoom, 0.05f*zoom));
361 else if(mKeyboard->isKeyDown(OIS::KC_MINUS) && zoom<ZOOM_MAX) {
362 zoom += ZOOM_SPEED*evt.timeSinceLastFrame;
363 mSceneManager->getSceneNode("CameraWhiteNode")->setPosition(50.0f*zoom, 0.0f, 0.0f);
364 mSceneManager->getSceneNode("CameraTableNode")->setPosition(70.0f*zoom, 0.0f, 0.0f);
365 mSceneManager->getSceneNode("CrossNode")->setScale(Vector3(0.05f*zoom, 0.05f*zoom, 0.05f*zoom));
367 else if (mKeyboard->isKeyDown(OIS::KC_ESCAPE)) {
368 mUI->setScene(M_IN_GAME_MENU);
371 if(game->isWaitingForShot(evt.timeSinceLastFrame)) {
372 if(game->isWhiteBallMovable()) {
373 Vector3 pos = game->getBallPosition(0);
375 mSceneManager->getSceneNode("WhiteNode")->setVisible(game->isBallOnTable(0));
377 if(mKeyboard->isKeyDown(OIS::KC_F)) {
378 pos[1] -= MOVE_SPEED*getMoveScale();
379 game->setWhitePosition(pos);
380 }else if(mKeyboard->isKeyDown(OIS::KC_H)) {
381 pos[1] += MOVE_SPEED*getMoveScale();
382 game->setWhitePosition(pos);
383 }else if(mKeyboard->isKeyDown(OIS::KC_T)) {
384 pos[0] -= MOVE_SPEED*getMoveScale();
385 game->setWhitePosition(pos);
386 }else if(mKeyboard->isKeyDown(OIS::KC_G)) {
387 pos[0] += MOVE_SPEED*getMoveScale();
388 game->setWhitePosition(pos);
391 mSceneManager->getSceneNode("WhiteNode")->setPosition(pos);
393 if(mCameraLocation) mSceneManager->getEntity("Cross")->setVisible(false);
394 else mSceneManager->getEntity("Cross")->setVisible(true);
396 if (mKeyboard->isKeyDown(OIS::KC_SPACE) && !mCameraLocation)
398 game->shot(horizontal, vertical, ballH, ballV, mUI->getPowerShot());
402 else {
403 mSceneManager->getSceneNode("CrossRotationNode")->rotate(Vector3::UNIT_Z, Degree(ballH));
404 mSceneManager->getSceneNode("CrossVerticalRotationNode")->rotate(Vector3::UNIT_Y, Degree(ballV));
405 ballH = 0.0f;
406 ballV = 0.0f;
407 mSceneManager->getSceneNode("WhiteNode")->setVisible(game->isBallOnTable(0));
408 mSceneManager->getSceneNode("WhiteNode")->setPosition(game->getBallPosition(0));
409 mSceneManager->getSceneNode("WhiteRotationNode")->setOrientation(game->getBallOrientation(0));
410 mSceneManager->getSceneNode("Ball1Node")->setVisible(game->isBallOnTable(1));
411 mSceneManager->getSceneNode("Ball1Node")->setPosition(game->getBallPosition(1));
412 mSceneManager->getSceneNode("Ball1RotationNode")->setOrientation(game->getBallOrientation(1));
413 mSceneManager->getSceneNode("Ball2Node")->setVisible(game->isBallOnTable(2));
414 mSceneManager->getSceneNode("Ball2Node")->setPosition(game->getBallPosition(2));
415 mSceneManager->getSceneNode("Ball2RotationNode")->setOrientation(game->getBallOrientation(2));
416 mSceneManager->getSceneNode("Ball3Node")->setVisible(game->isBallOnTable(3));
417 mSceneManager->getSceneNode("Ball3Node")->setPosition(game->getBallPosition(3));
418 mSceneManager->getSceneNode("Ball3RotationNode")->setOrientation(game->getBallOrientation(3));
419 mSceneManager->getSceneNode("Ball4Node")->setVisible(game->isBallOnTable(4));
420 mSceneManager->getSceneNode("Ball4Node")->setPosition(game->getBallPosition(4));
421 mSceneManager->getSceneNode("Ball4RotationNode")->setOrientation(game->getBallOrientation(4));
422 mSceneManager->getSceneNode("Ball5Node")->setVisible(game->isBallOnTable(5));
423 mSceneManager->getSceneNode("Ball5Node")->setPosition(game->getBallPosition(5));
424 mSceneManager->getSceneNode("Ball5RotationNode")->setOrientation(game->getBallOrientation(5));
425 mSceneManager->getSceneNode("Ball6Node")->setVisible(game->isBallOnTable(6));
426 mSceneManager->getSceneNode("Ball6Node")->setPosition(game->getBallPosition(6));
427 mSceneManager->getSceneNode("Ball6RotationNode")->setOrientation(game->getBallOrientation(6));
428 mSceneManager->getSceneNode("Ball7Node")->setVisible(game->isBallOnTable(7));
429 mSceneManager->getSceneNode("Ball7Node")->setPosition(game->getBallPosition(7));
430 mSceneManager->getSceneNode("Ball7RotationNode")->setOrientation(game->getBallOrientation(7));
431 mSceneManager->getSceneNode("Ball8Node")->setVisible(game->isBallOnTable(8));
432 mSceneManager->getSceneNode("Ball8Node")->setPosition(game->getBallPosition(8));
433 mSceneManager->getSceneNode("Ball8RotationNode")->setOrientation(game->getBallOrientation(8));
434 mSceneManager->getSceneNode("Ball9Node")->setVisible(game->isBallOnTable(9));
435 mSceneManager->getSceneNode("Ball9Node")->setPosition(game->getBallPosition(9));
436 mSceneManager->getSceneNode("Ball9RotationNode")->setOrientation(game->getBallOrientation(9));
437 mSceneManager->getSceneNode("Ball10Node")->setVisible(game->isBallOnTable(10));
438 mSceneManager->getSceneNode("Ball10Node")->setPosition(game->getBallPosition(10));
439 mSceneManager->getSceneNode("Ball10RotationNode")->setOrientation(game->getBallOrientation(10));
440 mSceneManager->getSceneNode("Ball11Node")->setVisible(game->isBallOnTable(11));
441 mSceneManager->getSceneNode("Ball11Node")->setPosition(game->getBallPosition(11));
442 mSceneManager->getSceneNode("Ball11RotationNode")->setOrientation(game->getBallOrientation(11));
443 mSceneManager->getSceneNode("Ball12Node")->setVisible(game->isBallOnTable(12));
444 mSceneManager->getSceneNode("Ball12Node")->setPosition(game->getBallPosition(12));
445 mSceneManager->getSceneNode("Ball12RotationNode")->setOrientation(game->getBallOrientation(12));
446 mSceneManager->getSceneNode("Ball13Node")->setVisible(game->isBallOnTable(13));
447 mSceneManager->getSceneNode("Ball13Node")->setPosition(game->getBallPosition(13));
448 mSceneManager->getSceneNode("Ball13RotationNode")->setOrientation(game->getBallOrientation(13));
449 mSceneManager->getSceneNode("Ball14Node")->setVisible(game->isBallOnTable(14));
450 mSceneManager->getSceneNode("Ball14Node")->setPosition(game->getBallPosition(14));
451 mSceneManager->getSceneNode("Ball14RotationNode")->setOrientation(game->getBallOrientation(14));
452 mSceneManager->getSceneNode("Ball15Node")->setVisible(game->isBallOnTable(15));
453 mSceneManager->getSceneNode("Ball15Node")->setPosition(game->getBallPosition(15));
454 mSceneManager->getSceneNode("Ball15RotationNode")->setOrientation(game->getBallOrientation(15));
455 mSceneManager->getEntity("Cross")->setVisible(false);
457 return true;
460 private:
462 //! OIS keyboard handle
463 OIS::Keyboard *mKeyboard;
464 //! OIS mouse handle
465 OIS::Mouse *mMouse;
466 Real mToggle;
468 //! It's pointer to UI class, responsible for drawing 2D interface.
469 UI *mUI;
470 //! Whell position in last frame, used for whell position change calculation
471 int mLastWhellPosition;
473 //! Vartical mouse position in last frame, used for mouse position change calculation
474 int mLastYPosition;
476 //! Horizontal mouse position in last frame, used for mouse position change calculation
477 int mLastXPosition;
479 //! OGRE camera handle
480 Camera *mCamera;
481 //! OGRE game scene manager
482 SceneManager *mSceneManager;
483 //! Class with game state
484 PoolGame *game;
485 //! Vertical angle the camera is facing
486 int mCameraLocation;
488 //! Decribes camera vertical position
489 //! Camera horizontal position
490 //! White ball rotations settings (horizontal)
491 //! White ball rotations settings (vertical)
492 Real vertical, horizontal, ballH, ballV;
493 //! Currently set zoom
494 Real zoom;
495 //! This member is toggled by mousePressed and mouseReleased methods.
496 bool mMousePressed;
498 //! This member means amount of time from last rendered frame
499 Real mTimeSinceLastFrame;
501 //! This is pointer to second FrameListener class, which inherit also MouseListener class in order to handle some OIS events
502 MouseListener *mMenuListener;
506 * This method returns how the actions taken by user should be scalled (how fast camera moves after pressing a button).
508 Real GameListener::getMoveScale() {
509 if(mKeyboard->isKeyDown(OIS::KC_LCONTROL))
510 return 0.1;
511 if(mKeyboard->isKeyDown(OIS::KC_LMENU))
512 return 10.0;
513 return 1.0;
517 * This function is run when the game starts. It initializes all the structures, data.
519 void Application::go() {
520 mInputManager = NULL;
521 mSystem = NULL;
522 mRenderer = NULL;
523 mUI = NULL;
524 mListener = NULL;
525 mRoot = NULL;
527 game = new PoolGame();
529 createRoot();
530 defineResources();
531 setupRenderSystem();
532 createRenderWindow();
533 initializeResourceGroups();
534 setupScene();
535 setupInputSystem();
536 setupCEGUI();
537 createFrameListener();
538 startRenderLoop();
542 * Main game desctructor. It is run when the game quits.
544 Application::~Application() {
545 if(mInputManager) {
546 mInputManager->destroyInputObject(mKeyboard);
547 mInputManager->destroyInputObject(mMouse);
548 OIS::InputManager::destroyInputSystem(mInputManager);
550 if(mSystem) delete mSystem;
551 if(mRenderer) delete mRenderer;
552 if(mUI) delete mUI;
553 if(mListener) delete mListener;
554 if(mRoot) delete mRoot;
558 * This function is used to initialze OGRE engine. It creates scene root node.
560 void Application::createRoot() {
561 mRoot = new Root();
565 * This function loads the resources defined in resource.cfg.
567 void Application::defineResources() {
568 String secName, typeName, archName;
569 ConfigFile cf;
570 cf.load("resources.cfg");
572 ConfigFile::SectionIterator seci = cf.getSectionIterator();
573 while (seci.hasMoreElements()) {
574 secName = seci.peekNextKey();
575 ConfigFile::SettingsMultiMap *settings = seci.getNext();
576 ConfigFile::SettingsMultiMap::iterator i;
577 for (i = settings->begin(); i != settings->end(); ++i) {
578 typeName = i->first;
579 archName = i->second;
580 ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
586 * This function is responsible for the dialog that is shown when the game starts. The user can set some graphics options in it.
588 void Application::setupRenderSystem() {
589 if(!mRoot->showConfigDialog())
590 throw Exception(52, "User canceled the config dialog!", "Application::setupRenderSystem()");
594 * This function creates the game window.
596 void Application::createRenderWindow() {
597 mRoot->initialise(true, "Pool Simulation Game");
601 * This function sets some options connected to resources (e.g. mipmap points).
602 * It loads all objects to memory as well.
604 void Application::initializeResourceGroups() {
605 TextureManager::getSingleton().setDefaultNumMipmaps(5);
606 ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
610 * This function places the objects in the scene. It sets the positions, creates scene nodes so we can easily rotate and move them.
611 * This function defines lights as well.
613 void Application::setupScene() {
614 SceneManager *mgr = mRoot->createSceneManager(ST_GENERIC, "SceneManager");
615 Camera *cam = mgr->createCamera("Camera");
616 Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam);
618 cam->lookAt(Vector3(-1.0f, 0.0f, 0.0f));
619 cam->setNearClipDistance(0.01f);
620 cam->roll(Degree(90));
621 mgr->setAmbientLight(ColourValue(1.0f, 1.0f, 1.0f));
623 mgr->setShadowTechnique(SHADOWTYPE_STENCIL_ADDITIVE);//SHADOWTYPE_TEXTURE_MODULATIVE);
624 mgr->setShadowColour(ColourValue(0.5f,0.5f,0.5f));
626 /* Rozmiar stolu 60x30 */
627 Entity *table = mgr->createEntity("PoolTable", "table.mesh");
628 table->setCastShadows(false);
629 Entity *white = mgr->createEntity("White", "white.mesh");
630 Entity *ball1 = mgr->createEntity("Ball1", "ball1.mesh");
631 Entity *ball2 = mgr->createEntity("Ball2", "ball2.mesh");
632 Entity *ball3 = mgr->createEntity("Ball3", "ball3.mesh");
633 Entity *ball4 = mgr->createEntity("Ball4", "ball4.mesh");
634 Entity *ball5 = mgr->createEntity("Ball5", "ball5.mesh");
635 Entity *ball6 = mgr->createEntity("Ball6", "ball6.mesh");
636 Entity *ball7 = mgr->createEntity("Ball7", "ball7.mesh");
637 Entity *ball8 = mgr->createEntity("Ball8", "ball8.mesh");
638 Entity *ball9 = mgr->createEntity("Ball9", "ball9.mesh");
639 Entity *ball10 = mgr->createEntity("Ball10", "ball10.mesh");
640 Entity *ball11 = mgr->createEntity("Ball11", "ball11.mesh");
641 Entity *ball12 = mgr->createEntity("Ball12", "ball12.mesh");
642 Entity *ball13 = mgr->createEntity("Ball13", "ball13.mesh");
643 Entity *ball14 = mgr->createEntity("Ball14", "ball14.mesh");
644 Entity *ball15 = mgr->createEntity("Ball15", "ball15.mesh");
645 Entity *cross = mgr->createEntity("Cross", "cross.mesh");
647 SceneNode *tableNode = mgr->getRootSceneNode()->createChildSceneNode("TableNode");
648 SceneNode *tableCenterNode = tableNode->createChildSceneNode("TableCenterNode", Vector3(1.1f, -2.0f, -20.0f));
649 SceneNode *whiteNode = mgr->getRootSceneNode()->createChildSceneNode("WhiteNode", Vector3(0.0f, 0.0f, 0.6f));
650 SceneNode *whiteRotationNode = whiteNode->createChildSceneNode("WhiteRotationNode");
651 SceneNode *whiteCenterNode = whiteRotationNode->createChildSceneNode("WhiteCenterNode", Vector3(-12.96f, -4.2f, -20.6f));
652 SceneNode *cameraTableRotationNode = tableNode->createChildSceneNode("CameraTableRotationNode", Vector3(0.0f, 0.0f, 1.0f));
653 SceneNode *cameraWhiteRotationNode = whiteNode->createChildSceneNode("CameraWhiteRotationNode", Vector3(0.0f, 0.0f, 1.0f));
654 SceneNode *cameraLookAtTableNode = cameraTableRotationNode->createChildSceneNode("CameraLookAtTableNode");
655 SceneNode *cameraLookAtWhiteNode = cameraWhiteRotationNode->createChildSceneNode("CameraLookAtWhiteNode");
656 SceneNode *cameraWhiteNode = cameraLookAtWhiteNode->createChildSceneNode("CameraWhiteNode", Vector3(50.0f, 0.0f, 0.0f));
657 SceneNode *cameraTableNode = cameraLookAtTableNode->createChildSceneNode("CameraTableNode", Vector3(70.0f, 0.0f, 0.0f));
658 cameraLookAtTableNode->rotate(Vector3::UNIT_Y, -Degree(10));
659 cameraLookAtWhiteNode->rotate(Vector3::UNIT_Y, -Degree(10));
660 tableCenterNode->attachObject(table);
661 whiteCenterNode->attachObject(white);
662 cameraWhiteNode->attachObject(cam);
663 whiteNode->setVisible(false);
665 SceneNode *crossRotationNode = cameraWhiteRotationNode->createChildSceneNode("CrossRotationNode");
666 SceneNode *crossVerticalRotationNode = crossRotationNode->createChildSceneNode("CrossVerticalRotationNode", Vector3(0.0f, 0.0f, -1.0f));
667 SceneNode *crossNode = crossVerticalRotationNode->createChildSceneNode("CrossNode", Vector3(1.0f, 0.0f, 0.0f));
668 crossRotationNode->rotate(Vector3::UNIT_Y, -Degree(10));
669 crossNode->setScale(Vector3(0.05f, 0.05f, 0.05f));
670 crossNode->attachObject(cross);
672 SceneNode *ball1Node = mgr->getRootSceneNode()->createChildSceneNode("Ball1Node", Vector3(0.0f, 0.0f, 0.6f));
673 SceneNode *ball1RotationNode = ball1Node->createChildSceneNode("Ball1RotationNode");
674 SceneNode *ball1CenterNode = ball1RotationNode->createChildSceneNode("Ball1CenterNode", Vector3(18.7f, -2.15f, -20.6f));
675 ball1CenterNode->attachObject(ball1);
676 ball1Node->setVisible(false);
678 SceneNode *ball2Node = mgr->getRootSceneNode()->createChildSceneNode("Ball2Node", Vector3(0.0f, 0.0f, 0.6f));
679 SceneNode *ball2RotationNode = ball2Node->createChildSceneNode("Ball2RotationNode");
680 SceneNode *ball2CenterNode = ball2RotationNode->createChildSceneNode("Ball2CenterNode", Vector3(25.82f, -0.33f, -20.6f));
681 ball2CenterNode->attachObject(ball2);
682 ball2Node->setVisible(false);
684 SceneNode *ball3Node = mgr->getRootSceneNode()->createChildSceneNode("Ball3Node", Vector3(0.0f, 0.0f, 0.6f));
685 SceneNode *ball3RotationNode = ball3Node->createChildSceneNode("Ball3RotationNode");
686 SceneNode *ball3CenterNode = ball3RotationNode->createChildSceneNode("Ball3CenterNode", Vector3(20.40f, -1.17f, -20.6f));
687 ball3CenterNode->attachObject(ball3);
688 ball3Node->setVisible(false);
690 SceneNode *ball4Node = mgr->getRootSceneNode()->createChildSceneNode("Ball4Node", Vector3(0.0f, 0.0f, 0.6f));
691 SceneNode *ball4RotationNode = ball4Node->createChildSceneNode("Ball4RotationNode");
692 SceneNode *ball4CenterNode = ball4RotationNode->createChildSceneNode("Ball4CenterNode", Vector3(22.08f, -4.30f, -20.6f));
693 ball4CenterNode->attachObject(ball4);
694 ball4Node->setVisible(false);
696 SceneNode *ball5Node = mgr->getRootSceneNode()->createChildSceneNode("Ball5Node", Vector3(0.0f, 0.0f, 0.6f));
697 SceneNode *ball5RotationNode = ball5Node->createChildSceneNode("Ball5RotationNode");
698 SceneNode *ball5CenterNode = ball5RotationNode->createChildSceneNode("Ball5CenterNode", Vector3(23.83f, -5.37f, -20.6f));
699 ball5CenterNode->attachObject(ball5);
700 ball5Node->setVisible(false);
702 SceneNode *ball6Node = mgr->getRootSceneNode()->createChildSceneNode("Ball6Node", Vector3(0.0f, 0.0f, 0.6f));
703 SceneNode *ball6RotationNode = ball6Node->createChildSceneNode("Ball6RotationNode");
704 SceneNode *ball6CenterNode = ball6RotationNode->createChildSceneNode("Ball6CenterNode", Vector3(25.74f, -4.27f, -20.6f));
705 ball6CenterNode->attachObject(ball6);
706 ball6Node->setVisible(false);
708 SceneNode *ball7Node = mgr->getRootSceneNode()->createChildSceneNode("Ball7Node", Vector3(0.0f, 0.0f, 0.6f));
709 SceneNode *ball7RotationNode = ball7Node->createChildSceneNode("Ball7RotationNode");
710 SceneNode *ball7CenterNode = ball7RotationNode->createChildSceneNode("Ball7CenterNode", Vector3(23.83f, 0.58f, -20.6f));
711 ball7CenterNode->attachObject(ball7);
712 ball7Node->setVisible(false);
714 SceneNode *ball8Node = mgr->getRootSceneNode()->createChildSceneNode("Ball8Node", Vector3(0.0f, 0.0f, 0.6f));
715 SceneNode *ball8RotationNode = ball8Node->createChildSceneNode("Ball8RotationNode");
716 SceneNode *ball8CenterNode = ball8RotationNode->createChildSceneNode("Ball8CenterNode", Vector3(22.15f, -2.36f, -20.6f));
717 ball8CenterNode->attachObject(ball8);
718 ball8Node->setVisible(false);
720 SceneNode *ball9Node = mgr->getRootSceneNode()->createChildSceneNode("Ball9Node", Vector3(0.0f, 0.0f, 0.6f));
721 SceneNode *ball9RotationNode = ball9Node->createChildSceneNode("Ball9RotationNode");
722 SceneNode *ball9CenterNode = ball9RotationNode->createChildSceneNode("Ball9CenterNode", Vector3(25.75f, -2.33f, -20.6f));
723 ball9CenterNode->attachObject(ball9);
724 ball9Node->setVisible(false);
726 SceneNode *ball10Node = mgr->getRootSceneNode()->createChildSceneNode("Ball10Node", Vector3(0.0f, 0.0f, 0.6f));
727 SceneNode *ball10RotationNode = ball10Node->createChildSceneNode("Ball10RotationNode");
728 SceneNode *ball10CenterNode = ball10RotationNode->createChildSceneNode("Ball10CenterNode", Vector3(23.9f, -1.38f, -20.6f));
729 ball10CenterNode->attachObject(ball10);
730 ball10Node->setVisible(false);
732 SceneNode *ball11Node = mgr->getRootSceneNode()->createChildSceneNode("Ball11Node", Vector3(0.0f, 0.0f, 0.6f));
733 SceneNode *ball11RotationNode = ball11Node->createChildSceneNode("Ball11RotationNode");
734 SceneNode *ball11CenterNode = ball11RotationNode->createChildSceneNode("Ball11CenterNode", Vector3(22.12f, -0.4f, -20.6f));
735 ball11CenterNode->attachObject(ball11);
736 ball11Node->setVisible(false);
738 SceneNode *ball12Node = mgr->getRootSceneNode()->createChildSceneNode("Ball12Node", Vector3(0.0f, 0.0f, 0.6f));
739 SceneNode *ball12RotationNode = ball12Node->createChildSceneNode("Ball12RotationNode");
740 SceneNode *ball12CenterNode = ball12RotationNode->createChildSceneNode("Ball12CenterNode", Vector3(25.59f, -6.18f, -20.6f));
741 ball12CenterNode->attachObject(ball12);
742 ball12Node->setVisible(false);
744 SceneNode *ball13Node = mgr->getRootSceneNode()->createChildSceneNode("Ball13Node", Vector3(0.0f, 0.0f, 0.6f));
745 SceneNode *ball13RotationNode = ball13Node->createChildSceneNode("Ball13RotationNode");
746 SceneNode *ball13CenterNode = ball13RotationNode->createChildSceneNode("Ball13CenterNode", Vector3(25.66f, 1.61f, -20.6f));
747 ball13CenterNode->attachObject(ball13);
748 ball13Node->setVisible(false);
750 SceneNode *ball14Node = mgr->getRootSceneNode()->createChildSceneNode("Ball14Node", Vector3(0.0f, 0.0f, 0.6f));
751 SceneNode *ball14RotationNode = ball14Node->createChildSceneNode("Ball14RotationNode");
752 SceneNode *ball14CenterNode = ball14RotationNode->createChildSceneNode("Ball14CenterNode", Vector3(23.86f, -3.42f, -20.6f));
753 ball14CenterNode->attachObject(ball14);
754 ball14Node->setVisible(false);
756 SceneNode *ball15Node = mgr->getRootSceneNode()->createChildSceneNode("Ball15Node", Vector3(0.0f, 0.0f, 0.6f));
757 SceneNode *ball15RotationNode = ball15Node->createChildSceneNode("Ball15RotationNode");
758 SceneNode *ball15CenterNode = ball15RotationNode->createChildSceneNode("Ball15CenterNode", Vector3(20.37f, -3.19f, -20.6f));
759 ball15CenterNode->attachObject(ball15);
760 ball15Node->setVisible(false);
762 Light* light = mgr->createLight("Light1");
763 light->setType(Light::LT_SPOTLIGHT);
764 light->setDiffuseColour(ColourValue(0.25f,0.25f,0.0f));
765 light->setSpecularColour(ColourValue(0.25f,0.25f,0.0f));
766 light->setSpotlightRange(Degree(60), Degree(70));
767 light->setDirection(Vector3::NEGATIVE_UNIT_Z);
768 SceneNode *lightNode = mgr->getRootSceneNode()->createChildSceneNode("LightNode1", Vector3(-20.0f, 0.0f, 30.0f));
769 lightNode->attachObject(light);
770 light->setVisible(false);
772 light = mgr->createLight("Light2");
773 light->setType(Light::LT_SPOTLIGHT);
774 light->setDiffuseColour(ColourValue(0.25f,0.25f,0.0f));
775 light->setSpecularColour(ColourValue(0.25f,0.25f,0.0f));
776 light->setSpotlightRange(Degree(60), Degree(70));
777 light->setDirection(Vector3::NEGATIVE_UNIT_Z);
778 lightNode = mgr->getRootSceneNode()->createChildSceneNode("LightNode2", Vector3(0.0f, 0.0f, 30.0f));
779 lightNode->attachObject(light);
780 light->setVisible(false);
782 light = mgr->createLight("Light3");
783 light->setType(Light::LT_SPOTLIGHT);
784 light->setDiffuseColour(ColourValue(0.25f,0.25f,0.0f));
785 light->setSpecularColour(ColourValue(0.25f,0.25f,0.0f));
786 light->setSpotlightRange(Degree(60), Degree(70));
787 light->setDirection(Vector3::NEGATIVE_UNIT_Z);
788 lightNode = mgr->getRootSceneNode()->createChildSceneNode("LightNode3", Vector3(20.0f, 0.0f, 30.0f));
789 lightNode->attachObject(light);
790 light->setVisible(false);
794 * This function initializes the OIS library (initializes mouse and keyboard).
796 void Application::setupInputSystem() {
797 size_t windowHnd = 0;
798 std::ostringstream windowHndStr;
799 OIS::ParamList pl;
800 RenderWindow *win = mRoot->getAutoCreatedWindow();
802 win->getCustomAttribute("WINDOW", &windowHnd);
803 windowHndStr << windowHnd;
804 pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
805 mInputManager = OIS::InputManager::createInputSystem(pl);
807 try {
808 mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, true));
809 mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, true));
811 const OIS::MouseState &ms = mMouse->getMouseState();
812 ms.width = win->getWidth()/2;
813 ms.height = win->getHeight()/2;
815 catch (const OIS::Exception &e) {
816 throw Exception(42, e.eText, "Application::setupInputSystem");
821 * This method initializes Crazy Edie User Interface (CEGUI). It loads
822 * configuration files (with widget look and feel, definition of menus,
823 * default font) and draw main menu on screen
825 void Application::setupCEGUI() {
826 SceneManager *mgr = mRoot->getSceneManager("SceneManager");
827 RenderWindow *win = mRoot->getAutoCreatedWindow();
828 mContinue = true;
830 mRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mgr);
831 mSystem = new CEGUI::System(mRenderer);
832 mWindowManager = CEGUI::WindowManager::getSingletonPtr();
834 CEGUI::Logger::getSingleton().setLoggingLevel (CEGUI::Informative);
835 CEGUI::SchemeManager::getSingleton().loadScheme("psg.scheme");
836 CEGUI::FontManager::getSingleton().createFont("DejaVuSans-10.font");
837 mSystem->setDefaultFont((CEGUI::utf8*) "DejaVuSans-10");
839 mSystem->setDefaultMouseCursor((CEGUI::utf8*) "PSG",
840 (CEGUI::utf8*) "MouseArrow");
843 mUI = new UI(mRoot, mRenderer, mSystem, mWindowManager, &mContinue, game);
844 mUI->setScene(M_MAIN_MENU);
849 * This method creates frame listeners. Frame listeners are executed everytime the frame is to be rendered (or after rendering).
850 * It defines frame listener for game and menus.
852 void Application::createFrameListener() {
853 mMenuListener = new MenuFrameListener (mKeyboard,
854 mMouse,
855 mRoot->getSceneManager("SceneManager"),
856 mSystem,&mContinue,game);
857 mListener = new GameListener(mKeyboard, mMouse,
858 mRoot->getSceneManager("SceneManager"),
859 game, mUI, mMenuListener);
861 mRoot->addFrameListener(mListener);
862 mRoot->addFrameListener(mMenuListener);
867 * This function says OGRE that it should start rendering now.
869 void Application::startRenderLoop() {
870 mRoot->startRendering();
874 * This is the main function. It executes Application::go function.
876 int main(int argc, char **argv)
878 Application app;
880 try {
881 app.go();
882 } catch(Ogre::Exception &e) {
883 fprintf(stderr, "Failed to initialize application: %s\n", e.what());
886 return 0;