Skip to content

Commit a50cf46

Browse files
akheronveikkos
authored andcommitted
More accurate frame rate limiter
Mimic the DOS version's frame rate limiting more accurately.
1 parent 68cd7b0 commit a50cf46

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

SDLPORT.PAS

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,22 @@ begin
7979
end;
8080

8181
function TimerCallback(interval: UInt32; param: Pointer): UInt32; cdecl;
82-
var frameIntervalMs, nowMs : LongInt;
82+
var frameIntervalMs, nowMs, elapsed : LongInt;
8383
begin
8484
frameIntervalMs := 1000 div targetFrames;
8585
nowMs := SDL_GetTicks();
86-
subFrameCount += nowMs - lastFrameTick;
87-
lastFrameTick := nowMs;
88-
89-
while (subFrameCount >= frameIntervalMs) do
86+
elapsed := nowMs - lastFrameTick;
87+
if (elapsed >= frameIntervalMs) then
9088
begin
91-
dec(subFrameCount, frameIntervalMs);
92-
inc(framecount);
93-
end;
89+
lastFrameTick := nowMs;
90+
subFrameCount += elapsed;
9491

92+
while (subFrameCount >= frameIntervalMs) do
93+
begin
94+
dec(subFrameCount, frameIntervalMs);
95+
inc(framecount);
96+
end;
97+
end;
9598
TimerCallback:=interval;
9699
end;
97100

@@ -263,11 +266,11 @@ end;
263266

264267
procedure WaitRaster;
265268
begin
269+
lastFrameCount := framecount;
266270
while(lastFrameCount = framecount) do
267271
begin
268272
SDL_Delay(1);
269273
end;
270-
lastFrameCount := framecount;
271274
end;
272275

273276
function KeyPressed : boolean;

0 commit comments

Comments
 (0)