# Multi-framerate ... ?

## Recommended Posts

Hi! I'm currently working on something that I can't figure out... It's a 2D game (using direct3d), and I want to have multiple frame rate for my sprites; what I mean by this is that if a frame is generated 20 times per second, I don't necessarily want my animated sprite to change frame 20 times per second :( It might sound simple but I've been on this for 2 days, I'm exhausted -_- so does anyone have an example of a loop or frame switching mechanisms etc... to achieve this ?? I joined a code snippet... Dropping the frame rate is not an option since there are sprites I want to update that much, but if anyone have a completely other way of doing this I would really appreciate any info ^_^ thanks ! m@x.
void Logic(OBJECT go[], INPUTDATA* id) {

static int this_time = 0;
static int last_time = 0;
static int elapsed_time = 0;

// These 3 lines are used to know the elapsed time between
// each frames and store the value in elapsed_time
this_time = GetTickCount();
elapsed_time += (this_time - last_time);
last_time = GetTickCount();

//
if(elapsed_time >= int( 1000 / FRAME_RATE )) {
// Here I switch the frames etc...
elapsed_time = 0;
}

}



##### Share on other sites
Quote:
 Original post by m4x30000Hi!I'm currently working on something that I can't figure out... It's a 2D game (using direct3d), and I want to have multiple frame rate for my sprites; what I mean by this is that if a frame is generated 20 times per second, I don't necessarily want my animated sprite to change frame 20 times per second :( It might sound simple but I've been on this for 2 days, I'm exhausted -_- so does anyone have an example of a loop or frame switching mechanisms etc... to achieve this ?? I joined a code snippet... Dropping the frame rate is not an option since there are sprites I want to update that much, but if anyone have a completely other way of doing this I would really appreciate any info ^_^thanks !m@x.*** Source Snippet Removed ***

you could let each sprite know its framerate and the time since its last switch.

so

thisTime = GetTickCount();frameTime = thisTime-lastTime;lastTime = thisTime;for (int i=0; i<spriteCount;i++) {    sprite[i].timeSinceLastFrameSwitch+=frameTime;    if (sprite[i].timeSinceLastFrameSwitch >= 1000 / sprite[i].frameRate) {        sprite[i].nextFrame();        sprite[i].timeSinceLastFrameSwitch -= 1000 / sprite[i].frameRate);    }}

(Ofcourse its a good idea to use a linked list or vector to store the sprite, and perhaps move the animation code into the sprite class so that you only need to pass the frameTime to the sprites)

##### Share on other sites
You need to look up time-based animation (as opposed to frame-based animation)

I.e don't change your sprite's image based on how many frames have elapsed (in your case it seems to be every frame) but based on how much time has elapsed.

Good luck

##### Share on other sites
My Animation class has a default updateRate of 1, which means that every time there's a sim update, the animation advances 1 frame. I can set it to less than 1 for a slower animation (e.g. sim update 1, animation frame 1, sim update 2, animation frame still 1 (internal 1.5), sim update 3, animation frame 2). Basically the way this works is, every time I update the animation, I advance its 'frame' field by an arbitrary number. When it comes time to draw the animation, I truncate that number to an int, and use it to index into the animation's 'surfaces' field to know what image should be displayed.

I even went a bit overboard and let my Animations use arbitrary functions in their update function, so they can base their update rates on anything they want. For example, I have a running animation whose update rate depends on the horizontal velocity of the object, and a turnaround animation that reverses itself if you press backwards, by returning a negative number.

##### Share on other sites
This is what I've been using for my animated cursors:
int frame = currentTime - (currentTime/animTimeTotal)*animTimeTotal;
frame = (frame<0)? 0 : Clamp( frame/animTime, 0, animFrames-1 );

Variables (all values are int):
frame - the animations frame to draw
currentTime - obvious
animFrames - number of frames the animation has
animTime = the time (in ms) one frame should be shown ( i.e. 1000/fps )
animTimeTotal = animTime * animFrames

##### Share on other sites
Hi you can try this : -
In your sprite class define two more variables
Delay = minimum number of tickers required to be passed before
updating the frame of the sprite
lasttick = when you update your frame update this lasttick to current value

Here is an example : -

[source = "cpp"]class Sprite{// am skipping other code in sprite classint delay,lasttick;//suppose this is the function updating the frame of your animated spritevoid updateFrame(int currentTick) { if( (currentTick - lasttick) < Delay )  {    return;  }  //code to update frame lasttick = currentTick; }};Sprite spriteobj;//Render Loopvoid Render(){int tick = GetTickCount();spriteobj.updateFrame(tick);}

This may not be the best solution but may be one of the simple ones.

I have found one nice article on Game Loop few days back .
You can refer to that article. However its about making game speed independent of FPS but making some more changes there it can serve your purpose too.

##### Share on other sites
Thanks a lot guys !

I've been able to make it the easy way ;) adding two more variables (mintick, lasttick) -- but I've gather some cool info on time-based animation and game loops -- It's my first game so these will be very helpful in the future -_^

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628389
• Total Posts
2982410

• 10
• 9
• 19
• 24
• 11