Cleaning up the house

We have a pretty complete game. Sure, it's not going to set any records or make anyone rich, but if this is your first game, then congratulations!

We have been remiss in one area: good programming dictates that any time we create an object, we delete it when we are done using it. Up to now, you may be wondering if we were ever going to do this! Well, now is the time.

We made a placeholder for all of these operations in the EndGame function. Now, we will add the necessary code to properly release our resources.

Release sprites

Let's start by clearing out our sprites. It is important to remember that when we remove any resource, we need to make sure that it is also releasing its own resources. This is the purpose of the class destructor. Let's use the Sprite class as an example. Open Sprite.cpp and you should see a destructor defined using the following code:

Sprite::~Sprite()
{
  for (int i = 0; i < m_textureIndex; i++)
  {
    glDeleteTextures(1, &m_textures[i]);
  }
  delete[] m_textures;
  m_textures = NULL;
}

We first want to release all of the textures in the m_textures array. Then we use delete[] to release the m_textures array. It is also good programming practice to set the variable to NULL once an object has been deleted.

The Sprite destructor will be called when we call delete on a sprite object. So, the first thing we need to add to EndGame is a delete operation for each sprite that was created for our game. Add the following lines of code to the EndGame function:

delete robot_left;
delete robot_right;
delete robot_right_strip;
delete robot_left_strip;
delete background;
delete pickup;
delete enemy;
delete pauseButton;
delete resumeButton;
delete splashScreen;
delete menuScreen;
delete creditsScreen;
delete playButton;
delete creditsButton;
delete exitButton;
delete menuButton;
delete nextLevelScreen;
delete continueButton;
delete gameOverScreen;
delete replayButton;

Tip

If you look closely, you will notice that we did not delete the player object. This is because player was only used as a pointer to sprites that had already been created. Put another way, we never used player to create a new Sprite. A good rule of thumb is that there should be exactly one delete for every new.

Release input

Our next system to shut down is the input system. First, let's complete the Input destructor. Add the highlighted code to the destructor in the Input class:

Input::~Input()
{
  delete[] m_uiElements;
  m_uiElements = NULL;
}

We have to delete the uiElements array, which was an array of pointers to the sprites that were part of the input system. Note that we did not delete the actual sprites here because they were not created by the input system.

Now, add the following line of code to EndGame:

delete inputManager;

Releasing fonts

Add this line to release the display lists we used to store our fonts:

KillFont();

Releasing audio

Our final cleanup is the audio system. Add the following lines of code to EndGame:

sfxWater->release();
sfxOilcan->release();
sfxJump->release();
sfxMovement->release();
sfxButton->release();
musBackground->release();
audiomgr->release(); 

Congratulations! Your house is all cleaned up.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.171.64