Hello Daniweb,
I am writing a game engine in C++ with SDL, and am faced with a problem. I need to store the sprite animation coordinates for every different animation in a different array. The problem I am facing is, since I am putting the arrays in one single Player class, and there will be different players with different animations and amount of animations, how would I implement this?
Is it possible to add class members dynamically? Could I load the member variables from a text file? I've been sitting at my computer for a good hour/ hour and a half trying to figure this out.

Thanks, and all help is appreciated,
~EpicAsian

Recommended Answers

All 3 Replies

what you need is a collection of animation coordinate arrays. The STL is your friend here: Do you want to always iterate through all the sprites? Then use a vector. Do you want to look up sprites by name? Use a map. Each Player instance then has one collection of sprite data.

Yes you could have them stored in a text file and then have a vector in your sprite class to hold the data. Then if you need it you can read the data from the file and store it into the vector.

Better yet, you should decouple the system. A player class should not have an array
that stores the animation sequence. Try something like this for example.

class Player{
  //blah blah blah
 void move(int x, int y){ /*logic goes here */ }
 //more blah blah blah
};

template<typename CoordType>
struct Animation{
 typename typedef std::pair<CoordType,CoordType> Coordinate;
 typename typedef std::vector<Coordinate> AnimationType;

 virtual void animate()= 0;
};


class PlayerAnimation: public Animation<float>{
private: 
 AnimationType sequenceOfMoves;
 Player thePlayer;
public:
 PlayerAnimation(const Player& player,const AnimationType& c) : thePlayer(player), sequenceOfMoves(c){};

 virutal void animate()const{
   //psuedocode here
   foreach(Coordinates coordinate in sequenceOfMoves){
       thePlayer.move(coordinate.x(), coordinate.y());
   }
 }
};
void fillPlayerMoves(const Animation::AnimationType& seq){
 /* logic to animate player goes here */
}
int main(){
 Player player;
 Animation::AnimationType listOfMoves;
 fillPlayerMoves(listOfMoves);
 Animation animation = new PlayerAnimation(player,listOfMoves);
 animation.animate();
}
commented: Nice method. +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.