Re-visiting my Animation Class [Not Solved]
Posted: Thu May 12, 2011 8:27 am
Hello! I'm currently working on my animation class, and I've come across some problems while trying to make it more flexible.
I am using SDL, but I guess that's not much of relevance on this topic.
What i've used to do so far, is to have an SDL_Rect to each "animation" (one SDL_Rect for "idle", one for "walkingLeft", and so on), as an array to hold 4 frames (4 frames for each state).
And to set them up, I've done something like this:
This can become very tedious. And I'm not quite sure how I can loop all these either to do the work for me.
So, I want to be able, in it's init function, to specify how many states I want for my characters. With a function like "void Anim::init(int numberOfStates, int numberOfFrames);"
And then the animation class should be able to handle all this by itself, with no fixed number of states and frames.
To sum it up: What I need help with, or be guided to, is to make my animation class a little more flexible, so I'm not stuck to the hardcoded states and frames.
In contrast of how my animation class is working now: I am limited to 4 frames (I can add more, but then I'd need to continue hardcoding it), limited to 4 states (the same as frames).
Here is some copy-paste of some of my animation class (my most horryfying class):
Anim.h
Anim.cpp
I am using SDL, but I guess that's not much of relevance on this topic.
What i've used to do so far, is to have an SDL_Rect to each "animation" (one SDL_Rect for "idle", one for "walkingLeft", and so on), as an array to hold 4 frames (4 frames for each state).
And to set them up, I've done something like this:
Code: Select all
walkingRight[0].x = 0;
walkingRight[0].y = 0;
walkingRight[0].w = width;
walkingRight[0].h = height;
walkingRight[1].x = width;
...
walkingLeft[0].x = something
So, I want to be able, in it's init function, to specify how many states I want for my characters. With a function like "void Anim::init(int numberOfStates, int numberOfFrames);"
And then the animation class should be able to handle all this by itself, with no fixed number of states and frames.
To sum it up: What I need help with, or be guided to, is to make my animation class a little more flexible, so I'm not stuck to the hardcoded states and frames.
In contrast of how my animation class is working now: I am limited to 4 frames (I can add more, but then I'd need to continue hardcoding it), limited to 4 states (the same as frames).
Here is some copy-paste of some of my animation class (my most horryfying class):
Anim.h
Code: Select all
class Anim : public Graph
{
protected:
int frame, status;
int frameCount, frameMax;
SDL_Rect clipsUp[4];
SDL_Rect clipsDown[4];
SDL_Rect clipsLeft[4];
SDL_Rect clipsRight[4];
public:
Anim();
~Anim();
void setClips();
// Setters
void setFrame(int myFrame);
// Getters
SDL_Rect getClipsUp();
SDL_Rect getClipsDown();
SDL_Rect getClipsLeft();
SDL_Rect getClipsRight();
};
Code: Select all
Anim::Anim() {
frame = 0;
status = 0;
frameCount = 0;
frameMax = 10;
}
Anim::~Anim() {
//
}
void Anim::setClips() {
// DOWN
// #1 frame
clipsRight[ 0 ].x = 0;
clipsRight[ 0 ].y = 0;
clipsRight[ 0 ].w = width;
clipsRight[ 0 ].h = height;
// #2 frame
clipsRight[ 1 ].x = width;
clipsRight[ 1 ].y = 0;
clipsRight[ 1 ].w = width;
clipsRight[ 1 ].h = height;
// #3 frame
clipsRight[ 2 ].x = width * 2;
clipsRight[ 2 ].y = 0;
clipsRight[ 2 ].w = width;
clipsRight[ 2 ].h = height;
// #4 frame
clipsRight[ 3 ].x = width * 3;
clipsRight[ 3 ].y = 0;
clipsRight[ 3 ].w = width;
clipsRight[ 3 ].h = height;
//UP
// #1 frame
clipsLeft[ 0 ].x = 0;
clipsLeft[ 0 ].y = height;
clipsLeft[ 0 ].w = width;
clipsLeft[ 0 ].h = height;
// #2 frame
clipsLeft[ 1 ].x = width;
clipsLeft[ 1 ].y = height;
clipsLeft[ 1 ].w = width;
clipsLeft[ 1 ].h = height;
// #3 frame
clipsLeft[ 2 ].x = width * 2;
clipsLeft[ 2 ].y = height;
clipsLeft[ 2 ].w = width;
clipsLeft[ 2 ].h = height;
// #4 frame
clipsLeft[ 3 ].x = width * 3;
clipsLeft[ 3 ].y = height;
clipsLeft[ 3 ].w = width;
clipsLeft[ 3 ].h = height;
// LEFT
// #1 frame
clipsDown[ 0 ].x = 0;
clipsDown[ 0 ].y = height * 2;
clipsDown[ 0 ].w = width;
clipsDown[ 0 ].h = height * 2;
// #2 frame
clipsDown[ 1 ].x = width;
clipsDown[ 1 ].y = height * 2;
clipsDown[ 1 ].w = width;
clipsDown[ 1 ].h = height * 2;
// #3 frame
clipsDown[ 2 ].x = width * 2;
clipsDown[ 2 ].y = height * 2;
clipsDown[ 2 ].w = width;
clipsDown[ 2 ].h = height * 2;
// #4 frame
clipsDown[ 3 ].x = width * 3;
clipsDown[ 3 ].y = height * 2;
clipsDown[ 3 ].w = width;
clipsDown[ 3 ].h = height * 2;
// RIGHT
// #1 frame
clipsUp[ 0 ].x = 0;
clipsUp[ 0 ].y = height * 3;
clipsUp[ 0 ].w = width;
clipsUp[ 0 ].h = height * 3;
// #2 frame
clipsUp[ 1 ].x = width;
clipsUp[ 1 ].y = height * 3;
clipsUp[ 1 ].w = width;
clipsUp[ 1 ].h = height * 3;
// #3 frame
clipsUp[ 2 ].x = width * 2;
clipsUp[ 2 ].y = height * 3;
clipsUp[ 2 ].w = width;
clipsUp[ 2 ].h = height * 3;
// #4 frame
clipsUp[ 3 ].x = width * 3;
clipsUp[ 3 ].y = height * 3;
clipsUp[ 3 ].w = width;
clipsUp[ 3 ].h = height * 3;
}
void Anim::setFrame(int myFrame) {
frame = myFrame;
}
SDL_Rect Anim::getClipsUp() {
return clipsUp[frame];
}
SDL_Rect Anim::getClipsDown() {
return clipsDown[frame];
}
SDL_Rect Anim::getClipsLeft() {
return clipsLeft[frame];
}
SDL_Rect Anim::getClipsRight() {
return clipsRight[frame];
}