PocketMatrix forums

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

Smooth Game Animation - GAPIDRAW APPROACH

Hi There,

I've read thousand posts regarding animations. So I decided to post my approach on GapiDraw. Can you make this as an sticky Johan? Hope this will enlighten our budding programmers out there.

I will assume that you're already using GapiDraw ;-)

1. Framerate Time/Ratio Animation
- this means that you have to maintain your increment ratio based on the target framerate / current framerate. so if your target and current is 40/40 then x increments by 5 * 1 which is equal to 5. if the framerate drops at 20fps the the ratio is 40/20 = 2 so x will increment by 5 * 2 which is equal to 10 etc

Code:

#define TARGET_FPS 40
#define MINIMUM_FPS 20

FLOAT m_FrameRate;
FLOAT m_SyncSpeed;

HRESULT CMyApplication::ProcessNextFrame(CGapiSurface* pBackBuffer, DWORD dwFlags)
{
OnSyncSpeed();

// all you need to do here is multiply everything that moves with the variable 'm_SyncSpeed'

// example

static int x = 0; //or float/fixed

x = x + 5 * m_SyncSpeed;

// just dont forget to cast your variables (int) x or use FIXED or FLOAT to avoid rounding off a lot.

return S_OK;
}

void CMyApplication::OnSyncSpeed()
{
m_pTimer->GetActualFrameRate(&m_FrameRate);

if (m_FrameRate > TARGET_FPS)
{
m_FrameRate = TARGET_FPS;
}
if (m_FrameRate < MINIMUM_FPS)
{
m_FrameRate = MINIMUM_FPS;
}

m_SyncSpeed = TARGET_FPS/m_FrameRate;
}



2. Time Based Animation
- this means that you only need the increment x after 1 sec. so if you want to increment the x after every 5 secs then add 5000 instead.

Code:

int m_Timer;

if (m_Timer < (int) GetTickCount())
{ // this must be in the game loop

m_Timer = GetTickCount() + 1000; // 1 sec

x = x + 5;
}



Animation is as simple as that ;-)

Good luck.

/special thanks to FASTRX for FLOAT/FIXED comments.