PocketMatrix forums

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

Sprites and the framerate

I've got several frames for one sprite and want to make an animation. The problem is not the animation but the timing. Every frame of the sprite should be displayed several frames in the game. So one frame of the sprite should last for say 5ms. I could implement some kind of frame counter and shift the sprite frame every 5 frames of the game but I think there should be a better way or is this the way to go?
Or is there even a nice sprite engine that could do this for me?
What I basically do in my sprite engine is use SetTimer to create an asynchronous timer of n milliseconds. I then use this timer to increment a counter which is monitored within the ProcessNextFrame method to cycle the frames of my animations. This will allow you to increment animation frames based on the duration of your timer, while still allowing you to sync the draw times to ProcessNextFrame.
Well after some problems with the SetTimer I created some sort of timer class. The problem was the callback function because that seemed to be the best solution. Thank you for the help.
This is the prototype of the class that I use in the sprite engine.
Code:

class TimerC
{
private:
UINT id;
static map<UINT , TimerC*> m_timermap;
static void stopTimer(int id) ;
static void CALLBACK timerProc(HWND hwnd, UINT wMsg, UINT_PTR nIDEvent, DWORD dwTime);

protected:
virtual void timer(DWORD dwTime) = 0 ;

public:
UINT GetId();
TimerC() : id(NULL) {};
~TimerC() {stop();};
BOOL start(UINT msec) ;
void stop() ;
};

Why don't you use something like this. It wil save you timer troubles and every sprite can have its own timing:


Code:


int anim1[] =
{
3, 0, /* timeout, framenr */
3, 1,
3, 2
}


// and every frame run this code on your sprite:

if (m_nTimeout == 0)
{
int* p = anim1;

m_nMaxTimeout = *p++;
m_nFrame = *p;
}
m_nTimeout++;

if (m_nTimeout == m_nMaxTimeout)
{
m_nTimeout = 0;
m_nIndex++;
}

// and then draw the sprite with framenumber m_nFrame EVERY frame!



And this is easy to adjust with x-y movement as wel :)
spacemonkey · 24 Apr 2003, 11:14 UTC
Cos I'm writing my prog as a more generic enginey thing I put a few variables against each spriteable item. something like:


Code:

struct item {
int animcount;
int animmax;
int animstep;
}


Then in the drawnext frame I do:
Code:

item.animcount++;
item.animcount % item.animmax;

int animframe = item.animcount / item.animstep;
SetRect(&rect,animframe*32,0,animframe*32+32,32);


Not sure about the setrect syntax, just scribbling from memory. So if you you're blitting 32x32 tiles, and your source is layed out with consecutive frames to the right. It'll move the animation on every item.animstep frames and loop after item.animmax/item.animstep animation steps.

Bit heavy on the math but lighter on the if's (not sure how that balances out on performance, but it doesn't seem to kill my app too much and I'm blitting about 150 sprites per drawing frame.
What I did to solve this was to write an Animation class that determined what frame to display based on a passed time dif. All animations had a set frequency based on a set framerate.

I tested this out from 0 frames up to a couple 1000 frames and regardless of the rendering speed it returned the right frame to render. Created an interesting motion blur when sped up actually.
My Timer construction seems a but overkill after reading this. This is just something I use for animations so not for complete levels.
Code:

class SpriteC : public TimerC
{
private:
const TCHAR* m_sprite;
UINT m_spriteFrames;
UINT m_spriteCurrentFrame;
RECT m_spriteRect;
RECT m_posRect;
CGapiSurface m_myimage;
UINT m_frameWidth;
UINT m_frameHeight;
UINT m_timerSpeed;
public:
SpriteC();
void DrawSprite(CGapiSurface& backbuffer,const DWORD &x, const DWORD &y);
void InitSprite(const TCHAR* sprite,
const UINT &spriteFrames,
const UINT &frameWidth,
const UINT &frameHeight,
const UINT timerSpeed);
protected:
void timer(DWORD dwTimer);
};

Then in the void timer the increment on the next fram and this way I can set the number of ms a frame has to last. I also considered to use the multimedia timer but that one is not available on the smartphone.
Krelis, are you creating a timer for each sprite? I'm not sure I would want to do that. I've not looked into how much overhead multiple timers generate, but it wouldn't seem like a good thing if you're creating a boatload of sprites.

I create a handful of timers of different frequencies at the start of my application, and derive all my sprite timing from those.
But if you've got only a handfull of timers (which I also think is better) Do you also have one or more functions that are in charge of selecting the right frame in multiple sprites? So somesort of container class containing multiple sprites and that container class has one timer and based upon that timer and the frequency settings of the animation you select the right frame?
Sounds like the solution Hosed proposed?
Each sprite contains data members covering first, last and current frames, frame timing, flags to indicate if the animation is bidirectional, looping and running forward or in reverse, along with access methods for each. This data is spread out across multiple animation definitions within a sprite (for instance you have a person walking in 8 different directions, therefore you have 8 animation definitions).

The timing of each sprite is based on the timer with the resolution most appropriate to what I need - usually 50 or 100ms is fine (10 - 20 fps of animation).

All frame updates are done via a SpriteHandler which walks thru the various zorder layers, updates the frames as needed and BLTs the sprites to the backbuffer. This is all done within ProcessNextFrame so everything is nicely synced up.

Obviously I've not covered my sprite class in great detail here, but this will give you a basic idea of how I implement it.
You've been a great help. I will try all this and see if I can get something up an running for my first game.
Your sprite animation needs to be based on time.. Don't use counters or timers. Simply store the last update time for that animation sequence and use GetTickCount() or some other global game time to update the animation when necessary. Different animations may have different timings. Regardless of what the frame rate of your game is, the animation will stay in sync.

This is called adaptive timing and is basic Game Programming 101 stuff. Good luck!
The problem with using GetTickCount( ) is the resolution varies from platform to platform. Most WinCE devices have a resolution of ~1ms, NT/Win2K has a resolution of ~10ms and Win95/98/ME is on the order of ~50ms. Using SetTimer ensures you have a stable time base for animation, which is a necessity for reliable, platform independent animation.

[edit]Of course multimedia timers are the preferred way to do animations, but we don't have that option on CE devices. An alternative would be to create a thread that does a Sleep(timeout) then calls a callback function to trigger the animation frame update. This would give excellent resolution and be pretty stable.
Hi. Here is my 2 cents.

What I did was to include both frame based and time based animations.

Frame based for the walking animations of my characters. The higher the frame rate, the faster they walk. I found that the on-screen result was a lot nicer for the characters with frame-based animations.

I use time-based animations for the world objects. So no matter what the frame rate is, and no matter how fast the characters are walking around, the world objects are animated on a steady base.

Now, if timing is important on other platforms than PPC, you may want to take a look at the performance counter. They offer very (very) high resolution. However my understanding is that they are not availailable on all systems. My iPaq 3850 has them. All my PCs have them too (Athlon XP, P3, etc).

Check out QueryPerformanceCounter() and QueryPerformanceFrequency() for more on this. What you could do is make some base timing functions that first try to use the performance counter, if unavailable fall back to GetTickCount().

And there's also timeGetTime() on non PPC platforms. This has been a favorite of mine in the past.

--Eric
Good point Eric... I was just about to post about QueryPerformanceCounter but wanted to verify first that it may or may not be supported on all winCE devices as its an OEM implementation.
ppcStudios wrote:
Good point Eric... I was just about to post about QueryPerformanceCounter but wanted to verify first that it may or may not be supported on all winCE devices as its an OEM implementation.

You are right about that because it is not available on the smartphone platform.
I'm currently going for the timer option and I'm calculating the updates with two different timers.
Still I'm having trouble with my data structure,
for example should a single surface with multiple animations be one object or more or is it better to split all the animations into different surfaces.
I generally use a single surface for an animation object. For example, a man walking may have 8 different directions, each with say 10 frames of animation. My image would be a 10x8 array of sprites all loaded onto a single surface. Then you just need to update the x coordinates of the source rectangle depending on the animation frame, and the y coordinates depending on the direction of movement.