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 2 : Game modes

Previous step | Next step

Each game has several "modes", and will execute code differently depending on the current mode. A typical game defines at least the following four modes:

  • Introduction screen
  • In-game
  • End game
  • Settings

We will define support for these game modes in our application, which will make it easier for us to add application logic to the appropriate places later.

Add the following code to myapplication.h:

class CMyApplication : public CGapiApplication
{
    ...
    enum 
    {
        GAMEMODE_INTRO = 1,
        GAMEMODE_SETTINGS,
        GAMEMODE_INGAME,
        GAMEMODE_ENDGAME
    };
    DWORD m_dwGameMode;
    BOOL m_bInitComplete;
 
    HRESULT GameModeIntro(CGapiSurface* pBackBuffer, DWORD dwFlags);
    HRESULT GameModeInGame(CGapiSurface* pBackBuffer, DWORD dwFlags);
    HRESULT GameModeSettings(CGapiSurface* pBackBuffer, DWORD dwFlags);
    HRESULT GameModeEndGame(CGapiSurface* pBackBuffer, DWORD dwFlags); 
 
    HRESULT GameInit(CGapiSurface* pBackBuffer, DWORD dwFlags);
}

Add the following code to myapplication.cpp:

CMyApplication::CMyApplication(const GDAPPCONFIG& config) : CGapiApplication(config)
{
    ...
    m_bInitComplete = FALSE;
    m_dwGameMode = GAMEMODE_INTRO;
    ...
}
 
HRESULT CMyApplication::GameModeIntro(CGapiSurface* pBackBuffer, DWORD dwFlags)
{
    GameMoveBat(pBackBuffer);
    for (DWORD dwSubPixelCounter = 0; dwSubPixelCounter < GAMEPARAM_SUBPIXELSTEPS; dwSubPixelCounter++)
    {
        GameMoveBall(pBackBuffer);
        GameCheckBat(pBackBuffer);
        GameCheckBricks(pBackBuffer);
    }
    GameDrawBackgrund(pBackBuffer);
    GameDrawObjects(pBackBuffer);
    GameDrawScore(pBackBuffer);
 
    return S_OK;
}
 
HRESULT CMyApplication::GameModeInGame(CGapiSurface* pBackBuffer, DWORD dwFlags)
{
    GameMoveBat(pBackBuffer);
    for (DWORD dwSubPixelCounter = 0; dwSubPixelCounter < GAMEPARAM_SUBPIXELSTEPS; dwSubPixelCounter++)
    {
        GameMoveBall(pBackBuffer);
        GameCheckBat(pBackBuffer);
        GameCheckBricks(pBackBuffer);
    }
    GameDrawBackgrund(pBackBuffer);
    GameDrawObjects(pBackBuffer);
    GameDrawScore(pBackBuffer);
 
    return S_OK;
}
 
HRESULT CMyApplication::GameModeSettings(CGapiSurface* pBackBuffer, DWORD dwFlags)
{
    return S_OK;
}
 
HRESULT CMyApplication::GameModeEndGame(CGapiSurface* pBackBuffer, DWORD dwFlags)
{
    return S_OK;
}
 
HRESULT CMyApplication::GameInit(CGapiSurface* pBackBuffer, DWORD dwFlags)
{
    return S_OK;
}
 
HRESULT CMyApplication::ProcessNextFrame(CGapiSurface* pBackBuffer, DWORD dwFlags)
{
    ...
    if (m_bInitComplete == FALSE)
    {
        GameInit(pBackBuffer, dwFlags);
        m_bInitComplete = TRUE;
    }
 
    HRESULT hr = S_OK;
    switch (m_dwGameMode)
    {
    case GAMEMODE_INTRO:
        hr = GameModeIntro(pBackBuffer, dwFlags);
        break;
    case GAMEMODE_INGAME:
        hr = GameModeInGame(pBackBuffer, dwFlags);
        break;
    case GAMEMODE_SETTINGS:
        hr = GameModeSettings(pBackBuffer, dwFlags);
        break;
    case GAMEMODE_ENDGAME:
        hr = GameModeEndGame(pBackBuffer, dwFlags);
        break;
    }
}

So, we should now be finished with our application shell.

Let's add a scrolling background to the application!