I have been experimenting with the devkitARM toolchain for NDS homebrew recently. Something I would like better understand, however, is how to control sprite animation speeds. The only way I know of doing this is by 'counting frames'. For example, the following code could be placed into the "animate_simple" example included with devkitpro:

int main(void) {
    int frame = 0;
    ...
    while(1) {
    ...
        if(++frame > 9)
            frame = 0;
    }
    return 0;
}

This is generally fine, but it ensures that all the animation initialized in the main loop runs at a set speed. How would I go about having two different sprites, each animating at different speeds? Any insight would be greatly appreciated.

有帮助吗?

解决方案

Use a separate frame counter for each sprite. For example, you can create a sprite struct:

typedef struct _Sprite Sprite;
struct _Sprite
{
    int frame;
    int count;
    int delay; /* handles the speed of animation */
    int limit;
};

// initialize all fields

void sprite_update(Sprite* s) 
{
    if ( ( s->count++ % s->delay ) == 0 ) )
    {
        if ( s->frame++ > s->limit ) s->frame = 0;
    }
}

Give delay small value for faster animation, and a larger value for slow animation.

Sample Usage:

Sprite my_spr, my_spr_2;

/* initialize the fields, or write some function for initializing them. */

my_spr.delay = 5; /* fast */
my_spr_2.delay = 10; /* slow */

/* < in Main Loop > */

while(1){
...
   sprite_update(&my_spr);
   sprite_update(&my_spr_2);
}

Note: Since you are targeting only one platform, the best way to control animation speed is to monitor the frame rate ( or "counting frames" as you call it ). Good thing about programming for consoles is, you don't have to set timed delays. Since all console devices of the same model usually run at the same speed, so the animation speed you get on your machine ( or emulator ) will be the same everyone gets. Different Processors Speeds are a real headache when programming for the PC.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top