Documentation
[SDK Documentation] [Creating a complete game: Breakout]

 

Creating a complete game: Breakout

This document has been updated for use with GapiDraw 4.0 or later.
Last updated on October 6, 2008.

 

Step 11 : Button input

Previous step | Next step

We will now add button input to the application so that the player can control the bat. For this we will use the following two functions in CGapiApplication:

  • KeyDown
  • KeyUp

On some devices multiple keys can be pressed simultaneously, so take this into consideration when designing your button handler.

When handling the button input on Windows Mobile devices you can only count on one thing: nothing is standardized. Long ago Microsoft tried to standardize the button codes for three standard keys on Pocket PC devices, but few manufacturers followed those guidelines so today the codes received will vary from device to device. Your best option is to provide a settings screen shown during startup that allows the user to configure the buttons in your application to match the device.

For our simple game we will capture two hardware keys and two directional keys.

Add the following code to myapplication.cpp:

HRESULT CMyApplication::KeyDown(DWORD dwKey, GDKEYLIST& keylist)
{
    // Exit main loop on any key press
    if (m_dwGameMode == GAMEMODE_INGAME)
    {
        if (dwKey == keylist.vkA)
        {
            m_dwGameMode = GAMEMODE_INTRO;
        }
        else if (dwKey == keylist.vkB)
        {
            m_dwGameMode = GAMEMODE_INTRO;
        }
        else if (dwKey == keylist.vkLeft)
        {
            m_pBat->SetXSpeed(-m_nGameBallSpeed);
        }
        else if (dwKey == keylist.vkRight)
        {
            m_pBat->SetXSpeed(m_nGameBallSpeed);
        }
        return S_OK;
    }
    if (dwKey == keylist.vkA)
    {
        // Key '1' pressed
        m_dwGameMode = GAMEMODE_INGAME;
        GameInit(GetBackBuffer(), 0);
    }
    else if (dwKey == keylist.vkB)
    {
        // Key '2' pressed
        Shutdown();
    }
 
    return S_OK;
}
 
HRESULT CMyApplication::KeyUp(DWORD dwKey, GDKEYLIST& keylist)
{
    if (m_dwGameMode == GAMEMODE_INGAME)
    {
        m_pBat->SetXSpeed(0);
    }
    return S_OK;
}

As you can see, depending on the current game mode the buttons do different things.

No let's go on and add sound to our application.