PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

timing woes

I'm having some timing issues where I want certain animation to run slower than game loop (like every third time)

I'm not sure what the best way to do this is... I don't want to add Sleep() because I want the game loop to process as normal - getting clicks, keys etc

let's say I have some psuedo code like

Code:

while (1)
{
resettimer();
surf->clear( 0 );

ProcessKeysAndClicks();
MoveSprite1();
MoveSprite2();
Draw();

while (msecs() < 30) {};
update();
}


but I want Sprite2 to only be updated every 3 game loops cycles and Sprite1 to update every 2 game loops

the MoveSprite functions are simply like x++,y++

I figure I can do
Sprite1.x+=2
Sprite2.x+=3
but that might screw up my movement

Another thing I thought about doing was having a unsigned long int that constantly increments each game loop and do
if (longint%2==0) MoveSprite1
if (longint%3==0) MoveSprite2

anyone else have any ideas or know the most common way to do this??

Thanks !
You can use fixed-point to simulate fractional moves, do moves based on the time elapsed or both.

I didn't read the gameframe documentation yet, so I don't know how is the actual support for fixed-point, then here is some pseudo-code using floats:

Code:

// this is the real speed
// that is going to be scaled by the
// time elapsed -- note that this
// must be a low number, imagine
// if 30ms elapsed, this is 0.1f * 30...
float speedx = 0.1f;
float speedy = 0.2f;

// scale the speed by the time elapsed,
// the more time elapses, the more
// the sprite will move.
// like, 0.1f * 30 ...
int ix = speedx * timeElapsed;
int iy = speedy * timeElapsed;

// update the sprite coordinates
spritex += ix;
spritey += iy;

// draw the sprite
m_sprite->Draw ( ... );


This is not too fast on the PPC, you already know that. The best is to use fixed-point for the speeds, scale with the time elapsed, then bring it back.
[/code]