Sign in to follow this  
lipsryme

Frame time limiting problems

Recommended Posts

I'm having problem trying to limit my rendering code (or at least the update loop) in my dx11 based application.

Here's my "game loop":
[CODE]
while(isRunning)
{
int errorCode = this->window->UpdateMessages();

this->timer->Frame();
double frameTime = this->timer->GetFrameTime();
static double frame = frameTime;
if(errorCode == 0)
{
if(frameTime >= (1000 / 60))
{
double deltaTime = frameTime / 1000.0; // delta frame time in seconds
// Update Input
if(this->engineDesc.cameraDesc.type != Static)
{
this->directInput->DetectInput((FreeCamera*)this->GetCamera(), this->profiler, deltaTime);
}
else
{
this->directInput->DetectInput(NULL, this->profiler, deltaTime);
}
// Call scene's Update()
this->scene->Update(deltaTime);
// Call renderer's Update()
this->renderer->Update(deltaTime);
// Call profiler's Update()
this->profiler->Update(frameTime);
}
// Call our Render function()
this->Render();
}
else if(errorCode == 2)
{
this->Stop();
}
}
[/CODE]

And here the timer functions:
[CODE]
void Timer::Initialize()
{
QueryPerformanceFrequency((LARGE_INTEGER*)&this->frequency);
this->ticksPerMs = (double)(this->frequency / 1000);
QueryPerformanceCounter((LARGE_INTEGER*)&this->startTime);
return;
}
void Timer::Frame()
{
INT64 currentTime;
double timeDifference;
QueryPerformanceCounter((LARGE_INTEGER*)&currentTime);
timeDifference = (double)(currentTime - this->startTime);
this->frameTime = timeDifference / this->ticksPerMs;
this->startTime = currentTime;
}

double Timer::GetFrameTime()
{
return this->frameTime;
}
[/CODE]

No matter what I try the picture is either black, white, or just still without any input or movement working.
Any ideas what I'm doing wrong ?

Share this post


Link to post
Share on other sites
Hi Lipsryme,

You want to limit your fps to 60. What I don't understand is why you divide your "timeDifference" by the ticks of the CPU. Maybe should you try to assign "frameTime" to "timeDifference".

I think that what you're doing by dividing by cpu's tick limit the "frameTime" and so don't trigger the "if" condition.
[source lang="cpp"]
if(frameTime >= (1000 / 60))
[/source]

So let's try with :

[source lang="cpp"]
void Timer::Frame()
{
INT64 currentTime;
double timeDifference;
QueryPerformanceCounter((LARGE_INTEGER*)&currentTime);
timeDifference = (double)(currentTime - this->startTime);
this->frameTime = timeDifference /* this->ticksPerMs*/;
this->startTime = currentTime;
}
[/source]

Tell me if it work, or not [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img].

Regards,
Drayan Edited by Drayan

Share this post


Link to post
Share on other sites
Hmm if I do that I'm getting locked (or rather not locked) to around 21000ms per frame.
Which is probably not the correct time the frame takes but the actual value frameTime has now.

So that would basically be my ticks per frame or am I wrong ?.
And the only thing to do now would be to convert this to milliseconds ?
But isn't that what I was already doing with the / ticksPerMs ?

Update: Wait. I think I know what my problem is...let me try something...
Update2: Well I've got it working now using an elapsedTime variable which does the trick (tested it in a console application) but it does not work for the render method, even with vsync on. Update loop limiting at least works. Edited by lipsryme

Share this post


Link to post
Share on other sites
I have posted a full solution to this here: [url="http://lspiroengine.com/?p=378"]Fixed-Time-Step Implementation[/url]
The problem you are having is also addressed there, but if you don’t see it feel free to tell me why and I will try to upgrade my tutorial with your advice.


L. Spiro

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this