Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411562 Posts in 69384 Topics- by 58443 Members - Latest Member: junkmail

May 03, 2024, 06:52:32 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Time regulation and such
Pages: [1]
Print
Author Topic: Time regulation and such  (Read 1098 times)
Sivak
Level 0
*


View Profile
« on: April 14, 2010, 02:00:55 PM »

Hey all.  I'm working on a simple shmup game.  Technical details of the project:
-Using VC++ 2008 Express
-Using SFML
-My machine is a WinXP Pro one.

Let me start by saying:  I DO NOT want to use delta time.  It's not necessary in this case.  Most of these games I've seen regulate time based on frame rate and that's the approach I want to take.

Anyway, SFML's built-in timing functions are no good, so I've taken the approach of using the windows ones and seem to be having some success, though I'm not certain if my approach is the best.

This is my regulation code (doesn't have class includes):
Code:
void TimeController::FrameRegulation() 
{ int fps=60;
GameClass& game = GameClass::GetTheGame();
signed long dif = (signed)(GetTix() - nLastTick );
nLastTick += dif;
nAccum += dif * fps;

    if(nAccum < 1000)  // not yet time for next frame
    { if(nAccum < nMinSleepThreshold)
Sleep(1);
return;
    }

// we will be running at least 1 frame here.
//  see if we need to run more (falling behind)
    if(nAccum >= (nSkipFramesBehind*1000))
    {   if(nFramesSkipped >= nMaxFrameSkip)
            ResyncFrameTime();
        else
        {   ++nFramesSkipped;
nAccum -= 1000;
            game.GameLogic();
return;
        }
    }

// run/draw a frame
    nAccum -= 1000;
    nFramesSkipped = 0;
    game.GameLogic();
    game.GameRender();
}

//This is called before the game loop starts.
void TimeController::ResyncFrameTime()
{ nFramesSkipped = 0;
nLastTick = GetTix();
nAccum = 0;
StartTiming();
}

//Get the ticks using the seemingly accurate QPC!!!
unsigned long TimeController::GetTix()
{ LARGE_INTEGER freq;
LARGE_INTEGER tick;
    if(!QueryPerformanceFrequency(&freq) || !freq.QuadPart)
        return GetTickCount();  // QPC not supported, fall back to GetTickCount
    QueryPerformanceCounter(&tick);
    return (unsigned long)(tick.QuadPart * 1000 / freq.QuadPart);
}

It DOES seem to be consistent on my computer along with some other people's (though some report lag...  Might be the under-sleeping), but what I'm wondering is if anyone's taken the approach of just regulating frame rate and has maybe done it better.

If anyone cares to test the app as it is, I have it here:
http://sivak.nintendoage.com/Shmuppy.zip

Note:  The in-game FPS reader is wrong.  If you have Fraps, try that.
Note #2:  If you drag the window or press alt, the game goes insane fast.  I'm wondering how this will be fixable...
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic