Skip to content

Commit

Permalink
More accurate frame rate limiter
Browse files Browse the repository at this point in the history
Mimic the DOS version's frame rate limiting more accurately.
  • Loading branch information
akheron authored and veikkos committed Apr 7, 2024
1 parent 68cd7b0 commit a50cf46
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions SDLPORT.PAS
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,22 @@ begin
end;

function TimerCallback(interval: UInt32; param: Pointer): UInt32; cdecl;
var frameIntervalMs, nowMs : LongInt;
var frameIntervalMs, nowMs, elapsed : LongInt;
begin
frameIntervalMs := 1000 div targetFrames;
nowMs := SDL_GetTicks();
subFrameCount += nowMs - lastFrameTick;
lastFrameTick := nowMs;

while (subFrameCount >= frameIntervalMs) do
elapsed := nowMs - lastFrameTick;
if (elapsed >= frameIntervalMs) then
begin
dec(subFrameCount, frameIntervalMs);
inc(framecount);
end;
lastFrameTick := nowMs;
subFrameCount += elapsed;

while (subFrameCount >= frameIntervalMs) do
begin
dec(subFrameCount, frameIntervalMs);
inc(framecount);
end;
end;
TimerCallback:=interval;
end;

Expand Down Expand Up @@ -263,11 +266,11 @@ end;

procedure WaitRaster;
begin
lastFrameCount := framecount;
while(lastFrameCount = framecount) do
begin
SDL_Delay(1);
end;
lastFrameCount := framecount;
end;

function KeyPressed : boolean;
Expand Down

0 comments on commit a50cf46

Please sign in to comment.