?/TD>
Microsoft DirectX 9.0

Tutorial 1 Samples


Sample Function 1: DI_Init

This application-defined sample function creates a Microsoft?DirectInput?object, initializes it, and retrieves the necessary interface pointers, assigning them to global variables. When initialization is complete, it acquires the device.

If any part of the initialization fails, this function calls the DI_Term application-defined sample function to deallocate DirectInput objects and interface pointers in preparation for terminating the program. For more information, see Sample Function 2: DI_Term.

In addition to creating the DirectInput object, the DI_Init function performs the tasks discussed in the following tutorial steps.

The following is the DI_Init function.

/* The following variables are presumed to be initialized:
HINSTANCE             g_hinst;    // application instance
HWND                  g_hwndMain; // application window
*/

LPDIRECTINPUT8        g_lpDI; 
LPDIRECTINPUTDEVICE8  g_lpDIDevice; 

BOOL WINAPI DI_Init() 
{ 
    HRESULT hr; 

    // Create the DirectInput object. 
    hr = DirectInput8Create(g_hinst, DIRECTINPUT_VERSION, 
                            IID_IDirectInput8, (void**)&g_lpDI, NULL); 

    if FAILED(hr) return FALSE; 

    // Retrieve a pointer to an IDirectInputDevice8 interface 
    hr = g_lpDI->CreateDevice(GUID_SysKeyboard, &g_lpDIDevice, NULL); 

    if FAILED(hr) 
    { 
        DI_Term(); 
        return FALSE; 
    } 

    // Now that you have an IDirectInputDevice8 interface, get 
    // it ready to use. 

    // Set the data format using the predefined keyboard data 
    // format provided by the DirectInput object for keyboards. 

    hr = g_lpDIDevice->SetDataFormat(&c_dfDIKeyboard); 

    if FAILED(hr) 
    { 
        DI_Term(); 
        return FALSE; 
    } 

    // Set the cooperative level 
    hr = g_lpDIDevice->SetCooperativeLevel(g_hwndMain, 
                             DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); 
    if FAILED(hr) 
    { 
        DI_Term(); 
        return FALSE; 
    } 

    // Get access to the input device. 
    hr = g_lpDIDevice->Acquire(); 
    if FAILED(hr) 
    { 
        DI_Term(); 
        return FALSE; 
    } 

    return TRUE; 
}

Sample Function 2: DI_Term

This application-defined sample function deallocates existing DirectInput interface pointers in preparation for program shutdown or in the event of a failure to properly initialize a device.

/* The following variables are presumed initialized:
LPDIRECTINPUT8        g_lpDI; 
LPDIRECTINPUTDEVICE8  g_lpDIDevice; 
*/
 
void WINAPI DI_Term() 
{ 
    if (g_lpDI) 
    { 
        if (g_lpDIDevice) 
        { 
        // Always unacquire device before calling Release(). 
            g_lpDIDevice->Unacquire(); 
            g_lpDIDevice->Release();
            g_lpDIDevice = NULL; 
        } 
        g_lpDI->Release();
        g_lpDI = NULL; 
    } 
} 
 

For additional DirectInput tutorials, see DirectInput C/C++ Tutorials.



© 2002 Microsoft Corporation. All rights reserved.