紧跟上一节的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;
}