1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
| /*
-----------------------------------------------------------------------------
Filename: BaseApplication.cpp
-----------------------------------------------------------------------------
*/
#include "BaseApplication.h"
#include "LoadingBar.h"
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#include "../res/resource.h"
#endif
template<> BaseApplication* Ogre::Singleton<BaseApplication>::ms_Singleton = 0;
CEGUI::MouseButton convertOISMouseButtonToCegui(int buttonID)
{
switch (buttonID)
{
case 0: return CEGUI::LeftButton;
case 1: return CEGUI::RightButton;
case 2: return CEGUI::MiddleButton;
case 3: return CEGUI::X1Button;
default: return CEGUI::LeftButton;
}
}
//-------------------------------------------------------------------------------------
BaseApplication::BaseApplication(void)
: mRoot(0),
mCamera(0),
mSceneMgr(0),
mWindow(0),
mLoadingBar(0),
mGUISystem(0),
mGUIRenderer(0),
mShutdownRequested(false),
mSceneDetailIndex(0),
mMoveSpeed(100),
mRotateSpeed(36),
mDebugOverlay(0),
mDebugText(""),
mInputManager(0),
mMouse(0),
mKeyboard(0),
mTranslateVector(Vector3::ZERO),
mStatsOn(true),
mUseBufferedInputKeys(false),
mUseBufferedInputMouse(false),
mInputTypeSwitchingOn(false),
mNumScreenShots(0),
mMoveScale(0.0f),
mRotScale(0.0f),
mTimeUntilNextToggle(0),
mRotX(0),
mRotY(0),
mFiltering(TFO_BILINEAR),
mAniso(1)
{
}
//-------------------------------------------------------------------------------------
BaseApplication::~BaseApplication(void)
{
//Remove ourself as a Window listener
WindowEventUtilities::removeWindowEventListener(mWindow, this);
windowClosed(mWindow);
if(mGUISystem)
{
delete mGUISystem;
mGUISystem = 0;
}
if(mGUIRenderer)
{
delete mGUIRenderer;
mGUIRenderer = 0;
}
delete mRoot;
}
//-------------------------------------------------------------------------------------
bool BaseApplication::configure(void)
{
// Show the configuration dialog and initialise the system
// You can skip this and use root.restoreConfig() to load configuration
// settings if you were sure there are valid ones saved in ogre.cfg
if(mRoot->showConfigDialog())
{
// If returned true, user clicked OK so initialise
// Here we choose to let the system create a default rendering window by passing 'true'
//mRoot->restoreConfig();
mWindow = mRoot->initialise(true);
// Let's add a nice window icon
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
HWND hwnd;
mWindow->getCustomAttribute("WINDOW", (void*)&hwnd);
LONG iconID = (LONG)LoadIcon( GetModuleHandle(0), MAKEINTRESOURCE(IDI_APPICON) );
SetClassLong( hwnd, GCL_HICON, iconID );
#endif
return true;
}
else
{
return false;
}
}
//-------------------------------------------------------------------------------------
void BaseApplication::chooseSceneManager(void)
{
// Get the SceneManager, in this case a generic one
mSceneMgr = mRoot->createSceneManager(ST_GENERIC);
}
//-------------------------------------------------------------------------------------
void BaseApplication::createCamera(void)
{
// Create the camera
mCamera = mSceneMgr->createCamera("PlayerCam");
// Position it at 500 in Z direction
mCamera->setPosition(Vector3(0,5,12));
// Look back along -Z
mCamera->lookAt(Vector3(0,0,-20));
mCamera->setNearClipDistance(5);
}
//-------------------------------------------------------------------------------------
void BaseApplication::createFrameListener(void)
{
mDebugOverlay = OverlayManager::getSingleton().getByName("Core/DebugOverlay");
mUseBufferedInputKeys = false;
mUseBufferedInputMouse = true;
mInputTypeSwitchingOn = mUseBufferedInputKeys || mUseBufferedInputMouse;
mRotateSpeed = 36;
mMoveSpeed = 100;
using namespace OIS;
LogManager::getSingletonPtr()->logMessage("*** Initialisation OIS ***");
ParamList pl;
size_t windowHnd = 0;
std::ostringstream windowHndStr;
mWindow->getCustomAttribute("WINDOW", &windowHnd);
windowHndStr << windowHnd;
pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
mInputManager = InputManager::createInputSystem( pl );
mKeyboard = static_cast<Keyboard*>(mInputManager->createInputObject( OISKeyboard, mUseBufferedInputKeys ));
mMouse = static_cast<Mouse*>(mInputManager->createInputObject( OISMouse, mUseBufferedInputMouse ));
mMouse->setEventCallback(this);
mKeyboard->setEventCallback(this);
mShutdownRequested = false;
//Set initial mouse clipping size
windowResized(mWindow);
//Register as a Window listener
WindowEventUtilities::addWindowEventListener(mWindow, this);
mStatsOn = true;
mNumScreenShots = 0;
mTimeUntilNextToggle = 0;
mSceneDetailIndex = 0;
mMoveScale = 0.0f;
mRotScale = 0.0f;
mTranslateVector = Vector3::ZERO;
mAniso = 1;
mFiltering = TFO_BILINEAR;
showDebugOverlay(true);
mRoot->addFrameListener(this);
}
//-------------------------------------------------------------------------------------
void BaseApplication::destroyScene(void)
{
}
//-------------------------------------------------------------------------------------
void BaseApplication::createViewports(void)
{
// Create one viewport, entire window
Viewport* vp = mWindow->addViewport(mCamera);
vp->setBackgroundColour(ColourValue(0,0,0));
// Alter the camera aspect ratio to match the viewport
mCamera->setAspectRatio(
Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
}
//-------------------------------------------------------------------------------------
void BaseApplication::setupResources(void)
{
// Load resource paths from config file
ConfigFile cf;
cf.load("./resources.cfg");
// Go through all sections & settings in the file
ConfigFile::SectionIterator seci = cf.getSectionIterator();
String secName, typeName, archName;
while (seci.hasMoreElements())
{
secName = seci.peekNextKey();
ConfigFile::SettingsMultiMap *settings = seci.getNext();
ConfigFile::SettingsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); ++i)
{
typeName = i->first;
archName = i->second;
ResourceGroupManager::getSingleton().addResourceLocation(
archName, typeName, secName);
}
}
}
//-------------------------------------------------------------------------------------
void BaseApplication::createResourceListener(void)
{
}
//-------------------------------------------------------------------------------------
void BaseApplication::loadResources(void)
{
mLoadingBar = new LoadingBar();
mLoadingBar->start(mWindow, 1, 1, 0.75);
// Turn off rendering of everything except overlays
mSceneMgr->clearSpecialCaseRenderQueues();
mSceneMgr->addSpecialCaseRenderQueue(RENDER_QUEUE_OVERLAY);
mSceneMgr->setSpecialCaseRenderQueueMode(SceneManager::SCRQM_INCLUDE);
// Initialise the rest of the resource groups, parse scripts etc
ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
ResourceGroupManager::getSingleton().loadResourceGroup(
ResourceGroupManager::getSingleton().getWorldResourceGroupName(),
false, true);
// Back to full rendering
mSceneMgr->clearSpecialCaseRenderQueues();
mSceneMgr->setSpecialCaseRenderQueueMode(SceneManager::SCRQM_EXCLUDE);
mLoadingBar->finish();
delete mLoadingBar;
}
//-------------------------------------------------------------------------------------
void BaseApplication::go(void)
{
if (!setup())
return;
showDebugOverlay(true);
mRoot->startRendering();
// clean up
destroyScene();
}
//-------------------------------------------------------------------------------------
bool BaseApplication::setup(void)
{
mRoot = new Root();
setupResources();
bool carryOn = configure();
if (!carryOn) return false;
chooseSceneManager();
createCamera();
createViewports();
// Set default mipmap level (NB some APIs ignore this)
TextureManager::getSingleton().setDefaultNumMipmaps(5);
// Create any resource listeners (for loading screens)
createResourceListener();
// Load resources
loadResources();
// Create the scene
createScene();
createFrameListener();
return true;
};
//-------------------------------------------------------------------------------------
void BaseApplication::updateStats(void)
{
static String currFps = "FPS en cours : ";
static String avgFps = "Moyenne FPS : ";
static String bestFps = "Meilleur FPS : ";
static String worstFps = "Basse FPS : ";
static String tris = "Nombre de triangles : ";
// update stats when necessary
try {
OverlayElement* guiAvg = OverlayManager::getSingleton().getOverlayElement("Core/AverageFps");
OverlayElement* guiCurr = OverlayManager::getSingleton().getOverlayElement("Core/CurrFps");
OverlayElement* guiBest = OverlayManager::getSingleton().getOverlayElement("Core/BestFps");
OverlayElement* guiWorst = OverlayManager::getSingleton().getOverlayElement("Core/WorstFps");
const RenderTarget::FrameStats& stats = mWindow->getStatistics();
guiAvg->setCaption(avgFps + StringConverter::toString(stats.avgFPS));
guiCurr->setCaption(currFps + StringConverter::toString(stats.lastFPS));
guiBest->setCaption(bestFps + StringConverter::toString(stats.bestFPS)
+" "+StringConverter::toString(stats.bestFrameTime)+" ms");
guiWorst->setCaption(worstFps + StringConverter::toString(stats.worstFPS)
+" "+StringConverter::toString(stats.worstFrameTime)+" ms");
OverlayElement* guiTris = OverlayManager::getSingleton().getOverlayElement("Core/NumTris");
guiTris->setCaption(tris + StringConverter::toString(stats.triangleCount));
OverlayElement* guiDbg = OverlayManager::getSingleton().getOverlayElement("Core/DebugText");
guiDbg->setCaption(mDebugText);
}
catch(...)
{
// ignore
}
}
//-------------------------------------------------------------------------------------
bool BaseApplication::processUnbufferedKeyInput(const FrameEvent& evt)
{
using namespace OIS;
OIS::Keyboard* mInputDevice = mKeyboard; // Nice hack, eh? :)
if (mInputDevice->isKeyDown(KC_A))
{
// Move camera left
mTranslateVector.x = -mMoveScale;
}
if (mInputDevice->isKeyDown(KC_D))
{
// Move camera RIGHT
mTranslateVector.x = mMoveScale;
}
/* Move camera forward by keypress. */
if (mInputDevice->isKeyDown(KC_UP) || mInputDevice->isKeyDown(KC_W) )
{
mTranslateVector.z = -mMoveScale;
}
/* Move camera backward by keypress. */
if (mInputDevice->isKeyDown(KC_DOWN) || mInputDevice->isKeyDown(KC_S) )
{
mTranslateVector.z = mMoveScale;
}
if (mInputDevice->isKeyDown(KC_PGUP))
{
// Move camera up
mTranslateVector.y = mMoveScale;
}
if (mInputDevice->isKeyDown(KC_PGDOWN))
{
// Move camera down
mTranslateVector.y = -mMoveScale;
}
if (mInputDevice->isKeyDown(KC_RIGHT))
{
mCamera->yaw(-mRotScale);
}
if (mInputDevice->isKeyDown(KC_LEFT))
{
mCamera->yaw(mRotScale);
}
if( mInputDevice->isKeyDown( KC_ESCAPE) )
{
return false;
}
// see if switching is on, and you want to toggle
if (mInputTypeSwitchingOn && mInputDevice->isKeyDown(KC_M) && mTimeUntilNextToggle <= 0)
{
switchMouseMode();
mTimeUntilNextToggle = 1;
}
if (mInputTypeSwitchingOn && mInputDevice->isKeyDown(KC_K) && mTimeUntilNextToggle <= 0)
{
// must be going from immediate keyboard to buffered keyboard
switchKeyMode();
mTimeUntilNextToggle = 1;
}
if (mInputDevice->isKeyDown(KC_F) && mTimeUntilNextToggle <= 0)
{
mStatsOn = !mStatsOn;
showDebugOverlay(mStatsOn);
mTimeUntilNextToggle = 1;
}
if (mInputDevice->isKeyDown(KC_T) && mTimeUntilNextToggle <= 0)
{
switch(mFiltering)
{
case TFO_BILINEAR:
mFiltering = TFO_TRILINEAR;
mAniso = 1;
break;
case TFO_TRILINEAR:
mFiltering = TFO_ANISOTROPIC;
mAniso = 8;
break;
case TFO_ANISOTROPIC:
mFiltering = TFO_BILINEAR;
mAniso = 1;
break;
default:
break;
}
MaterialManager::getSingleton().setDefaultTextureFiltering(mFiltering);
MaterialManager::getSingleton().setDefaultAnisotropy(mAniso);
showDebugOverlay(mStatsOn);
mTimeUntilNextToggle = 1;
}
if (mInputDevice->isKeyDown(KC_SYSRQ) && mTimeUntilNextToggle <= 0)
{
std::ostringstream ss;
ss << "CaptureEcran_" << ++mNumScreenShots << ".png";
mWindow->writeContentsToFile(ss.str());
mTimeUntilNextToggle = 0.5;
mDebugText = "Enregistré: " + ss.str();
}
if (mInputDevice->isKeyDown(KC_R) && mTimeUntilNextToggle <=0)
{
mSceneDetailIndex = (mSceneDetailIndex+1)%3 ;
switch(mSceneDetailIndex) {
case 0 : mCamera->setPolygonMode(PM_SOLID) ; break ;
case 1 : mCamera->setPolygonMode(PM_WIREFRAME) ; break ;
case 2 : mCamera->setPolygonMode(PM_POINTS) ; break ;
}
mTimeUntilNextToggle = 0.5;
}
static bool displayCameraDetails = false;
if (mInputDevice->isKeyDown(KC_P) && mTimeUntilNextToggle <= 0)
{
displayCameraDetails = !displayCameraDetails;
mTimeUntilNextToggle = 0.5;
if (!displayCameraDetails)
mDebugText = "";
}
if (displayCameraDetails)
{
// Print camera details
mDebugText = "P: " + StringConverter::toString(mCamera->getDerivedPosition()) +
" " + "O: " + StringConverter::toString(mCamera->getDerivedOrientation());
}
// Return true to continue rendering
return true;
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
bool BaseApplication::processUnbufferedMouseInput(const FrameEvent& evt)
{
using namespace OIS;
// Rotation factors, may not be used if the second mouse button is pressed
// 2nd mouse button - slide, otherwise rotate
const MouseState &ms = mMouse->getMouseState();
if( ms.buttonDown( MB_Right ) )
{
mTranslateVector.x += ms.X.rel * 0.13;
mTranslateVector.y -= ms.Y.rel * 0.13;
}
else
{
mRotX = Degree(-ms.X.rel * 0.13);
mRotY = Degree(-ms.Y.rel * 0.13);
}
return true;
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
void BaseApplication::moveCamera()
{
// Make all the changes to the camera
// Note that YAW direction is around a fixed axis (freelook style) rather than a natural YAW (e.g. airplane)
mCamera->yaw(mRotX);
mCamera->pitch(mRotY);
mCamera->moveRelative(mTranslateVector);
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
void BaseApplication::showDebugOverlay(bool show)
{
if (mDebugOverlay)
{
if (show)
{
mDebugOverlay->show();
}
else
{
mDebugOverlay->hide();
}
}
}
//-------------------------------------------------------------------------------------
void BaseApplication::requestShutdown(void)
{
mShutdownRequested = true;
}
//-------------------------------------------------------------------------------------
bool BaseApplication::frameStarted(const FrameEvent& evt)
{
if(mWindow->isClosed())
return false;
//Need to capture/update each device
mKeyboard->capture();
mMouse->capture();
mUseBufferedInputMouse = mMouse->buffered();
mUseBufferedInputKeys = mKeyboard->buffered();
if(mUseBufferedInputMouse)
{
CEGUI::MouseCursor::getSingleton().show( );
}
else
{
CEGUI::MouseCursor::getSingleton().hide( );
}
if ( !mUseBufferedInputMouse || !mUseBufferedInputKeys)
{
// one of the input modes is immediate, so setup what is needed for immediate mouse/key movement
if (mTimeUntilNextToggle >= 0)
mTimeUntilNextToggle -= evt.timeSinceLastFrame;
// If this is the first frame, pick a speed
if (evt.timeSinceLastFrame == 0)
{
mMoveScale = 1;
mRotScale = 0.1;
}
// Otherwise scale movement units by time passed since last frame
else
{
// Move about 100 units per second,
mMoveScale = mMoveSpeed * evt.timeSinceLastFrame;
// Take about 10 seconds for full rotation
mRotScale = mRotateSpeed * evt.timeSinceLastFrame;
}
mRotX = 0;
mRotY = 0;
mTranslateVector = Vector3::ZERO;
}
if (mUseBufferedInputKeys)
{
// no need to do any processing here, it is handled by event processor and
// you get the results as KeyEvents
}
else
{
if (processUnbufferedKeyInput(evt) == false)
{
return false;
}
}
if (mUseBufferedInputMouse)
{
// no need to do any processing here, it is handled by event processor and
// you get the results as MouseEvents
}
else
{
if (processUnbufferedMouseInput(evt) == false)
{
return false;
}
}
if ( !mUseBufferedInputMouse || !mUseBufferedInputKeys)
{
// one of the input modes is immediate, so update the movement vector
moveCamera();
}
return true;
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
bool BaseApplication::frameEnded(const FrameEvent& evt)
{
if (mShutdownRequested)
return false;
updateStats();
return true;
}
//-------------------------------------------------------------------------------------
void BaseApplication::switchMouseMode()
{
mUseBufferedInputMouse = !mUseBufferedInputMouse;
mMouse->setBuffered(mUseBufferedInputMouse);
}
//-------------------------------------------------------------------------------------
void BaseApplication::switchKeyMode()
{
mUseBufferedInputKeys = !mUseBufferedInputKeys;
mKeyboard->setBuffered(mUseBufferedInputKeys);
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
bool BaseApplication::keyPressed( const OIS::KeyEvent &arg )
{
if( arg.key == OIS::KC_ESCAPE )
mShutdownRequested = true;
CEGUI::System::getSingleton().injectKeyDown( arg.key );
CEGUI::System::getSingleton().injectChar( arg.text );
return true;
}
bool BaseApplication::keyReleased( const OIS::KeyEvent &arg )
{
if( arg.key == OIS::KC_M )
mMouse->setBuffered( !mMouse->buffered() );
else if( arg.key == OIS::KC_K )
mKeyboard->setBuffered( !mKeyboard->buffered() );
CEGUI::System::getSingleton().injectKeyUp( arg.key );
return true;
}
bool BaseApplication::mouseMoved( const OIS::MouseEvent &arg )
{
CEGUI::System::getSingleton().injectMouseMove( arg.state.X.rel, arg.state.Y.rel );
return true;
}
bool BaseApplication::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
CEGUI::System::getSingleton().injectMouseButtonDown(convertOISMouseButtonToCegui(id));
return true;
}
bool BaseApplication::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
CEGUI::System::getSingleton().injectMouseButtonUp(convertOISMouseButtonToCegui(id));
return true;
}
//Adjust mouse clipping area
void BaseApplication::windowResized(RenderWindow* rw)
{
unsigned int width, height, depth;
int left, top;
rw->getMetrics(width, height, depth, left, top);
const OIS::MouseState &ms = mMouse->getMouseState();
ms.width = width;
ms.height = height;
}
//Unattach OIS before window shutdown (very important under Linux)
void BaseApplication::windowClosed(RenderWindow* rw)
{
//Only close for window that created OIS (the main window in these demos)
if( rw == mWindow )
{
if( mInputManager )
{
mInputManager->destroyInputObject( mMouse );
mInputManager->destroyInputObject( mKeyboard );
OIS::InputManager::destroyInputSystem(mInputManager);
mInputManager = 0;
}
}
}
//-------------------------------------------------------------------------------------
void BaseApplication::setupEventHandlers(void)
{
}
//-------------------------------------------------------------------------------------
bool BaseApplication::handleQuit(const CEGUI::EventArgs& e)
{
requestShutdown();
return true;
} |
Partager