Книга: DirectX 8 Programming Tutorial

Getting mouse data

Getting mouse data

As with the keyboard, we have a new function called ProcessMouse which is called from our Render function. This means that ProcessMouse will also be called once per frame. The code for ProcessMouse is shown below.

void CGame::ProcessMouse() {
 DIMOUSESTATE MouseState;
 if (FAILED(m_pMouse->GetDeviceState(sizeof(MouseState),(LPVOID)&MouseState))) {
  return;
 }
 //Is the left mouse button down?
 if (MOUSEBUTTONDOWN(MouseState.rgbButtons[MOUSEBUTTON_LEFT])) {
  m_nMouseLeft = 1;
 } else {
  m_nMouseLeft = 0;
 }
 //Is the right mouse button down?
 if (MOUSEBUTTONDOWN(MouseState.rgbButtons[MOUSEBUTTON_RIGHT])) {
  m_nMouseRight = 1;
 } else {
  m_nMouseRight = 0;
 }
 m_nMouseX += MouseState.lX;
 m_nMouseY += MouseState.lY;
}

So, to get access to the mouse data we make a call to GetDeviceState which fills a DIMOUSESTATE structure with data from the mouse. The DIMOUSESTATE structure has four members:

· lX: Distance the mouse has moved along the x-axis since the last call to GetDeviceState.

· lY: Distance the mouse has moved along the y-axis since the last call to GetDeviceState.

· lZ: Distance mouse wheel has moved since the last call to GetDeviceState.

· rgbButtons: Array of mouse button states.

lX, lY and lZ return the distance moved in device units (NOT pixels) since the last call to GetDeviceState. If you want to get the current screen position in pixels, you can use the Win32 GetCursorPos function. We use the macro MOUSEBUTTONDOWN to determine if a given mouse button is down or not. This macro is shown below along with some constants used for referring to mouse buttons.

#define MOUSEBUTTONDOWN(key) (key & 0x80)
#define MOUSEBUTTON_LEFT 0
#define MOUSEBUTTON_RIGHT 1
#define MOUSEBUTTON_MIDDLE 2

Оглавление книги


Генерация: 0.953. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз