在Ogre中使用OIS相应键盘事件

紧跟上一节的Ogre最小系统,这次添加键盘相应事件到系统中(鼠标和键盘十分相似,这里就没有列出了)


/* ===================================================== */
//	
//    ◢█████◣               ◢██████◣
// ◢◤             ◥◣         ◢◤             ◥◣ 
// ◤                 ◥◣     ◢◤                 █ 
// ▎      ◢█◣       ◥◣◢◤       ◢█◣       █
// ◣     ◢◤  ◥◣               ◢◣   ◥◣   ◢ 
// ◥██◤   ◢◤                   ◥◣   ◥█◤
//          █   ●               ●    █ 
//          █   〃       ▄       〃   █ 
//           ◥◣       ╚╩╝       ◢◤ 
//             ◥█▅▃▃   ▃▃▅█◤ 
//               ◢◤       ◥◣  
//               █           █  
//             ◢◤▕       ▎◥◣
//           ▕▃◣◢▅▅▅◣◢▃▎
//            
//                 Author : Wangxu
//            Create Date :				
//            Description : 
//
/* ===================================================== */	

#pragma warning(disable: 4251)
#pragma warning(disable: 4193)
#pragma warning(disable: 4275)

// 需要包含OIS.h头文件,并连接其lib
#include <Ogre/Ogre.h>
#include <OIS/OIS.h>
// 链接基本的类和函数
#pragma comment(lib,"OgreMain_d.lib")
#pragma comment(lib,"OIS_d.lib")
using namespace Ogre;

// 程序终止的控制变量
bool g_bShouldExit = false;

class MyKeyListener : public OIS::KeyListener
{
public:
	virtual bool keyPressed(const OIS::KeyEvent& arg)
	{
		return true;
	}

	virtual bool keyReleased(const OIS::KeyEvent&arg)
	{
		// 如果有按键释放,并且为ESC,则设置退出控制变量为true
		if (arg.key == OIS::KC_ESCAPE)
		{
			g_bShouldExit = true;
		}
		return true;
	}
};

int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
	Root* root = new Root("min_plugins.cfg");
	if(!root->showConfigDialog())
	{
		return -1;
	}

	RenderWindow* window = root->initialise(true);
	SceneManager* sceneMgr = root->createSceneManager(ST_GENERIC);
	Camera* cam = sceneMgr->createCamera("MainCamera");
	Viewport* viewport = window->addViewport(cam);
	cam->setAspectRatio((Ogre::Real)viewport->getActualWidth() / (Ogre::Real)viewport->getActualHeight());
	cam->setNearClipDistance(5);
	cam->setPosition(0,0,600);

	// 设置输入系统的参数
	OIS::ParamList pl;
	size_t winHandle = 0;
	std::ostringstream winHandleStr;
	window->getCustomAttribute("WINDOW", &winHandle);
	winHandleStr << winHandle;
	pl.insert(std::make_pair("WINDOW", winHandleStr.str()));

	// 创建并注册输入系统
	OIS::InputManager* mInputMgr = OIS::InputManager::createInputSystem(pl);
	OIS::Keyboard* mKeyboard = static_cast<OIS::Keyboard*>(mInputMgr->createInputObject(OIS::OISKeyboard, true));
	mKeyboard->setEventCallback(new MyKeyListener());


	// 显示窗口,开始渲染,进入无限循环
	// 这次我们手动调用renderOneFrame来渲染界面,通过g_bShouldExit判断是否退出
	while(!g_bShouldExit)
	{
		// 取消息,否则窗口不处理用户输入
		WindowEventUtilities::messagePump();
		root->renderOneFrame();
		// 捕获用户的输入,如果有按键,则回调MyKeyListener中的两个方法
		mKeyboard->capture();
			
	}

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值