I have an OpenGL window within my Delphi application, which I want to render with fixed or uncapped FPS, depending on the choice of the user. Rendering the window every (e.g. 30 FPS) seems to work, what doesn't work is computing these FPS and displaying it. This is weird, since it works for the uncapped part. Here is the code, I also tried it with TStopWatch, but I am running into the same issue, which I explain below.
procedure TFoMain.IdleHandler(Sender: TObject; var Done: Boolean);
begin
if Self.FPS <> 0 then begin
if (GetTickCount - PrevTime >= (1000 / FPS)) then begin
StartTime := GetTickCount;
Render();
PrevTime := GetTickCount;
DrawTime := PrevTime - StartTime;
Inc(TimeCount, DrawTime);
Inc(FrameCount);
end;
end
else begin
StartTime := GetTickCount;
Render();
PrevTime := GetTickCount;
DrawTime := PrevTime - StartTime;
Inc(TimeCount, DrawTime);
Inc(FrameCount);
end;
if (TimeCount >= 1000) then begin
Frames := FrameCount;
TimeCount := TimeCount - 1000;
FrameCount:= 0;
end;
Done := false;
end;
As for the FPS
variable, I define frames to be uncapped when FPS
is set to 0. Here is the issue that I am having: In uncapped FPS-mode, everything works fine, but as soon as I switch to capped FPS, for some reason StartTime
and PrevTime
is always equal, therefore DrawTime
becomes 0. This leads to the fact, that TimeCount
does not increase and I never get into the if-condition, in which I check whether TimeCount >= 1000 and set the Frames
variable, which I am displaying in my window.
I am very confused, since for uncapped and capped FPS it is the same exact code snippet, how can I get such different values for these variables? I thought maybe it is an issue with GetTickCount
, but it's not, since I am getting the same outcome with TStopWatch
.
Can someone please tell me what I am doing wrong or how to do it correctly?