ogre 3d 1.7 beginner’s guide 第四章 第五小节 中文翻译 添加输入支持
Adding input support
【 添加输入支持 】
We now have a moving scene, but we would like to be able to exit our application like
before. Therefore, we are now going to add input support, and when Escape is pressed, we exit our application. Up until now, we only used Ogre 3D; now we will also use OIS(Object Oriented Input System), which comes with the Ogre 3D SDK because it is used by the ExampleFrameListener, but otherwise is totally independent from Ogre 3D.
【 我们现在已经有含移动物体的场景,但是我们想要程序可以像原来一样退出。因此,我们将会添加输入支持,并当Escape键按下的时候,我们可以退出程序。目前为止,我们只使用了Ogre 3D;现在,我们将会使用OIS (Object Oriented Input System[面向对象的输入系统]),OIS是Ogre 3D SDK自带的,因为它被ExampleFrameListener所使用,但是在其他方面它是从Ogre 3D 中完全独立出来的。】
Time for action – adding input support
【 实践时刻 —— 添加输入支持 】
Again, we use the previous code and add the necessary additions to get input support:
【 同样的,我们使用之前的代码并添加必要的代码以获得输入支持:】
- We need to add a new parameter to the constructor of our listener. We need a pointer to the render window that Ogre 3D uses to render. To add the new parameter, the code should look like this:
【 1. 我们需要添加一个新的参数到Listener的构造函数。】
Example27FrameListener(Ogre::SceneNode* node,RenderWindow* win)
- When changing the constructor, we also need to change the instantiation:
【 2. 当改变构造函数时,我们也需要改变实例化的地方:】
Ogre::FrameListener* FrameListener = new Example27FrameListener(_
SinbadNode,mWindow);
3. After this, we need to add code into the constructor of the listener. First, we need
two helper variables:
【 3. 在这之后,我们需要添加代码到listener的构造函数。首先,我们需要两个辅助变量: 】
size_t windowHnd = 0;
std::stringstream windowHndStr;
- Now ask Ogre 3D for the window handle it renders to:
【 3. 现在请求Ogre 3D获取渲染的窗口句柄:】
win->getCustomAttribute("WINDOW", &windowHnd);
- Convert the handle into a string:
【 4. 转换handle为一个string:】
windowHndStr << windowHnd;
6. Create a parameter list for OIS and add the window handle to it:
【 6.创建一个OIS参数表并添加窗口句柄:】
(译者注:这章中参数表代表ParamList,而ParamList是OISPrereqs.h中std::multimap<std::string, std::string>的别名,而multimap请参考STL的容器。)
OIS::ParamList pl;
pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
7. Create the input system using this parameter list:
【 7. 使用参数表创建输入系统:】
_man = OIS::InputManager::createInputSystem( pl );
8. Then create a keyboard so that we can check for user input:
【 8. 然后创建一个keyboard,这样我们就可以检查用户的输入了:】
_key = static_cast<OIS::Keyboard*>(_man->createInputObject(OIS::OISKeyboard, false ));
9. What we create, we must destroy. Add a destructor to the FrameListener, which
destroys our created OIS objects:
【 9. 我们创建的对象,必须在最后回收。添加一个析构函数到帧监听类中,这将会析构我们的OIS对象:】
~Example27FrameListener()
{
_man->destroyInputObject(_key);
OIS::InputManager::destroyInputSystem(_man);
}
10. After we have finished the initialization, add the following code into the
frameStarted() method after the node translation and before the return:
【 10. 在我们完成初始化之后,添加下面的代码到frameStarted() 函数中的node变换之后,return之前:】
_key->capture();
if(_key->isKeyDown(OIS::KC_ESCAPE))
{
return false;
}
11. Add the used input objects as member variables to the FrameListener:
【 11. 添加用户输入对象作为成员变量到帧监听类中:】
OIS::InputManager* _man;
OIS::Keyboard* _key;
12. Compile and run the application. You should now be able to exit the application by
pressing the Escape key.
【 12. 编译运行程序。你现在应该可以看到程序可以通过按Escape键退出。】
What just happened?
【 刚刚发生了什么?】
We created an instance of an input system and used this to capture key presses from the
user. Because we need the window handle for the creation of the input system, we changed the constructor of the FrameListener so it gets a pointer to the render window passed. This was done in step 1. We then converted the handle from a number into a string and added this string into the parameter list of OIS. With this list, we created our instance of the input system.
【 我们创建了一个输入系统的实例并使用实例来获取用户的键盘输入。因为我们需要窗口的句柄来创建输入系统,我们改变帧监听类的构造函数,这样它就可以接受传递过来的一个渲染窗口的指针。这已经在第一步中完成了。我们然后从数值到字符串转换句柄并添加字符串到OIS的参数表。通过参数表,我们可以创建我们输入系统的实例。】