Same speed on every system

Started by
3 comments, last by L-Tryosine 22 years, 9 months ago
What makes a game run smooter, on fast or old cpus: 1-Make it runs at the same fixed FPS on all systems (let''s say 30, for example) 2-Extract the max FPS possible and work by lag as measure for sprites actions, timers, etc. The 2 is much more hard to do, and i think is the best way. What is the right way to implement it on a game engine?
Advertisement
The best way is to determine the time between each frame and multiply this time with all movements. For example:

timekey := timegettime - oldtime;
oldtime := timekey;

ship.x := ship.x + 10*timekey;

Now the object moves on every computer at every framerate the same way in the same time.

Look at the tutorials (high performance timers) at www.neobrothers.de

Neo
Webmaster @www.neobrothers.de .: 3D Spiele mit Delphi programmieren :.
There was a big discussion on this topic in a flipCode forum thread a while back.

In my current project, I was all for having a variable frame rate but have since been converted to using a fixed rate of 30fps.

Neo, your code shown will not work properly. Try this...

timekey := timegettime - oldtime;
oldtime := oldtime + timekey;

What I do with a time delta such as this is to convert it to a floating point value that represents the fractional part of a second. To do this, divide timekey by the number of ticks per seconds.
Steve 'Sly' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers
The DXTimer OnTimer event gives you the elapsed time since the last call (LagCount). Use LagCount as parameter for you move events and then calc the movements in pixels per sec (FDX/FDY):
newX := X + FDX*(MoveCount/1000);
newY := Y + FDY*(MoveCount/1000);
Well for very perfect movements try this code:

var
Frequency, Current_Time, Last_Time: Int64;
Time_Elapsed , Time_Scale: Double;
Counter_Available: Boolean;

in your init procedure:

Counter_Available := false;
if QueryPerformanceFrequency(frequency) = false then
begin
Last_Time := timeGetTime;
Time_Scale := 0.001;
end
else
begin
Counter_Available := true;
Time_Scale := 1.0 / frequency;
QueryPerformanceCounter(Last_Time);
end;


call this function every frame:

function GetTimeKey: Double;
begin
if Counter_Available then
QueryPerformanceCounter(Current_Time)
else
Current_Time := timeGetTime;

Time_Elapsed := (Current_Time - Last_Time) * Time_Scale;
last_time := current_time;
result := time_elapsed;
end;

Now your result is a number, which you can use to multiplay with your movements and everything works fine.

Neo
Webmaster @www.neobrothers.de .: 3D Spiele mit Delphi programmieren :.

This topic is closed to new replies.

Advertisement