sunlight

Input Configuration

Introduction

In this part of the tutorial, we will cover:

  • Further tidying up: background music, pausing
  • DirectInput input configuration

Previous View Code Download Code Next

Further Tidying

We'll add a couple of extra features here. We'll add a 'pause' action and a 'help' action (which we will write in a second).

    { ACTIONS_PAUSE,        DIKEYBOARD_P,                   0, "Pause" },
    { ACTIONS_HELP,         DIKEYBOARD_F1,                  0, "Help" },

We'll restructure our idle-time code to move all the logic away from the drawing code, to allow us to implement the pause functionality. This is also good programming practice. (There's rather a lot of this, so have a look at the source code.) I've also used the timing macros I introduced in the last page to time the drawing code, which averages about 130ms on this computer (that's microseconds).

DirectInput Configuration

One of the most useful side benefits of using action maps is that we get - for free - configuration of our control system, including a usable UI.

Using this UI is simply a case of initialising a DICONFIGUREDEVICESPARAMS structure, unacquiring devices, flipping to a GDI surface (so that DirectInput can write to the display), then calling IDirectInput8::ConfigureDevices. Once we've finished configuration, our previous action map is invalid, so we must release and recreate our device array. 

// Display the input configuration box
BOOL ConfigureInput()
{
    DICONFIGUREDEVICESPARAMS dicdp;
    
    ZeroMemory(&dicdp, sizeof(dicdp));

    dicdp.dwSize = sizeof(dicdp);
    dicdp.dwcUsers = 1;
    dicdp.lptszUserNames = NULL;
    dicdp.dwcFormats = 1;
    dicdp.lprgFormats = &g_diaf;
    dicdp.hwnd = hWndMain;
    dicdp.lpUnkDDSTarget = NULL;

    // Set up a colour set, which will allow us to make the 
    //  configuration box look like the rest of our program.
    //  If this is initialised to zero, DirectInput will use 
    //  the default colour set.
    dicdp.dics.dwSize = sizeof(DICOLORSET);

    // Let go of any devices so that the configuration 
    //  interface can have a go
    UnacquireDevices();

    // Display action configuration
    g_pDD->FlipToGDISurface();
    g_pDI->ConfigureDevices(NULL, &dicdp, DICD_EDIT, NULL);

    // Devices are no longer valid, so reinitialise.
    for (int iDevice = 0; iDevice < g_nDevices; iDevice++)
        g_pDeviceArray[iDevice]->Release();

    delete [] g_pDeviceArray;
    g_pDeviceArray = NULL;
    g_nDevices = 0;

    HRESULT h = g_pDI->EnumDevicesBySemantics(NULL, &g_diaf, 
        DIEnumDevicesBySemanticsCallback,
        NULL, DIEDBSFL_ATTACHEDONLY);
    if (FAILED(h))
        return FALSE;

    AcquireDevices();

    return TRUE;
}

We can also call up a reduced form of this box for the user to view their configuration:

// Display the input configuration.
BOOL DisplayInput()
{
    DICONFIGUREDEVICESPARAMS dicdp;
    
    ZeroMemory(&dicdp, sizeof(dicdp));

    dicdp.dwSize = sizeof(dicdp);
    dicdp.dwcUsers = 1;
    dicdp.lptszUserNames = NULL;
    dicdp.dwcFormats = 1;
    dicdp.lprgFormats = &g_diaf;
    dicdp.hwnd = hWndMain;
    dicdp.lpUnkDDSTarget = NULL;

    dicdp.dics.dwSize = sizeof(DICOLORSET);

    UnacquireDevices();

    // Display action configuration
    g_pDD->FlipToGDISurface();
    g_pDI->ConfigureDevices(NULL, &dicdp, DICD_DEFAULT, NULL);

    AcquireDevices();
    return TRUE;
}

As you can see, by changing the DICOLORSET structure within the DICONFIGUREDEVICESPARAMS structure, you can set the colour scheme of the box so that it matches your application. We will stay with the default.

All that remains is to call up these boxes at the appropriate times. We will bring up the DisplayInput box when the Help action occurs, and the ConfigureInput box at the start.

    if (g_bShowHelp)
    {
        g_bShowHelp = false;
        DisplayInput();
    } 

We've added an input configuration dialog box - all for a few lines of code, which makes the 170 lines of DirectInput code before it all worthwhile...

In the next section, we'll learn how to use the MP3 format for audio files, allowing us to use digital audio for background music. 

 

Copyright © David McCabe, 1998 - 2001. All rights reserved.

You will need to download and install the m-math control to display any equations on this Web site. Without this control, you will not see most of the equations. Please do not e-mail me asking why the equations do not display!