现在的位置: 首页 > 综合 > 正文

第五章 Buffered input

2013年07月29日 ⁄ 综合 ⁄ 共 6455字 ⁄ 字号 评论关闭

缓冲输入和非缓冲输入的区别就是:缓冲输入靠事件来通知,一旦事件触发了再会去执行;而非缓冲区是每帧都去检测,这样会消耗了一些不必要的资源

/*------------------------------------------------------------
	BasicTutorial5.h -- Buffered Input head file
			(c) Seamanj.2013/8/21
------------------------------------------------------------*/

#ifndef __BasicTutorial05_h_
#define __BasicTutorial05_h_
 
#include "BaseApplication.h"
 
class BasicTutorial05 : public BaseApplication
{
public:
    BasicTutorial05(void);
    virtual ~BasicTutorial05(void);
 
protected:
    virtual void createScene(void);
	virtual void createFrameListener(void);
 
	// Ogre::FrameListener
    virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt);
    // OIS::KeyListener
    virtual bool keyPressed( const OIS::KeyEvent &arg );
    virtual bool keyReleased( const OIS::KeyEvent &arg );
    // OIS::MouseListener
    virtual bool mouseMoved( const OIS::MouseEvent &arg );
    virtual bool mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id );
    virtual bool mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id );
 
	Ogre::Real mRotate;          // The rotate constant
	Ogre::Real mMove;            // The movement constant
 
	Ogre::SceneNode *mCamNode;   // The SceneNode the camera is currently attached to
 
	Ogre::Vector3 mDirection;     // Value to move in the correct direction
 
};
 
#endif // #ifndef __BasicTutorial05_h_

/*------------------------------------------------------------
	BasicTutorial5.cpp -- Buffered Input source file
			(c) Seamanj.2013/8/21
------------------------------------------------------------*/

/*
In the previous tutorial we used unbuffered input, that is, every frame we queried the state of OIS::Keyboard and 
OIS::Mouse instances to see what keys and mouse buttons were being held down. Buffered input uses listener interfaces
to inform your program that events have occurred. For example, when a key is pressed, a KeyListener::keyPressed event
is fired and when the button is released (no longer being pressed) a KeyListener::keyReleased event is fired to all 
registered KeyListener classes. This takes care of having to keep track of toggle times or whether the key was unpressed 
the previous frame. 
*/

#include "BasicTutorial5.h"

//-------------------------------------------------------------------------------------
BasicTutorial05::BasicTutorial05(void)
{
}
//-------------------------------------------------------------------------------------
BasicTutorial05::~BasicTutorial05(void)
{
}

//-------------------------------------------------------------------------------------
void BasicTutorial05::createScene(void)
{
	mSceneMgr->setAmbientLight(Ogre::ColourValue(0.25, 0.25, 0.25));

	// add the ninja
	Ogre::Entity *ent = mSceneMgr->createEntity("Ninja", "ninja.mesh");
	Ogre::SceneNode *node = mSceneMgr->getRootSceneNode()->createChildSceneNode("NinjaNode");
	node->attachObject(ent);

	// create the light
	Ogre::Light *light = mSceneMgr->createLight("Light1");
	light->setType(Ogre::Light::LT_POINT);
	light->setPosition(Ogre::Vector3(250, 150, 250));
	light->setDiffuseColour(Ogre::ColourValue::White);
	light->setSpecularColour(Ogre::ColourValue::White);

	// Create the scene node
	node = mSceneMgr->getRootSceneNode()->createChildSceneNode("CamNode1", Ogre::Vector3(-400, 200, 400));
	// Make it look towards the ninja
	node->yaw(Ogre::Degree(-45));
	node->attachObject(mCamera);


	// create the second camera  node
	mSceneMgr->getRootSceneNode()->createChildSceneNode("CamNode2", Ogre::Vector3(0, 200, 400));


}
void BasicTutorial05::createFrameListener(void){
	BaseApplication::createFrameListener();
	/*注意键盘,鼠标等的触发事件通过,下面这两局来设置
	mMouse->setEventCallback(this);
	mKeyboard->setEventCallback(this);
	注意BaseApplication的基类有KeyListener,MouseListener
	class BaseApplication : public Ogre::FrameListener, 
	public Ogre::WindowEventListener, 
	public OIS::KeyListener, 
	public OIS::MouseListener, 
	OgreBites::SdkTrayListener
	这里我们不需要添加,因为BaseApplication::createFrameListener();里面已经添加这两句了
	*/

	// Populate the camera container
	mCamNode = mCamera->getParentSceneNode();

	// set the rotation and move speed
	mRotate = 0.13;
	mMove = 250;

	mDirection = Ogre::Vector3::ZERO;

}
bool BasicTutorial05::frameRenderingQueued(const Ogre::FrameEvent& evt){
	if(mWindow->isClosed())
		return false;

	if(mShutDown)
		return false;

	//Need to capture/update each device
	mKeyboard->capture();
	mMouse->capture();

	mTrayMgr->frameRenderingQueued(evt);//makes sure our tray UI is rendered correctly.
	mCamNode->translate(mDirection * evt.timeSinceLastFrame, Ogre::Node::TS_LOCAL);
	return true;}

// OIS::KeyListener
bool BasicTutorial05::keyPressed( const OIS::KeyEvent &arg ){
	switch (arg.key)
	{
	case OIS::KC_1:
		mCamera->getParentSceneNode()->detachObject(mCamera);
		mCamNode = mSceneMgr->getSceneNode("CamNode1");
		mCamNode->attachObject(mCamera);
		break;

	case OIS::KC_2:
		mCamera->getParentSceneNode()->detachObject(mCamera);
		mCamNode = mSceneMgr->getSceneNode("CamNode2");
		mCamNode->attachObject(mCamera);
		break;
	case OIS::KC_UP:
	case OIS::KC_W:
		mDirection.z = -mMove;
		break;

	case OIS::KC_DOWN:
	case OIS::KC_S:
		mDirection.z = mMove;
		break;

	case OIS::KC_LEFT:
	case OIS::KC_A:
		mDirection.x = -mMove;
		break;

	case OIS::KC_RIGHT:
	case OIS::KC_D:
		mDirection.x = mMove;
		break;

	case OIS::KC_PGDOWN:
	case OIS::KC_E:
		mDirection.y = -mMove;
		break;

	case OIS::KC_PGUP:
	case OIS::KC_Q:
		mDirection.y = mMove;
		break;

	case OIS::KC_ESCAPE: 
		mShutDown = true;
		break;
	default:
		break;
	}
	return true;
}

bool BasicTutorial05::keyReleased( const OIS::KeyEvent &arg ){

	switch (arg.key)
	{
	case OIS::KC_UP:
	case OIS::KC_W:
		mDirection.z = 0;
		break;

	case OIS::KC_DOWN:
	case OIS::KC_S:
		mDirection.z = 0;
		break;

	case OIS::KC_LEFT:
	case OIS::KC_A:
		mDirection.x = 0;
		break;

	case OIS::KC_RIGHT:
	case OIS::KC_D:
		mDirection.x = 0;
		break;

	case OIS::KC_PGDOWN:
	case OIS::KC_E:
		mDirection.y = 0;
		break;

	case OIS::KC_PGUP:
	case OIS::KC_Q:
		mDirection.y = 0;
		break;

	default:
		break;
	}
	return true;
}
// OIS::MouseListener
bool BasicTutorial05::mouseMoved( const OIS::MouseEvent &arg ){
	if (arg.state.buttonDown(OIS::MB_Right))
	{
		mCamNode->yaw(Ogre::Degree(-mRotate * arg.state.X.rel), Ogre::Node::TS_WORLD);
		mCamNode->pitch(Ogre::Degree(-mRotate * arg.state.Y.rel), Ogre::Node::TS_LOCAL);
	}
	return true;
}
bool BasicTutorial05::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id ){
	Ogre::Light *light = mSceneMgr->getLight("Light1");
	switch (id)
	{
	case OIS::MB_Left:
		light->setVisible(! light->isVisible());
		break;
	default:
		break;
	}
	return true;
}
bool BasicTutorial05::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id ){return true;}



#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
	INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
	int main(int argc, char *argv[])
#endif
	{
		// Create application object
		BasicTutorial05 app;

		try {
			app.go();
		} catch( Ogre::Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
			MessageBoxA( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
			std::cerr << "An exception has occured: " <<
				e.getFullDescription().c_str() << std::endl;
#endif
		}

		return 0;
	}

#ifdef __cplusplus
}
#endif

抱歉!评论已关闭.