Книга: DirectX 8 Programming Tutorial

Getting keyboard data

Getting keyboard data

We have a new function called ProcessKeyboard which is called from our Render function. This means that ProcessKeyboard will be called once per frame. The code for ProcessKeyboard is shown below.

void CGame::ProcessKeyboard() {
 char KeyboardState[256];
 if (FAILED(m_pKeyboard->GetDeviceState(sizeof(KeyboardState),(LPVOID)&KeyboardState))) {
  return;
 }
 if (KEYDOWN(KeyboardState, DIK_ESCAPE)) {
  //Escape key pressed. Quit game.
  m_fQuit = true;
 }
 //Rotate Earth
 if (KEYDOWN(KeyboardState, DIK_RIGHT)) {
  m_rRotate –= 0.5f;
 } else if(KEYDOWN(KeyboardState, DIK_LEFT)) {
  m_rRotate += 0.5f;
 }
 //Set an upper and lower limit for the rotation factor
 if (m_rRotate < –20.0f) {
  m_rRotate = –20.0f;
 } else if(m_rRotate > 20.0f) {
  m_rRotate = 20.0f;
 }
 //Scale Earth
 if (KEYDOWN(KeyboardState, DIK_UP)) {
  m_rScale += 0.5f;
 } else if (KEYDOWN(KeyboardState, DIK_DOWN)) {
  m_rScale –= 0.5f;
 }
 //Set an upper and lower limit for the scale factor
 if (m_rScale < 1.0f) {
  m_rScale = 1.0f;
 } else if(m_rScale > 20.0f) {
  m_rScale = 20.0f;
 }
}

This function is pretty straightforward. At the start we call GetDeviceState which gets the current state of the keyboard device. The function call fills a char array with the key states of the keyboard. You can see from the function above, that once the array has been populated with key states, we can then use a simple macro called KEYDOWN to ascertain if a given key is in the down position. The KEYDOWN macro is shown below. So, based on certain key states, we manipulate some member variables that control scale and rotation.

#define KEYDOWN(name, key) (name[key] & 0x80)

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


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