⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cdirectinput.cpp

📁 This tutorial will deal with getting input using Direct Input. All you will need to run and/or com
💻 CPP
字号:
/*
   Class Name:

      CDirectInputSystem, CInputKeyboard, CInputMouse.

   Created by:

      Allen Sherrod (Programming Ace of www.UltimateGameProgramming.com).

   Description:

      These classes are used with DirectInput to get input from a keyboard and/or mouse.
*/


#include"CDirectInput.h"      // Our input system, keyboard, and mouse class.


CDirectInputSystem::CDirectInputSystem()
{
   // Initialize objects...
   m_Keyboard = NULL;
   m_Mouse = NULL;
}


CDirectInputSystem::~CDirectInputSystem()
{
   // Shut everything down.
   Shutdown();
}


bool CDirectInputSystem::Initialize(HWND hwnd, HINSTANCE hInstance, bool mouseExclusive)
{
   // Now we are ready to create the input system.  Call DirectInput8Create() function
   // to create the Direct Input object.  This function needs the HINSTANCE object, the
   // version, what is called a unique identifier, the LPDIRECTINPUT8 object, and
   // the last field is used for COM.  Check out the DirectX SDK for more on that one.
   if(FAILED(DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8,
                                (void **)&m_InputSystem, NULL)))
      return false;

   // Create the keyboard object.
   m_Keyboard = new CInputKeyboard;

   // Error checking...
   if(m_Keyboard == NULL)
      return false;

   // Intialize out keyboard object.  The keyboard will need a the LPDIRECTINPUT8 object
   // and the HWND object in order to correctly create the device.
   if(!m_Keyboard->Initialize(m_InputSystem, hwnd))
      return false;

   // Create the mouse object.
   m_Mouse = new CInputMouse;

   // Error checking...
   if(m_Mouse == NULL)
      return false;

   // Initialize the mouse object.  This too needs the LPDIRECTINPUT8 object, HWND object,
   // and a value if this mouse is exclusive to this application or not.
   if(!m_Mouse->Initialize(m_InputSystem, hwnd, false))
      return false;

   return true;
}


bool CDirectInputSystem::AcquireDevices()
{
   // Each device has a function called Acquire().  This function will give the application
   // access to the device in question.  So AcquireDevices function of our system will
   // give us access to the mouse and keyboard.  If you was to minimize your game you would
   // unacquire the devices so you can use them then we the game is restored Acquire them so
   // we can play the game.
   if(m_Mouse)
      m_Mouse->AcquireDevice();

   if(m_Keyboard)
      m_Keyboard->AcquireDevice();

   return true;
}


bool CDirectInputSystem::UnAcquireDevices()
{
   // The opposite of AcquireDevices.  This will take access away from out application.
   if(m_Mouse)
      m_Mouse->UnAcquireDevice();

   if(m_Keyboard)
      m_Keyboard->UnAcquireDevice();

   return true;
}


bool CDirectInputSystem::UpdateDevices()
{
   // Updating devices will allow us to check for input either which key(s) are pressed
   // on the keyboard or which mouse button(s) are being pressed.
   if(m_Mouse)
      m_Mouse->UpdateDevice();

   if(m_Keyboard)
      m_Keyboard->UpdateDevice();

   return true;
}


bool CDirectInputSystem::KeyUp(int key)
{
   // If the key in question is not pressed then we return true, else false.
   if(!m_Keyboard)
      return false;

   return m_Keyboard->KeyUp(key);
}


bool CDirectInputSystem::KeyDown(int key)
{
   // If the key in question is pressed then we return true, else false.
   if(!m_Keyboard)
      return false;

   return m_Keyboard->KeyDown(key);
}


bool CDirectInputSystem::MouseButtonUp(int button)
{
   // If the button in question is not clicked we return true, else false.
   if(!m_Mouse)
      return false;

   return m_Mouse->ButtonUp(button);
}


bool CDirectInputSystem::MouseButtonDown(int button)
{
   // If the button in question is clicked we return true, else false.
   if(!m_Mouse)
      return false;

   return m_Mouse->ButtonDown(button);
}


bool CDirectInputSystem::Shutdown()
{
   // Take access away from all devices...
   UnAcquireDevices();

   // Delete each object...
   if(m_Keyboard == NULL)
      {
         delete m_Keyboard;
         m_Keyboard = NULL;
      }


   if(m_Mouse == NULL)
      {
         delete m_Mouse;
         m_Mouse = NULL;
      }

   // Release all of the resources out Direct Input object used.
   if(m_InputSystem)
      {
         if(FAILED(m_InputSystem->Release()))
            return false;
      }

   return true;
}


/*//////////////////////////////////////////////////////////////////////////////

Mouse Class

*//////////////////////////////////////////////////////////////////////////////


CInputMouse::CInputMouse()
{

}


CInputMouse::~CInputMouse()
{
   // Shutdown the mouse object.
   Shutdown();
}


bool CInputMouse::Initialize(LPDIRECTINPUT8 m_InputSystem, HWND hwnd, bool isExclusive)
{
   DWORD flags;

   // First we call CreateDevice to create the input device.  This function needs a ID
   // for the device, our input device object that will represent this device, and
   // some stuff for COM.  Check the SDK for more on the last one.
   if(FAILED(m_InputSystem->CreateDevice(GUID_SysMouse, &m_Mouse, NULL)))
      return false;

   // Next we will need to set the data format.  This will tell the input device object
   // what kind of object this is (keyboard, mouse, joystick, etc).
   if(FAILED(m_Mouse->SetDataFormat(&c_dfDIMouse)))
      return false;

   // Next we will set some flags for our mouse.   The first is for if we want the mouse
   // to work exclusively with only our application, the second for the opposite.
   if(isExclusive)
      flags = DISCL_FOREGROUND | DISCL_EXCLUSIVE | DISCL_NOWINKEY;
   else
      flags = DISCL_FOREGROUND | DISCL_NONEXCLUSIVE;

   // Calling SetCooperativeLevel will set the flags and options for our mouse device.
   if(FAILED(m_Mouse->SetCooperativeLevel(hwnd, flags)))
      return false;

   // Now we can gain access to the device.  This is done by calling Acquire().  Acquire()
   // will give out application access to the mouse.
   if(FAILED(m_Mouse->Acquire()))
      return false;

   // GetDeviceState() will give us the state the mouse is in.  In order words we can find
   // out if the mouse is moving or if one of its buttons are pressed.  All of this information
   // is saved in m_Mouse_State object.  This function needs the size of the state object and
   // it also needs the object to hold the device states.
   if(FAILED(m_Mouse->GetDeviceState(sizeof(DIMOUSESTATE), &m_Mouse_State)))
      return false;

   return true;
}


bool CInputMouse::ButtonUp(int button)
{
   // Here we will test if the button in question is not pressed.  We do this by
   // testing the state of the mouse's buttons.  If the one we are looking for
   // is not pressed then we return true, else false.
   if(m_Mouse_State.rgbButtons[button] & 0x80)
      return false;

   return true;
}


bool CInputMouse::ButtonDown(int button)
{
   // Here we will test if the button in question is pressed.  We do this by
   // testing the state of the mouse's buttons.  If the one we are looking for
   // is pressed then we return true, else false.
   if(m_Mouse_State.rgbButtons[button] & 0x80)
      return true;

   return false;
}


bool CInputMouse::AcquireDevice()
{
   // Acquire() will give our application access to this device.
   if(FAILED(m_Mouse->Acquire()))
      return false;

   return true;
}


bool CInputMouse::UnAcquireDevice()
{
   // Here we will take away access from our application using Unacquire().
   if(FAILED(m_Mouse->Unacquire()))
      return false;

   return true;
}


bool CInputMouse::UpdateDevice()
{
   // UpdateDevice() will update the device by setting all the buttons that are pressed
   // equal to a non 0 value.  If there is a issue with calling the GetDeviceState()
   // function we will call Acquire() to make sure we have access.  If that fails then
   // something is really wrong.  If it works then we will try to call GetDeviceState()
   // again.  If all is good we return true, else false because something is really wrong.
   // The button states are saved in m_Mouse_State object so we can test it later for the
   // information we want.
   if(FAILED(m_Mouse->GetDeviceState(sizeof(DIMOUSESTATE), &m_Mouse_State)))
      {
         if(FAILED(m_Mouse->Acquire()))
            return false;

         if(FAILED(m_Mouse->GetDeviceState(sizeof(DIMOUSESTATE), &m_Mouse_State)))
            return false;
      }

   return true;
}


bool CInputMouse::Shutdown()
{
   // Take away access of the mouse and free any resources we used.
   if(m_Mouse)
      {
         UnAcquireDevice();
         m_Mouse->Release();
      }

   return true;
}


/*//////////////////////////////////////////////////////////////////////////////

Keyboard Class

*//////////////////////////////////////////////////////////////////////////////


CInputKeyboard::CInputKeyboard()
{

}


CInputKeyboard::~CInputKeyboard()
{
   // Shut down the keyboard object.
   Shutdown();
}


bool CInputKeyboard::Initialize(LPDIRECTINPUT8 m_InputSystem, HWND hwnd)
{
   // First lets create the device.  CreateDevice() requires a ID, input device object,
   // and a little something for COM.  The last can be NULL.
   if(FAILED(m_InputSystem->CreateDevice(GUID_SysKeyboard, &m_Keyboard, NULL)))
      return false;

   // Here we need to Set the data format.  This will tell direct input what kind of device
   // this is.  This must be called before we can gain access to the device.
   if(FAILED(m_Keyboard->SetDataFormat(&c_dfDIKeyboard)))
      return false;

   // Now we will set the flags and options for our keyboard object.
   if(FAILED(m_Keyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
      return false;

   // Now we call Acquire() to gain access to our input device.
   if(FAILED(m_Keyboard->Acquire()))
      return false;

   // ClearKeys() will clear out the array of keys we have.
   ClearKeys();

   return true;
}


bool CInputKeyboard::KeyUp(int key)
{
   // If the key in the variable key is not pressed then we would return true, else false.
   if(m_Keys[key] & 0x80)
      return false;

   return true;
}


bool CInputKeyboard::KeyDown(int key)
{
   // If the key in the variable key is pressed then we would return true, else false.
   if(m_Keys[key] & 0x80)
      return true;

   return false;
}


bool CInputKeyboard::AcquireDevice()
{
   // First we will clear the array of keys we have.
   ClearKeys();

   // Next we will gain access to the device using the Acquire() function.
   if(FAILED(m_Keyboard->Acquire()))
      return false;

   return true;
}


bool CInputKeyboard::UnAcquireDevice()
{
   // First we clear out the array of keys.
   ClearKeys();

   // Next we will take away access from our application using Unacquire().
   if(FAILED(m_Keyboard->Unacquire()))
      return false;

   return true;
}


bool CInputKeyboard::UpdateDevice()
{
   // UpdateDevice() will update the device by setting all the keys that are pressed
   // equal to a non 0 value.  If there is a issue with calling the GetDeviceState()
   // function we will call Acquire() to make sure we have access.  If that fails then
   // something is really wrong.  If it works then we will try to call GetDeviceState()
   // again.  If all is good we return true, else false because something is really wrong.
   if(FAILED(m_Keyboard->GetDeviceState(sizeof(m_Keys), (LPVOID)m_Keys)))
      {
         if(FAILED(m_Keyboard->Acquire()))
            return false;

         if(FAILED(m_Keyboard->GetDeviceState(sizeof(m_Keys), (LPVOID)m_Keys)))
            return false;
      }

   return true;
}


void CInputKeyboard::ClearKeys()
{
   // This will set every element in our array to 0 so we can start fresh.
   ZeroMemory(m_Keys, KEYS_SIZE * sizeof(char));
}


bool CInputKeyboard::Shutdown()
{
   // Take access away from our keyboard object and release anything it used.
   if(!m_Keyboard)
      return false;

   m_Keyboard->Unacquire();
   m_Keyboard->Release();

   return true;
}


// Copyright March 2003
// All Rights Reserved!
// Allen Sherrod
// ProgrammingAce@UltimateGameProgramming.com
// www.UltimateGameProgramming.com

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -