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?

Recommended Answers

All 9 Replies

my original hard coding produced poor performance.

You're not going to get any better performance either. If you want good performance then use a game engine that is designed for that, such as DirectX or OpenGL (there are probably many others too).

Eh... I guess I will have to deal with poor performance. This is indtroductory programming course where we learn C. We have to make a game for final project using graphics.h library as this is all we have been shown and worked with so far. DirectX sounds like quite a complicated library where you REALLY have to know all the ins and outs of the programming language to utilize... I am not quite there haha.

If you're using graphics.h then I suspect you are using Turbo C. That compiler can not use either of the libraries I mentioned because it's too old amd obsolete. I don't know how to help you because it's only been 30 years since I used that compiler.

Oh I am using visual studio 2012. We were never really supposed to learn graphics, but the whole class wanted to so since we had never programmed before our teacher only showed us graphics.h since we wouldn't be able to handle other libraries at this point and has us make a game using graphics.h.

I am also confused on where to place my keyboard inputs. I want to use wasd movement for this game, but don't know where to check for keyboard hit. I wonder if that will solve unhandled exception? Since right now it just loops through and redraws character facing all directions. If I put keyboard hit it would ONLY draw one image at a time instead of all of them.

That compiler doesn't know how to use graphics.h, unless you found a 3d party library that implemented it for 32-bit and/or 64-bit MS-Windows os. graphics.h was used in the 1980s by Turbo C for MS-DOS 16-bit operating system. Pretty nice in it's day, but not useful today.

Of course, you might be talking about a completely different graphics.h. If so, where did it come from because it's not part of VC++ installation.

Well my professor created a rather advanced calculator for class use that we have been writing our own programs in and the calculator runs them (like games and what not). He implemented graphics.h into this program and we use it. He wrote a header file, and put graphics.h into the header file, and now we write programs in the header file.

But graphics.h working or not is not the issue here as it does indeed work. My problem is with my code above and implementing key hits to actually move the character.

Your professor wrote his/her own version of graphics.h (and associaged library). It's not the same one that is commonly used by Turbo C. Too bad he named it graphics.h because its confusing with Turbo C's graphics.h which most if not all older programmers are familiar with.

Does your professor's graphics.h contain non-standard keyboard handling functions? such as those found in conio.h, which is also non-standard? The standard C keyboard functions can't do what you have in mind because you always have to press <Enter> key afterwards. Better to use getch() from conio.h because it doesn't have that problem. getch() returns immediately, 0 or 224 if a special key was hit such as Function keys, one of the arrow keys, home, end, PgUp, PgDown. When that happens you just call getch() a second time to find out which of those keys was hit.

You might try putting it at line 101, after that if statement

void character_animation()
{
    for (int i = 0; i < MAX_LOOPS; i++){
        if( ! character[i].draws)
            continue;
    // wait for keyboard input
    while( !_kbhit() )
       Sleep(100);
    int c = getch();
    if( c == 0 || c == 224)
        c = -getch(); // make it negative

    //Puts image of character on screen based on inputs from struct

Here is a short test program to display key values

#include <conio.h>

#include <iostream>
using std::cout;

int main(int argc, char* argv[])
{
    int c;
    while(1)
    {
        c = _getch();
        cout << c << '\n';
        if( c == 0 || c == 224)
        {
            cout << _getch() << '\n';
        }
    }
}

Oooh... I didn't know about the turbo C thing haha. He might have mentioned he wrote it in class? I don't remember though :P. I thought it was the graphics.h.

Thanks for the help. I will try putting after that for() loop and see what happens and post back the results.

Alright! We are getting somewhere! The character now animates and "moves" only when I press a button and the program is void of unhandled exceptions :P.

Now, I say "moves" in quotes because he doesn't actually move anywhere... just goes through animation. Lol... Now I must figure out what is wrong that prevents the incrementation of his position.

holy mother mary I solved the problem! Where i was doing:

character[i].xCoord += character[i].xMove;

I just needed to do: character[i].xCoord = character[i].xCoord + 5!

Genius!

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.