How to run short animation sequences?

Member
Posts: 65
Joined: 2009.01
Post: #1
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?
Quote this message in a reply
Member
Posts: 166
Joined: 2009.04
Post: #2
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)
Quote this message in a reply
Moderator
Posts: 3,573
Joined: 2003.06
Post: #3
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);
            }
        }
    }
}
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  How to design short game levels vangelisfx 1 2,225 Dec 23, 2009 01:27 PM
Last Post: ThemsAllTook
  Requesting short OpenGL ES example SafariAl 2 2,982 Dec 10, 2008 01:39 PM
Last Post: SafariAl
  how to play short sounds asynchronously ishaq 1 3,122 Sep 11, 2008 10:03 PM
Last Post: AnotherJake