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 3 : Scrolling background

Previous step | Next step

Ok, let's show some graphics. Let's begin with a classic scrolling background. In practice, nothing is ever scrolled, what is happening is just that we draw a bitmap image at different vertical offsets many times each second, to give the illusion that it is moving.

Bat
Our scrolling background tile.

Download the file back2.png and save it to your Breakout\Common\res folder. If you instead would like a brighter background you can download back1.png instead. Add the file to your Visual Studio project as a PNG image and name it IDB_BACKGROUND.

Add the following code to myapplication.h:

class CMyApplication : public CGapiApplication
{
    ...
    int m_nBackScrollIndex;
    CGapiSurface* m_pBackground;
    ...
}

Add the following code to myapplication.cpp:

CMyApplication::CMyApplication(const GDAPPCONFIG& config) : CGapiApplication(config)
{
    ...
    m_nBackScrollIndex = 0;
    m_pBackground = new CGapiSurface(GetGlobal());
    ...
}
 
CMyApplication::~CMyApplication()
{
    ...
    delete m_pBackground;
    ...
}
 
HRESULT CMyApplication::CreateVidMemSurfaces(CGapiSurface* pBackBuffer, HINSTANCE hInstance)
{
    ...
    m_pBackground->CreateSurface(0, hInstance, IDB_BACKGROUND, _T("PNG"));
    ...
}
 
HRESULT CMyApplication::GameDrawBackgrund(CGapiSurface* pBackBuffer)
{
    int nScreenWidth = pBackBuffer->GetWidth();
    int nScreenHeight = pBackBuffer->GetHeight();
 
    // Draw scrolling bitmap
    int nBackWidth = m_pBackground->GetWidth();
    int nBackHeight = m_pBackground->GetHeight();
    int nNumXBlits = pBackBuffer->GetWidth() / nBackWidth + 1;
    int nNumYBlits = pBackBuffer->GetHeight() / nBackHeight + 2; // One extra due to scrolling
    int nX, nY;
    for (nY=0; nY < nNumYBlits; nY++)
    {
        for (nX=0; nX < nNumXBlits; nX++)
        {
            pBackBuffer->BltFast(nX*nBackWidth, -nBackHeight+nY*nBackHeight+m_nBackScrollIndex, m_pBackground, NULL, 0, NULL);
        }
    }
    if (m_nBackScrollIndex++ == nBackWidth)
    {
        m_nBackScrollIndex = 0;
    }
 
    return S_OK;
}

Yes, we have a scrolling bitmap on the display!

Now on to adding some nice border graphics!