class VECTOR2D{
public:
float X,Y;
VECTOR2D();
VECTOR2D(float,float);
~VECTOR2D();
VECTOR2D &VECTOR2D::operator=(VECTOR2D);
};
VECTOR2D::VECTOR2D(){
}
VECTOR2D::VECTOR2D(float x, float y){
X = x;
Y = y;
}
VECTOR2D &VECTOR2D::operator=(VECTOR2D v){
X = v.X;
Y = v.Y;
return *this;
}
VECTOR2D::~VECTOR2D(){
}
VECTOR2D Rotate(VECTOR2D Vector, VECTOR2D Origin, float Angle){ // Rotates a 2D Vector over a point.
VECTOR2D RVEC;
float RAD = Angle/180*3.14159265;
RVEC.X = Origin.X + ((Vector.X - Origin.X) * cos(RAD)) - ((Origin.Y - Vector.Y) * sin(RAD));
RVEC.Y = Origin.Y + ((Origin.Y - Vector.Y) * cos(RAD)) - ((Vector.X - Origin.X) * sin(RAD));
return RVEC;
}
void glVertexV2(VECTOR2D V,float Z = 0){
glVertex3f(V.X,V.Y,Z);
}
class LIMB{ // Each individual piece to an object or Entity
private:
VECTOR2D Vertices[4];
public:
VECTOR2D Pos, Origin;
float Size, Z,Rot;
GLint Texture;
LIMB(VECTOR2D,VECTOR2D,int,int,char*);
~LIMB();
LIMB &LIMB::operator=(LIMB);
void SetRotation(float);
void SetTexture(char *);
void Draw();
};
LIMB &LIMB::operator=(LIMB L){
Pos = L.Pos;
Origin = L.Origin;
Size = L.Size;
Z = L.Z;
Texture = L.Texture;
Rot = L.Rot;
for(int i = 0; i < 4; i++){
Vertices[i] = L.Vertices[i];
}
return *this;
}
void LIMB::SetRotation(float){
Vertices[0].X = -Size/200; // Sets Vertices back to initial to preform a rotation
Vertices[0].Y = -Size/200;
Vertices[1].X = Size/200;
Vertices[1].Y = -Size/200;
Vertices[2].X = Size/200;
Vertices[2].Y = Size/200;
Vertices[3].X = -Size/200;
Vertices[3].Y = Size/200;
for(int i = 0; i < 4; i++){
Vertices[i] = Rotate(Vertices[i],Origin,Rot*2); // Rotates all the Vertices
}
}
void LIMB::SetTexture(char * Name){ // Sets the texture
Texture = ltex(Name,16,16,true,true);
}
LIMB::LIMB(VECTOR2D position, VECTOR2D origin, int size, int rotation, char * name){
Pos = position;
Origin = origin;
Size = size;
Rot = rotation;
SetRotation(Rot);
SetTexture(name);
}
void LIMB::Draw(){ // Draws the Limb using each Vertex for rotation
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,Texture);
glPushMatrix();
glTranslated(Pos.X,Pos.Y,Z);
glBegin(GL_QUADS);
glTexCoord2i(0,0);
glVertexV2(Vertices[0]);
glTexCoord2i(1,0);
glVertexV2(Vertices[1]);
glTexCoord2i(1,1);
glVertexV2(Vertices[2]);
glTexCoord2i(0,1);
glVertexV2(Vertices[3]);
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
LIMB::~LIMB(){
}
struct FRAME{ // Hold rotation, position, and frame data
VECTOR2D Pos;
float Rot;
int NumberOfSubFrames;
};
class ENTITY{ // Class for all moving objects in the game
private:
bool LoopAnimation, Animating;
int CurrentKeyFrame, CurrentSubFrame, NumberOfLimbs, NumberOfKeyFrames, NumberOfSubFrames;
FRAME * Frames;
public:
VECTOR2D Pos;
LIMB * Limbs;
int Action,Health;
void LoadAnimation(char *);
void Animate();
void Display();
};
void ENTITY::Animate(){
for(int i = 0; i < NumberOfLimbs; i++){ // Runs through all the frames, then interpolates for subframes
FRAME NewFrame;
NewFrame.Pos.X = LinearInterpolate(CurrentSubFrame,1,Frames[CurrentKeyFrame].Pos.X,NumberOfSubFrames,Frames[CurrentKeyFrame+1].Pos.X);
NewFrame.Pos.Y = LinearInterpolate(CurrentSubFrame,1,Frames[CurrentKeyFrame].Pos.Y,NumberOfSubFrames,Frames[CurrentKeyFrame+1].Pos.Y);
NewFrame.Rot = LinearInterpolate(CurrentSubFrame,1,Frames[CurrentKeyFrame].Rot,NumberOfSubFrames,Frames[CurrentKeyFrame+1].Rot);
}
CurrentSubFrame++; // Sets the SubFrame further for a different piece of animation
if(CurrentSubFrame == NumberOfSubFrames){ // Checks to see if the SubFrames is maxed to use a new Key Frame
CurrentSubFrame = 0;
CurrentKeyFrame++;
}
if(CurrentKeyFrame == NumberOfKeyFrames){ // Checks to make sure there are more KeyFrames
if(LoopAnimation){ // Resets if there are no more frames and looping
CurrentKeyFrame = 0;
}
else{
Animating = false; // If not, the Entity isn't animating
}
}
}
void ENTITY::Display(){ // Displays the Entity
glPushMatrix();
glTranslated(Pos.X,Pos.Y,0); // Translates the Entity to position
for(int i = 0; i < NumberOfLimbs; i++){ // Runs through and displays all the limbs
Limbs[i].Draw();
}
glPopMatrix();
}