Page 3 of 3 FirstFirst 123
Results 21 to 28 of 28

Thread: Serious problems getting LockFPS to work.

  1. #21

    Serious problems getting LockFPS to work.

    Latest version of sdlticks...

    [pascal]
    interface

    uses
    sdl;

    type
    TSDLTicks = class
    private
    FStartTime : UInt32;
    FTicksPerSecond : UInt32;
    FElapsedLastTime : UInt32;
    FFPSLastTime : UInt32;
    FLockFPSLastTime : UInt32;
    public
    constructor Create;
    destructor Destroy; override; // destructor

    {************************************************* ****************************
    Init
    If the hi-res timer is present, the tick rate is stored and the function
    returns true. Otherwise, the function returns false, and the timer should
    not be used.
    ************************************************** ***************************}
    function Init : boolean;

    {************************************************* **************************
    GetGetElapsedSeconds
    Returns the Elapsed time, since the function was last called.
    ************************************************** *************************}
    function GetElapsedSeconds : Single;

    {************************************************* **************************
    GetFPS
    Returns the average frames per second.
    If this is not called every frame, the client should track the number
    of frames itself, and reset the value after this is called.
    ************************************************** *************************}
    function GetFPS : single;

    {************************************************* **************************
    LockFPS
    Used to lock the frame rate to a set amount. This will block until enough
    time has passed to ensure that the fps won't go over the requested amount.
    Note that this can only keep the fps from going above the specified level;
    it can still drop below it. It is assumed that if used, this function will
    be called every frame. The value returned is the instantaneous fps, which
    will be less than or equal to the targetFPS.
    ************************************************** *************************}
    procedure LockFPS( targetFPS : Byte );
    end;

    implementation

    { TSDLTicks }
    constructor TSDLTicks.Create;
    begin
    inherited;
    FTicksPerSecond := 1000;
    end;

    destructor TSDLTicks.Destroy;
    begin
    inherited;
    end;

    function TSDLTicks.GetElapsedSeconds : Single;
    var
    currentTime : Cardinal;
    begin
    currentTime := SDL_GetTicks;

    result := ( currentTime - FElapsedLastTime ) / FTicksPerSecond;

    // reset the timer
    FElapsedLastTime := currentTime;
    end;

    function TSDLTicks.GetFPS : Single;
    var
    currentTime, FrameTime : UInt32;
    fps : single;
    begin
    currentTime := SDL_GetTicks;

    FrameTime := ( currentTime - FFPSLastTime );

    if FrameTime = 0 then
    FrameTime := 1;

    fps := FTicksPerSecond / FrameTime;

    // reset the timer
    FFPSLastTime := currentTime;
    result := fps;
    end;

    function TSDLTicks.Init : boolean;
    begin
    FStartTime := SDL_GetTicks;
    FElapsedLastTime := FStartTime;
    FFPSLastTime := FStartTime;
    FLockFPSLastTime := FStartTime;
    result := true;
    end;

    procedure TSDLTicks.LockFPS( targetFPS : Byte );
    var
    currentTime : UInt32;
    targetTime : single;
    begin
    if ( targetFPS = 0 ) then
    targetFPS := 1;

    targetTime := FTicksPerSecond / targetFPS;

    // delay to maintain a constant frame rate
    repeat
    currentTime := SDL_GetTicks;
    until ( ( currentTime - FLockFPSLastTime ) > targetTime );

    // reset the timer
    FLockFPSLastTime := currentTime;
    end;
    [/pascal]
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  2. #22

    Serious problems getting LockFPS to work.

    Dom, Both GetFPS and LockFPS modify the s_LastTime variable. This means that you can't use them together. You can either view the FPS or Lock the FPS. I don't know if this is a design feature but I was hoping to use them both.. first of all as a test that LockFPS was working and to see when the FPS went really low.


    In my game the process is...

    [pascal]Loop

    Reset the counters

    Do some Stuff... some rendering etc

    Draw the FPS using GetFPS

    Lock the FPS to a specific value

    Until Quitting[/pascal]

    At the moment, as it stands, GetFPS is resetting the counter before LockFPS has a chance to limit the FPS.

    They either need to use 2 variables or there has to be an reset function which is called before anything happens.

  3. #23

    Serious problems getting LockFPS to work.

    That's my design flaw, as I was only thinking about using them independently. I'll add fix later tonight, or you can submit a patch .
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  4. #24

    Serious problems getting LockFPS to work.

    Well, I believe that the GetFPS should be a non-destructive reporting function only...

    Successive calls to GetFPS should return the same value (well, pretty similar anyway)

    Successive calls to LockFPS should not. So it might be OK for LockFPS to modify the start time but not GetFPS.

    I'll take a look at this code tonight.

  5. #25

    Serious problems getting LockFPS to work.

    I've edited the previous sdlticks.pas post. Testing shows that they all work independently now.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  6. #26

    Serious problems getting LockFPS to work.

    Sweet

  7. #27

    Serious problems getting LockFPS to work.

    Sweet as a honey coated lollypop brought with lottery winnings.

    It works a treat. the frame rate is nicely balanced at 30fps. Gonna try it on the wife's laptop, I want to see what the performance difference is..

    Before my optimisations, I was getting a logic time of 9 now I get 4.. She was getting 4...

    Should be smooth

  8. #28

    Serious problems getting LockFPS to work.

    This version of sdlticks.pas has not been checked into CVS.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

Page 3 of 3 FirstFirst 123

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •