Andy Pike - DirectX 8 Programming Tutorial

Здесь есть возможность читать онлайн «Andy Pike - DirectX 8 Programming Tutorial» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Жанр: Программирование, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

DirectX 8 Programming Tutorial: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «DirectX 8 Programming Tutorial»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

DirectX 8 Programming Tutorial — читать онлайн бесплатно полную книгу (весь текст) целиком

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «DirectX 8 Programming Tutorial», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

LogError("

  • Unable to create DirectInput interface.");

    return false;

    } else {

    LogInfo("

  • DirectInput interface created OK");

    }

    The first parameter to DirectInput8Create is the HINSTANCE of our application which has been passed through from our WinMain function. The second parameter is the DirectInput version, this will always be DIRECTINPUT_VERSION. The third parameter is the desired interface, this will always be IID_IDirectInput8. The fourth parameter is the important one, this is a pointer the DirectInput interface which we will use later. The fifth parameter, for our examples is always NULL.

    m_pDirectInput is a member variable of CGame and is of type LPDIRECTINPUT8.

    Setting up the keyboard

    Once we have created DirectInput interface pointer, we are ready to setup the keyboard. Shown below is the code required to setup the keyboard, take a look at it and I'll explain it below.

    //KEYBOARD =======================================================================

    //Create the keyboard device object

    if (FAILED(m_pDirectInput->CreateDevice(GUID_SysKeyboard, &m_pKeyboard, NULL))) {

    CleanUpDirectInput();

    LogError("

  • Unable to create DirectInput keyboard device interface.");

    return false;

    } else {

    LogInfo("

  • DirectInput keyboard device interface created OK.");

    }

    //Set the data format for the keyboard

    if (FAILED(m_pKeyboard->SetDataFormat(&c_dfDIKeyboard))) {

    CleanUpDirectInput();

    LogError("

  • Unable to set the keyboard data format.");

    return false;

    } else {

    LogInfo("

  • Set the keyboard data format OK.");

    }

    //Set the cooperative level for the keyboard

    if (FAILED(m_pKeyboard->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE))) {

    CleanUpDirectInput();

    LogError("

  • Unable to set the keyboard cooperative level.");

    return false;

    } else {

    LogInfo("

  • Set the keyboard cooperative level OK.");

    }

    //Acquire the keyboard

    if (m_pKeyboard) {

    m_pKeyboard->Acquire();

    }

    First of all, we need to create a keyboard device. We do this by calling the CreateDevice method of DirectInput. The first parameter is the GUID (Globally Unique Identifier) of the device to create, in our case the system keyboard device, so we simply pass in the GUID_SysKeyboard predefined GUID. The next parameter will receive a pointer to the keyboard device, we have defined this variable as a member of CGame. The last parameter is always NULL, take a look in the SDK for further details.

    The next thing to do is set the data format for the keyboard device. To do this we simply pass in the predefined keyboard format c_dfDIKeyboard.

    Then, we need to set how our input device (the keyboard) will cooperate with our application and Windows. We will need to specify the cooperation level by selecting "foreground" or "background" and "exclusive" or "nonexclusive". To do this, we need to pass in the relevant flags (DISCL_FOREGROUND, DISCL_BACKGROUND, DISCL_EXCLUSIVE and DISCL_NONEXCLUSIVE) to the SetCooperativeLevel method.

    · DISCL_FOREGROUND: Input device is available when the application is in the foreground (has focus).

    · DISCL_BACKGROUND: Input device is available at all times, foreground and background.

    · DISCL_EXCLUSIVE: No other instance of the device can obtain exclusive access to the device while it is acquired.

    · DISCL_NONEXCLUSIVE: Access to the device does not interfere with other applications that are accessing the same device.

    The final thing to do is to acquire the keyboard, this means that we can now get access to the device's data. We do this by calling the Acquire method of the device.

    Setting up the mouse

    As with the keyboard, we need to set up the mouse before we can get access to it. Below is the code required, take a look and you'll notice that it is very similar to the keyboard set up code above.

    //MOUSE =======================================================================

    //Create the mouse device object

    if (FAILED(m_pDirectInput->CreateDevice(GUID_SysMouse, &m_pMouse, NULL))) {

    CleanUpDirectInput();

    LogError("

  • Unable to create DirectInput mouse device interface.");

    return false;

    } else {

    LogInfo("

  • DirectInput mouse device interface created OK.");

    }

    //Set the data format for the mouse

    if (FAILED(m_pMouse->SetDataFormat(&c_dfDIMouse))) {

    CleanUpDirectInput();

    LogError("

  • Unable to set the mouse data format.");

    return false;

    } else {

    LogInfo("

  • Set the mouse data format OK.");

    }

    //Set the cooperative level for the mouse

    if (FAILED(m_pMouse->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE))) {

    CleanUpDirectInput();

    LogError("

  • Unable to set the mouse cooperative level.");

    return false;

    } else {

    LogInfo("

  • Set the mouse cooperative level OK.");

    }

    //Acquire the mouse

    if (m_pMouse) {

    m_pMouse->Acquire();

    }

    So, for the mouse we have the same basic steps as for the keyboard. One of the differences is that we pass in the predefined GUID for the system mouse (GUID_SysMouse) into the CreateDevice method instead of the one for the keyboard which will return a pointer to the mouse. Another difference is that we need to use a different data format, c_dfDIMouse instead of c_dfDIKeyboard. We then set the cooperation level as before and finally acquire the mouse. We are now ready to get data from the keyboard and mouse.

    Buffered and Immediate Data

    Now that we have setup the mouse and keyboard, we need to get data from them. There are two forms of data that we can get, buffered and immediate data. Buffered data is a record of events that are saved (buffered) until the application requires them. Immediate data is the current state of the device. In this tutorial we will look at immediate 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;

  • Читать дальше
    Тёмная тема
    Сбросить

    Интервал:

    Закладка:

    Сделать

    Похожие книги на «DirectX 8 Programming Tutorial»

    Представляем Вашему вниманию похожие книги на «DirectX 8 Programming Tutorial» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


    Отзывы о книге «DirectX 8 Programming Tutorial»

    Обсуждение, отзывы о книге «DirectX 8 Programming Tutorial» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.

    x