OIS使用心得

OIS使用心得

总的来说,非常的好用,Ogre在即将推出的1.4版本中将完全去处原来破破烂烂的输入部分,取而代之得是OIS(Object-oriented Input Library),对我来说,Ogre去除自带的输入系统是个非常好的消息,这本来就不是一个图形渲染系统该有的,OIS出现的太及时了,OIS的作者是Ogre的MVP,我看过他的代码,功力深厚,虽然现在OIS功能还不是很高,但是比原来的那个还是要好的多,提供了立即模式和缓冲模式(可共用),也就是设计模式那一套路,用个监听器得到消息,这样的封装用起来非常简单。
class EventHandler : public KeyListener, public MouseListener, public JoyStickListener
{
public:
 EventHandler() {}
 ~EventHandler() {}
 bool keyPressed( const KeyEvent &arg ) {
  std::cout << "\nKeyPressed {" << arg.key
   << ", " << ((Keyboard*)(arg.device))->getAsString(arg.key)
   << "} || Character (" << (char)arg.text << ")" << std::endl;
  return true;
 }
 bool keyReleased( const KeyEvent &arg ) {
  if( arg.key == KC_ESCAPE || arg.key == KC_Q )
   appRunning = false;
  return true;
 }
 bool mouseMoved( const MouseEvent &arg ) {
  const OIS::MouseState& s = arg.state;
  std::cout << "\nMouseMoved: Abs("
      << s.abX << ", " << s.abY << ", " << s.abZ << ") Rel("
      << s.relX << ", " << s.relY << ", " << s.relZ << ")";
  return true;
 }
 bool mousePressed( const MouseEvent &arg, MouseButtonID id ) {
  std::cout << "\nMousePressed: " << id << " time[" << arg.timeStamp << "]";
  return true;
 }
 bool mouseReleased( const MouseEvent &arg, MouseButtonID id ) {
  std::cout << "\nMouseReleased: " << id << " time[" << arg.timeStamp << "]";
  return true;
 }
 bool buttonPressed( const JoyStickEvent &arg, int button ) {
  std::cout << "\nJoy ButtonPressed: " << button << " time[" << arg.timeStamp << "]";
  return true;
 }
 bool buttonReleased( const JoyStickEvent &arg, int button ) {
  return true;
 }
 bool axisMoved( const JoyStickEvent &arg, int axis )
 {
     std::cout << "\nJoy Axis " << axis << " " << arg.state.mAxes[axis].abX
                  << " " << arg.state.mAxes[axis].abY;
  return true;
 }
 bool povMoved( const JoyStickEvent &arg, int pov )
 {
  std::cout << "\nJoy POV" << pov << " ";

  if( arg.state.mPOV[pov].direction & Pov::North ) //Going up
   std::cout << "North";
  else if( arg.state.mPOV[pov].direction & Pov::South ) //Going down
   std::cout << "South";

  if( arg.state.mPOV[pov].direction & Pov::East ) //Going right
   std::cout << "East";
  else if( arg.state.mPOV[pov].direction & Pov::West ) //Going left
   std::cout << "West";

  if( arg.state.mPOV[pov].direction == Pov::Centered ) //stopped/centered out
   std::cout << "Centered";
  return true;
 }
};

上面的代码就是定义了一个集键盘、鼠标、手柄消息控制于一身的监听器,有相应事件产生就会调用相应的函数,使用及其方便,我把OIS的VC8版本接入到我原来的一个2D引擎中,工作非常顺利,这也使我彻底的淘汰了原来不良的设计,如果你有自己的引擎,你也可以试一下,OIS 下载 http://sourceforge.net/projects/wgois/ ,再来个Ogre对于他的介绍 http://www.ogre3d.org/wiki/index.php/Using_OIS 。

你可能感兴趣的:(OIS使用心得)