Here is the code u need to set up a precise update system.
// Global variablesfloat g_fCalcDelta;__int64 PerfTimerRate;__int64 PerfTimerDivider = 0;// Call this on game initbool InitPerfTimer(void){ __int64 tempInt64; // Aquire the timer frequency if(!QueryPerformanceFrequency((LARGE_INTEGER*)&PerfTimerRate)) { return FALSE; // System doesnt have a Performance timer } // Check the rate (usually 1193180) if(PerfTimerRate==0) { return FALSE; } // Calculate millisecond divider PerfTimerDivider = (__int64)( (__int64)PerfTimerRate / 1000); if(!QueryPerformanceCounter((LARGE_INTEGER*)&tempInt64)) { return FALSE; // win errors } return TRUE; // Success}WORD FTime(void){ __int64 tempInt64; QueryPerformanceCounter((LARGE_INTEGER*)&tempInt64); return (WORD)((_int64)tempInt64 / (__int64)PerfTimerDivider);}// Call this every frame to get the frame diff.void UpdateTimeDeltas(){ static WORD timerDelta = 0; static WORD oldftime = 0; static WORD newftime = 0; static WORD oldnDiff = (WORD)FTime(); // Calculate the frame delta. newftime = FTime(); if( oldftime > newftime ) oldftime = newftime; timerDelta = newftime - oldftime; oldftime = newftime; float fCalcDelta = float(timerDelta / 1000.0f); // Set the global fCalcDelta and counter. g_fCalcDelta = fCalcDelta;}// Call every frame to update the sprite positions.void UpdateSprites(bool bLeft, bool bRight){ // Player velocity vel = 100.0f; if(bLeft) Player.xpos -= g_fCalcDelta * vel; if(bRight) Player.xpos += g_fCalcDelta * vel; // Note ''Player.xpos'' is a float variable.}
As for the background scrolling, give your background and main sprite a global position, and whenever you update the position of the main sprite, at the same time update the position of the background minus the sprite offset of the main sprite.
For example (640x480):
void SetBgndPos(){ // Center the player. Level.xpos = WORD(Player.xpos) - 320; Level.ypos = WORD(Player.ypos) - 240;}void UpdateFrame(){ // Set the Bgnd pos in relation to the player pos. UpdateTimeDeltas(); UpdateSprites(..DInput status..); SetBgndPos(); // Blit the background tiles. Blit(Level.lpDDSurface, Level.xpos-640, Level.ypos); Blit(Level.lpDDSurface, Level.xpos, Level.ypos); Blit(Level.lpDDSurface, Level.xpos+640, Level.ypos); // Blit the sprite Blit(Player.lpDDSurface, 320, Player.ypos); }
Note; The Blit() function would be your own routine.
Hope this helps
Downloads: ZeroOne Realm