0
\$\begingroup\$

I'm currently drawing my character from a single file, and my sprite class handles it taking the file path as an argument. Now, I need to add a simple animation, cycling between two frames. The problem is that I have the other animation frame in another file, not in the same file as the first frame. How can I handle the animation without loading and unloading the two files and without merging the files into one?

\$\endgroup\$
2
  • \$\begingroup\$ An example of your sprite sheets would be helpful. \$\endgroup\$
    – Grey
    Commented Feb 18, 2014 at 19:02
  • \$\begingroup\$ @Grey They're just 16x16 sprites starting from the origin, each in their separate files. \$\endgroup\$
    – user42390
    Commented Feb 18, 2014 at 19:16

2 Answers 2

2
\$\begingroup\$

If you want to keep it with your current system, you should make a Sprite class with an array of frames (SDL_Surface/etc) that you load at the beginning of the game. Have the renderer choose the correct image based on time or some other constraint. Also, I'd advise you use sprite sheets as there is less overhead and they are generally easier to work with (More encapsulated). And definitely do not load/unload the files during run-time as that would be a huge slowdown.

\$\endgroup\$
1
  • \$\begingroup\$ yes. for reference, my engine (carnage engine) does sprite animation by regrouping frames in the same image and displaying subrects at different times. code source is available: sourceforge.net/projects/carnage-engine \$\endgroup\$
    – v.oddou
    Commented Feb 19, 2014 at 2:18
1
\$\begingroup\$

As CobaltHex said, using a sprite sheet would make this much less complicated, and can speed up your program overall. By creating two different SDL_Rect objects that correspond to the coordinates and dimensions of the two animation sprites you want to use, you can create two options for your what you want a character/entity to look like.

After that, simply using a timer and implementing a switch statement or if-else statement can create a neat looping animation, perhaps for an idle animation, or for a running animation, etc. Take this advice with a little caution though: I'm using this method in my game, and I'm a total newbie to the gamedev community, so I'm sure there are better methods out there.

int initialTime;

while(!flag)
{
    initialTime = SDL_GetTicks();
    int phase = (initialTime/250)%16; //16 divisions, each .25 seconds long
    if(phase%2 == 1) /*apply surface #1*/;
    else /*apply surface #2*/;
}

So, technically, you could have 16 frames in our idle animation, or change around the length of each frame, whatever you want.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .