Time for action — adding input support

Again, we use the previous code and add the necessary additions to get input support:

  1. 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:
    Example27FrameListener(Ogre::SceneNode* node,RenderWindow* win)
    
  2. When changing the constructor, we also need to change the instantiation:
    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:
    size_t windowHnd = 0;
    std::stringstream windowHndStr;
    
  4. Now ask Ogre 3D for the window handle it renders to:
    win->getCustomAttribute("WINDOW", &windowHnd);
    
  5. Convert the handle into a string:
    windowHndStr << windowHnd;
    
  6. Create a parameter list for OIS and add the window handle to it:
    OIS::ParamList pl;
    pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
    
  7. Create the input system using this parameter list:
    _man = OIS::InputManager::createInputSystem( pl );
    
  8. Then create a keyboard so that we can check for user input:
    _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:
    ~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:
    _key->capture();
    if(_key->isKeyDown(OIS::KC_ESCAPE))
    {
    return false;
    }
    
  11. Add the used input objects as member variables to the FrameListener:
    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.

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.

Window handle

A window handle is simply a number that is used as an identifier for a certain window. This number is created by the operating system and each window has a unique handle. The input system needs this handle because without it, it couldn't get the input events. Ogre 3D creates a window for us. So to get the window handle, we need to ask it the following line:

win->getCustomAttribute("WINDOW", &windowHnd);

There are several attributes that a render window has, so Ogre 3D implements a general getter function. It is also needed to be platform independent; each platform has its own variable types for window handles, so this is the only way for it to be cross platform. WINDOW is the keyword for the window handle. We need to pass to the function a pointer to storage for the handle value; into this pointer the value will be written. After we receive the handle, we convert it into a string using a stringstream because this is what OIS expects. OIS has the same problem and uses the same solution. During creation, we give OIS a list with parameter pairs consisting of an identifier string and a value string. In step 6, we created this parameter list and added the window handle string. Step 7 used this list to create the input system. With the input system, we can create our keyboard interface in step 8. This interface will be used to query the system which keys are pressed by the user? This is done with the code in step 9. Every time, before we render a frame, we capture the new state of the keyboard using the capture() function. If we didn't call this function, we won't get the newest state of the keyboard and therefore we won't receive any keyboard events ever. After updating the state, we query the keyboard if the escape key is pressed right now. When this is true, we know the user wants to quit the application. This means we must return false to let Ogre 3D know that we want the application to be shut down. Otherwise, if the user wants the application to keep running, we can return true to keep the application running.

Pop quiz — window questions

What is a window handle and how is it used by our application and the operating system?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.189.129