SpriteHandler class

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Ciidian
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Mon Nov 03, 2008 4:59 pm

SpriteHandler class

Post by Ciidian »

I've been making some basic games for a while now (mostly working with primitives), and am slowly working my way up to building my own engine. I feel I am getting to the point now of creating much more reusable code instead of hard-coding everything I do.

To do this I am basically starting all over again, and creating a Sprite based Pong clone. In doing so I want to make a SpriteHandler class that handles all of the Sprites I want for the game. I have thought of many different ways to handle this, and wanted to get some input from those more experienced on how to implement. This is what I have currently:

This is SpriteHandler.h

Code: Select all

#ifndef _SPRITE_HANDLER_H_
#define _SPRITE_HANDLER_H_

#include "SDL\SDL.h"
#include "Sprite.h"
#include <vector>

using namespace std;

// SpriteHandler class manages all Sprites in the game.
class SpriteHandler
{
   public:
      SpriteHandler();
      ~SpriteHandler();
      
      int addSprite(char*,int,int);
      Sprite* getSprite(int);
      
   private:
      vector<Sprite*> mySpriteList;
};

#endif
And this is SpriteHandler.cpp

Code: Select all

#include "SpriteHandler.h"

SpriteHandler::SpriteHandler()
{
}

SpriteHandler::~SpriteHandler()
//Frees up all the memory used from loading each Sprite.
{
   for(int i=0; i<mySpriteList.size(); i++)
      delete mySpriteList[i];
}

int SpriteHandler::addSprite(char* file, int xPos, int yPos)
//Creates and loads a new Sprite. Then adds the Sprite to the Sprite List.
{
   Sprite *sprite = new Sprite;
   sprite->loadSprite(file);
   sprite->setSpritePos(xPos, yPos);
   
   mySpriteList.push_back(sprite);
   return(mySpriteList.size()-1);
}

Sprite* SpriteHandler::getSprite(int index)
//Returns a Sprite based on the index that the function is given.
{
   return(mySpriteList[index]);
}
It is pretty basic right now. So some questions I have are:

Is using a vector a good choice or is using a linked list better?
Should I have more than just a default constructor?
Is there a more efficient way to handle this?
Should I use a singleton for the SpriteHandler class?

Thanks!
User avatar
zeid
Chaos Rift Junior
Chaos Rift Junior
Posts: 201
Joined: Fri Apr 24, 2009 11:58 pm

Re: SpriteHandler class

Post by zeid »

What you have now seems pretty good.

Linked list has the advantages of being able to remove or add an entry without having to update the entire data-sets order. The dissadvantage comes in the searching through the data, as you can only scan through a linked list 1 way (2 ways if doubly linked).

I think leaving it as a Vector is somewhat fine... unless you plan on regularly change the sprites order. I personally prefer using a map.

Make it a singleton.
Axolotl Pop!
Image
Or, try it for free.

For many more free games online visit www.sam-zeid.com
Post Reply