Defining the global variables of the game

Every game we make henceforth will have a common header file included and global data defined in a separate class. This class has been made for convenience only and—technically—you can choose not to have it at all. However, all the enums, constants, and macros used in the game will be stored here, so be sure to include it in the sources where needed.

Let's take a look at the GameGlobals.h file:

#ifndef GAME_GLOBALS_H_
#define GAME_GLOBALS_H_

#include "cocos2d.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;

using namespace std;

#define SCREEN_SIZE GameGlobals::screen_size_
#define SOUND_ENGINE CocosDenshion::SimpleAudioEngine::sharedEngine()
#define MAX_STARS 15
#define BULLET_MOVE_DURATION 1.5f
#define MAX_BULLETS 25
#define MAX_LEVELS 5

// enum used for proper z-ordering
enum E_ZORDER
{
  E_LAYER_BACKGROUND = 0,
  E_LAYER_FOREGROUND = 2,
  E_LAYER_ENEMIES_BRICKS = 4,
  E_LAYER_BULLETS = 6,
  E_LAYER_PLAYER = 8,
  E_LAYER_HUD = 10,
  E_LAYER_POPUP = 12,
};

class GameGlobals
{
public:
  GameGlobals(void){};
  ~GameGlobals(void){};

  // initialise common global data here...called when application finishes launching
  static void Init();
  // load initial/all game data here
  static void LoadData();

  // save screen size for fast access
  static CCSize screen_size_;

  // function takes comma separated string & returns vector of values
  static vector<float> GetFloatListFromString(string input);
  // function takes comma separated string & returns CCPoint
  static CCPoint GetPointFromString(string input);
};

#endif // GAME_GLOBALS_H_

First, we included all headers that will be required and we also declared the namespaces we will be using throughout the project. We then defined all the pre-processor directives that will be needed, starting with the familiar SCREEN_SIZE that points to the screen_size_ variable from GameGlobals. We also created a convenience macro SOUND_ENGINE to fetch the singleton object of the class SimpleAudioEngine.

The constants that follow define the maximum number of stars in the background, the default duration for bullet movement, the maximum number of bullets currently on screen, and the maximum number of levels in SpaceCraze. Next up, we have an enum named E_ZORDER that will basically specify the order of rendering for this game.

Now if you look at the members of the class GameGlobals, you will notice that they're all static. That's simply because these functions are basic helpers to the game and the variables are basic data banks, and it's convenient to have them available to the entire codebase. After the constructor and destructor, you have an Init function that will load default values for the member variables. LoadData is responsible for loading all our spritesheets and sounds. Last but not the least, we have a few helper functions: one that will return a vector of float values from a comma-separated string and another that will return a CCPoint from a comma-separated string. Let's quickly define these methods so we can then actually begin with the game.

The implementation GameGlobals.cpp file looks like this:

#include "GameGlobals.h"

CCSize GameGlobals::screen_size_ = CCSizeZero;

void GameGlobals::Init()
{
  screen_size_ = CCDirector::sharedDirector()->getWinSize();
  LoadData();
}

void GameGlobals::LoadData()
{
  // add Resources folder to search path. This is necessary when releasing for win32
  CCFileUtils::sharedFileUtils()->addSearchPath("Resources");

  // load sprite sheet/s
  CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("spacetex.plist");

  // load sound effects & background music
  SOUND_ENGINE->preloadEffect("blast_brick.wav");
  SOUND_ENGINE->preloadEffect("blast_enemy.wav");
  SOUND_ENGINE->preloadEffect("blast_player.wav");
  SOUND_ENGINE->preloadEffect("game_over.wav");
  SOUND_ENGINE->preloadEffect("level_complete.wav");
  SOUND_ENGINE->preloadEffect("shoot_enemy.wav");
  SOUND_ENGINE->preloadEffect("shoot_player.wav");
}

// function takes comma separated string & returns vector of values
vector<float> GameGlobals::GetFloatListFromString(string input)
{
  vector<float> result;
  result.clear();

  if(input == "")
  return result;

  stringstream ss(input);
  float i;
  while (ss >> i)
  {
    result.push_back(i);
    if (ss.peek() == ',')
    ss.ignore();
  }
  return result;
}

// function takes comma separated string & returns CCPoint
CCPoint GameGlobals::GetPointFromString(string input)
{
  CCPoint point = CCPointZero;
  if(input == "")
  return point;
  vector<float> list = GetFloatListFromString(input);
  point.x = list[0];
  point.y = list[1];
  return point;
}

We assigned a default value for the screen size and specified the default constructor and destructor. Inside the Init function, we saved the screen size from CCDirector and finally called the LoadData function.

In the LoadData function, we added the Resources path to the search paths inside CCFileUtils. This points CCFileUtils to our resources. Then, we loaded our sprite sheet and all our sound effects.

Finally, we defined our helper functions, starting with the GetFloatListFromString function. This function uses the functions inside std::string to turn an input string such as "10,20,30,40,50" into std::vector containing float values such as {10.0f, 20.0f, 30.0f, 40.0f, 50.0f}.

The GetPointFromString function uses GetFloatListFromString to return a CCPoint from an input string. Alright, things are about to get exciting now! The first thing you get to learn is how to extend CCSprite and create your own version of it!

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

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