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 8 : Explosions

Previous step | Next step

Every game needs explosions, right? Let's add some!

Shine
The seven frames making up our Explosion animation.

Each time a brick is removed from the game board we will trigger an explosion where the ball hit the brick.

First, download the file explosion.png and save it to your Breakout\Common\res folder. Then add it to your Visual Studio project as a PNG image and name it IDB_EXPLOSION.

Add the following code to myapplication.h:

class CMyApplication : public CGapiApplication
{
    ...
    CGapiSurface* m_pExplosion;
    ...
}

Add the following code to myapplication.cpp:

CMyApplication::CMyApplication(const GDAPPCONFIG& config) : CGapiApplication(config)
{
    ...
    m_pExplosion = new CGapiSurface(GetGlobal());
    ...
}
 
CMyApplication::~CMyApplication()
{
    ...
    delete m_pExplosion;
    ...
}
 
HRESULT CMyApplication::CreateVidMemSurfaces(CGapiSurface* pBackBuffer, HINSTANCE hInstance)
{
    ...
    m_pExplosion->CreateSurface(0, hInstance, IDB_EXPLOSION, _T("PNG"));
    m_pExplosion->SetColorKey(RGB(255, 0, 255));
}
 
HRESULT CMyApplication::GameDrawObjects(CGapiSurface* pBackBuffer)
{
    ...
    // Explosions
    for (DWORD dwBrickIndex=0; dwBrickIndex < GAMEPARAM_NUMLINES * m_dwGameBricksPerLine; dwBrickIndex++)
    {
        if (m_pBricks[dwBrickIndex].dwExplosionIndex > 0)
        {
            int nX = m_pBricks[dwBrickIndex].nExplosionCenterX;
            int nY = m_pBricks[dwBrickIndex].nExplosionCenterY;
            int nExplosionWidth = m_pExplosion->GetWidth() / GAMEPARAM_NUMEXPLOSIONS;
            int nExplosionHeight = m_pExplosion->GetHeight();
            int nXOffset = nExplosionWidth * (GAMEPARAM_NUMEXPLOSIONS - m_pBricks[dwBrickIndex].dwExplosionIndex);
            pBackBuffer->BltFast(nX-(nExplosionWidth>>1), nY-(nExplosionHeight>>1), m_pExplosion, CRect(nXOffset, 0, nXOffset+nExplosionWidth, nExplosionHeight), GDBLTFAST_KEYSRC, NULL);
            m_pBricks[dwBrickIndex].dwExplosionIndex--;
        }
    }
 
    return S_OK;
}

Explosions done!

Now let's add some shiny effects to the bricks!