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 12 : Sound

Previous step | Next step

In this last step we will add sound effects to our game. We will use the standard Win32 waveOut API for sound effects, and I will base my code upon an example project that was downloaded from the Pocket PC Developer Network.

Begin by downloading the file wavefile.h and save it in your Breakout\common folder. This file exposes a simple to use API to playback sounds. Using this API you can play multiple sounds simultaneously as well as loop sounds.

Then download the files reflect_brick.wav and reflect_bat.wav and save them in the same folder as Breakout.exe. Remember that if you run Breakout from the Visual Studio environment, the current folder when running the application will be the same folder as the solution file (.SLN), so place the wave files there if you want to run Breakout.exe with sounds from the Visual Studio environment. Also, if you plan to run Breakout.exe in the emulator or on a Windows Mobile device you will have to copy the wave files manually to the device.

Add the following code to myapplication.h:

#include "WaveFile.h"
 
class CMyApplication : public CGapiApplication
{
    ...
    CWaveFile* m_pWaveReflectBat;
    CWaveFile* m_pWaveReflectBrick;
    ...
}

Add the following code to myapplication.cpp:

CMyApplication::CMyApplication(const GDAPPCONFIG& config) : CGapiApplication(config)
{
    ...
    m_pWaveReflectBat = new CWaveFile(_T("reflect_bat.wav"));
    m_pWaveReflectBrick = new CWaveFile(_T("reflect_brick.wav"));
    ...
}
 
CMyApplication::~CMyApplication()
{
    ...
    delete m_pWaveReflectBat;
    delete m_pWaveReflectBrick;
    ...
}
 
HRESULT CMyApplication::GameCheckBat(CGapiSurface* pBackBuffer)
{
    ...
    if (((nBallX + nBallWidth) >= nBatX) && (nBallX <= (nBatX + nBatWidth)))
    {
        // Play sound when ball hits bat
        if (m_pWaveReflectBat)
        {
            m_pWaveReflectBat->Play();
        }
        ...
}
 
HRESULT CMyApplication::GameCheckBricks(CGapiSurface* pBackBuffer)
{
    ...
    if (nBallY >= (nY - nBallHeight) && nBallY < (nY + GAMEPARAM_BRICKHEIGHT) &&
        nBallX >= (nX - nBallWidth) && nBallX < (nX + GAMEPARAM_BRICKWIDTH))
    {
        // Remove brick and trigger an explosion
        if (m_pWaveReflectBrick)
        {
            m_pWaveReflectBrick->Play();
        }
        ...
}

That's it! You will now have audio playback in your game!

This was the last step in our Breakout tutorial, and we hope you enjoyed it! Now go and copy the Minimal application and create your own game!

Breakout