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
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]);
}
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!