на главную | войти | регистрация | DMCA | контакты | справка | donate |      

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
А Б В Г Д Е Ж З И Й К Л М Н О П Р С Т У Ф Х Ц Ч Ш Щ Э Ю Я


моя полка | жанры | рекомендуем | рейтинг книг | рейтинг авторов | впечатления | новое | форум | сборники | читалки | авторам | добавить



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)


Buffered and Immediate Data | DirectX 8 Programming Tutorial | Getting mouse data