⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 application.cpp

📁 使用stl技术,(还没看,是听说的)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/** @file Application.cpp
	@brief Contains the implementation of the Application class.
*/

// Includes
#include "Demo.h"

// Constructor
Application::Application()
{
	// Initialise member variables
    mRoot = 0;
	mCamera = 0;
	mSceneMgr = 0;
	mWindow = 0;
	mSimulation = 0;
	mBullet = 0;

	// OGRE objects
    mRoot = 0;
    mCamera = 0;
    mSceneMgr = 0;
    mWindow = 0;
    mInputDevice = 0;

	// Input
    mTimeUntilNextAction = 0.0f;

	// Camera movement
    mTranslateVector = Vector3::ZERO;
    mMoveScale = 0.0f;
    mRotScale = 0.0f;
    mRotX = 0.0f;
	mRotY = 0.0f;

	// Physics
	mSimulation = 0;
	mBullet = 0;
	mPhysicsPaused = false;
	mPhysicsSpeed = 1.0f;

	// Misc
	mSceneDetailIndex = 0;
	mStatsOn = true;
	mNumScreenShots = 0;
}

// Destructor
Application::~Application()
{
	// Destroy the input reader
	PlatformManager::getSingleton().destroyInputReader(mInputDevice);

	// Delete existing member objects
	if (mSimulation)
	{
		delete mSimulation;
	}
	if (mRoot)
	{
		delete mRoot;
	}
}

// go()
void Application::go(void)
{
	// Setup
	if (!setup())
	{
		// Failure
		return;
	}

	// Begin rendering cycle
	mRoot->startRendering();
}

// setup()
bool Application::setup(void)
{
	// Create a new Ogre root
	mRoot = new Root();

	// Setup resources
	setupResources();

	// Get configuration
	if (!configure())
	{
		// Failure
		return false;
	}

	// Get a generic scene manager
	mSceneMgr = mRoot->getSceneManager(ST_GENERIC);

	// Create the camera
	mCamera = mSceneMgr->createCamera("BasicCam");
	mCamera->setPosition(Vector3(0,200,750));
	mCamera->lookAt(Vector3(0,200,-300));
	mCamera->setNearClipDistance(5);

	// Create one viewport covering the entire window
	Viewport* vp = mWindow->addViewport(mCamera);
	vp->setBackgroundColour(ColourValue(0,0,0));

	// Set default mipmap level
	TextureManager::getSingleton().setDefaultNumMipMaps(5);

	// Create the Simulation
	mSimulation = new Simulation();

	// Set ambient light
	mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));

	// Set skydome
	mSceneMgr->setSkyDome(true, "Demo/Sky", 5, 8);

	// Set up a single light to give things some definition
	Light* l = mSceneMgr->createLight("sceneLight");
	l->setType(Light::LT_POINT);
	l->setPosition(-1000.0f, 1000.0f, 1000.f);

	// Setup the basic testing sim
	mBullet = setupTestSim1(mSceneMgr->getRootSceneNode());
	GuiManager::getSingleton().getGuiElement("OgreTok/StatsPanel/Description")->setCaption("Test Sim #1: A 5 story building and arbitrary obstacles."); 

	// Create an input reader
	mInputDevice = PlatformManager::getSingleton().createInputReader();
	mInputDevice->initialise(mWindow, true, true);

	// Create the basic frame listener
	this->showOverlay("OgreTok/BasicOverlay", true);
	mRoot->addFrameListener(this);

	// Successful setup
	return true;
}

// configure()
bool Application::configure(void)
{
	// Get user-selected configuration
	if (mRoot->showConfigDialog())
	{
		// Create a default rendering window
		 mWindow = mRoot->initialise(true);

		// Successful configuration
		return true;
	}
	else
	{
		// User clicked cancel
		return false;
	}
}

// setupResources()
void Application::setupResources(void)
{
	// Load resource paths from the config file
	ConfigFile cf;
	cf.load("resources.cfg");

	// Get an iterator to move through the settings
	ConfigFile::SettingsIterator i = cf.getSettingsIterator();

	// Strings to hold the key and value
	String key, value;

	// Iterate through the settings
	while (i.hasMoreElements())
	{
		// Get the key (eg. Zip, FileSystem)
		key = i.peekNextKey();

		// Get the value (eg. ../media/skybox.zip) and progress to the next setting
		value = i.getNext();

		// Add this source as a common path for all resource types
		ResourceManager::addCommonArchiveEx(value, key);
	}
}

// frameStarted()
bool Application::frameStarted(const FrameEvent& evt)
{
	// Capture input data
	mInputDevice->capture();

	// Pass time to next allowed mode action
	if (mTimeUntilNextAction >= 0)
	{
		mTimeUntilNextAction -= evt.timeSinceLastFrame;
	}

	// First frame
	if (evt.timeSinceLastFrame == 0)
	{
		// Set movement and rotation speed
		mMoveScale = 1;
		mRotScale = 0.1;
	}
	// Scale movement units by time passed since last frame
	else
	{
		// Move about 100 units per second,
		mMoveScale = 100.0 * evt.timeSinceLastFrame;

		// Take about 10 seconds for full rotation
		mRotScale = 36 * evt.timeSinceLastFrame;
	}

	// Reset camera movement
	mRotX = 0;
	mRotY = 0;
	mTranslateVector = Vector3::ZERO;

	// Process key input
	if (processUnbufferedKeyInput() == false)
	{
		return false;
	}

	//Process mouse input
	if (processUnbufferedMouseInput() == false)
	{
		return false;
	}
		
	// Rotate the camera
	mCamera->yaw(mRotX);
	mCamera->pitch(mRotY);

	// Move the camera
	mCamera->moveRelative(mTranslateVector);

	// Simulation not paused
	if(!mPhysicsPaused)
	{
		// Update physical simulation
		Simulation::getSingleton().update(evt.timeSinceLastFrame * mPhysicsSpeed);
	}

	// Continue program
	return true;
}

// frameEnded()
bool Application::frameEnded(const FrameEvent& evt)
{
	// Update debug stats
	this->updateStats();

	// Continue program
	return true;
}

// processUnbufferedKeyInput()
bool Application::processUnbufferedKeyInput(void)
{
	// Key 'Escape' is down
	if (mInputDevice->isKeyDown(KC_ESCAPE))
	{
		// Quit program
		return false;
	}

	// Key 'Left' or 'A' down
	if (mInputDevice->isKeyDown(KC_LEFT) || mInputDevice->isKeyDown(KC_A))
	{
		// Move camera left
		mTranslateVector.x = -mMoveScale;
	}

	// Key 'Right' or 'D' down
	if (mInputDevice->isKeyDown(KC_RIGHT) || mInputDevice->isKeyDown(KC_D))
	{
		// Move camera right
		mTranslateVector.x = mMoveScale;
	}

	// Key 'Up' or 'W' is down
	if (mInputDevice->isKeyDown(KC_UP) || mInputDevice->isKeyDown(KC_W))
	{
		// Move camera forward
		mTranslateVector.z = -mMoveScale;
	}

	// Key 'Down' or 'S' is down
	if (mInputDevice->isKeyDown(KC_DOWN) || mInputDevice->isKeyDown(KC_S) )
	{
		// Move camera backward
		mTranslateVector.z = mMoveScale;
	}

	// Key 'PageUp' is down
	if (mInputDevice->isKeyDown(KC_PGUP))
	{
		// Move camera up
		mTranslateVector.y = mMoveScale;
	}

	// Key 'PageDown' is down
	if (mInputDevice->isKeyDown(KC_PGDOWN))
	{
		// Move camera down
		mTranslateVector.y = -mMoveScale;
	}

	// Key 'F' is down and it has been sufficient time since the last action
	if (mInputDevice->isKeyDown(KC_F) && mTimeUntilNextAction <= 0.0f)
	{
		// action debug overlay
		mStatsOn = !mStatsOn;
		this->showOverlay("OgreTok/BasicOverlay", mStatsOn);

		// Wait at least 0.25 seconds to perform another mode action
		mTimeUntilNextAction = 0.25f;
	}

	// Key 'SysRq' is down and it has been sufficient time since the last action
	if (mInputDevice->isKeyDown(KC_SYSRQ) && mTimeUntilNextAction <= 0.0f)
	{
		// Create a filename
		char fileName[20];
		sprintf(fileName, "screenshot_%d.png", ++mNumScreenShots);

		// Screenshot the window to the file
		mWindow->writeContentsToFile(fileName);

		// Wait at least 0.25 seconds to perform another action
		mTimeUntilNextAction = 0.25f;
		mWindow->setDebugText(String("Wrote ") + fileName);
	}

	// Key 'R' is down and it has been sufficient time since the last action
	if (mInputDevice->isKeyDown(KC_R) && mTimeUntilNextAction <= 0.0f)
	{
		// Set next level of detail
		mSceneDetailIndex = ((mSceneDetailIndex + 1) % 3);

		// Apply level of detail to camera

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -