Ok, so I attempted using a struct to animate my character, since my original hard coding produced poor performance. Here is my code:
//struct template for animating character movement
struct animate {
int draws;
int animationFrames;
char *frames[TOTAL_FRAMES];
int charWidth;
int charHeight;
int xCoord;
int yCoord;
int xMove;
int yMove;
int currentFrame;
};
//Actual struct for character movement
struct animate character[TOTAL_FRAMES] =
{
{
//Character moving left
1,
3,
{
"PIX\\Character\\left_r.gif",
"PIX\\Character\\left_still.gif",
"PIX\\Character\\left_l.gif",
"PIX\\Character\\left_still.gif",
},
50,
50,
x9,
y9,
x9 = x9 + 5,
0,
0,
},
{
//Character moving right
2,
3,
{
"PIX\\Character\\right_r.gif",
"PIX\\Character\\right_still.gif",
"PIX\\Character\\right_l.gif",
"PIX\\Character\\right_still.gif",
},
50,
50,
x9,
y9,
x9 = x9 + 5,
0,
0,
},
{
//Character moving up
3,
3,
{
"PIX\\Character\\up_r.gif",
"PIX\\Character\\up_still.gif",
"PIX\\Character\\up_l.gif",
"PIX\\Character\\up_still.gif",
},
50,
50,
x9,
y9,
0,
y9 = y9 - 5,
0,
},
{
//Character moving down
4,
3, //number of frames for array to consider
{
"PIX\\Character\\down_r.gif",
"PIX\\Character\\down_still.gif",
"PIX\\Character\\down_l.gif",
"PIX\\Character\\down_still.gif",
},
50, //width of character
50, //Height of character
x9, //starting position
y9,
0,
y9 = y9 + 5, //Character movement
0,
},
};
void character_animation()
{
for (int i = 0; i < MAX_LOOPS; i++){
if( ! character[i].draws)
continue;
//Puts image of character on screen based on inputs from struct
readimagefile(character[i].frames[character[i].currentFrame],
character[i].xCoord,
character[i].yCoord,
character[i].xCoord + character[i].charWidth,
character[i].yCoord + character[i].charHeight );
character[i].currentFrame++; //increments the "frame" by one, thus only animating one image at a time
if(character[i].currentFrame == character[i].animationFrames){
character[i].currentFrame = 0; //When frame 4 is reached, it resorts back to first frame and restarts
character[i].draws = 0;}
//These next two lines of code dictate the movement of the character
character[i].xCoord += character[i].xMove;
character[i].yCoord += character[i].yMove;
}
putimage(0, 0, bkimage, COPY_PUT);
Sleep(10);
}
TOTAL_FRAMES = 4, and MAX_LOOPS = 10.
Right now I am getting an unhandled exception access violation. I am needing the character to move in this way because I will be implementing an attack animation whenever space bar is pressed in a similar manner.
I attempted a trouble shoot and I would find out when the unhandled exception happens, but I don't understand how to fix it haha.
It currently prints the character facing all four directions then breaks. I will also be needing to get keyboard input for this. I will be having it move up with w, right - d, left - a, down - s. If I put in the key commands will it keep from breaking since it will only animate one thing at a time instead of everything?