Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
SDL Game Development

You're reading from  SDL Game Development

Product type Book
Published in Jun 2013
Publisher Packt
ISBN-13 9781849696821
Pages 256 pages
Edition 1st Edition
Languages
Author (1):
Shaun Mitchell Shaun Mitchell
Profile icon Shaun Mitchell

Chapter 8. Creating Alien Attack

The framework has come on in leaps and bounds and we are almost ready to make our first game. We are going to create a simple 2D sidescrolling shooter in the vein of the classic '80's and '90's shooter games such as R-Type or Pulstar. However, the game will not be set in space. Aliens have attacked earth and only you and your weaponized helicopter can stop them. One level of fast-paced action is available in the source code downloads and this chapter will cover the steps taken to create it. Here is a screenshot of the game we will be creating:

And another slightly more hectic shot:

There are still a few things that the framework must handle before we can create this game. These additions include:

  • Sound

  • Collision detection

By the end of the chapter you will have a good understanding of how this game was built using the framework and you will have the ability to continue and improve it. In this chapter, we will cover:

  • Implementing sound

  • Creating game-specific object...

Using the SDL_mixer extension for sound


The SDL_mixer extension has its own Mercurial repository that can be used to grab the latest source for the extension. It is located at http://hg.libsdl.org/SDL_mixer. The TortoiseHg application can again be used to clone the extension's Mercurial repository. Follow these steps to build the library:

  1. Open up TortoiseHg and press CTRL+SHIFT+N to start cloning a new repository.

  2. Type http://hg.libsdl.org/SDL_mixer into the source box.

  3. The Destination will be C:\SDL2_mixer.

  4. Hit Clone and wait for completion.

  5. Navigate to C:\SDL2_mixer\VisualC\ and open SDL_mixer.vcproj in Visual Studio 2010.

  6. As long as the x64 folder outlined in Chapter 2, Drawing in SDL was created, the project will convert with no issues.

  7. We are going to build the library without MP3 support as we are not going to need it, and also it does not work particularly well with SDL 2.0.

  8. Add MP3_MUSIC_DISABLED to the Preprocessor Definitions in the project properties, which can be found by navigating...

Setting up the basic game objects


The majority of the work that went into creating Alien Attack was done in the object classes, while almost everything else was already being handled by manager classes in the framework. Here are the most important changes:

GameObject revamped

The GameObject base class has a lot more to it than it previously did.

class GameObject
{
public:
  // base class needs virtual destructor
  virtual ~GameObject() {}
  // load from file 
  virtual void load(std::unique_ptr<LoaderParams> const &pParams)=0;
  // draw the object
  virtual void draw()=0;
  // do update stuff
  virtual void update()=0;
  // remove anything that needs to be deleted
  virtual void clean()=0;
  // object has collided, handle accordingly
  virtual void collision() = 0;
  // get the type of the object
  virtual std::string type() = 0;
  // getters for common variables
  Vector2D& getPosition() { return m_position; }
  int getWidth() { return m_width; }
  int getHeight() { return m_height...

Handling bullets


Most objects in the game fire bullets and they all pretty much need to be checked for collisions against bullets as well; the bottom line—bullets are important in Alien Attack. The game has a dedicated BulletHandler class that handles the creation, destruction, updating, and rendering of bullets.

Two types of bullets

There are two types of bullets in the game, PlayerBullet and EnemyBullet, both of which are handled in the same BulletManager class. Both of the bullet classes are declared and defined in Bullet.h:

class PlayerBullet : public ShooterObject
{
public:

  PlayerBullet() : ShooterObject()
  {
  }

  virtual ~PlayerBullet() {}

  virtual std::string type() { return "PlayerBullet"; }

  virtual void load(std::unique_ptr<LoaderParams> pParams, Vector2D 
  heading)
  {
    ShooterObject::load(std::move(pParams));
    m_heading = heading;
  }

  virtual void draw()
  {
    ShooterObject::draw();
  }

  virtual void collision()
  {
    m_bDead = true;
  }

  virtual...

Dealing with collisions


With so many bullets flying around and having the Enemy objects to check collisions against, it is important that there be a separate class that does this collision checking for us. This way we know where to look if we decide we want to implement a new way of checking for collisions or optimize the current code. The Collision.h file contains a static method that checks for collisions between two SDL_Rect objects:

const static int s_buffer = 4;

static bool RectRect(SDL_Rect* A, SDL_Rect* B)
{
  int aHBuf = A->h / s_buffer;
  int aWBuf = A->w / s_buffer;

  int bHBuf = B->h / s_buffer;
  int bWBuf = B->w / s_buffer;

  // if the bottom of A is less than the top of B - no collision
  if((A->y + A->h) - aHBuf <= B->y + bHBuf)  { return false; }

  // if the top of A is more than the bottom of B = no collision
  if(A->y + aHBuf >= (B->y + B->h) - bHBuf)  { return false; }

  // if the right of A is less than the left of B - no collision...

Possible improvements


Alien Attack is a pretty robust game at the moment; we highly recommend looking through the source code and becoming familiar with every aspect of it. Once you have a good understanding of most of the areas of the game, it is a lot easier to see where certain areas could be enhanced. Here are some ideas that could be added to improve the game:

  • Bullets could be created at the start of a level and stored in an object pool; so rather than creating and deleting bullets all the time they can be pulled from and put back into the object pool. The main advantage of this approach is that the creation and destruction of objects can be quite expensive when it comes to performance. Eliminating this while the game is running could give a real performance boost.

  • Collision detection could be optimized further, possibly through the addition of a Quadtree to stop unnecessary collision checks.

  • The source code has a few areas that use string comparisons to check types. This can be a bit...

Summary


The framework has been successfully used to create a game—Alien Attack. Throughout this chapter, the most important parts of the game were covered, along with a short explanation of why they were designed in such a way. With the source code for this game available, there is now a great project to start practicing with.

lock icon The rest of the chapter is locked
You have been reading a chapter from
SDL Game Development
Published in: Jun 2013 Publisher: Packt ISBN-13: 9781849696821
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}