iDevGames Forums
How to run short animation sequences? - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: How to run short animation sequences? (/thread-674.html)



How to run short animation sequences? - johncmurphy - Oct 14, 2009 02:04 PM

Suppose I have a standard game setup: the main loop calls the update() function at some regular interval, which updates the game world, while the render() function takes care of all of the graphics.
So how do I present a short animation sequence, like an enemy being blown up? I don't want the whole game to stop while the render() function plays the animation sequence. Do I need a separate thread?


How to run short animation sequences? - warmi - Oct 14, 2009 03:09 PM

johncmurphy Wrote:Suppose I have a standard game setup: the main loop calls the update() function at some regular interval, which updates the game world, while the render() function takes care of all of the graphics.
So how do I present a short animation sequence, like an enemy being blown up? I don't want the whole game to stop while the render() function plays the animation sequence. Do I need a separate thread?

No, you just need a function/class which can handle animations based on some time delta parameter.

First you need to measure how much time has passed since the last frame and update your animation based on that value.
For instance if your animation was supposed to last 5 seconds, on each frame you would update it based on the time delta parameter ( if your game is running at say 30 fps , the value of that parameter would be around 33 ms or 0.03 second)


How to run short animation sequences? - AnotherJake - Oct 14, 2009 03:38 PM

As warmi already said, no, you definitely do not need a separate thread for this.

Here's some completely made-up pseudocode. Maybe it'll help, maybe it won't, I dunno...

Code:
#define NUM_MAX_ENEMIES            667 <-- even one more enemy than pure EVIL!
#define ENEMY_SPEED                23.5f

// we'll make enemies blow up for 2 seconds in this contrived demo, but you'd calc
// it based upon your enemy blow-up animation duration
#define HOW_LONG_ENEMY_BLOWS_UP    2.0

typedef struct
{
    bool    isAlive;
    float    x;
    float    y;
    bool    isBlowingUp;
    double    blowingUpTime;
    int        blowUpFrame;
} Enemy;

Enemy    enemy[NUM_MAX_ENEMIES];

void init(void)
{    
    int    i;
    for (i = 0; i < NUM_MAX_ENEMIES; i++)
    {
        enemy[i].isAlive = false;
        enemy[i].x = 0.0f;
        enemy[i].y = 0.0f;
        enemy[i].isBlowingUp = false;
        enemy[i].blowingUpTime = 0.0;
        enemy[i].blowUpFrame = 0;
    }
    
    // create one enemy to start out with, because the forces of evil haven't amassed yet
    enemy[0].isAlive = true;
}

void update(double deltaTime)
{
    int    i;
    for (i = 0; i < NUM_MAX_ENEMIES; i++)
    {
        if (enemy[i].isAlive)
        {
            enemy[i].x += ENEMY_SPEED * deltaTime;
            enemy[i].y += ENEMY_SPEED * deltaTime;
            
            // if enemy goes to the wrong part of the map, we'll explode him!
            if (!enemy[i].isBlowingUp && enemy[i].x > 66.6f && enemy[i].y > 66.6f)
            {
                enemy[i].isBlowingUp = true;
                enemy[i].blowingUpTime = 0.0;
                enemy[i].blowUpFrame = 0;
            }
            
            if (enemy[i].isBlowingUp)
            {
                enemy[i].blowingUpTime += deltaTime;
                
                if (enemy[i].blowingUpTime >= HOW_LONG_ENEMY_BLOWS_UP)
                {
                    // the enemy is well and truly dead now, so take him out of action
                    enemy[i].isBlowingUp = false;
                    enemy[0].isAlive = false;
                }
                else
                {
                    // calc which frame of the enemy blow-up sequence should be played, based upon enemy[i].blowingUpTime
                    // you can easily figure this out yerself, but I didn't want to add anymore code to this sample cuz I'm lazy ;-P
                    enemy[i].blowUpFrame = your fancy calc here
                }
            }
        }
    }
}

void render(void)
{
    int    i;
    for (i = 0; i < NUM_MAX_ENEMIES; i++)
    {
        if (enemy[i].isAlive)
        {
            if (enemy[i].isBlowingUp)
            {
                myDrawEnemyBlowupFrame(i);
            }
            else
            {
                myDrawEnemy(i);
            }
        }
    }
}